X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/8c4f1350d63e7d8bee903123eed8a48263787265..51c15f41e0c686a58c8e8e082f7485e3102199db:/src/gpx.c?ds=sidebyside diff --git a/src/gpx.c b/src/gpx.c index e847c5d6..53c08b88 100644 --- a/src/gpx.c +++ b/src/gpx.c @@ -3,6 +3,10 @@ * * Copyright (C) 2003-2005, Evan Battaglia * + * Some of the code adapted from GPSBabel 1.2.7 + * http://gpsbabel.sf.net/ + * Copyright (C) 2002, 2003, 2004, 2005 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 @@ -25,8 +29,10 @@ #include "viking.h" #include #include +#include +#include -// see ~/evan/programs/gpsbabel-1.2.7/gpx.c +#define GPX_TIME_FORMAT "%Y-%m-%dT%H:%M:%SZ" typedef enum { tt_unknown = 0, @@ -37,6 +43,7 @@ typedef enum { tt_wpt_desc, tt_wpt_name, tt_wpt_ele, + tt_wpt_sym, tt_wpt_link, /* New in GPX 1.1 */ tt_trk, @@ -47,6 +54,10 @@ typedef enum { tt_trk_trkseg_trkpt, tt_trk_trkseg_trkpt_ele, tt_trk_trkseg_trkpt_time, + + tt_waypoint, + tt_waypoint_coord, + tt_waypoint_name, } tag_type; typedef struct tag_mapping { @@ -61,20 +72,27 @@ typedef struct tag_mapping { */ tag_mapping tag_path_map[] = { - { tt_gpx, "/gpx" }, { tt_wpt, "/gpx/wpt" }, + + { tt_waypoint, "/loc/waypoint" }, + { tt_waypoint_coord, "/loc/waypoint/coord" }, + { tt_waypoint_name, "/loc/waypoint/name" }, + { tt_wpt_ele, "/gpx/wpt/ele" }, { tt_wpt_name, "/gpx/wpt/name" }, { tt_wpt_desc, "/gpx/wpt/desc" }, + { tt_wpt_sym, "/gpx/wpt/sym" }, + { tt_wpt_sym, "/loc/waypoint/type" }, { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */ { tt_trk, "/gpx/trk" }, + { tt_trk, "/gpx/rte" }, { tt_trk_name, "/gpx/trk/name" }, { tt_trk_desc, "/gpx/trk/desc" }, { tt_trk_trkseg, "/gpx/trk/trkseg" }, - { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" }, + { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" }, { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" }, { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" }, @@ -127,8 +145,8 @@ static const char *get_attr ( const char **attr, const char *key ) static gboolean set_c_ll ( const char **attr ) { if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) { - c_ll.lat = strtod(c_slat, NULL); - c_ll.lon = strtod(c_slon, NULL); + c_ll.lat = g_strtod(c_slat, NULL); + c_ll.lon = g_strtod(c_slon, NULL); return TRUE; } return FALSE; @@ -136,6 +154,8 @@ static gboolean set_c_ll ( const char **attr ) static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) { + static const gchar *tmp; + g_string_append_c ( xpath, '/' ); g_string_append ( xpath, el ); current_tag = get_tag ( xpath->str ); @@ -185,7 +205,28 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr) case tt_trk_desc: case tt_trk_name: g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */ + break; + + case tt_waypoint: + c_wp = vik_waypoint_new (); + c_wp->altitude = VIK_DEFAULT_ALTITUDE; + c_wp->visible = TRUE; + break; + + case tt_waypoint_coord: + if ( set_c_ll( attr ) ) + vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll ); + break; + case tt_waypoint_name: + if ( ( tmp = get_attr(attr, "id") ) ) { + if ( c_wp_name ) + g_free ( c_wp_name ); + c_wp_name = g_strdup ( tmp ); + } + g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */ + break; + default: break; } } @@ -197,10 +238,12 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) switch ( current_tag ) { + case tt_waypoint: case tt_wpt: if ( ! c_wp_name ) c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++); - g_hash_table_insert ( vik_trw_layer_get_waypoints ( vtl ), c_wp_name, c_wp ); + vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp ); + g_free ( c_wp_name ); c_wp = NULL; c_wp_name = NULL; break; @@ -208,7 +251,8 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) case tt_trk: if ( ! c_tr_name ) c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_waypoints++); - g_hash_table_insert ( vik_trw_layer_get_tracks ( vtl ), c_tr_name, c_tr ); + vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr ); + g_free ( c_tr_name ); c_tr = NULL; c_tr_name = NULL; break; @@ -228,15 +272,16 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) break; case tt_wpt_ele: - c_wp->altitude = strtod ( c_cdata->str, NULL ); + c_wp->altitude = g_strtod ( c_cdata->str, NULL ); g_string_erase ( c_cdata, 0, -1 ); break; case tt_trk_trkseg_trkpt_ele: - c_tp->altitude = strtod ( c_cdata->str, NULL ); + c_tp->altitude = g_strtod ( c_cdata->str, NULL ); g_string_erase ( c_cdata, 0, -1 ); break; + case tt_waypoint_name: /* .loc name is really description. */ case tt_wpt_desc: vik_waypoint_set_comment ( c_wp, c_cdata->str ); g_string_erase ( c_cdata, 0, -1 ); @@ -247,17 +292,27 @@ static void gpx_end(VikTrwLayer *vtl, const char *el) g_string_erase ( c_cdata, 0, -1 ); break; + case tt_wpt_sym: { + gchar *tmp_lower = g_utf8_strdown(c_cdata->str, -1); /* for things like Geocache */ + vik_waypoint_set_symbol ( c_wp, tmp_lower ); + g_free ( tmp_lower ); + g_string_erase ( c_cdata, 0, -1 ); + break; + } + case tt_trk_desc: vik_track_set_comment ( c_tr, c_cdata->str ); g_string_erase ( c_cdata, 0, -1 ); break; case tt_trk_trkseg_trkpt_time: - if ( strptime(c_cdata->str, "%Y-%m-%dT%H:%M:%SZ", &tm) != c_cdata->str ) { /* it read at least one char */ + + if ( strptime(c_cdata->str, GPX_TIME_FORMAT, &tm) != c_cdata->str ) { /* it read at least one char */ c_tp->timestamp = mktime(&tm); c_tp->has_timestamp = TRUE; } g_string_erase ( c_cdata, 0, -1 ); + break; default: break; } @@ -273,9 +328,11 @@ static void gpx_cdata(void *dta, const XML_Char *s, int len) case tt_wpt_ele: case tt_trk_trkseg_trkpt_ele: case tt_wpt_desc: + case tt_wpt_sym: case tt_wpt_link: case tt_trk_desc: case tt_trk_trkseg_trkpt_time: + case tt_waypoint_name: /* .loc name is really description. */ g_string_append_len ( c_cdata, s, len ); break; @@ -296,7 +353,7 @@ void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) { gchar buf[4096]; - //g_assert ( f != NULL && vtl != NULL ); + g_assert ( f != NULL && vtl != NULL ); xpath = g_string_new ( "" ); c_cdata = g_string_new ( "" ); @@ -313,3 +370,304 @@ void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) { g_string_free ( xpath, TRUE ); g_string_free ( c_cdata, TRUE ); } + +/**** entitize from GPSBabel ****/ +typedef struct { + const char * text; + const char * entity; + int not_html; +} entity_types; + +static +entity_types stdentities[] = { + { "&", "&", 0 }, + { "'", "'", 1 }, + { "<", "<", 0 }, + { ">", ">", 0 }, + { "\"", """, 0 }, + { NULL, NULL, 0 } +}; + +void utf8_to_int( const char *cp, int *bytes, int *value ) +{ + if ( (*cp & 0xe0) == 0xc0 ) { + if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; + *bytes = 2; + *value = ((*cp & 0x1f) << 6) | + (*(cp+1) & 0x3f); + } + else if ( (*cp & 0xf0) == 0xe0 ) { + if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; + *bytes = 3; + *value = ((*cp & 0x0f) << 12) | + ((*(cp+1) & 0x3f) << 6) | + (*(cp+2) & 0x3f); + } + else if ( (*cp & 0xf8) == 0xf0 ) { + if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; + *bytes = 4; + *value = ((*cp & 0x07) << 18) | + ((*(cp+1) & 0x3f) << 12) | + ((*(cp+2) & 0x3f) << 6) | + (*(cp+3) & 0x3f); + } + else if ( (*cp & 0xfc) == 0xf8 ) { + if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault; + *bytes = 5; + *value = ((*cp & 0x03) << 24) | + ((*(cp+1) & 0x3f) << 18) | + ((*(cp+2) & 0x3f) << 12) | + ((*(cp+3) & 0x3f) << 6) | + (*(cp+4) & 0x3f); + } + else if ( (*cp & 0xfe) == 0xfc ) { + if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault; + if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault; + *bytes = 6; + *value = ((*cp & 0x01) << 30) | + ((*(cp+1) & 0x3f) << 24) | + ((*(cp+2) & 0x3f) << 18) | + ((*(cp+3) & 0x3f) << 12) | + ((*(cp+4) & 0x3f) << 6) | + (*(cp+5) & 0x3f); + } + else { +dodefault: + *bytes = 1; + *value = (unsigned char)*cp; + } +} + +static +char * +entitize(const char * str) +{ + int elen, ecount, nsecount; + entity_types *ep; + const char * cp; + char * p, * tmp, * xstr; + + char tmpsub[20]; + int bytes = 0; + int value = 0; + ep = stdentities; + elen = ecount = nsecount = 0; + + /* figure # of entity replacements and additional size. */ + while (ep->text) { + cp = str; + while ((cp = strstr(cp, ep->text)) != NULL) { + elen += strlen(ep->entity) - strlen(ep->text); + ecount++; + cp += strlen(ep->text); + } + ep++; + } + + /* figure the same for other than standard entities (i.e. anything + * that isn't in the range U+0000 to U+007F */ + for ( cp = str; *cp; cp++ ) { + if ( *cp & 0x80 ) { + + utf8_to_int( cp, &bytes, &value ); + cp += bytes-1; + elen += sprintf( tmpsub, "&#x%x;", value ) - bytes; + nsecount++; + } + } + + /* enough space for the whole string plus entity replacements, if any */ + tmp = g_malloc((strlen(str) + elen + 1)); + strcpy(tmp, str); + + /* no entity replacements */ + if (ecount == 0 && nsecount == 0) + return (tmp); + + if ( ecount != 0 ) { + for (ep = stdentities; ep->text; ep++) { + p = tmp; + while ((p = strstr(p, ep->text)) != NULL) { + elen = strlen(ep->entity); + + xstr = g_strdup(p + strlen(ep->text)); + + strcpy(p, ep->entity); + strcpy(p + elen, xstr); + + g_free(xstr); + + p += elen; + } + } + } + + if ( nsecount != 0 ) { + p = tmp; + while (*p) { + if ( *p & 0x80 ) { + utf8_to_int( p, &bytes, &value ); + if ( p[bytes] ) { + xstr = g_strdup( p + bytes ); + } + else { + xstr = NULL; + } + sprintf( p, "&#x%x;", value ); + p = p+strlen(p); + if ( xstr ) { + strcpy( p, xstr ); + g_free(xstr); + } + } + else { + p++; + } + } + } + return (tmp); +} +/**** end GPSBabel code ****/ + +/* export GPX */ + +static void gpx_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) +{ + static struct LatLon ll; + gchar *s_lat,*s_lon; + gchar *tmp; + vik_coord_to_latlon ( &(wp->coord), &ll ); + s_lat = a_coords_dtostr( ll.lat ); + s_lon = a_coords_dtostr( ll.lon ); + fprintf ( f, "\n", + s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" ); + g_free ( s_lat ); + g_free ( s_lon ); + + tmp = entitize ( name ); + fprintf ( f, " %s\n", tmp ); + g_free ( tmp); + + if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) + { + tmp = a_coords_dtostr ( wp->altitude ); + fprintf ( f, " %s\n", tmp ); + g_free ( tmp ); + } + if ( wp->comment ) + { + tmp = entitize(wp->comment); + fprintf ( f, " %s\n", tmp ); + g_free ( tmp ); + } + if ( wp->image ) + { + tmp = entitize(wp->image); + fprintf ( f, " %s\n", tmp ); + g_free ( tmp ); + } + if ( wp->symbol ) + { + tmp = entitize(wp->symbol); + fprintf ( f, " %s\n", tmp); + g_free ( tmp ); + } + + fprintf ( f, "\n" ); +} + +static void gpx_write_trackpoint ( VikTrackpoint *tp, FILE *f ) +{ + static struct LatLon ll; + gchar *s_lat,*s_lon, *s_alt; + static gchar time_buf[30]; + vik_coord_to_latlon ( &(tp->coord), &ll ); + + if ( tp->newsegment ) + fprintf ( f, " \n \n" ); + + s_lat = a_coords_dtostr( ll.lat ); + s_lon = a_coords_dtostr( ll.lon ); + fprintf ( f, " \n", s_lat, s_lon ); + g_free ( s_lat ); + g_free ( s_lon ); + + if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) + { + s_alt = a_coords_dtostr ( tp->altitude ); + fprintf ( f, " %s\n", s_alt ); + g_free ( s_alt ); + } + if ( tp->has_timestamp ) { + time_buf [ strftime ( time_buf, sizeof(time_buf)-1, GPX_TIME_FORMAT, localtime(&(tp->timestamp)) ) ] = '\0'; + fprintf ( f, " \n", time_buf ); + } + fprintf ( f, " \n" ); +} + + +static void gpx_write_track ( const gchar *name, VikTrack *t, FILE *f ) +{ + gchar *tmp; + gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */ + + tmp = entitize ( name ); + fprintf ( f, "\n %s\n", t->visible ? "" : " hidden=\"hidden\"", tmp ); + g_free ( tmp ); + + if ( t->comment ) + { + tmp = entitize ( t->comment ); + fprintf ( f, " %s\n", tmp ); + g_free ( tmp ); + } + + fprintf ( f, " \n" ); + + if ( t->trackpoints && t->trackpoints->data ) { + first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment; + VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write already */ + g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, f ); + VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */ + } + + fprintf ( f, "\n\n" ); +} + +static void gpx_write_header( FILE *f ) +{ + fprintf(f, "\n" + "\n"); +} + +static void gpx_write_footer( FILE *f ) +{ + fprintf(f, "\n"); +} + +void a_gpx_write_file( VikTrwLayer *vtl, FILE *f ) +{ + gpx_write_header ( f ); + g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_write_waypoint, f ); + g_hash_table_foreach ( vik_trw_layer_get_tracks ( vtl ), (GHFunc) gpx_write_track, f ); + gpx_write_footer ( f ); +} + +void a_gpx_write_track_file ( const gchar *name, VikTrack *t, FILE *f ) +{ + gpx_write_header ( f ); + gpx_write_track ( name, t, f ); + gpx_write_footer ( f ); +}