1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 //#include <QCoreApplication>
35 #include <QStringList>
38 #include <QTextStream>
41 int main(int argc, char *argv[])
44 std::cerr << "Usage: qpatch file.list oldQtDir newQtDir" << std::endl;
48 const QByteArray files = argv[1];
49 const QByteArray qtDirPath = argv[2];
50 const QByteArray newQtPath = argv[3];
52 if (qtDirPath.size() < newQtPath.size()) {
53 std::cerr << "qpatch: error: newQtDir needs to be less than " << qtDirPath.size() << " characters."
58 QFile fn(QFile::decodeName(files));
59 if (! fn.open(QFile::ReadOnly)) {
60 std::cerr << "qpatch: error: file not found" << std::endl;
64 QStringList filesToPatch, textFilesToPatch;
65 bool readingTextFilesToPatch = false;
67 // read the input file
71 const QString line = in.readLine();
76 else if (line.isEmpty())
79 else if (line.startsWith(QLatin1String("%%")))
80 readingTextFilesToPatch = true;
82 else if (readingTextFilesToPatch)
83 textFilesToPatch.append(line);
86 filesToPatch.append(line);
89 foreach (QString fileName, filesToPatch) {
90 QString prefix = QFile::decodeName(newQtPath);
92 if (! prefix.endsWith(QLatin1Char('/')))
93 prefix += QLatin1Char('/');
95 fileName.prepend(prefix);
98 if (! file.open(QFile::ReadOnly)) {
99 std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' not found" << std::endl;
103 const QByteArray source = file.readAll();
108 if (! file.open(QFile::WriteOnly | QFile::Truncate)) {
109 std::cerr << "qpatch: error: file `" << qPrintable(fileName) << "' not writable" << std::endl;
112 std::cout << "patching file `" << qPrintable(fileName) << "'" << std::endl;
115 int start = source.indexOf(qtDirPath, index);
119 int endOfString = start;
120 for (; endOfString < source.size(); ++endOfString) {
121 if (! source.at(endOfString))
125 ++endOfString; // include the '\0'
128 file.write(source.constData() + index, start - index);
130 int length = endOfString - start;
133 for (const char *x = newQtPath.constData(); x != newQtPath.constEnd(); ++x)
136 const int qtDirPathLength = qtDirPath.size();
138 for (const char *x = source.constData() + start + qtDirPathLength;
139 x != source.constData() + endOfString; ++x)
142 const int oldSize = s.size();
144 for (int i = oldSize; i < length; ++i)
148 std::cout << "replace string: " << source.mid(start, endOfString - start).constData()
149 << " with: " << s.constData() << std::endl;
152 file.write(s.constData(), s.size());
158 std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' didn't contain string to patch" << std::endl;
161 if (index != source.size())
162 file.write(source.constData() + index, source.size() - index);
165 foreach (QString fileName, textFilesToPatch) {
166 QString prefix = QFile::decodeName(newQtPath);
168 if (! prefix.endsWith(QLatin1Char('/')))
169 prefix += QLatin1Char('/');
171 fileName.prepend(prefix);
173 QFile file(fileName);
174 if (! file.open(QFile::ReadOnly)) {
175 std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' not found" << std::endl;
179 QByteArray source = file.readAll();
182 if (! file.open(QFile::WriteOnly | QFile::Truncate)) {
183 std::cerr << "qpatch: error: file `" << qPrintable(fileName) << "' not writable" << std::endl;
187 std::cout << "patching text file `" << qPrintable(fileName) << "'" << std::endl;
189 source.replace(qtDirPath, newQtPath);