-
Notifications
You must be signed in to change notification settings - Fork 0
/
adif2xml.cpp
208 lines (188 loc) · 6.45 KB
/
adif2xml.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "adif2xml.hpp"
#include "adif2xml_version.hpp"
#include "getopt.h"
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/sax/AttributeList.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include "date/date.h"
#include "adifio.hpp"
#include "ediio.hpp"
#include "attributelistimpl.hpp"
#include "SAXPrint.hpp"
using namespace std;
using namespace date;
using namespace ADIFIO;
using namespace EDIIO;
using namespace XERCES_CPP_NAMESPACE;
static void help(const char *name)
{
cerr << "Usage: " << name << endl
<< "\t-i <filename> ............ Read ADIF file from <filename> (default: STDIN)" << endl
<< "\t-o <filename> ............ Write XMLfile to <filename> (defalt: STDOUT)" << endl
<< "\t-v ....................... Write verbose messages to STDERR" << endl
<< "\t-h ....................... Print help (this message)" << endl;
}
static void headerCB(const ObjectMap_t &tokens, void *userData)
{
SAXPrintHandlers &ph{*static_cast<SAXPrintHandlers*>(userData)};
AttributeListImpl empty_list;
ph.stream() << " ";
ph.startElement(X("Header"), empty_list);
ph.stream() << endl;
for_each(tokens.begin(), tokens.end(), [&](auto &pair) {
const string &key{pair.first};
const Object &obj{pair.second};
const string str{obj.to_string()};
AttributeListImpl rg_a;
rg_a.add("type", Object::getObjectTypeName(obj.type).c_str());
ph.stream() << " ";
ph.startElement(X(key.c_str()), rg_a);
ph.characters(X(str.c_str()), str.length());
ph.endElement(X(key.c_str()));
ph.stream() << endl;
}); // end for_each //
ph.stream() << " ";
ph.endElement(X("Header"));
ph.stream() << endl;
#if 0
ostream &oss{*static_cast<ostream*>(userData)};
oss << "Header:" << endl;
for_each(tokens.begin(), tokens.end(), [&](auto &pair) {
const string &key{pair.first};
const Object &val{pair.second};
oss << key << " = " << val.to_string() << endl;
}); // end for_each //
#endif
}
static void recordCB(const ObjectMap_t &tokens, void *userData)
{
SAXPrintHandlers &ph{*static_cast<SAXPrintHandlers*>(userData)};
AttributeListImpl empty_list;
ph.stream() << " ";
ph.startElement(X("Record"), empty_list);
ph.stream() << endl;
for_each(tokens.begin(), tokens.end(), [&](auto &pair) {
const string &key{pair.first};
const Object &obj{pair.second};
const string str{obj.to_string()};
AttributeListImpl rg_a;
rg_a.add("type", Object::getObjectTypeName(obj.type).c_str());
ph.stream() << " ";
ph.startElement(X(key.c_str()), rg_a);
ph.characters(X(str.c_str()), str.length());
ph.endElement(X(key.c_str()));
ph.stream() << endl;
}); // end for_each //
ph.stream() << " ";
ph.endElement(X("Record"));
ph.stream() << endl;
#if 0
ostream &oss{*static_cast<ostream*>(userData)};
#define EDI
#ifdef EDI
write_qso_record(oss, tokens);
#else
oss << "Record:" << endl;
for_each(tokens.begin(), tokens.end(), [&](auto &pair) {
const string &key{pair.first};
const Object &val{pair.second};
oss << key << " = " << val.to_string() << endl;
}); // end for_each //
#endif
#endif
}
int main(int argc, char* argv[]) {
int option{0}, verbose{0};
string inputfile{""}, outputfile{""};
// Get options:
while ((option = getopt(argc, argv, "hi:o:v")) >= 0) {
switch (option) {
case 'h':
help(argv[0]);
return EXIT_SUCCESS;
case 'i':
if (inputfile.length() > 0) {
help(argv[0]);
return EXIT_FAILURE;
}
inputfile = optarg;
break;
case 'o':
if (outputfile.length() > 0) {
help(argv[0]);
return EXIT_FAILURE;
}
outputfile = optarg;
break;
case 'v':
++verbose;
break;
default:
help(argv[0]);
return EXIT_FAILURE;
} // end switch //
} // end while //
if (verbose)
cerr << "This is " APP_NAME << " version " << APP_VERSION << endl
<< " Copyright (c) " << APP_COPYRIGHT << " see <" << APP_WEBSITE << ">" << endl;
bool ok{true};
try {
istream *input{(inputfile.length() > 0) ? new ifstream(inputfile) : &cin};
ostream *output{(outputfile.length() > 0) ? new ofstream(outputfile) : &cout};
if (inputfile.length() > 0) {
if (!filesystem::exists(inputfile))
throw runtime_error("File not found: \"" + inputfile + "\"");
if (input->bad())
throw runtime_error("Bad inputfile: \"" + inputfile + "\"");
}
if (verbose)
cerr << "Reading ADIF from "
<< ((inputfile.length() > 0) ? inputfile : "<STDIN>")
<< endl;
if (outputfile.length() > 0) {
if (output->bad())
throw runtime_error("Bad outputfile: \"" + outputfile + "\"");
}
if (verbose)
cerr << "Writing XML to "
<< ((outputfile.length() > 0) ? outputfile : "<STDOUT>")
<< endl;
XMLPlatformUtils::Initialize();
ADIFIO::Reader r;
XMLFormatter::UnRepFlags unrepFlags{XMLFormatter::UnRep_CharRef};
SAXPrintHandlers printHandlers(*output, "UTF-8", unrepFlags);
printHandlers.startDocument();
AttributeListImpl empty_attribute_list;
printHandlers.startElement(X("ADIF"), empty_attribute_list);
printHandlers.stream() << endl;
r.readADI(*input, recordCB, &printHandlers, headerCB, &printHandlers);
printHandlers.endElement(X("ADIF"));
printHandlers.stream() << endl;
printHandlers.endDocument();
}
catch (const OutOfMemoryException&)
{
cerr << "OutOfMemoryException" << endl;
ok = false;
}
catch (const XMLException& toCatch)
{
cerr << "XML error:" << endl
<< StrX(toCatch.getMessage()) << endl;
ok = false;
}
catch (const exception& ex) {
cerr << "ERROR: " << ex.what() << endl;
ok = false;
}
catch (...) {
cerr << "ERROR: Unidentified failure!" << endl;
ok = false;
}
XMLPlatformUtils::Terminate();
return (ok) ? EXIT_SUCCESS : EXIT_FAILURE;
}