]>
Commit | Line | Data |
---|---|---|
8c4f1350 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
bf35388d EB |
6 | * Some of the code adapted from GPSBabel 1.2.7 |
7 | * http://gpsbabel.sf.net/ | |
8 | * Copyright (C) 2002, 2003, 2004, 2005 Robert Lipe, robertlipe@usa.net | |
9 | * | |
8c4f1350 EB |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 | * | |
24 | */ | |
25 | ||
26 | ||
27 | #define _XOPEN_SOURCE /* glibc2 needs this */ | |
28 | ||
29 | #include "viking.h" | |
30 | #include <expat.h> | |
31 | #include <string.h> | |
8904c540 GB |
32 | #include <glib.h> |
33 | #include <math.h> | |
8c4f1350 | 34 | |
561e6ad0 EB |
35 | #define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ" |
36 | ||
8c4f1350 EB |
37 | typedef enum { |
38 | tt_unknown = 0, | |
39 | ||
40 | tt_gpx, | |
41 | ||
42 | tt_wpt, | |
43 | tt_wpt_desc, | |
44 | tt_wpt_name, | |
45 | tt_wpt_ele, | |
42c2ac3e | 46 | tt_wpt_sym, |
8c4f1350 EB |
47 | tt_wpt_link, /* New in GPX 1.1 */ |
48 | ||
49 | tt_trk, | |
50 | tt_trk_desc, | |
51 | tt_trk_name, | |
52 | ||
53 | tt_trk_trkseg, | |
54 | tt_trk_trkseg_trkpt, | |
55 | tt_trk_trkseg_trkpt_ele, | |
56 | tt_trk_trkseg_trkpt_time, | |
bf35388d EB |
57 | |
58 | tt_waypoint, | |
59 | tt_waypoint_coord, | |
60 | tt_waypoint_name, | |
8c4f1350 EB |
61 | } tag_type; |
62 | ||
63 | typedef struct tag_mapping { | |
64 | tag_type tag_type; /* enum from above for this tag */ | |
65 | const char *tag_name; /* xpath-ish tag name */ | |
66 | } tag_mapping; | |
67 | ||
68 | /* | |
69 | * xpath(ish) mappings between full tag paths and internal identifers. | |
70 | * These appear in the order they appear in the GPX specification. | |
71 | * If it's not a tag we explictly handle, it doesn't go here. | |
72 | */ | |
73 | ||
74 | tag_mapping tag_path_map[] = { | |
8c4f1350 EB |
75 | |
76 | { tt_wpt, "/gpx/wpt" }, | |
bf35388d EB |
77 | |
78 | { tt_waypoint, "/loc/waypoint" }, | |
79 | { tt_waypoint_coord, "/loc/waypoint/coord" }, | |
80 | { tt_waypoint_name, "/loc/waypoint/name" }, | |
81 | ||
8c4f1350 EB |
82 | { tt_wpt_ele, "/gpx/wpt/ele" }, |
83 | { tt_wpt_name, "/gpx/wpt/name" }, | |
84 | { tt_wpt_desc, "/gpx/wpt/desc" }, | |
42c2ac3e | 85 | { tt_wpt_sym, "/gpx/wpt/sym" }, |
08b251ec | 86 | { tt_wpt_sym, "/loc/waypoint/type" }, |
8c4f1350 EB |
87 | { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */ |
88 | ||
89 | { tt_trk, "/gpx/trk" }, | |
561e6ad0 | 90 | { tt_trk, "/gpx/rte" }, |
8c4f1350 EB |
91 | { tt_trk_name, "/gpx/trk/name" }, |
92 | { tt_trk_desc, "/gpx/trk/desc" }, | |
93 | { tt_trk_trkseg, "/gpx/trk/trkseg" }, | |
8c4f1350 | 94 | { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" }, |
561e6ad0 | 95 | { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" }, |
8c4f1350 EB |
96 | { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" }, |
97 | { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" }, | |
98 | ||
99 | {0} | |
100 | }; | |
101 | ||
102 | static tag_type get_tag(const char *t) | |
103 | { | |
104 | tag_mapping *tm; | |
105 | for (tm = tag_path_map; tm->tag_type != 0; tm++) | |
106 | if (0 == strcmp(tm->tag_name, t)) | |
107 | return tm->tag_type; | |
108 | return tt_unknown; | |
109 | } | |
110 | ||
111 | /******************************************/ | |
112 | ||
113 | tag_type current_tag = tt_unknown; | |
114 | GString *xpath = NULL; | |
115 | GString *c_cdata = NULL; | |
116 | ||
117 | /* current ("c_") objects */ | |
118 | VikTrackpoint *c_tp = NULL; | |
119 | VikWaypoint *c_wp = NULL; | |
120 | VikTrack *c_tr = NULL; | |
121 | ||
122 | gchar *c_wp_name = NULL; | |
123 | gchar *c_tr_name = NULL; | |
124 | ||
125 | /* temporary things so we don't have to create them lots of times */ | |
126 | const gchar *c_slat, *c_slon; | |
127 | struct LatLon c_ll; | |
128 | ||
129 | /* specialty flags / etc */ | |
130 | gboolean f_tr_newseg; | |
131 | guint unnamed_waypoints = 0; | |
132 | guint unnamed_tracks = 0; | |
133 | ||
134 | ||
135 | static const char *get_attr ( const char **attr, const char *key ) | |
136 | { | |
137 | while ( *attr ) { | |
138 | if ( strcmp(*attr,key) == 0 ) | |
139 | return *(attr + 1); | |
140 | attr += 2; | |
141 | } | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | static gboolean set_c_ll ( const char **attr ) | |
146 | { | |
147 | if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) { | |
3a96ce6b EB |
148 | c_ll.lat = g_strtod(c_slat, NULL); |
149 | c_ll.lon = g_strtod(c_slon, NULL); | |
8c4f1350 EB |
150 | return TRUE; |
151 | } | |
152 | return FALSE; | |
153 | } | |
154 | ||
155 | static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) | |
156 | { | |
bf35388d EB |
157 | static const gchar *tmp; |
158 | ||
8c4f1350 EB |
159 | g_string_append_c ( xpath, '/' ); |
160 | g_string_append ( xpath, el ); | |
161 | current_tag = get_tag ( xpath->str ); | |
162 | ||
163 | switch ( current_tag ) { | |
164 | ||
165 | case tt_wpt: | |
166 | if ( set_c_ll( attr ) ) { | |
167 | c_wp = vik_waypoint_new (); | |
168 | c_wp->altitude = VIK_DEFAULT_ALTITUDE; | |
169 | if ( ! get_attr ( attr, "hidden" ) ) | |
170 | c_wp->visible = TRUE; | |
171 | ||
172 | vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); | |
173 | } | |
174 | break; | |
175 | ||
176 | case tt_trk: | |
177 | c_tr = vik_track_new (); | |
178 | if ( ! get_attr ( attr, "hidden" ) ) | |
179 | c_tr->visible = TRUE; | |
180 | break; | |
181 | ||
182 | case tt_trk_trkseg: | |
183 | f_tr_newseg = TRUE; | |
184 | break; | |
185 | ||
186 | case tt_trk_trkseg_trkpt: | |
187 | if ( set_c_ll( attr ) ) { | |
188 | c_tp = vik_trackpoint_new (); | |
189 | c_tp->altitude = VIK_DEFAULT_ALTITUDE; | |
190 | vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); | |
191 | if ( f_tr_newseg ) { | |
192 | c_tp->newsegment = TRUE; | |
193 | f_tr_newseg = FALSE; | |
194 | } | |
195 | c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp ); | |
196 | } | |
197 | break; | |
198 | ||
199 | case tt_trk_trkseg_trkpt_ele: | |
200 | case tt_trk_trkseg_trkpt_time: | |
201 | case tt_wpt_desc: | |
202 | case tt_wpt_name: | |
203 | case tt_wpt_ele: | |
204 | case tt_wpt_link: | |
205 | case tt_trk_desc: | |
206 | case tt_trk_name: | |
207 | g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */ | |
bf35388d | 208 | break; |
8c4f1350 | 209 | |
bf35388d EB |
210 | case tt_waypoint: |
211 | c_wp = vik_waypoint_new (); | |
212 | c_wp->altitude = VIK_DEFAULT_ALTITUDE; | |
213 | c_wp->visible = TRUE; | |
214 | break; | |
215 | ||
216 | case tt_waypoint_coord: | |
217 | if ( set_c_ll( attr ) ) | |
218 | vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); | |
219 | break; | |
220 | ||
221 | case tt_waypoint_name: | |
222 | if ( ( tmp = get_attr(attr, "id") ) ) { | |
223 | if ( c_wp_name ) | |
224 | g_free ( c_wp_name ); | |
225 | c_wp_name = g_strdup ( tmp ); | |
226 | } | |
227 | g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */ | |
228 | break; | |
229 | ||
8c4f1350 EB |
230 | default: break; |
231 | } | |
232 | } | |
233 | ||
234 | static void gpx_end(VikTrwLayer *vtl, const char *el) | |
235 | { | |
236 | static struct tm tm; | |
237 | g_string_truncate ( xpath, xpath->len - strlen(el) - 1 ); | |
238 | ||
239 | switch ( current_tag ) { | |
240 | ||
bf35388d | 241 | case tt_waypoint: |
8c4f1350 EB |
242 | case tt_wpt: |
243 | if ( ! c_wp_name ) | |
244 | c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++); | |
805d282e EB |
245 | vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp ); |
246 | g_free ( c_wp_name ); | |
8c4f1350 EB |
247 | c_wp = NULL; |
248 | c_wp_name = NULL; | |
249 | break; | |
250 | ||
251 | case tt_trk: | |
252 | if ( ! c_tr_name ) | |
253 | c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_waypoints++); | |
805d282e EB |
254 | vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr ); |
255 | g_free ( c_tr_name ); | |
8c4f1350 EB |
256 | c_tr = NULL; |
257 | c_tr_name = NULL; | |
258 | break; | |
259 | ||
260 | case tt_wpt_name: | |
261 | if ( c_wp_name ) | |
262 | g_free ( c_wp_name ); | |
263 | c_wp_name = g_strdup ( c_cdata->str ); | |
264 | g_string_erase ( c_cdata, 0, -1 ); | |
265 | break; | |
266 | ||
267 | case tt_trk_name: | |
268 | if ( c_tr_name ) | |
269 | g_free ( c_tr_name ); | |
270 | c_tr_name = g_strdup ( c_cdata->str ); | |
271 | g_string_erase ( c_cdata, 0, -1 ); | |
272 | break; | |
273 | ||
274 | case tt_wpt_ele: | |
3a96ce6b | 275 | c_wp->altitude = g_strtod ( c_cdata->str, NULL ); |
8c4f1350 EB |
276 | g_string_erase ( c_cdata, 0, -1 ); |
277 | break; | |
278 | ||
279 | case tt_trk_trkseg_trkpt_ele: | |
3a96ce6b | 280 | c_tp->altitude = g_strtod ( c_cdata->str, NULL ); |
8c4f1350 EB |
281 | g_string_erase ( c_cdata, 0, -1 ); |
282 | break; | |
283 | ||
bf35388d | 284 | case tt_waypoint_name: /* .loc name is really description. */ |
8c4f1350 EB |
285 | case tt_wpt_desc: |
286 | vik_waypoint_set_comment ( c_wp, c_cdata->str ); | |
287 | g_string_erase ( c_cdata, 0, -1 ); | |
288 | break; | |
289 | ||
290 | case tt_wpt_link: | |
291 | vik_waypoint_set_image ( c_wp, c_cdata->str ); | |
292 | g_string_erase ( c_cdata, 0, -1 ); | |
293 | break; | |
294 | ||
ea3933fc EB |
295 | case tt_wpt_sym: { |
296 | gchar *tmp_lower = g_utf8_strdown(c_cdata->str, -1); /* for things like <type>Geocache</type> */ | |
297 | vik_waypoint_set_symbol ( c_wp, tmp_lower ); | |
298 | g_free ( tmp_lower ); | |
42c2ac3e AF |
299 | g_string_erase ( c_cdata, 0, -1 ); |
300 | break; | |
ea3933fc | 301 | } |
42c2ac3e | 302 | |
8c4f1350 EB |
303 | case tt_trk_desc: |
304 | vik_track_set_comment ( c_tr, c_cdata->str ); | |
305 | g_string_erase ( c_cdata, 0, -1 ); | |
306 | break; | |
307 | ||
308 | case tt_trk_trkseg_trkpt_time: | |
5f304fd7 | 309 | |
561e6ad0 | 310 | if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */ |
8c4f1350 EB |
311 | c_tp->timestamp = mktime(&tm); |
312 | c_tp->has_timestamp = TRUE; | |
313 | } | |
314 | g_string_erase ( c_cdata, 0, -1 ); | |
bf35388d | 315 | break; |
8c4f1350 EB |
316 | |
317 | default: break; | |
318 | } | |
319 | ||
320 | current_tag = get_tag ( xpath->str ); | |
321 | } | |
322 | ||
323 | static void gpx_cdata(void *dta, const XML_Char *s, int len) | |
324 | { | |
325 | switch ( current_tag ) { | |
326 | case tt_wpt_name: | |
327 | case tt_trk_name: | |
328 | case tt_wpt_ele: | |
329 | case tt_trk_trkseg_trkpt_ele: | |
330 | case tt_wpt_desc: | |
42c2ac3e | 331 | case tt_wpt_sym: |
8c4f1350 EB |
332 | case tt_wpt_link: |
333 | case tt_trk_desc: | |
334 | case tt_trk_trkseg_trkpt_time: | |
bf35388d | 335 | case tt_waypoint_name: /* .loc name is really description. */ |
8c4f1350 EB |
336 | g_string_append_len ( c_cdata, s, len ); |
337 | break; | |
338 | ||
339 | default: break; /* ignore cdata from other things */ | |
340 | } | |
341 | } | |
342 | ||
343 | // make like a "stack" of tag names | |
344 | // like gpspoint's separated like /gpx/wpt/whatever | |
345 | ||
346 | void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) { | |
347 | XML_Parser parser = XML_ParserCreate(NULL); | |
348 | int done=0, len; | |
349 | ||
350 | XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end); | |
351 | XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */ | |
352 | XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata); | |
353 | ||
354 | gchar buf[4096]; | |
355 | ||
bf35388d | 356 | g_assert ( f != NULL && vtl != NULL ); |
8c4f1350 EB |
357 | |
358 | xpath = g_string_new ( "" ); | |
359 | c_cdata = g_string_new ( "" ); | |
360 | ||
361 | unnamed_waypoints = 0; | |
362 | unnamed_tracks = 0; | |
363 | ||
364 | while (!done) { | |
365 | len = fread(buf, 1, sizeof(buf)-7, f); | |
366 | done = feof(f) || !len; | |
367 | XML_Parse(parser, buf, len, done); | |
368 | } | |
369 | ||
370 | g_string_free ( xpath, TRUE ); | |
371 | g_string_free ( c_cdata, TRUE ); | |
372 | } | |
561e6ad0 EB |
373 | |
374 | /**** entitize from GPSBabel ****/ | |
375 | typedef struct { | |
376 | const char * text; | |
377 | const char * entity; | |
378 | int not_html; | |
379 | } entity_types; | |
380 | ||
381 | static | |
382 | entity_types stdentities[] = { | |
383 | { "&", "&", 0 }, | |
384 | { "'", "'", 1 }, | |
385 | { "<", "<", 0 }, | |
386 | { ">", ">", 0 }, | |
387 | { "\"", """, 0 }, | |
388 | { NULL, NULL, 0 } | |
389 | }; | |
390 | ||
391 | void utf8_to_int( const char *cp, int *bytes, int *value ) | |
392 | { | |
393 | if ( (*cp & 0xe0) == 0xc0 ) { | |
394 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
395 | *bytes = 2; | |
396 | *value = ((*cp & 0x1f) << 6) | | |
397 | (*(cp+1) & 0x3f); | |
398 | } | |
399 | else if ( (*cp & 0xf0) == 0xe0 ) { | |
400 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
401 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
402 | *bytes = 3; | |
403 | *value = ((*cp & 0x0f) << 12) | | |
404 | ((*(cp+1) & 0x3f) << 6) | | |
405 | (*(cp+2) & 0x3f); | |
406 | } | |
407 | else if ( (*cp & 0xf8) == 0xf0 ) { | |
408 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
409 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
410 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
411 | *bytes = 4; | |
412 | *value = ((*cp & 0x07) << 18) | | |
413 | ((*(cp+1) & 0x3f) << 12) | | |
414 | ((*(cp+2) & 0x3f) << 6) | | |
415 | (*(cp+3) & 0x3f); | |
416 | } | |
417 | else if ( (*cp & 0xfc) == 0xf8 ) { | |
418 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
419 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
420 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
421 | if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault; | |
422 | *bytes = 5; | |
423 | *value = ((*cp & 0x03) << 24) | | |
424 | ((*(cp+1) & 0x3f) << 18) | | |
425 | ((*(cp+2) & 0x3f) << 12) | | |
426 | ((*(cp+3) & 0x3f) << 6) | | |
427 | (*(cp+4) & 0x3f); | |
428 | } | |
429 | else if ( (*cp & 0xfe) == 0xfc ) { | |
430 | if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; | |
431 | if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; | |
432 | if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; | |
433 | if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault; | |
434 | if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault; | |
435 | *bytes = 6; | |
436 | *value = ((*cp & 0x01) << 30) | | |
437 | ((*(cp+1) & 0x3f) << 24) | | |
438 | ((*(cp+2) & 0x3f) << 18) | | |
439 | ((*(cp+3) & 0x3f) << 12) | | |
440 | ((*(cp+4) & 0x3f) << 6) | | |
441 | (*(cp+5) & 0x3f); | |
442 | } | |
443 | else { | |
444 | dodefault: | |
445 | *bytes = 1; | |
446 | *value = (unsigned char)*cp; | |
447 | } | |
448 | } | |
449 | ||
450 | static | |
451 | char * | |
452 | entitize(const char * str) | |
453 | { | |
454 | int elen, ecount, nsecount; | |
455 | entity_types *ep; | |
456 | const char * cp; | |
457 | char * p, * tmp, * xstr; | |
458 | ||
459 | char tmpsub[20]; | |
460 | int bytes = 0; | |
461 | int value = 0; | |
462 | ep = stdentities; | |
463 | elen = ecount = nsecount = 0; | |
464 | ||
465 | /* figure # of entity replacements and additional size. */ | |
466 | while (ep->text) { | |
467 | cp = str; | |
468 | while ((cp = strstr(cp, ep->text)) != NULL) { | |
469 | elen += strlen(ep->entity) - strlen(ep->text); | |
470 | ecount++; | |
471 | cp += strlen(ep->text); | |
472 | } | |
473 | ep++; | |
474 | } | |
475 | ||
476 | /* figure the same for other than standard entities (i.e. anything | |
477 | * that isn't in the range U+0000 to U+007F */ | |
478 | for ( cp = str; *cp; cp++ ) { | |
479 | if ( *cp & 0x80 ) { | |
480 | ||
481 | utf8_to_int( cp, &bytes, &value ); | |
482 | cp += bytes-1; | |
483 | elen += sprintf( tmpsub, "&#x%x;", value ) - bytes; | |
484 | nsecount++; | |
485 | } | |
486 | } | |
487 | ||
488 | /* enough space for the whole string plus entity replacements, if any */ | |
489 | tmp = g_malloc((strlen(str) + elen + 1)); | |
490 | strcpy(tmp, str); | |
491 | ||
492 | /* no entity replacements */ | |
493 | if (ecount == 0 && nsecount == 0) | |
494 | return (tmp); | |
495 | ||
496 | if ( ecount != 0 ) { | |
497 | for (ep = stdentities; ep->text; ep++) { | |
498 | p = tmp; | |
499 | while ((p = strstr(p, ep->text)) != NULL) { | |
500 | elen = strlen(ep->entity); | |
501 | ||
502 | xstr = g_strdup(p + strlen(ep->text)); | |
503 | ||
504 | strcpy(p, ep->entity); | |
505 | strcpy(p + elen, xstr); | |
506 | ||
507 | g_free(xstr); | |
508 | ||
509 | p += elen; | |
510 | } | |
511 | } | |
512 | } | |
513 | ||
514 | if ( nsecount != 0 ) { | |
515 | p = tmp; | |
516 | while (*p) { | |
517 | if ( *p & 0x80 ) { | |
518 | utf8_to_int( p, &bytes, &value ); | |
519 | if ( p[bytes] ) { | |
520 | xstr = g_strdup( p + bytes ); | |
521 | } | |
522 | else { | |
523 | xstr = NULL; | |
524 | } | |
525 | sprintf( p, "&#x%x;", value ); | |
526 | p = p+strlen(p); | |
527 | if ( xstr ) { | |
528 | strcpy( p, xstr ); | |
529 | g_free(xstr); | |
530 | } | |
531 | } | |
532 | else { | |
533 | p++; | |
534 | } | |
535 | } | |
536 | } | |
537 | return (tmp); | |
538 | } | |
539 | /**** end GPSBabel code ****/ | |
540 | ||
541 | /* export GPX */ | |
8904c540 | 542 | |
561e6ad0 EB |
543 | static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) |
544 | { | |
545 | static struct LatLon ll; | |
8904c540 | 546 | gchar *s_lat,*s_lon; |
561e6ad0 EB |
547 | gchar *tmp; |
548 | vik_coord_to_latlon ( &(wp->coord), &ll ); | |
161aa492 EB |
549 | s_lat = a_coords_dtostr( ll.lat ); |
550 | s_lon = a_coords_dtostr( ll.lon ); | |
8904c540 GB |
551 | fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n", |
552 | s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" ); | |
553 | g_free ( s_lat ); | |
554 | g_free ( s_lon ); | |
561e6ad0 EB |
555 | |
556 | tmp = entitize ( name ); | |
557 | fprintf ( f, " <name>%s</name>\n", tmp ); | |
558 | g_free ( tmp); | |
559 | ||
560 | if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) | |
8904c540 | 561 | { |
161aa492 | 562 | tmp = a_coords_dtostr ( wp->altitude ); |
8904c540 GB |
563 | fprintf ( f, " <ele>%s</ele>\n", tmp ); |
564 | g_free ( tmp ); | |
565 | } | |
561e6ad0 EB |
566 | if ( wp->comment ) |
567 | { | |
568 | tmp = entitize(wp->comment); | |
569 | fprintf ( f, " <desc>%s</desc>\n", tmp ); | |
570 | g_free ( tmp ); | |
571 | } | |
572 | if ( wp->image ) | |
573 | { | |
574 | tmp = entitize(wp->image); | |
575 | fprintf ( f, " <link>%s</link>\n", tmp ); | |
576 | g_free ( tmp ); | |
577 | } | |
42c2ac3e AF |
578 | if ( wp->symbol ) |
579 | { | |
580 | tmp = entitize(wp->symbol); | |
581 | fprintf ( f, " <sym>%s</sym>\n", tmp); | |
582 | g_free ( tmp ); | |
583 | } | |
584 | ||
561e6ad0 EB |
585 | fprintf ( f, "</wpt>\n" ); |
586 | } | |
587 | ||
588 | static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f ) | |
589 | { | |
590 | static struct LatLon ll; | |
8904c540 | 591 | gchar *s_lat,*s_lon, *s_alt; |
561e6ad0 EB |
592 | static gchar time_buf[30]; |
593 | vik_coord_to_latlon ( &(tp->coord), &ll ); | |
594 | ||
595 | if ( tp->newsegment ) | |
596 | fprintf ( f, " </trkseg>\n <trkseg>\n" ); | |
597 | ||
161aa492 EB |
598 | s_lat = a_coords_dtostr( ll.lat ); |
599 | s_lon = a_coords_dtostr( ll.lon ); | |
8904c540 GB |
600 | fprintf ( f, " <trkpt lat=\"%s\" lon=\"%s\">\n", s_lat, s_lon ); |
601 | g_free ( s_lat ); | |
602 | g_free ( s_lon ); | |
561e6ad0 EB |
603 | |
604 | if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) | |
8904c540 | 605 | { |
161aa492 | 606 | s_alt = a_coords_dtostr ( tp->altitude ); |
8904c540 GB |
607 | fprintf ( f, " <ele>%s</ele>\n", s_alt ); |
608 | g_free ( s_alt ); | |
609 | } | |
561e6ad0 EB |
610 | if ( tp->has_timestamp ) { |
611 | time_buf [ strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&(tp->timestamp)) ) ] = '\0'; | |
612 | fprintf ( f, " <time>%s</time>\n", time_buf ); | |
613 | } | |
614 | fprintf ( f, " </trkpt>\n" ); | |
615 | } | |
616 | ||
617 | ||
618 | static void gpx_write_track ( const gchar *name, VikTrack *t, FILE *f ) | |
619 | { | |
620 | gchar *tmp; | |
494eb388 | 621 | gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */ |
561e6ad0 EB |
622 | |
623 | tmp = entitize ( name ); | |
624 | fprintf ( f, "<trk%s>\n <name>%s</name>\n", t->visible ? "" : " hidden=\"hidden\"", tmp ); | |
625 | g_free ( tmp ); | |
626 | ||
627 | if ( t->comment ) | |
628 | { | |
629 | tmp = entitize ( t->comment ); | |
630 | fprintf ( f, " <desc>%s</desc>\n", tmp ); | |
631 | g_free ( tmp ); | |
632 | } | |
633 | ||
634 | fprintf ( f, " <trkseg>\n" ); | |
635 | ||
636 | if ( t->trackpoints && t->trackpoints->data ) { | |
637 | first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment; | |
638 | VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */ | |
494eb388 | 639 | g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, f ); |
561e6ad0 | 640 | VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */ |
494eb388 | 641 | } |
561e6ad0 EB |
642 | |
643 | fprintf ( f, "</trkseg>\n</trk>\n" ); | |
644 | } | |
645 | ||
5092de80 | 646 | static void gpx_write_header( FILE *f ) |
561e6ad0 EB |
647 | { |
648 | fprintf(f, "<?xml version=\"1.0\"?>\n" | |
649 | "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n" | |
650 | "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" | |
651 | "xmlns=\"http://www.topografix.com/GPX/1/0\"\n" | |
652 | "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n"); | |
5092de80 GB |
653 | } |
654 | ||
655 | static void gpx_write_footer( FILE *f ) | |
656 | { | |
561e6ad0 | 657 | fprintf(f, "</gpx>\n"); |
5092de80 | 658 | } |
561e6ad0 | 659 | |
5092de80 GB |
660 | void a_gpx_write_file( VikTrwLayer *vtl, FILE *f ) |
661 | { | |
662 | gpx_write_header ( f ); | |
663 | g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_write_waypoint, f ); | |
664 | g_hash_table_foreach ( vik_trw_layer_get_tracks ( vtl ), (GHFunc) gpx_write_track, f ); | |
665 | gpx_write_footer ( f ); | |
666 | } | |
561e6ad0 | 667 | |
5092de80 GB |
668 | void a_gpx_write_track_file ( const gchar *name, VikTrack *t, FILE *f ) |
669 | { | |
670 | gpx_write_header ( f ); | |
671 | gpx_write_track ( name, t, f ); | |
672 | gpx_write_footer ( f ); | |
561e6ad0 | 673 | } |