OSDN Git Service

8ad95484d3b38b4ef4c799a32ccfd38aa545ee15
[qt-creator-jp/qt-creator-jp.git] / src / tools / qpatch / qpatch.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 //#include <QCoreApplication>
35 #include <QStringList>
36 #include <QFile>
37 #include <QVector>
38 #include <QTextStream>
39 #include <iostream>
40
41 int main(int argc, char *argv[])
42 {
43     if (argc != 4) {
44         std::cerr << "Usage: qpatch file.list oldQtDir newQtDir" << std::endl;
45         return EXIT_FAILURE;
46     }
47
48     const QByteArray files = argv[1];
49     const QByteArray qtDirPath = argv[2];
50     const QByteArray newQtPath = argv[3];
51
52     if (qtDirPath.size() < newQtPath.size()) {
53         std::cerr << "qpatch: error: newQtDir needs to be less than " << qtDirPath.size() << " characters."
54                 << std::endl;
55         return EXIT_FAILURE;
56     }
57
58     QFile fn(QFile::decodeName(files));
59     if (! fn.open(QFile::ReadOnly)) {
60         std::cerr << "qpatch: error: file not found" << std::endl;
61         return EXIT_FAILURE;
62     }
63
64     QStringList filesToPatch, textFilesToPatch;
65     bool readingTextFilesToPatch = false;
66
67     // read the input file
68     QTextStream in(&fn);
69
70     forever {
71         const QString line = in.readLine();
72
73         if (line.isNull())
74             break;
75
76         else if (line.isEmpty())
77             continue;
78
79         else if (line.startsWith(QLatin1String("%%")))
80             readingTextFilesToPatch = true;
81
82         else if (readingTextFilesToPatch)
83             textFilesToPatch.append(line);
84
85         else
86             filesToPatch.append(line);
87     }
88
89     foreach (QString fileName, filesToPatch) {
90         QString prefix = QFile::decodeName(newQtPath);
91
92         if (! prefix.endsWith(QLatin1Char('/')))
93             prefix += QLatin1Char('/');
94
95         fileName.prepend(prefix);
96
97         QFile file(fileName);
98         if (! file.open(QFile::ReadOnly)) {
99             std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' not found" << std::endl;
100             continue;
101         }
102
103         const QByteArray source = file.readAll();
104         file.close();
105
106         int index = 0;
107
108         if (! file.open(QFile::WriteOnly | QFile::Truncate)) {
109             std::cerr << "qpatch: error: file `" << qPrintable(fileName) << "' not writable" << std::endl;
110             continue;
111         }
112         std::cout << "patching file `" << qPrintable(fileName) << "'" << std::endl;
113
114         forever {
115             int start = source.indexOf(qtDirPath, index);
116             if (start == -1)
117                 break;
118
119             int endOfString = start;
120             for (; endOfString < source.size(); ++endOfString) {
121                 if (! source.at(endOfString))
122                     break;
123             }
124
125             ++endOfString; // include the '\0'
126
127             if (index != start)
128                 file.write(source.constData() + index, start - index);
129
130             int length = endOfString - start;
131             QVector<char> s;
132
133             for (const char *x = newQtPath.constData(); x != newQtPath.constEnd(); ++x)
134                 s.append(*x);
135
136             const int qtDirPathLength = qtDirPath.size();
137
138             for (const char *x = source.constData() + start + qtDirPathLength;
139                     x != source.constData() + endOfString; ++x)
140                 s.append(*x);
141
142             const int oldSize = s.size();
143
144             for (int i = oldSize; i < length; ++i)
145                 s.append('\0');
146
147 #if 0
148             std::cout << "replace string: " << source.mid(start, endOfString - start).constData()
149                     << " with: " << s.constData() << std::endl;
150 #endif
151
152             file.write(s.constData(), s.size());
153
154             index = endOfString;
155         }
156
157         if (index == 0) {
158             std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' didn't contain string to patch" << std::endl;
159         }
160
161         if (index != source.size())
162             file.write(source.constData() + index, source.size() - index);
163     }
164
165     foreach (QString fileName, textFilesToPatch) {
166         QString prefix = QFile::decodeName(newQtPath);
167
168         if (! prefix.endsWith(QLatin1Char('/')))
169             prefix += QLatin1Char('/');
170
171         fileName.prepend(prefix);
172
173         QFile file(fileName);
174         if (! file.open(QFile::ReadOnly)) {
175             std::cerr << "qpatch: warning: file `" << qPrintable(fileName) << "' not found" << std::endl;
176             continue;
177         }
178
179         QByteArray source = file.readAll();
180         file.close();
181
182         if (! file.open(QFile::WriteOnly | QFile::Truncate)) {
183             std::cerr << "qpatch: error: file `" << qPrintable(fileName) << "' not writable" << std::endl;
184             continue;
185         }
186
187         std::cout << "patching text file `" << qPrintable(fileName) << "'" << std::endl;
188
189         source.replace(qtDirPath, newQtPath);
190         file.write(source);
191     }
192
193     return 0;
194 }