forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
garmin_fs.cc
318 lines (281 loc) · 9.31 KB
/
garmin_fs.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
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
/*
Implementation of special data used by Garmin products.
Copyright (C) 2006, 2007, 2008 Olaf Klein, [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 <cstdio> // for snprintf, sscanf
#include <cstdlib> // for strtod
#include <QString> // for QString, QStringLiteral
#include <QXmlStreamWriter> // for QXmlStreamWriter
#include <Qt> // for CaseInsensitive
#include "defs.h"
#include "garmin_fs.h"
#include "garmin_tables.h" // for gt_switch_display_mode_value, gt_display_mode_symbol, gt_display_mode_symbol_and_comment, gt_display_mode_symbol_and_name
#include "inifile.h" // for inifile_readstr
#define MYNAME "garmin_fs"
garmin_fs_t*
garmin_fs_alloc(const int protocol)
{
auto* result = new garmin_fs_t;
result->protocol = protocol;
return result;
}
garmin_fs_t* garmin_fs_t::clone() const
{
auto* copy = new garmin_fs_t(*this);
/* do not deep copy interlinks, only increment the reference counter */
if (ilinks != nullptr) {
ilinks->ref_count++;
}
#ifdef GMSD_EXPERIMENTAL
memcopy(subclass, other.subclass, sizeof(subclass));
#endif
return copy;
}
garmin_fs_t::~garmin_fs_t()
{
garmin_ilink_t* links;
if ((links = ilinks) != nullptr) {
links->ref_count--;
if (links->ref_count <= 0) {
while (links != nullptr) {
garmin_ilink_t* tmp = links;
links = links->next;
xfree(tmp);
}
}
}
}
/* GPX - out */
void
garmin_fs_xml_fprint(const Waypoint* waypt,
QXmlStreamWriter* writer)
{
garmin_fs_t* gmsd = garmin_fs_t::find(waypt);
if (gmsd == nullptr) {
return;
}
/* Find out if there is at least one field set */
QString addr = garmin_fs_t::get_addr(gmsd, "");
if (addr.isEmpty()) {
addr = garmin_fs_t::get_city(gmsd, "");
}
if (addr.isEmpty()) {
addr = garmin_fs_t::get_country(gmsd, "");
}
if (addr.isEmpty()) {
addr = garmin_fs_t::get_postal_code(gmsd, "");
}
if (addr.isEmpty()) {
addr = garmin_fs_t::get_state(gmsd, "");
}
QString phone = garmin_fs_t::get_phone_nr(gmsd, "");
if (!addr.isEmpty() || !phone.isEmpty() ||
(gmsd->flags.category && gmsd->category) ||
waypt->depth_has_value() ||
waypt->proximity_has_value() ||
waypt->temperature_has_value() ||
gmsd->flags.display) {
writer->writeStartElement(QStringLiteral("extensions"));
writer->writeStartElement(QStringLiteral("gpxx:WaypointExtension"));
writer->writeNamespace(QStringLiteral("http://www.garmin.com/xmlschemas/GpxExtensions/v3"),
"gpxx");
if (waypt->proximity_has_value()) {
writer->writeTextElement(QStringLiteral("gpxx:Proximity"), QString::number(waypt->proximity_value(), 'f', 6));
}
if (waypt->temperature_has_value()) {
writer->writeTextElement(QStringLiteral("gpxx:Temperature"), QString::number(waypt->temperature_value(), 'f', 6));
}
if (waypt->depth_has_value()) {
writer->writeTextElement(QStringLiteral("gpxx:Depth"), QString::number(waypt->depth_value(), 'f', 6));
}
if (gmsd->flags.display) {
const char* cx;
switch (gmsd->display) {
case gt_display_mode_symbol:
cx = "SymbolOnly";
break;
case gt_display_mode_symbol_and_comment:
cx = "SymbolAndDescription";
break;
default:
cx = "SymbolAndName";
break;
}
writer->writeTextElement(QStringLiteral("gpxx:DisplayMode"), cx);
}
if (gmsd->flags.category && gmsd->category) {
uint16_t cx = gmsd->category;
writer->writeStartElement(QStringLiteral("gpxx:Categories"));
for (int i = 0; i < 16; i++) {
if (cx & 1) {
writer->writeTextElement(QStringLiteral("gpxx:Category"), QStringLiteral("Category %1").arg(i+1));
}
cx = cx >> 1;
}
writer->writeEndElement(); // gpxx:Categories
}
if (!addr.isEmpty()) {
QString str;
writer->writeStartElement(QStringLiteral("gpxx:Address"));
if (!(str = garmin_fs_t::get_addr(gmsd, nullptr)).isEmpty()) {
writer->writeTextElement(QStringLiteral("gpxx:StreetAddress"), str);
}
if (!(str = garmin_fs_t::get_city(gmsd, nullptr)).isEmpty()) {
writer->writeTextElement(QStringLiteral("gpxx:City"), str);
}
if (!(str = garmin_fs_t::get_state(gmsd, nullptr)).isEmpty()) {
writer->writeTextElement(QStringLiteral("gpxx:State"), str);
}
if (!(str = garmin_fs_t::get_country(gmsd, nullptr)).isEmpty()) {
writer->writeTextElement(QStringLiteral("gpxx:Country"), str);
}
if (!(str = garmin_fs_t::get_postal_code(gmsd, nullptr)).isEmpty()) {
writer->writeTextElement(QStringLiteral("gpxx:PostalCode"), str);
}
writer->writeEndElement(); // /gpxx::Address
}
if (!phone.isEmpty()) {
writer->writeTextElement(QStringLiteral("gpxx:PhoneNumber"), phone);
}
writer->writeEndElement(); // /gpxx::WaypointExtension
writer->writeEndElement(); // /extensions.
}
}
void
garmin_fs_xml_convert(const int base_tag, int tag, const QString& qstr, Waypoint* waypt)
{
// FIXME: eliminate C string copy/use here:
const char *cdatastr = xstrdup(qstr);
garmin_fs_t* gmsd = garmin_fs_t::find(waypt);
if (gmsd == nullptr) {
gmsd = garmin_fs_alloc(-1);
waypt->fs.FsChainAdd(gmsd);
}
tag -= base_tag;
/*
tt_garmin_waypt_extension, -> 0
tt_garmin_proximity, -> 1
tt_garmin_temperature,-> 2
tt_garmin_depth, -> 3
tt_garmin_display_mode, -> 4
tt_garmin_categories, -> 5
tt_garmin_category, -> 6
tt_garmin_addr, -> 7
tt_garmin_city, -> 8
tt_garmin_state, -> 9
tt_garmin_country, -> 10
tt_garmin_postal_code, -> 11
tt_garmin_phone_nr, -> 12
*/
switch (tag) {
case 1:
if (*cdatastr) {
waypt->set_proximity(strtod(cdatastr, nullptr));
}
break;
case 2:
if (*cdatastr) {
waypt->set_temperature(strtod(cdatastr, nullptr));
}
break;
case 3:
if (*cdatastr) {
waypt->set_depth(strtod(cdatastr, nullptr));
}
break;
case 4:
if (case_ignore_strcmp(cdatastr, "SymbolOnly") == 0) {
garmin_fs_t::set_display(gmsd, gt_display_mode_symbol);
} else if (case_ignore_strcmp(cdatastr, "SymbolAndDescription") == 0) {
garmin_fs_t::set_display(gmsd, gt_display_mode_symbol_and_comment);
} else {
garmin_fs_t::set_display(gmsd, gt_display_mode_symbol_and_name);
}
break;
case 6:
if (! garmin_fs_merge_category(cdatastr, waypt)) {
// There's nothing a user can really do about this (well, they could
// create a gpsbabel.ini that mapped them to garmin category numbers
// but that feature is so obscure and used in so few outputs that
// there's no reason to alarm the user. Just silently disregard
// category names that don't map cleanly.
// warning(MYNAME ": Unable to convert category \"%s\"!\n", cdatastr);
}
break;
case 7:
garmin_fs_t::set_addr(gmsd, cdatastr);
break;
case 8:
garmin_fs_t::set_city(gmsd, cdatastr);
break;
case 9:
garmin_fs_t::set_state(gmsd, cdatastr);
break;
case 10:
garmin_fs_t::set_country(gmsd, cdatastr);
break;
case 11:
garmin_fs_t::set_postal_code(gmsd, cdatastr);
break;
case 12:
garmin_fs_t::set_phone_nr(gmsd, cdatastr);
break;
}
xfree(cdatastr);
}
unsigned char
garmin_fs_convert_category(const char* category_name, uint16_t* category)
{
int i;
int cat = 0;
// Is the name "Category" followed by a number? Use that number.
if ((case_ignore_strncmp(category_name, "Category ", 9) == 0) &&
(1 == sscanf(category_name + 9, "%d", &i)) &&
(i >= 1) && (i <= 16)) {
cat = (1 << --i);
} else if (global_opts.inifile != nullptr) {
// Do we have a gpsbabel.ini that maps category names to category #'s?
for (i = 0; i < 16; i++) {
QString key = QString::number(i + 1);
QString c = inifile_readstr(global_opts.inifile, GMSD_SECTION_CATEGORIES, key);
if (c.compare(category_name, Qt::CaseInsensitive) == 0) {
cat = (1 << i);
break;
}
}
}
if (cat == 0) {
return 0;
} else {
*category = cat;
return 1;
}
}
unsigned char
garmin_fs_merge_category(const char* category_name, Waypoint* waypt)
{
uint16_t cat;
// Attempt to get a textual category name to a category number.
if (!garmin_fs_convert_category(category_name, &cat)) {
return 0;
}
garmin_fs_t* gmsd = garmin_fs_t::find(waypt);
cat = cat | (garmin_fs_t::get_category(gmsd, 0));
if (gmsd == nullptr) {
gmsd = garmin_fs_alloc(-1);
waypt->fs.FsChainAdd(gmsd);
}
garmin_fs_t::set_category(gmsd, cat);
return 1;
}