File indexing completed on 2024-05-19 15:23:03

0001 /*
0002     This file is part of LibSyndication.
0003     SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0006 */
0007 
0008 #include "documentsource.h"
0009 #include "feed.h"
0010 #include "parsercollection.h"
0011 #include "specificdocument.h"
0012 
0013 #include <QByteArray>
0014 #include <QDebug>
0015 #include <QFile>
0016 #include <QString>
0017 
0018 #include <iostream>
0019 
0020 using namespace Syndication;
0021 
0022 void printUsage(const QString &error)
0023 {
0024     std::cerr << "testlibsyndication - (C) Frank Osterfeld 2006" << std::endl;
0025     std::cerr << std::endl;
0026     if (!error.isNull()) {
0027         std::cerr << error.toUtf8().data() << std::endl;
0028         std::cerr << std::endl;
0029     } else {
0030         std::cerr << "Prints the parsed content of a feed file to standard output." << std::endl;
0031         std::cerr << std::endl;
0032     }
0033     std::cerr << "Usage: testlibsyndication [--specific-format] <file> [--compare <expectedfile>]" << std::endl;
0034     std::cerr << std::endl;
0035     std::cerr << "--specific-format: If set, the debug output for the specific" << std::endl;
0036     std::cerr << "feed format is printed to stdout, otherwise the debug output" << std::endl;
0037     std::cerr << "for the abstraction" << std::endl;
0038 }
0039 
0040 int main(int argc, char **argv)
0041 {
0042     qputenv("LC_ALL", "C");
0043 
0044     int pcompare = 2;
0045     if (argc < 2) {
0046         printUsage(QStringLiteral("filename expected"));
0047         return 1;
0048     }
0049 
0050     QString filename(QFile::decodeName(argv[1]));
0051 
0052     bool specificformat = false;
0053 
0054     if (filename == QLatin1String("--specific-format")) {
0055         if (argc < 3) {
0056             printUsage(QStringLiteral("filename expected"));
0057             return 1;
0058         }
0059         filename = QFile::decodeName(argv[2]);
0060         specificformat = true;
0061         pcompare += 1;
0062     }
0063 
0064     QString expfname;
0065 
0066     if (argc >= (pcompare + 1) //
0067         && QString::fromLatin1(argv[pcompare]) == QLatin1String("--compare")) {
0068         expfname = QString::fromLatin1(argv[pcompare + 1]);
0069     }
0070 
0071     QFile f(filename);
0072 
0073     if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
0074         printUsage(QStringLiteral("Couldn't open file"));
0075         return 1;
0076     }
0077 
0078     DocumentSource src(f.readAll(), QStringLiteral("http://libsyndicationtest"));
0079     f.close();
0080 
0081     FeedPtr ptr(Syndication::parse(src));
0082 
0083     if (ptr == nullptr) {
0084         printUsage(QStringLiteral("Couldn't parse file: (%1)").arg(Syndication::parserCollection()->lastError()));
0085         return 1;
0086     }
0087 
0088     QString res = specificformat ? ptr->specificDocument()->debugInfo() : ptr->debugInfo();
0089 #if 0
0090     QFile headerFile(QStringLiteral("/tmp/bb.txt"));
0091     headerFile.open(QIODevice::WriteOnly | QIODevice::Text);
0092     QTextStream outHeaderStream(&headerFile);
0093     outHeaderStream << res.toLatin1().trimmed();
0094     headerFile.close();
0095 #endif
0096     if (expfname.isNull()) {
0097         std::cout << res.toUtf8().data() << std::endl;
0098         return 0;
0099     } else {
0100         QFile expFile(expfname);
0101         expFile.open(QIODevice::ReadOnly | QIODevice::Text);
0102         QByteArray expected = expFile.readAll();
0103         expFile.close();
0104         if (expected.trimmed() != res.toUtf8().trimmed()) {
0105             qDebug() << "Obtained:\n" << res.toUtf8().trimmed();
0106             qDebug() << "Expected:\n" << expected.trimmed();
0107             return 1;
0108         }
0109         return 0;
0110     }
0111 
0112     return 0;
0113 }