1 /*
2     Standard RIFF MIDI File dump program
3     Copyright (C) 2006-2021, Pedro Lopez-Cabanillas <plcl@users.sf.net>
4 
5     This library is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 3 of the License, or
8     (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <QCoreApplication>
20 #include <QCommandLineParser>
21 #include <QCoreApplication>
22 #include <QFileInfo>
23 #include <QObject>
24 #include <QString>
25 #include <QTextCodec>
26 #include <QTextStream>
27 #include <cstdlib>
28 #include "dumprmi.h"
29 
30 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
31 #define right Qt::right
32 #define left Qt::left
33 #define endl Qt::endl
34 #endif
35 
36 using namespace drumstick::File;
37 
main(int argc,char ** argv)38 int main(int argc, char **argv)
39 {
40     const QString PGM_NAME = QStringLiteral("drumstick-dumprmi");
41     const QString PGM_DESCRIPTION = QStringLiteral("Drumstick command line utility for decoding RMI (RIFF MIDI) files");
42     QTextStream cerr(stderr, QIODevice::WriteOnly);
43 
44     DumpRmid spy;
45     QCoreApplication app(argc, argv);
46     QCoreApplication::setApplicationName(PGM_NAME);
47     QCoreApplication::setApplicationVersion(QStringLiteral(QT_STRINGIFY(VERSION)));
48 
49     QCommandLineParser parser;
50     parser.setApplicationDescription(PGM_DESCRIPTION);
51     auto helpOption = parser.addHelpOption();
52     auto versionOption = parser.addVersionOption();
53     QCommandLineOption extractOption({{"e", "extract"}, "Extracts the embedded standard MIDI file."});
54     parser.addOption(extractOption);
55 
56     parser.addPositionalArgument("file", "Input RMI file name.", "files...");
57     parser.process(app);
58 
59     if (parser.isSet(versionOption) || parser.isSet(helpOption)) {
60         return 0;
61     }
62 
63     spy.setExtract(parser.isSet(extractOption));
64 
65     QStringList fileNames, positionalArgs = parser.positionalArguments();
66     if (positionalArgs.isEmpty()) {
67         cerr << "Input file name(s) missing" << endl;
68         parser.showHelp();
69     }
70 
71     foreach(const QString& a, positionalArgs) {
72         QFileInfo f(a);
73         if (f.exists())
74             fileNames += f.canonicalFilePath();
75         else
76             cerr << "File not found: " << a << endl;
77     }
78 
79     int totalErrors = 0;
80     foreach(const QString& file, fileNames) {
81         spy.run(file);
82         totalErrors += spy.numErrors();
83     }
84     return totalErrors;
85 }
86