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