/
navicache.c
281 lines (245 loc) · 6.1 KB
/
navicache.c
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
/*
Copyright (C) 2003 Robert Lipe, robertlipe@usa.net
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., 59 Temple Place - Suite 330, Boston, MA 02111 USA
*/
#include "defs.h"
#include "cet_util.h"
#if HAVE_LIBEXPAT
#include <expat.h>
static XML_Parser psr;
#endif
static waypoint *wpt_tmp;
static gbfile *fin, *fout;
static char *noretired = NULL;
static
arglist_t nav_args[] = {
{"noretired", &noretired, "Suppress retired geocaches",
NULL, ARGTYPE_BOOL, ARG_NOMINMAX },
ARG_TERMINATOR
};
#define MYNAME "navicache"
#define MY_CBUF 4096
#if ! HAVE_LIBEXPAT
static void
nav_rd_init(const char *fname)
{
fatal(MYNAME ": This build excluded GPX support because expat was not installed.\n");
}
static void
nav_read(void)
{
}
#else
static struct
nc_type_mapping{
geocache_type type;
const char *name;
} nc_type_map[] = {
{ gt_unknown, "unknown" },
{ gt_traditional, "normal" },
{ gt_multi, "Multi-part" },
{ gt_virtual, "Virtual" },
{ gt_event, "event" }
};
static struct
nc_container_mapping{
geocache_container type;
const char *name;
} nc_container_map[] = {
{ gc_other, "Unknown" },
{ gc_micro, "Micro" },
{ gc_regular, "Normal" },
{ gc_large, "Large" },
{ gc_virtual, "Virtual" }
};
static
geocache_type
nc_mktype(const char *t)
{
int i;
int sz = sizeof(nc_type_map) / sizeof(nc_type_map[0]);
for (i = 0; i < sz; i++) {
if (0 == case_ignore_strcmp(t, nc_type_map[i].name)) {
return nc_type_map[i].type;
}
}
return gt_unknown;
}
static
geocache_container
nc_mkcont(const char *t)
{
int i;
int sz = sizeof(nc_container_map) / sizeof(nc_container_map[0]);
for (i = 0; i < sz; i++) {
if (0 == case_ignore_strcmp(t, nc_container_map[i].name)) {
return nc_container_map[i].type;
}
}
return gc_unknown;
}
static void
nav_start(void *data, const XML_Char *xml_el, const XML_Char **xml_attr)
{
const char *el;
const char **attr;
el = xml_convert_to_char_string(xml_el);
attr = xml_convert_attrs_to_char_string(xml_attr);
if (0 == strcmp(el, "CacheDetails")) {
const char **ap;
geocache_data *gc_data;
wpt_tmp = waypt_new();
gc_data = waypt_alloc_gc_data(wpt_tmp);
for (ap = attr; *ap; ap+=2) {
if (0 == strcmp(ap[0], "cache_id")) {
wpt_tmp->shortname = xstrdup(ap[1]);
} else
if (0 == strcmp(ap[0], "name")) {
wpt_tmp->description = xstrdup(ap[1]);
} else
if (0 == strcmp(ap[0], "latitude")) {
sscanf(ap[1], "%lf",
&wpt_tmp->latitude);
} else
if (0 == strcmp(ap[0], "longitude")) {
sscanf(ap[1], "%lf",
&wpt_tmp->longitude);
} else
if (0 == strcmp(ap[0], "longitude")) {
sscanf(ap[1], "%lf",
&wpt_tmp->longitude);
} else
if (0 == strcmp(ap[0], "difficulty")) {
float x;
sscanf(ap[1], "%f", &x);
gc_data->diff = x * 10;
} else
if (0 == strcmp(ap[0], "terrain")) {
float x;
sscanf(ap[1], "%f", &x);
gc_data->terr = x * 10;
} else
if (0 == strcmp(ap[0], "cache_type")) {
static char buf[512];
gc_data->type = nc_mktype(ap[1]);
if (!strcmp(ap[1], "normal"))
wpt_tmp->icon_descr = "Geocache-regular";
else if (!strcmp(ap[1], "multi-part"))
wpt_tmp->icon_descr = "Geocache-multi";
else if (!strcmp(ap[1], "moving_travelling"))
wpt_tmp->icon_descr = "Geocache-moving";
else {
sprintf(buf, "Geocache-%-.20s", ap[1]);
wpt_tmp->icon_descr = xstrdup(buf);
}
} else
if (0 == strcmp(ap[0], "hidden_date")) {
struct tm tm;
sscanf(ap[1], "%d-%d-%d",
&tm.tm_year,
&tm.tm_mon,
&tm.tm_mday);
tm.tm_mon -= 1;
tm.tm_year -= 1900;
tm.tm_isdst = 0;
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
wpt_tmp->creation_time = mktime(&tm);
} else
if (0 == strcmp(ap[0], "retired")) {
if (!strcmp(ap[1], "yes") && noretired) {
xfree(wpt_tmp);
return;
}
} else
if (0 == strcmp(ap[0], "cache_size")) {
gc_data->container = nc_mkcont(ap[1]);
} else
if (0 == strcmp(ap[0], "description")) {
gc_data->desc_long.is_html = 1;
gc_data->desc_long.utfstring = xstrdup(ap[1]);
} else
if (0 == strcmp(ap[0], "comments")) {
gc_data->desc_short.is_html = 1;
gc_data->desc_short.utfstring = xstrdup(ap[1]);
}
}
waypt_add(wpt_tmp);
}
xml_free_converted_attrs(attr);
xml_free_converted_string(el);
}
static void
nav_end(void *data, const XML_Char *el)
{
}
static void
nav_rd_init(const char *fname)
{
fin = gbfopen(fname, "r", MYNAME);
psr = XML_ParserCreate(NULL);
if (!psr) {
fatal(MYNAME ":Cannot create XML parser\n");
}
XML_SetUnknownEncodingHandler(psr, cet_lib_expat_UnknownEncodingHandler, NULL);
XML_SetElementHandler(psr, nav_start, nav_end);
}
static void
nav_read(void)
{
int len;
char buf[MY_CBUF];
while ((len = gbfread(buf, 1, sizeof(buf), fin))) {
if (!XML_Parse(psr, buf, len, gbfeof(fin))) {
fatal(MYNAME ":Parse error at %d: %s\n",
(int) XML_GetCurrentLineNumber(psr),
XML_ErrorString(XML_GetErrorCode(psr)));
}
}
XML_ParserFree(psr);
}
#endif
static void
nav_rd_deinit(void)
{
gbfclose(fin);
}
static void
nav_wr_init(const char *fname)
{
fatal(MYNAME ": Does not support writing Navicache files.\n");
fout = gbfopen(fname, "w", MYNAME);
}
static void
nav_wr_deinit(void)
{
gbfclose(fout);
}
static void
nav_write(void)
{
}
ff_vecs_t navicache_vecs = {
ff_type_file,
{ ff_cap_read, ff_cap_none, ff_cap_none },
nav_rd_init,
nav_wr_init,
nav_rd_deinit,
nav_wr_deinit,
nav_read,
nav_write,
NULL,
nav_args,
CET_CHARSET_ASCII, 0 /* CET-REVIEW */
};