forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.cc
280 lines (244 loc) · 8.22 KB
/
geo.cc
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
/*
Copyright (C) 2002 Robert Lipe, [email protected]
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 2 of the License, or
(at your option) any later version.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QByteArray> // for QByteArray
#include <QIODevice> // for QIODevice
#include <QString> // for QString, operator==, QStringView::to...
#include <QStringView> // for QStringView
#include <QVector> // for QVector
#include <QXmlStreamAttributes> // for QXmlStreamAttributes
#include <QXmlStreamReader> // for QXmlStreamReader
#include <QXmlStreamWriter> // for QXmlStreamWriter, QXmlStreamReader::...
#include <QtCore> // for qPrintable, QIODeviceBase::ReadOnly
#include "defs.h"
#include "gbfile.h" // for gbfclose, gbfopen, gbfputs, gbfile
#include "geocache.h" // for Geocache, Geocache::container_t, Geo...
#include "src/core/file.h" // for File
static char* deficon = nullptr;
static char* nuke_placer;
static gbfile* ofd;
static QString ostring;
static QXmlStreamWriter writer(&ostring);
static
QVector<arglist_t> geo_args = {
{"deficon", &deficon, "Default icon name", nullptr, ARGTYPE_STRING, ARG_NOMINMAX, nullptr },
{"nuke_placer", &nuke_placer, "Omit Placer name", nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr },
};
#define MYNAME "geo"
// This really should be class-local...
static QXmlStreamReader reader;
static QString geo_read_fname;
static Geocache::container_t wpt_container(const QString&);
static void GeoReadLoc()
{
Waypoint* wpt = nullptr;
QString current_tag;
while (!reader.atEnd()) {
auto tag_name = reader.name();
if (reader.tokenType()==QXmlStreamReader::StartElement) {
current_tag.append("/");
current_tag.append(tag_name);
if (current_tag == u"/loc/waypoint") {
wpt = new Waypoint;
wpt->AllocGCData();
// There is no 'unknown' alt value and so many reference files have
// leaked it that we just paper over that here.
wpt->altitude = 0;
} else if (current_tag == u"/loc/waypoint/name") {
QXmlStreamAttributes a = reader.attributes();
wpt->shortname = a.value("id").toString();
wpt->description = reader.readElementText();
} else if (current_tag == u"/loc/waypoint/coord") {
QXmlStreamAttributes a = reader.attributes();
wpt->latitude = a.value("lat").toDouble();
wpt->longitude = a.value("lon").toDouble();
} else if (current_tag == u"/loc/waypoint/type") {
wpt->icon_descr = reader.readElementText();
} else if (current_tag == u"/loc/waypoint/link") {
QXmlStreamAttributes a = reader.attributes();
waypt_add_url(wpt,
reader.readElementText(), a.value("text").toString());
} else if (current_tag == u"/loc/waypoint/difficulty") {
wpt->gc_data->diff = reader.readElementText().toDouble() * 10;
} else if (current_tag == u"/loc/waypoint/terrain") {
wpt->gc_data->terr = reader.readElementText().toDouble() * 10;
} else if (current_tag == u"/loc/waypoint/container") {
wpt->gc_data->container = wpt_container(reader.readElementText());
}
}
// The tokenType may have changed to EndElement as a result of readElementText.
if (reader.tokenType() == QXmlStreamReader::EndElement) {
if (current_tag == u"/loc/waypoint") {
waypt_add(wpt);
}
current_tag.chop(tag_name.length() + 1);
}
reader.readNext();
}
}
static void
geo_rd_init(const QString& fname)
{
geo_read_fname = fname;
}
static void
geo_read()
{
gpsbabel::File file(geo_read_fname);
file.open(QIODevice::ReadOnly);
reader.setDevice(&file);
GeoReadLoc();
if (reader.hasError()) {
fatal(MYNAME ":Read error: %s (%s, line %ld, col %ld)\n",
qPrintable(reader.errorString()),
qPrintable(file.fileName()),
(long) reader.lineNumber(),
(long) reader.columnNumber());
}
}
Geocache::container_t wpt_container(const QString& args)
{
Geocache::container_t v;
switch (args.toInt()) {
case 1:
v = Geocache::container_t::gc_unknown;
break;
case 2:
v = Geocache::container_t::gc_micro;
break;
case 3:
v = Geocache::container_t::gc_regular;
break;
case 4:
v = Geocache::container_t::gc_large;
break;
case 5:
v = Geocache::container_t::gc_virtual;
break;
case 6:
v = Geocache::container_t::gc_other;
break;
case 8:
v = Geocache::container_t::gc_small;
break;
default:
v = Geocache::container_t::gc_unknown;
break;
}
return v;
}
static void
geo_rd_deinit()
{
}
static void
geo_wr_init(const QString& fname)
{
ofd = gbfopen(fname, "w", MYNAME);
//writer.setAutoFormatting(true);
writer.setAutoFormattingIndent(0);
writer.writeStartDocument();
}
static void
geo_wr_deinit()
{
writer.writeEndDocument();
gbfputs(ostring,ofd);
gbfclose(ofd);
ofd = nullptr;
}
static void
geo_waypt_pr(const Waypoint* waypointp)
{
writer.writeStartElement(QStringLiteral("waypoint"));
writer.writeStartElement(QStringLiteral("name"));
writer.writeAttribute(QStringLiteral("id"), waypointp->shortname);
// TODO: this could be writeCharacters, but it's here for compat with pre
// Qt writer.
writer.writeCDATA(waypointp->description);
writer.writeEndElement();
writer.writeStartElement(QStringLiteral("coord"));
writer.writeAttribute(QStringLiteral("lat"), QString::number(waypointp->latitude, 'f'));
writer.writeAttribute(QStringLiteral("lon"), QString::number(waypointp->longitude, 'f'));
writer.writeEndElement();
writer.writeTextElement(QStringLiteral("type"), deficon ? deficon : waypointp->icon_descr);
if (waypointp->HasUrlLink()) {
writer.writeStartElement(QStringLiteral("link"));
writer.writeAttribute(QStringLiteral("text "), QStringLiteral("Cache Details"));
UrlLink link = waypointp->GetUrlLink();
writer.writeCharacters(link.url_);
writer.writeEndElement();
}
if (waypointp->gc_data && waypointp->gc_data->diff) {
writer.writeTextElement(QStringLiteral("difficulty"),
QString::number(waypointp->gc_data->diff/10));
writer.writeTextElement(QStringLiteral("terrain"),
QString::number(waypointp->gc_data->terr/10));
int v = 1;
switch (waypointp->gc_data->container) {
case Geocache::container_t::gc_unknown:
v = 1;
break;
case Geocache::container_t::gc_micro:
v = 2;
break;
case Geocache::container_t::gc_regular:
v = 3;
break;
case Geocache::container_t::gc_large:
v = 4;
break;
case Geocache::container_t::gc_virtual:
v = 5;
break;
case Geocache::container_t::gc_other:
v = 6;
break;
case Geocache::container_t::gc_small:
v = 8;
break;
default:
v = 1;
break;
}
writer.writeTextElement(QStringLiteral("container"),
QString::number(v));
}
writer.writeEndElement();
}
static void
geo_write()
{
writer.writeStartElement(QStringLiteral("loc"));
writer.writeAttribute(QStringLiteral("version"), QStringLiteral("1.0"));
// TODO: This could be moved to wr_init, but the pre GPX version put the two
// lines above this, so mimic that behaviour exactly.
writer.setAutoFormatting(true);
writer.writeAttribute(QStringLiteral("src"), QStringLiteral("EasyGPS"));
waypt_disp_all(geo_waypt_pr);
writer.writeEndElement();
}
ff_vecs_t geo_vecs = {
ff_type_file,
{ (ff_cap)(ff_cap_read | ff_cap_write), ff_cap_none, ff_cap_none },
geo_rd_init,
geo_wr_init,
geo_rd_deinit,
geo_wr_deinit,
geo_read,
geo_write,
nullptr,
&geo_args,
NULL_POS_OPS
};