This repository was archived by the owner on Sep 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpxrm.cpp
More file actions
341 lines (280 loc) · 7.98 KB
/
gpxrm.cpp
File metadata and controls
341 lines (280 loc) · 7.98 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <iostream>
#include <cstring>
#include <fstream>
#include "XMLParser.h"
const std::string version= "0.1.0";
// ----------------------------------------------------------------------------
class GpxRm : public XMLParserHandler
{
public:
// -- Constructor -----------------------------------------------------------
GpxRm() :
_outputFile(&std::cout),
_segmentNr(0),
_inWaypoint(false),
_inRoute(false),
_inTrack(false),
_inSegment(false)
{
}
// -- Deconstructor ---------------------------------------------------------
virtual ~GpxRm()
{
}
// -- Properties ------------------------------------------------------------
void setWaypointName(const std::string &name) { _waypointName = name; }
const std::string &waypointName() const { return _waypointName; }
void setTrackName(const std::string &name) { _trackName = name; }
const std::string &trackName() const { return _trackName; }
void setSegmentNr(int nr) { _segmentNr = nr; }
int segmentNr() const { return _segmentNr; }
void setRouteName(const std::string &name) { _routeName = name; }
const std::string &routeName() const { return _routeName; }
// -- Parse a file ----------------------------------------------------------
bool parseFile(std::istream &input, std::ostream &output)
{
_path.clear();
_outputFile = &output;
XMLParser parser(this);
parser.parse(input);
return true;
}
private:
void log(const std::string &text)
{
if (_inWaypoint || _inRoute || _inTrack || _inSegment)
{
_currentText.append(text);
}
else
{
*_outputFile << text;
}
}
void doStartElement(const std::string &name)
{
_path.append("/");
_path.append(name);
if (_path == "/gpx/wpt")
{
if (!_waypointName.empty()) _inWaypoint = true;
_currentText.clear();
_currentName.clear();
_currentSegmentNr = 0;
}
else if (_path == "/gpx/rte")
{
if (!_routeName.empty()) _inRoute = true;
_currentText.clear();
_currentName.clear();
_currentSegmentNr = 0;
}
else if (_path == "/gpx/trk")
{
if (!_trackName.empty() && _segmentNr == 0) _inTrack = true;
_currentText.clear();
_currentName.clear();
_currentSegmentNr = 0;
}
else if (_path == "/gpx/trk/trkseg")
{
_currentSegmentNr++;
if (_segmentNr != 0 && _segmentNr == _currentSegmentNr)
{
_currentText.clear();
_inSegment = true;
}
}
}
void doEndElement()
{
if (_path == "/gpx/wpt")
{
if (_inWaypoint && _currentName != _waypointName) *_outputFile << _currentText;
_inWaypoint = false;
}
else if (_path == "/gpx/rte")
{
if (_inRoute && _currentName != _routeName) *_outputFile << _currentText;
_inRoute = false;
}
else if (_path == "/gpx/trk")
{
if (_inTrack && _currentName != _trackName) *_outputFile << _currentText;
_inTrack = false;
}
else if (_path == "/gpx/trk/trkseg")
{
if (_inSegment && _currentName != _trackName) *_outputFile << _currentText;
_inSegment = false;
}
size_t i = _path.find_last_of('/');
if (i != std::string::npos) _path.erase(i);
}
public:
// -- Callbacks -------------------------------------------------------------
virtual void xmlDecl(const std::string &text, const Attributes &)
{
log(text);
}
virtual void processingInstruction(const std::string &text, const std::string &, const std::string &)
{
log(text);
}
virtual void docTypeDecl(const std::string &text)
{
log(text);
}
virtual void unhandled(const std::string &text, int lineNumber, int columnNumber)
{
std::cerr << " ERROR: Unexpected gpx info: " << text << " on line: " << lineNumber << " columnNumber: " << columnNumber << std::endl;
exit(1);
}
virtual void cdataDecl(const std::string &text, const std::string &)
{
log(text);
}
virtual void comment(const std::string &text, const std::string &)
{
log(text);
}
virtual void startEndElement(const std::string &text, const std::string &name, const Attributes &)
{
doStartElement(name);
log(text);
doEndElement();
}
virtual void startElement(const std::string &text, const std::string &name, const Attributes &)
{
doStartElement(name);
log(text);
}
virtual void text(const std::string &text)
{
if (_path == "/gpx/wpt/name" || _path == "/gpx/rte/name" || _path == "/gpx/trk/name")
{
_currentName = XMLParser::translateEntityRefs(XMLParser::trim(text));
}
log(text);
}
virtual void endElement(const std::string &text, const std::string &)
{
log(text);
doEndElement();
}
private:
// Members
std::ostream *_outputFile;
std::string _waypointName;
std::string _trackName;
int _segmentNr; // 1..
std::string _routeName;
std::string _path;
bool _inWaypoint;
bool _inRoute;
bool _inTrack;
std::string _currentText;
std::string _currentName;
bool _inSegment;
int _currentSegmentNr;
};
// -- Main program ------------------------------------------------------------
int main(int argc, char *argv[])
{
GpxRm gpxrm;
std::string outputFilename;
int i = 1;
while (i < argc)
{
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0)
{
std::cout << "Usage: gpxrm [-h] [-v] [-w \"<name>\"] [-t \"<name>\" [-s <segment>]] [-r \"<name>\"] [-o <out.gpx>] <file.gpx>" << std::endl;
std::cout << " -h help" << std::endl;
std::cout << " -v show version" << std::endl;
std::cout << " -w \"name\" remove the waypoint with <name>" << std::endl;
std::cout << " -t \"<name>\" remove the track with <name>" << std::endl;
std::cout << " -s <segment> remove only segment in track (1..)" << std::endl;
std::cout << " -r \"<name>\" remove the route with <name>" << std::endl;
std::cout << " -o <out.gpx> the output gpx file (overwrites existing file)" << std::endl;
std::cout << " file.gpx the input gpx file" << std::endl << std::endl;
std::cout << " Remove a waypoint, track or route from a gpx file" << std::endl;
return 0;
}
else if (strcmp(argv[i], "-v") == 0)
{
std::cout << "gpxrm v" << version << std::endl;
return 0;
}
else if (strcmp(argv[i], "-w") == 0 && i+1 < argc)
{
gpxrm.setWaypointName(argv[++i]);
}
else if (strcmp(argv[i], "-t") == 0 && i+1 < argc)
{
gpxrm.setTrackName(argv[++i]);
}
else if (strcmp(argv[i], "-s") == 0 && i+1 < argc)
{
if (gpxrm.trackName().empty())
{
std::cerr << "Error: segment number without track filter" << std::endl;
return 1;
}
else
{
gpxrm.setSegmentNr(atoi(argv[++i]));
}
}
else if (strcmp(argv[i], "-r") == 0 && i < argc)
{
gpxrm.setRouteName(argv[++i]);
}
else if (strcmp(argv[i], "-o") == 0 && i < argc)
{
if (outputFilename.empty())
{
outputFilename = argv[++i];
}
else
{
std::cerr << "Error: output file specified twice." << std::endl;
return 1;
}
}
else if (argv[i][0] != '-')
{
std::ifstream stream(argv[i]);
if (!stream.is_open())
{
std::cerr << "Error: unable to open: " << argv[i] << std::endl;
return 1;
}
if (outputFilename.empty())
{
gpxrm.parseFile(stream, std::cout);
}
else
{
std::ofstream output(outputFilename.c_str());
if (output.is_open())
{
gpxrm.parseFile(stream, output);
output.close();
}
else
{
std::cerr << "Error: unable to open the outputfile: " << outputFilename << std::endl;
return 1;
}
}
break;
}
else
{
std::cerr << "Error: unknown option:" << argv[i] << std::endl;
return 1;
}
i++;
}
return 0;
}