File indexing completed on 2024-12-08 06:37:28

0001 /*  This file is part of the KDE libraries
0002  *    Copyright (C) 2016 Kåre Särs <kare.sars@iki.fi>
0003  *
0004  *    This library is free software; you can redistribute it and/or
0005  *    modify it under the terms of the GNU Library General Public
0006  *    License as published by the Free Software Foundation; either
0007  *    version 2 of the License, or (at your option) any later version.
0008  *
0009  *    This library is distributed in the hope that it will be useful,
0010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  *    Library General Public License for more details.
0013  *
0014  *    You should have received a copy of the GNU Library General Public License
0015  *    along with this library; see the file COPYING.LIB.  If not, write to
0016  *    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  *    Boston, MA 02110-1301, USA.
0018  */
0019 #include <QCommandLineParser>
0020 #include <QCoreApplication>
0021 #include <QDebug>
0022 #include <QFile>
0023 #include <QFileInfo>
0024 #include <QRegularExpression>
0025 #include <QString>
0026 
0027 QString link(const QString &path, const QString &fileName)
0028 {
0029     QFile in(path + QLatin1Char('/') + fileName);
0030     if (!in.open(QIODevice::ReadOnly)) {
0031         qDebug() << "failed to read" << path << fileName << in.fileName();
0032         return QString();
0033     }
0034 
0035     QString firstLine = QString::fromLocal8Bit(in.readLine());
0036     if (firstLine.isEmpty()) {
0037         qDebug() << in.fileName() << "line could not be read...";
0038         return QString();
0039     }
0040     QRegularExpression fNameReg(QStringLiteral("(.*\\.(?:svg|png|gif|ico))$"));
0041     QRegularExpressionMatch match = fNameReg.match(firstLine);
0042     if (!match.hasMatch()) {
0043         return QString();
0044     }
0045 
0046     QFileInfo linkInfo(path + QLatin1Char('/') + match.captured(1));
0047     QString aliasLink = link(linkInfo.path(), linkInfo.fileName());
0048     if (!aliasLink.isEmpty()) {
0049         // qDebug() <<  fileName << "=" << match.captured(1) << "=" << aliasLink;
0050         return aliasLink;
0051     }
0052 
0053     return path + QLatin1Char('/') + match.captured(1);
0054 }
0055 
0056 int parseFile(const QString &infile, const QString &outfile)
0057 {
0058     QFile in(infile);
0059     QFile out(outfile);
0060     QRegularExpression imageReg(QStringLiteral("<file>(.*\\.(?:svg|png|gif|ico))</file>"));
0061 
0062     if (!in.open(QIODevice::ReadOnly)) {
0063         qDebug() << "Failed to open" << infile;
0064         return -1;
0065     }
0066     if (!out.open(QIODevice::WriteOnly)) {
0067         qDebug() << "Failed to create" << outfile;
0068         return -2;
0069     }
0070 
0071     while (in.bytesAvailable()) {
0072         QString line = QString::fromLocal8Bit(in.readLine());
0073         QRegularExpressionMatch match = imageReg.match(line);
0074         if (!match.hasMatch()) {
0075             // qDebug() << "No Match: " << line;
0076             out.write(qPrintable(line));
0077             continue;
0078         }
0079 
0080         QFileInfo info(match.captured(1));
0081 
0082         QString aliasLink = link(info.path(), info.fileName());
0083         if (aliasLink.isEmpty()) {
0084             // qDebug() << "No alias: " << line;
0085             out.write(qPrintable(line));
0086             continue;
0087         }
0088 
0089         QString newLine = QStringLiteral("<file alias=\"%1\">%2</file>\n").arg(match.captured(1), aliasLink);
0090         // qDebug() << newLine;
0091         out.write(qPrintable(newLine));
0092     }
0093     return 0;
0094 }
0095 
0096 int main(int argc, char *argv[])
0097 {
0098     QCoreApplication app(argc, argv);
0099 
0100     QCommandLineParser parser;
0101 
0102     QCommandLineOption inOption(QStringList() << QLatin1String("i") << QLatin1String("infile"), QStringLiteral("Input qrc file"), QStringLiteral("infile"));
0103     QCommandLineOption outOption(QStringList() << QLatin1String("o") << QLatin1String("outfile"), QStringLiteral("Output qrc file"), QStringLiteral("outfile"));
0104     parser.setApplicationDescription(
0105         QLatin1String("On Windows git handles symbolic links by converting them "
0106                       "to text files containing the links to the actual file. This application "
0107                       "takes a .qrc file as input and outputs a .qrc file with the symbolic "
0108                       "links converted to qrc-aliases."));
0109     parser.addHelpOption();
0110     parser.addVersionOption();
0111     parser.addOption(inOption);
0112     parser.addOption(outOption);
0113     parser.process(app);
0114 
0115     const QString inName = parser.value(inOption);
0116     const QString outName = parser.value(outOption);
0117 
0118     return parseFile(inName, outName);
0119 }