+
+/**** 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, GpxWritingContext *context )
+{
+ FILE *f = context->file;
+ 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, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
+ s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
+ g_free ( s_lat );
+ g_free ( s_lon );
+
+ tmp = entitize ( name );
+ fprintf ( f, " <name>%s</name>\n", tmp );
+ g_free ( tmp);
+
+ if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
+ {
+ tmp = a_coords_dtostr ( wp->altitude );
+ fprintf ( f, " <ele>%s</ele>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( wp->comment )
+ {
+ tmp = entitize(wp->comment);
+ fprintf ( f, " <desc>%s</desc>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( wp->image )
+ {
+ tmp = entitize(wp->image);
+ fprintf ( f, " <link>%s</link>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( wp->symbol )
+ {
+ tmp = entitize(wp->symbol);
+ fprintf ( f, " <sym>%s</sym>\n", tmp);
+ g_free ( tmp );
+ }
+
+ fprintf ( f, "</wpt>\n" );
+}
+
+static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
+{
+ FILE *f = context->file;
+ static struct LatLon ll;
+ gchar *s_lat,*s_lon, *s_alt, *s_dop;
+ gchar *time_iso8601;
+ vik_coord_to_latlon ( &(tp->coord), &ll );
+
+ if ( tp->newsegment )
+ fprintf ( f, " </trkseg>\n <trkseg>\n" );
+
+ s_lat = a_coords_dtostr( ll.lat );
+ s_lon = a_coords_dtostr( ll.lon );
+ fprintf ( f, " <trkpt lat=\"%s\" lon=\"%s\">\n", s_lat, s_lon );
+ g_free ( s_lat ); s_lat = NULL;
+ g_free ( s_lon ); s_lon = NULL;
+
+ s_alt = NULL;
+ if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
+ {
+ s_alt = a_coords_dtostr ( tp->altitude );
+ }
+ else if ( context->options != NULL && context->options->force_ele )
+ {
+ s_alt = a_coords_dtostr ( 0 );
+ }
+ if (s_alt != NULL)
+ fprintf ( f, " <ele>%s</ele>\n", s_alt );
+ g_free ( s_alt ); s_alt = NULL;
+
+ time_iso8601 = NULL;
+ if ( tp->has_timestamp ) {
+ GTimeVal timestamp;
+ timestamp.tv_sec = tp->timestamp;
+ timestamp.tv_usec = 0;
+
+ time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
+ }
+ else if ( context->options != NULL && context->options->force_time )
+ {
+ GTimeVal current;
+ g_get_current_time ( ¤t );
+
+ time_iso8601 = g_time_val_to_iso8601 ( ¤t );
+ }
+ if ( time_iso8601 != NULL )
+ fprintf ( f, " <time>%s</time>\n", time_iso8601 );
+ g_free(time_iso8601);
+ time_iso8601 = NULL;
+
+ if (!isnan(tp->course)) {
+ gchar *s_course = a_coords_dtostr(tp->course);
+ fprintf ( f, " <course>%s</course>\n", s_course );
+ g_free(s_course);
+ }
+ if (!isnan(tp->speed)) {
+ gchar *s_speed = a_coords_dtostr(tp->speed);
+ fprintf ( f, " <speed>%s</speed>\n", s_speed );
+ g_free(s_speed);
+ }
+ if (tp->fix_mode == VIK_GPS_MODE_2D)
+ fprintf ( f, " <fix>2d</fix>\n");
+ if (tp->fix_mode == VIK_GPS_MODE_3D)
+ fprintf ( f, " <fix>3d</fix>\n");
+ if (tp->nsats > 0)
+ fprintf ( f, " <sat>%d</sat>\n", tp->nsats );
+
+ s_dop = NULL;
+ if ( tp->hdop != VIK_DEFAULT_DOP )
+ {
+ s_dop = a_coords_dtostr ( tp->hdop );
+ }
+ if (s_dop != NULL)
+ fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
+ g_free ( s_dop ); s_dop = NULL;
+
+ if ( tp->vdop != VIK_DEFAULT_DOP )
+ {
+ s_dop = a_coords_dtostr ( tp->vdop );
+ }
+ if (s_dop != NULL)
+ fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
+ g_free ( s_dop ); s_dop = NULL;
+
+ if ( tp->pdop != VIK_DEFAULT_DOP )
+ {
+ s_dop = a_coords_dtostr ( tp->pdop );
+ }
+ if (s_dop != NULL)
+ fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
+ g_free ( s_dop ); s_dop = NULL;
+
+
+ fprintf ( f, " </trkpt>\n" );
+}
+
+
+static void gpx_write_track ( const gchar *name, VikTrack *t, GpxWritingContext *context )
+{
+ FILE *f = context->file;
+ 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, "<trk%s>\n <name>%s</name>\n", t->visible ? "" : " hidden=\"hidden\"", tmp );
+ g_free ( tmp );
+
+ if ( t->comment )
+ {
+ tmp = entitize ( t->comment );
+ fprintf ( f, " <desc>%s</desc>\n", tmp );
+ g_free ( tmp );
+ }
+
+ fprintf ( f, " <trkseg>\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 </trkseg><trkseg> already */
+ g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
+ VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
+ }
+
+ fprintf ( f, "</trkseg>\n</trk>\n" );
+}
+
+static void gpx_write_header( FILE *f )
+{
+ fprintf(f, "<?xml version=\"1.0\"?>\n"
+ "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+ "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
+ "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
+}
+
+static void gpx_write_footer( FILE *f )
+{
+ fprintf(f, "</gpx>\n");
+}
+
+
+
+typedef struct {
+ VikWaypoint *wp;
+ const gchar *name;
+} gpx_waypoint_and_name;
+
+typedef struct {
+ gpx_waypoint_and_name *wps;
+ guint i;
+ guint n_wps;
+} gpx_gather_waypoints_passalong_t;
+
+/* Type to hold name of track and timestamp of first trackpoint */
+typedef struct {
+ time_t first_timestamp;
+ const gchar *name;
+} gpx_track_and_timestamp;
+
+typedef struct {
+ gpx_track_and_timestamp *trks;
+ guint i;
+ guint n_trks;
+} gpx_gather_tracks_passalong_t;
+
+static void gpx_collect_waypoint ( const gchar *name, VikWaypoint *wp, gpx_gather_waypoints_passalong_t *passalong )
+{
+ if ( passalong->i < passalong->n_wps ) {
+ passalong->wps[passalong->i].name = name;
+ passalong->wps[passalong->i].wp = wp;
+ passalong->i++;
+ }
+}
+
+/* Function to collect a track and the first timestamp in the list */
+static void gpx_collect_track (const gchar *name, VikTrack *track, gpx_gather_tracks_passalong_t *passalong)
+{
+ if (passalong->i < passalong->n_trks)
+ {
+ passalong->trks[passalong->i].name = name;
+ if (track && track->trackpoints && track->trackpoints->data)
+ {
+ VikTrackpoint *first_point = (VikTrackpoint *)track->trackpoints->data;
+ passalong->trks[passalong->i].first_timestamp = first_point->timestamp;
+ }
+ else
+ {
+ passalong->trks[passalong->i].first_timestamp = 0;
+ }
+ passalong->i++;
+ }
+}
+
+static int gpx_waypoint_and_name_compar(const void *x, const void *y)
+{
+ gpx_waypoint_and_name *a = (gpx_waypoint_and_name *)x;
+ gpx_waypoint_and_name *b = (gpx_waypoint_and_name *)y;
+ return strcmp(a->name,b->name);
+}
+
+/* Function to compare two tracks by their first timestamp */
+static int gpx_track_and_timestamp_compar(const void *x, const void *y)
+{
+ gpx_track_and_timestamp *a = (gpx_track_and_timestamp *)x;
+ gpx_track_and_timestamp *b = (gpx_track_and_timestamp *)y;
+ if (a->first_timestamp < b->first_timestamp)
+ {
+ return -1;
+ }
+ if (a->first_timestamp > b->first_timestamp)
+ {
+ return 1;
+ }
+ return 0;
+}
+
+void a_gpx_write_file( VikTrwLayer *vtl, FILE *f )
+{
+ a_gpx_write_file_options(NULL, vtl, f);
+}
+
+void a_gpx_write_file_options ( GpxWritingOptions *options, VikTrwLayer *vtl, FILE *f )
+{
+ GpxWritingContext context = { options, f };
+ int i;
+
+ gpx_write_header ( f );
+
+
+ gpx_gather_waypoints_passalong_t passalong;
+ passalong.n_wps = g_hash_table_size ( vik_trw_layer_get_waypoints ( vtl ) );
+ passalong.i = 0;
+ passalong.wps = g_new(gpx_waypoint_and_name,passalong.n_wps);
+ g_hash_table_foreach ( vik_trw_layer_get_waypoints ( vtl ), (GHFunc) gpx_collect_waypoint, &passalong );
+ /* gather waypoints in a list, then sort */
+ qsort(passalong.wps, passalong.n_wps, sizeof(gpx_waypoint_and_name), gpx_waypoint_and_name_compar);
+ for ( i = 0; i < passalong.n_wps; i++ )
+ gpx_write_waypoint ( passalong.wps[i].name, passalong.wps[i].wp, &context);
+ g_free ( passalong.wps );
+
+ gpx_gather_tracks_passalong_t passalong_tracks;
+ passalong_tracks.n_trks = g_hash_table_size ( vik_trw_layer_get_tracks (vtl) );
+ passalong_tracks.i = 0;
+ passalong_tracks.trks = g_new(gpx_track_and_timestamp,passalong_tracks.n_trks);
+ g_hash_table_foreach (vik_trw_layer_get_tracks(vtl), (GHFunc) gpx_collect_track, &passalong_tracks);
+ /* Sort by timestamp */
+ qsort(passalong_tracks.trks, passalong_tracks.n_trks, sizeof(gpx_track_and_timestamp), gpx_track_and_timestamp_compar);
+ for (i=0;i<passalong_tracks.n_trks; i++)
+ {
+ gpx_write_track(passalong_tracks.trks[i].name, (VikTrack *)g_hash_table_lookup(vik_trw_layer_get_tracks(vtl), passalong_tracks.trks[i].name), &context);
+ }
+ g_free ( passalong_tracks.trks );
+ gpx_write_footer ( f );
+}
+
+void a_gpx_write_track_file ( const gchar *name, VikTrack *t, FILE *f )
+{
+ a_gpx_write_track_file_options ( NULL, name, t, f );
+}
+
+void a_gpx_write_track_file_options ( GpxWritingOptions *options, const gchar *name, VikTrack *t, FILE *f )
+{
+ GpxWritingContext context = {options, f};
+ gpx_write_header ( f );
+ gpx_write_track ( name, t, &context );
+ gpx_write_footer ( f );
+}