File indexing completed on 2024-05-05 04:52:24

0001 /*
0002  * updatesource.cpp
0003  *
0004  * Copyright (C) 2011 Christoph Pfister <christophpfister@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License along
0017  * with this program; if not, write to the Free Software Foundation, Inc.,
0018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0019  */
0020 
0021 #include <QDebug>
0022 #if QT_VERSION < 0x050500
0023 # define qInfo qDebug
0024 #endif
0025 
0026 #include <QCoreApplication>
0027 #include <QDir>
0028 
0029 int main(int argc, char *argv[])
0030 {
0031     // QCoreApplication is needed for proper file name handling
0032     QCoreApplication application(argc, argv);
0033 
0034     QStringList paths;
0035     paths.append(".");
0036 
0037     for (int i = 0; i < paths.size(); ++i) {
0038         QString path = paths.at(i);
0039 
0040         if (QFileInfo(path).isDir()) {
0041             QStringList entries = QDir(path).entryList(QDir::AllEntries |
0042                 QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
0043 
0044             foreach (const QString &entry, entries) {
0045                 if (path == ".") {
0046                     if (entry != ".git") {
0047                         paths.append(entry);
0048                     }
0049                 } else {
0050                     paths.append(path + '/' + entry);
0051                 }
0052             }
0053 
0054             paths.removeAt(i);
0055             --i;
0056         }
0057     }
0058 
0059     std::sort(paths.begin(), paths.end());
0060 
0061     foreach (const QString &path, paths) {
0062         QFile file(path);
0063 
0064         if (!file.open(QIODevice::ReadOnly)) {
0065             qCritical() << "cannot open file" << file.fileName();
0066             return 1;
0067         }
0068 
0069         QByteArray data = file.readAll();
0070         file.close();
0071 
0072         if (data.isEmpty()) {
0073             qCritical() << "file is empty" << file.fileName();
0074             return 1;
0075         }
0076 
0077         if (path.endsWith(QLatin1String(".png")) || path.endsWith(QLatin1String(".svgz"))) {
0078             continue;
0079         }
0080 
0081         QString content = QString::fromUtf8(data.constData());
0082 
0083         if (data != content.toUtf8()) {
0084             qCritical() << "non-utf8 characters in file" << file.fileName();
0085             return 1;
0086         }
0087 
0088         // FIXME make src/scanfile.dvb ascii-only
0089 
0090         if ((path == "README") || (path == "src/scanfile.dvb") ||
0091             path.endsWith(QLatin1String(".desktop"))) {
0092             continue;
0093         }
0094 
0095         for (int i = 0; i < content.size(); ++i) {
0096             int value = content.at(i).unicode();
0097 
0098             if ((value > 0x7f) ||
0099                 ((value < 0x20) && (value != '\n') && (value != '\t'))) {
0100                 qCritical() << "non-ascii character" << content.at(i) <<
0101                     "in file" << file.fileName();
0102                 return 1;
0103             }
0104         }
0105 
0106         if (path.startsWith(QLatin1String("include/")) || path.startsWith(QLatin1String("tools/"))) {
0107             continue;
0108         }
0109 
0110         QStringList lines = content.split('\n');
0111         bool ignoreLineLength = false;
0112         QRegExp logRegExp("Log[^0-9A-Za-z]*[(][^0-9A-Za-z]*\"");
0113         QString logFunctionName;
0114         int bracketLevel = 0;
0115 
0116         for (int i = 0; i < lines.size(); ++i) {
0117             const QString &line = lines.at(i);
0118 
0119             if ((line == "// everything below this line is automatically generated") &&
0120                 ((path == "src/dvb/dvbsi.cpp") || (path == "src/dvb/dvbsi.h"))) {
0121                 ignoreLineLength = true;
0122             }
0123 
0124             if (!ignoreLineLength &&
0125                 (QString(line).replace('\t', "        ").size() > 99)) {
0126                 qWarning() << file.fileName() << "line" << (i + 1) <<
0127                     "is longer than 99 characters";
0128             }
0129 
0130             int logIndex = logRegExp.indexIn(line);
0131 
0132             if (logIndex >= 0) {
0133                 logIndex = (line.indexOf('"', logIndex) + 1);
0134 
0135                 if (!line.mid(logIndex).startsWith(logFunctionName)) {
0136                     int cutIndex = (line.indexOf(": ", logIndex) + 2);
0137 
0138                     if (cutIndex > logIndex) {
0139                         lines[i].remove(logIndex, cutIndex - logIndex);
0140                     }
0141 
0142                     lines[i].insert(logIndex, logFunctionName);
0143                 }
0144             }
0145 
0146             bracketLevel = (bracketLevel + line.count('{') - line.count('}'));
0147 
0148             if (bracketLevel < 0) {
0149                 qWarning() << file.fileName() << "line" << (i + 1) <<
0150                     "brackets do not match";
0151             }
0152 
0153             if ((bracketLevel == 0) && !line.startsWith('\t')) {
0154                 QRegExp logFunctionRegExp("[0-9A-Za-z:~]*[(]");
0155                 int index = logFunctionRegExp.indexIn(line);
0156 
0157                 if (index >= 0) {
0158                     logFunctionName =
0159                         line.mid(index, logFunctionRegExp.matchedLength());
0160                     logFunctionName.chop(1);
0161                     logFunctionName.append(": ");
0162                 }
0163             }
0164         }
0165 
0166         if (bracketLevel != 0) {
0167             qWarning() << file.fileName() << "brackets do not match";
0168         }
0169 
0170         if (content != lines.join("\n")) {
0171             if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
0172                 qCritical() << "cannot open file" << file.fileName();
0173                 return 1;
0174             }
0175 
0176             file.write(lines.join("\n").toUtf8());
0177         }
0178     }
0179 
0180     return 0;
0181 }