-
Notifications
You must be signed in to change notification settings - Fork 7
/
readesm.cpp
122 lines (103 loc) · 4.51 KB
/
readesm.cpp
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
/** \file readesm.cpp
\brief contains main() and handling of command line arguments.
readesm mainly defines which files to read and parse. The actual parsing
of files is described in DataDefinitions.xml and the information about the parsing of
the individual blocks just there.
*/
/**
\mainpage Readesm - Digital Tachograph file reader
\section intro_scope Uses
This program takes an ESM file, downloaded from a digital tachograph or a driver card, and converts it into human-readable form, either html or simple plaintext. Since the data format is taken from an european community regulation (OJ:L:2002:207, p. 1-252, http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=OJ:L:2002:207:0001:0252:EN:PDF),
\section intro_comp Compiling
Readesm uses cmake, and a couple of files are auto-generated by a python script(generate.py)
\section intro_run Running the program
Start the readesm executable from the command line or from the menu
*/
#include "config.h"
#include "fileformat/EsmFile.h"
#include "fileformat/Reporter/HtmlReporter.h"
#include "gui/mainWindow.h"
#include "OptionParser.h"
#include <QtCore/QByteArray>
#include <QtCore/QFile>
#include <QtCore/QLocale>
#include <QtCore/QString>
#include <QtCore/QTextStream>
#include <QtCore/QTranslator>
#include <QtGui/QApplication>
int main(int argc, char** argv)
{
QTextStream cerr(stderr);
QTextStream cout(stdout);
OptionParser parser;
parser.addOption("format","f","Format used for output");
parser.addOption("locale","l","Language used for output");
parser.parseArgs(argc,argv);
if(parser.checkFlag("help")){
cout << "Readesm " << QString("%1 (%2)")
.arg(VERSION)
.arg(VERSION_DATE);
cout << "\n\n Readesm takes an ESM file, as downloaded from a digital tachograph or a driver card, a compulsory equipment for trucks heavier than 3.5 tons within the European Union and converts it into human-readable form.\n\n";
cout << "usage: Readesm [OPTIONS] [INPUT FILE [OUTPUT FILE]]\n\n";
cout << parser.helpText();
cout << "\n\n Being a QT (http://qt.nokia.com/) application, you can also pass qt command line parameters to Readesm.";
cout << "\n\n 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 3 of the License, or (at your option) any later version.\n\n 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.\n";
return 0;
}
bool gui = true;
if(parser.argumentCount() >= 2) gui = false;
QApplication app(argc, argv, gui);
app.setApplicationName("Readesm");
app.setApplicationVersion(QString("%1 (%2)")
.arg(VERSION)
.arg(VERSION_DATE));
app.setOrganizationName(QString::fromUtf8("Andreas Gölzer"));
app.setOrganizationDomain("http://andreas.goelzer.de");
QTranslator qTranslator;
QString locale = parser.get("locale", QLocale::system().name());
if(locale != "C"){
QString trans = "readesm_" + locale;
if(!qTranslator.load(trans) && !qTranslator.load(trans, QCoreApplication::applicationDirPath() + "/../translations") && !qTranslator.load(trans, PREFIX "/share/readesm/translations")) {
cerr << "Could not load internationalization files for your locale :(" << endl;
}
app.installTranslator(&qTranslator);
}
if(!gui){
QString inputFile(parser.getArgument(0));
QString outputFile(parser.getArgument(1));
bool converted = false;
EsmFile esm(inputFile);
QByteArray outData;
//determine extension of output file
QString extension(outputFile.section(".",-1).toLower());
if(parser.get("format") == "html" || extension == "html" || extension == "htm" || extension == "xhtml") {
HtmlReporter rep;
rep << esm;
outData = rep.toQByteArray();
converted = true;
}
if(!converted) {
cerr << "Format for output not recognized." << endl;
return 1;
}
if(outputFile != "-"){
QFile out(outputFile);
out.open(QIODevice::WriteOnly | QIODevice::Text);
if(!out.isOpen()) {
cerr << "Could not open file " << outputFile << " for output. dumping to stdout" << endl;
cout << outData;
return 1;
} else out.write(outData);
} else {
cout << outData;
}
} else if(parser.argumentCount() < 2){
mainWindow widgetMainWindow;
widgetMainWindow.show();
if(parser.argumentCount() == 1) widgetMainWindow.openFile(parser.getArgument(0));
return app.exec();
} else {
cerr << "too many arguments to readesm";
return 1;
}
}