File: updatesource.cpp

package info (click to toggle)
kaffeine 2.0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,108 kB
  • sloc: cpp: 28,201; ansic: 535; xml: 457; sh: 205; makefile: 3
file content (181 lines) | stat: -rw-r--r-- 4,723 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * updatesource.cpp
 *
 * Copyright (C) 2011 Christoph Pfister <christophpfister@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <QDebug>
#if QT_VERSION < 0x050500
# define qInfo qDebug
#endif

#include <QCoreApplication>
#include <QDir>

int main(int argc, char *argv[])
{
	// QCoreApplication is needed for proper file name handling
	QCoreApplication application(argc, argv);

	QStringList paths;
	paths.append(".");

	for (int i = 0; i < paths.size(); ++i) {
		QString path = paths.at(i);

		if (QFileInfo(path).isDir()) {
			QStringList entries = QDir(path).entryList(QDir::AllEntries |
				QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);

			foreach (const QString &entry, entries) {
				if (path == ".") {
					if (entry != ".git") {
						paths.append(entry);
					}
				} else {
					paths.append(path + '/' + entry);
				}
			}

			paths.removeAt(i);
			--i;
		}
	}

	std::sort(paths.begin(), paths.end());

	foreach (const QString &path, paths) {
		QFile file(path);

		if (!file.open(QIODevice::ReadOnly)) {
			qCritical() << "cannot open file" << file.fileName();
			return 1;
		}

		QByteArray data = file.readAll();
		file.close();

		if (data.isEmpty()) {
			qCritical() << "file is empty" << file.fileName();
			return 1;
		}

		if (path.endsWith(QLatin1String(".png")) || path.endsWith(QLatin1String(".svgz"))) {
			continue;
		}

		QString content = QString::fromUtf8(data.constData());

		if (data != content.toUtf8()) {
			qCritical() << "non-utf8 characters in file" << file.fileName();
			return 1;
		}

		// FIXME make src/scanfile.dvb ascii-only

		if ((path == "README") || (path == "src/scanfile.dvb") ||
		    path.endsWith(QLatin1String(".desktop"))) {
			continue;
		}

		for (int i = 0; i < content.size(); ++i) {
			int value = content.at(i).unicode();

			if ((value > 0x7f) ||
			    ((value < 0x20) && (value != '\n') && (value != '\t'))) {
				qCritical() << "non-ascii character" << content.at(i) <<
					"in file" << file.fileName();
				return 1;
			}
		}

		if (path.startsWith(QLatin1String("include/")) || path.startsWith(QLatin1String("tools/"))) {
			continue;
		}

		QStringList lines = content.split('\n');
		bool ignoreLineLength = false;
		QRegExp logRegExp("Log[^0-9A-Za-z]*[(][^0-9A-Za-z]*\"");
		QString logFunctionName;
		int bracketLevel = 0;

		for (int i = 0; i < lines.size(); ++i) {
			const QString &line = lines.at(i);

			if ((line == "// everything below this line is automatically generated") &&
			    ((path == "src/dvb/dvbsi.cpp") || (path == "src/dvb/dvbsi.h"))) {
				ignoreLineLength = true;
			}

			if (!ignoreLineLength &&
			    (QString(line).replace('\t', "        ").size() > 99)) {
				qWarning() << file.fileName() << "line" << (i + 1) <<
					"is longer than 99 characters";
			}

			int logIndex = logRegExp.indexIn(line);

			if (logIndex >= 0) {
				logIndex = (line.indexOf('"', logIndex) + 1);

				if (!line.mid(logIndex).startsWith(logFunctionName)) {
					int cutIndex = (line.indexOf(": ", logIndex) + 2);

					if (cutIndex > logIndex) {
						lines[i].remove(logIndex, cutIndex - logIndex);
					}

					lines[i].insert(logIndex, logFunctionName);
				}
			}

			bracketLevel = (bracketLevel + line.count('{') - line.count('}'));

			if (bracketLevel < 0) {
				qWarning() << file.fileName() << "line" << (i + 1) <<
					"brackets do not match";
			}

			if ((bracketLevel == 0) && !line.startsWith('\t')) {
				QRegExp logFunctionRegExp("[0-9A-Za-z:~]*[(]");
				int index = logFunctionRegExp.indexIn(line);

				if (index >= 0) {
					logFunctionName =
						line.mid(index, logFunctionRegExp.matchedLength());
					logFunctionName.chop(1);
					logFunctionName.append(": ");
				}
			}
		}

		if (bracketLevel != 0) {
			qWarning() << file.fileName() << "brackets do not match";
		}

		if (content != lines.join("\n")) {
			if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
				qCritical() << "cannot open file" << file.fileName();
				return 1;
			}

			file.write(lines.join("\n").toUtf8());
		}
	}

	return 0;
}