2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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
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.
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.
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
27 #define _XOPEN_SOURCE /* glibc2 needs this */
33 #define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ"
44 tt_wpt_link, /* New in GPX 1.1 */
52 tt_trk_trkseg_trkpt_ele,
53 tt_trk_trkseg_trkpt_time,
60 typedef struct tag_mapping {
61 tag_type tag_type; /* enum from above for this tag */
62 const char *tag_name; /* xpath-ish tag name */
66 * xpath(ish) mappings between full tag paths and internal identifers.
67 * These appear in the order they appear in the GPX specification.
68 * If it's not a tag we explictly handle, it doesn't go here.
71 tag_mapping tag_path_map[] = {
73 { tt_wpt, "/gpx/wpt" },
75 { tt_waypoint, "/loc/waypoint" },
76 { tt_waypoint_coord, "/loc/waypoint/coord" },
77 { tt_waypoint_name, "/loc/waypoint/name" },
79 { tt_wpt_ele, "/gpx/wpt/ele" },
80 { tt_wpt_name, "/gpx/wpt/name" },
81 { tt_wpt_desc, "/gpx/wpt/desc" },
82 { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */
84 { tt_trk, "/gpx/trk" },
85 { tt_trk, "/gpx/rte" },
86 { tt_trk_name, "/gpx/trk/name" },
87 { tt_trk_desc, "/gpx/trk/desc" },
88 { tt_trk_trkseg, "/gpx/trk/trkseg" },
89 { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
90 { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
91 { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
92 { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
97 static tag_type get_tag(const char *t)
100 for (tm = tag_path_map; tm->tag_type != 0; tm++)
101 if (0 == strcmp(tm->tag_name, t))
106 /******************************************/
108 tag_type current_tag = tt_unknown;
109 GString *xpath = NULL;
110 GString *c_cdata = NULL;
112 /* current ("c_") objects */
113 VikTrackpoint *c_tp = NULL;
114 VikWaypoint *c_wp = NULL;
115 VikTrack *c_tr = NULL;
117 gchar *c_wp_name = NULL;
118 gchar *c_tr_name = NULL;
120 /* temporary things so we don't have to create them lots of times */
121 const gchar *c_slat, *c_slon;
124 /* specialty flags / etc */
125 gboolean f_tr_newseg;
126 guint unnamed_waypoints = 0;
127 guint unnamed_tracks = 0;
130 static const char *get_attr ( const char **attr, const char *key )
133 if ( strcmp(*attr,key) == 0 )
140 static gboolean set_c_ll ( const char **attr )
142 if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) {
143 c_ll.lat = strtod(c_slat, NULL);
144 c_ll.lon = strtod(c_slon, NULL);
150 static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
152 static const gchar *tmp;
154 g_string_append_c ( xpath, '/' );
155 g_string_append ( xpath, el );
156 current_tag = get_tag ( xpath->str );
158 switch ( current_tag ) {
161 if ( set_c_ll( attr ) ) {
162 c_wp = vik_waypoint_new ();
163 c_wp->altitude = VIK_DEFAULT_ALTITUDE;
164 if ( ! get_attr ( attr, "hidden" ) )
165 c_wp->visible = TRUE;
167 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
172 c_tr = vik_track_new ();
173 if ( ! get_attr ( attr, "hidden" ) )
174 c_tr->visible = TRUE;
181 case tt_trk_trkseg_trkpt:
182 if ( set_c_ll( attr ) ) {
183 c_tp = vik_trackpoint_new ();
184 c_tp->altitude = VIK_DEFAULT_ALTITUDE;
185 vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
187 c_tp->newsegment = TRUE;
190 c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp );
194 case tt_trk_trkseg_trkpt_ele:
195 case tt_trk_trkseg_trkpt_time:
202 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
206 c_wp = vik_waypoint_new ();
207 c_wp->altitude = VIK_DEFAULT_ALTITUDE;
208 c_wp->visible = TRUE;
211 case tt_waypoint_coord:
212 if ( set_c_ll( attr ) )
213 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
216 case tt_waypoint_name:
217 if ( ( tmp = get_attr(attr, "id") ) ) {
219 g_free ( c_wp_name );
220 c_wp_name = g_strdup ( tmp );
222 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */
229 static void gpx_end(VikTrwLayer *vtl, const char *el)
232 g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
234 switch ( current_tag ) {
239 c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++);
240 g_hash_table_insert ( vik_trw_layer_get_waypoints ( vtl ), c_wp_name, c_wp );
247 c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_waypoints++);
248 g_hash_table_insert ( vik_trw_layer_get_tracks ( vtl ), c_tr_name, c_tr );
255 g_free ( c_wp_name );
256 c_wp_name = g_strdup ( c_cdata->str );
257 g_string_erase ( c_cdata, 0, -1 );
262 g_free ( c_tr_name );
263 c_tr_name = g_strdup ( c_cdata->str );
264 g_string_erase ( c_cdata, 0, -1 );
268 c_wp->altitude = strtod ( c_cdata->str, NULL );
269 g_string_erase ( c_cdata, 0, -1 );
272 case tt_trk_trkseg_trkpt_ele:
273 c_tp->altitude = strtod ( c_cdata->str, NULL );
274 g_string_erase ( c_cdata, 0, -1 );
277 case tt_waypoint_name: /* .loc name is really description. */
279 vik_waypoint_set_comment ( c_wp, c_cdata->str );
280 g_string_erase ( c_cdata, 0, -1 );
284 vik_waypoint_set_image ( c_wp, c_cdata->str );
285 g_string_erase ( c_cdata, 0, -1 );
289 vik_track_set_comment ( c_tr, c_cdata->str );
290 g_string_erase ( c_cdata, 0, -1 );
293 case tt_trk_trkseg_trkpt_time:
294 if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */
295 c_tp->timestamp = mktime(&tm);
296 c_tp->has_timestamp = TRUE;
298 g_string_erase ( c_cdata, 0, -1 );
304 current_tag = get_tag ( xpath->str );
307 static void gpx_cdata(void *dta, const XML_Char *s, int len)
309 switch ( current_tag ) {
313 case tt_trk_trkseg_trkpt_ele:
317 case tt_trk_trkseg_trkpt_time:
318 case tt_waypoint_name: /* .loc name is really description. */
319 g_string_append_len ( c_cdata, s, len );
322 default: break; /* ignore cdata from other things */
326 // make like a "stack" of tag names
327 // like gpspoint's separated like /gpx/wpt/whatever
329 void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
330 XML_Parser parser = XML_ParserCreate(NULL);
333 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
334 XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
335 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata);
339 g_assert ( f != NULL && vtl != NULL );
341 xpath = g_string_new ( "" );
342 c_cdata = g_string_new ( "" );
344 unnamed_waypoints = 0;
348 len = fread(buf, 1, sizeof(buf)-7, f);
349 done = feof(f) || !len;
350 XML_Parse(parser, buf, len, done);
353 g_string_free ( xpath, TRUE );
354 g_string_free ( c_cdata, TRUE );
357 /**** entitize from GPSBabel ****/
365 entity_types stdentities[] = {
367 { "'", "'", 1 },
370 { "\"", """, 0 },
374 void utf8_to_int( const char *cp, int *bytes, int *value )
376 if ( (*cp & 0xe0) == 0xc0 ) {
377 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
379 *value = ((*cp & 0x1f) << 6) |
382 else if ( (*cp & 0xf0) == 0xe0 ) {
383 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
384 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
386 *value = ((*cp & 0x0f) << 12) |
387 ((*(cp+1) & 0x3f) << 6) |
390 else if ( (*cp & 0xf8) == 0xf0 ) {
391 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
392 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
393 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
395 *value = ((*cp & 0x07) << 18) |
396 ((*(cp+1) & 0x3f) << 12) |
397 ((*(cp+2) & 0x3f) << 6) |
400 else if ( (*cp & 0xfc) == 0xf8 ) {
401 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
402 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
403 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
404 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
406 *value = ((*cp & 0x03) << 24) |
407 ((*(cp+1) & 0x3f) << 18) |
408 ((*(cp+2) & 0x3f) << 12) |
409 ((*(cp+3) & 0x3f) << 6) |
412 else if ( (*cp & 0xfe) == 0xfc ) {
413 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
414 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
415 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
416 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
417 if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault;
419 *value = ((*cp & 0x01) << 30) |
420 ((*(cp+1) & 0x3f) << 24) |
421 ((*(cp+2) & 0x3f) << 18) |
422 ((*(cp+3) & 0x3f) << 12) |
423 ((*(cp+4) & 0x3f) << 6) |
429 *value = (unsigned char)*cp;
435 entitize(const char * str)
437 int elen, ecount, nsecount;
440 char * p, * tmp, * xstr;
446 elen = ecount = nsecount = 0;
448 /* figure # of entity replacements and additional size. */
451 while ((cp = strstr(cp, ep->text)) != NULL) {
452 elen += strlen(ep->entity) - strlen(ep->text);
454 cp += strlen(ep->text);
459 /* figure the same for other than standard entities (i.e. anything
460 * that isn't in the range U+0000 to U+007F */
461 for ( cp = str; *cp; cp++ ) {
464 utf8_to_int( cp, &bytes, &value );
466 elen += sprintf( tmpsub, "&#x%x;", value ) - bytes;
471 /* enough space for the whole string plus entity replacements, if any */
472 tmp = g_malloc((strlen(str) + elen + 1));
475 /* no entity replacements */
476 if (ecount == 0 && nsecount == 0)
480 for (ep = stdentities; ep->text; ep++) {
482 while ((p = strstr(p, ep->text)) != NULL) {
483 elen = strlen(ep->entity);
485 xstr = g_strdup(p + strlen(ep->text));
487 strcpy(p, ep->entity);
488 strcpy(p + elen, xstr);
497 if ( nsecount != 0 ) {
501 utf8_to_int( p, &bytes, &value );
503 xstr = g_strdup( p + bytes );
508 sprintf( p, "&#x%x;", value );
522 /**** end GPSBabel code ****/
525 static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f )
527 static struct LatLon ll;
529 vik_coord_to_latlon ( &(wp->coord), &ll );
530 fprintf ( f, "<wpt lat=\"%f\" lon=\"%f\"%s>\n",
531 ll.lat, ll.lon, wp->visible ? "" : " hidden=\"hidden\"" );
533 tmp = entitize ( name );
534 fprintf ( f, " <name>%s</name>\n", tmp );
537 if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
538 fprintf ( f, " <ele>%f</ele>\n", wp->altitude );
541 tmp = entitize(wp->comment);
542 fprintf ( f, " <desc>%s</desc>\n", tmp );
547 tmp = entitize(wp->image);
548 fprintf ( f, " <link>%s</link>\n", tmp );
551 fprintf ( f, "</wpt>\n" );
554 static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f )
556 static struct LatLon ll;
557 static gchar time_buf[30];
558 vik_coord_to_latlon ( &(tp->coord), &ll );
560 if ( tp->newsegment )
561 fprintf ( f, " </trkseg>\n <trkseg>\n" );
563 fprintf ( f, " <trkpt lat=\"%f\" lon=\"%f\">\n", ll.lat, ll.lon );
565 if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
566 fprintf ( f, " <ele>%f</ele>\n", tp->altitude );
567 if ( tp->has_timestamp ) {
568 time_buf [ strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&(tp->timestamp)) ) ] = '\0';
569 fprintf ( f, " <time>%s</time>\n", time_buf );
571 fprintf ( f, " </trkpt>\n" );
575 static void gpx_write_track ( const gchar *name, VikTrack *t, FILE *f )
578 gboolean first_tp_is_newsegment; /* must temporarily make it not so, but we want to restore state. not that it matters. */
580 tmp = entitize ( name );
581 fprintf ( f, "<trk%s>\n <name>%s</name>\n", t->visible ? "" : " hidden=\"hidden\"", tmp );
586 tmp = entitize ( t->comment );
587 fprintf ( f, " <desc>%s</desc>\n", tmp );
591 fprintf ( f, " <trkseg>\n" );
593 if ( t->trackpoints && t->trackpoints->data ) {
594 first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
595 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
597 g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, f );
598 if ( t->trackpoints && t->trackpoints->data )
599 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
601 fprintf ( f, "</trkseg>\n</trk>\n" );
604 void a_gpx_write_file( VikTrwLayer *vtl, FILE *f )
606 fprintf(f, "<?xml version=\"1.0\"?>\n"
607 "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
608 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
609 "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
610 "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
611 g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_write_waypoint, f );
612 g_hash_table_foreach ( vik_trw_layer_get_tracks ( vtl ), (GHFunc) gpx_write_track, f );
613 fprintf(f, "</gpx>\n");