##// END OF EJS Templates
Fix copyright header for normalize tool...
Miikka Heikkinen -
r2434:b9f3bb08b3e6
parent child
Show More
@@ -1,173 +1,193
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** All rights reserved.
4 ** Contact: http://www.qt-project.org/legal
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
5 **
7 ** This file is part of the utils of the Qt Toolkit.
6 ** This file is part of the utils of the Qt Toolkit.
8 **
7 **
9 ** $QT_BEGIN_LICENSE$
8 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
9 ** Commercial License Usage
11 ** accordance with the Qt Commercial License Agreement provided with the
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** 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.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
14 **
37 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
38 ** $QT_END_LICENSE$
18 **
39 **
19 ****************************************************************************/
40 ****************************************************************************/
20
21 #include <qcoreapplication.h>
41 #include <qcoreapplication.h>
22 #include <qdiriterator.h>
42 #include <qdiriterator.h>
23 #include <qfile.h>
43 #include <qfile.h>
24 #include <qmetaobject.h>
44 #include <qmetaobject.h>
25 #include <qstring.h>
45 #include <qstring.h>
26 #include <qtextstream.h>
46 #include <qtextstream.h>
27
47
28 #include <qdebug.h>
48 #include <qdebug.h>
29
49
30 #include <limits.h>
50 #include <limits.h>
31 #include <stdio.h>
51 #include <stdio.h>
32
52
33 static bool printFilename = true;
53 static bool printFilename = true;
34 static bool modify = false;
54 static bool modify = false;
35
55
36 QString signature(const QString &line, int pos)
56 QString signature(const QString &line, int pos)
37 {
57 {
38 int start = pos;
58 int start = pos;
39 // find first open parentheses
59 // find first open parentheses
40 while (start < line.length() && line.at(start) != QLatin1Char('('))
60 while (start < line.length() && line.at(start) != QLatin1Char('('))
41 ++start;
61 ++start;
42 int i = ++start;
62 int i = ++start;
43 int par = 1;
63 int par = 1;
44 // find matching closing parentheses
64 // find matching closing parentheses
45 while (i < line.length() && par > 0) {
65 while (i < line.length() && par > 0) {
46 if (line.at(i) == QLatin1Char('('))
66 if (line.at(i) == QLatin1Char('('))
47 ++par;
67 ++par;
48 else if (line.at(i) == QLatin1Char(')'))
68 else if (line.at(i) == QLatin1Char(')'))
49 --par;
69 --par;
50 ++i;
70 ++i;
51 }
71 }
52 if (par == 0)
72 if (par == 0)
53 return line.mid(start, i - start - 1);
73 return line.mid(start, i - start - 1);
54 return QString();
74 return QString();
55 }
75 }
56
76
57 bool checkSignature(const QString &fileName, QString &line, const char *sig)
77 bool checkSignature(const QString &fileName, QString &line, const char *sig)
58 {
78 {
59 static QStringList fileList;
79 static QStringList fileList;
60
80
61 int idx = -1;
81 int idx = -1;
62 bool found = false;
82 bool found = false;
63 while ((idx = line.indexOf(sig, ++idx)) != -1) {
83 while ((idx = line.indexOf(sig, ++idx)) != -1) {
64 const QByteArray sl(signature(line, idx).toLocal8Bit());
84 const QByteArray sl(signature(line, idx).toLocal8Bit());
65 QByteArray nsl(QMetaObject::normalizedSignature(sl.constData()));
85 QByteArray nsl(QMetaObject::normalizedSignature(sl.constData()));
66 if (sl != nsl) {
86 if (sl != nsl) {
67 found = true;
87 found = true;
68 if (printFilename && !fileList.contains(fileName)) {
88 if (printFilename && !fileList.contains(fileName)) {
69 fileList.prepend(fileName);
89 fileList.prepend(fileName);
70 printf("%s\n", fileName.toLocal8Bit().constData());
90 printf("%s\n", fileName.toLocal8Bit().constData());
71 }
91 }
72 if (modify)
92 if (modify)
73 line.replace(sl, nsl);
93 line.replace(sl, nsl);
74 //qDebug("expected '%s', got '%s'", nsl.data(), sl.data());
94 //qDebug("expected '%s', got '%s'", nsl.data(), sl.data());
75 }
95 }
76 }
96 }
77 return found;
97 return found;
78 }
98 }
79
99
80 void writeChanges(const QString &fileName, const QStringList &lines)
100 void writeChanges(const QString &fileName, const QStringList &lines)
81 {
101 {
82 QFile file(fileName);
102 QFile file(fileName);
83 if (!file.open(QIODevice::WriteOnly)) {
103 if (!file.open(QIODevice::WriteOnly)) {
84 qDebug("unable to open file '%s' for writing (%s)", fileName.toLocal8Bit().constData(), file.errorString().toLocal8Bit().constData());
104 qDebug("unable to open file '%s' for writing (%s)", fileName.toLocal8Bit().constData(), file.errorString().toLocal8Bit().constData());
85 return;
105 return;
86 }
106 }
87 QTextStream stream(&file);
107 QTextStream stream(&file);
88 for (int i = 0; i < lines.count(); ++i)
108 for (int i = 0; i < lines.count(); ++i)
89 stream << lines.at(i);
109 stream << lines.at(i);
90 file.close();
110 file.close();
91 }
111 }
92
112
93 void check(const QString &fileName)
113 void check(const QString &fileName)
94 {
114 {
95 QFile file(fileName);
115 QFile file(fileName);
96 if (!file.open(QIODevice::ReadOnly)) {
116 if (!file.open(QIODevice::ReadOnly)) {
97 qDebug("unable to open file: '%s' (%s)", fileName.toLocal8Bit().constData(), file.errorString().toLocal8Bit().constData());
117 qDebug("unable to open file: '%s' (%s)", fileName.toLocal8Bit().constData(), file.errorString().toLocal8Bit().constData());
98 return;
118 return;
99 }
119 }
100 QStringList lines;
120 QStringList lines;
101 bool found = false;
121 bool found = false;
102 while (true) {
122 while (true) {
103 QByteArray bline = file.readLine(16384);
123 QByteArray bline = file.readLine(16384);
104 if (bline.isEmpty())
124 if (bline.isEmpty())
105 break;
125 break;
106 QString line = QString::fromLocal8Bit(bline);
126 QString line = QString::fromLocal8Bit(bline);
107 Q_ASSERT_X(line.endsWith("\n"), "check()", fileName.toLocal8Bit().constData());
127 Q_ASSERT_X(line.endsWith("\n"), "check()", fileName.toLocal8Bit().constData());
108 found |= checkSignature(fileName, line, "SLOT");
128 found |= checkSignature(fileName, line, "SLOT");
109 found |= checkSignature(fileName, line, "SIGNAL");
129 found |= checkSignature(fileName, line, "SIGNAL");
110 if (modify)
130 if (modify)
111 lines << line;
131 lines << line;
112 }
132 }
113 file.close();
133 file.close();
114
134
115 if (found && modify) {
135 if (found && modify) {
116 printf("Modifying file: '%s'\n", fileName.toLocal8Bit().constData());
136 printf("Modifying file: '%s'\n", fileName.toLocal8Bit().constData());
117 writeChanges(fileName, lines);
137 writeChanges(fileName, lines);
118 }
138 }
119 }
139 }
120
140
121 void traverse(const QString &path)
141 void traverse(const QString &path)
122 {
142 {
123 QDirIterator dirIterator(path, QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files | QDir::NoSymLinks);
143 QDirIterator dirIterator(path, QDir::NoDotAndDotDot | QDir::Dirs | QDir::Files | QDir::NoSymLinks);
124
144
125 while (dirIterator.hasNext()) {
145 while (dirIterator.hasNext()) {
126 QString filePath = dirIterator.next();
146 QString filePath = dirIterator.next();
127 if (filePath.endsWith(".cpp"))
147 if (filePath.endsWith(".cpp"))
128 check(filePath);
148 check(filePath);
129 else if (QFileInfo(filePath).isDir())
149 else if (QFileInfo(filePath).isDir())
130 traverse(filePath); // recurse
150 traverse(filePath); // recurse
131 }
151 }
132 }
152 }
133
153
134 int main(int argc, char *argv[])
154 int main(int argc, char *argv[])
135 {
155 {
136 QCoreApplication app(argc, argv);
156 QCoreApplication app(argc, argv);
137
157
138 if (app.argc() < 2 || (app.argc() == 2 && (app.argv()[1][0] == '-'))) {
158 if (app.argc() < 2 || (app.argc() == 2 && (app.argv()[1][0] == '-'))) {
139 printf("usage: normalize [--modify] <path>\n");
159 printf("usage: normalize [--modify] <path>\n");
140 printf(" <path> can be a single file or a directory (default: look for *.cpp recursively)");
160 printf(" <path> can be a single file or a directory (default: look for *.cpp recursively)");
141 printf(" Outputs all filenames that contain non-normalized SIGNALs and SLOTs\n");
161 printf(" Outputs all filenames that contain non-normalized SIGNALs and SLOTs\n");
142 printf(" with --modify: fix all occurrences of non-normalized SIGNALs and SLOTs\n");
162 printf(" with --modify: fix all occurrences of non-normalized SIGNALs and SLOTs\n");
143 return 1;
163 return 1;
144 }
164 }
145
165
146 QString path;
166 QString path;
147 if (qstrcmp(app.argv()[1], "--modify") == 0) {
167 if (qstrcmp(app.argv()[1], "--modify") == 0) {
148 printFilename = false;
168 printFilename = false;
149 modify = true;
169 modify = true;
150 path = app.argv()[2];
170 path = app.argv()[2];
151 } else {
171 } else {
152 path = app.argv()[1];
172 path = app.argv()[1];
153 }
173 }
154
174
155 if (path.startsWith("-")) {
175 if (path.startsWith("-")) {
156 qWarning("unknown parameter: %s", path.toLocal8Bit().constData());
176 qWarning("unknown parameter: %s", path.toLocal8Bit().constData());
157 return 1;
177 return 1;
158 }
178 }
159
179
160 QFileInfo fi(path);
180 QFileInfo fi(path);
161 if (fi.isFile()) {
181 if (fi.isFile()) {
162 check(path);
182 check(path);
163 } else if (fi.isDir()) {
183 } else if (fi.isDir()) {
164 if (!path.endsWith("/"))
184 if (!path.endsWith("/"))
165 path.append("/");
185 path.append("/");
166 traverse(path);
186 traverse(path);
167 } else {
187 } else {
168 qWarning("Don't know what to do with '%s'", path.toLocal8Bit().constData());
188 qWarning("Don't know what to do with '%s'", path.toLocal8Bit().constData());
169 return 1;
189 return 1;
170 }
190 }
171
191
172 return 0;
192 return 0;
173 }
193 }
General Comments 0
You need to be logged in to leave comments. Login now