+
+ return status != XML_STATUS_ERROR;
+}
+
+/**** 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 ( VikWaypoint *wp, GpxWritingContext *context )
+{
+ // Don't write invisible waypoints when specified
+ if (context->options && !context->options->hidden && !wp->visible)
+ return;
+
+ 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 );
+ // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
+ // luckily most other GPX processing software ignores things they don't understand
+ 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 );
+
+ // Sanity clause
+ if ( wp->name )
+ tmp = entitize ( wp->name );
+ else
+ tmp = g_strdup ("waypoint");
+
+ 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->has_timestamp ) {
+ GTimeVal timestamp;
+ timestamp.tv_sec = wp->timestamp;
+ timestamp.tv_usec = 0;
+
+ gchar *time_iso8601 = g_time_val_to_iso8601 ( ×tamp );
+ if ( time_iso8601 != NULL )
+ fprintf ( f, " <time>%s</time>\n", time_iso8601 );
+ g_free ( time_iso8601 );
+ }
+
+ if ( wp->comment )
+ {
+ tmp = entitize(wp->comment);
+ fprintf ( f, " <cmt>%s</cmt>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( wp->description )
+ {
+ tmp = entitize(wp->description);
+ fprintf ( f, " <desc>%s</desc>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( wp->url )
+ {
+ tmp = entitize(wp->url);
+ fprintf ( f, " <url>%s</url>\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);
+ if ( a_vik_gpx_export_wpt_sym_name ( ) ) {
+ // Lowercase the symbol name
+ gchar *tmp2 = g_utf8_strdown ( tmp, -1 );
+ fprintf ( f, " <sym>%s</sym>\n", tmp2 );
+ g_free ( tmp2 );
+ }
+ else
+ 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 );
+
+ // No such thing as a rteseg! So make sure we don't put them in
+ if ( context->options && !context->options->is_route && tp->newsegment )
+ fprintf ( f, " </trkseg>\n <trkseg>\n" );
+
+ s_lat = a_coords_dtostr( ll.lat );
+ s_lon = a_coords_dtostr( ll.lon );
+ fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );
+ g_free ( s_lat ); s_lat = NULL;
+ g_free ( s_lon ); s_lon = NULL;
+
+ if (tp->name) {
+ gchar *s_name = entitize(tp->name);
+ fprintf ( f, " <name>%s</name>\n", s_name );
+ g_free(s_name);
+ }
+
+ 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, " </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" );
+}
+
+
+static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
+{
+ // Don't write invisible tracks when specified
+ if (context->options && !context->options->hidden && !t->visible)
+ return;
+
+ 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. */
+
+ // Sanity clause
+ if ( t->name )
+ tmp = entitize ( t->name );
+ else
+ tmp = g_strdup ("track");
+
+ // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
+ // luckily most other GPX processing software ignores things they don't understand
+ fprintf ( f, "<%s%s>\n <name>%s</name>\n",
+ t->is_route ? "rte" : "trk",
+ t->visible ? "" : " hidden=\"hidden\"",
+ tmp );
+ g_free ( tmp );
+
+ if ( t->comment )
+ {
+ tmp = entitize ( t->comment );
+ fprintf ( f, " <cmt>%s</cmt>\n", tmp );
+ g_free ( tmp );
+ }
+
+ if ( t->description )
+ {
+ tmp = entitize ( t->description );
+ fprintf ( f, " <desc>%s</desc>\n", tmp );
+ g_free ( tmp );
+ }
+
+ /* No such thing as a rteseg! */
+ if ( !t->is_route )
+ 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 */
+ }
+
+ /* NB apparently no such thing as a rteseg! */
+ if (!t->is_route)
+ fprintf ( f, " </trkseg>\n");
+
+ fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" );
+}
+
+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");
+}
+
+static int gpx_waypoint_compare(const void *x, const void *y)
+{
+ VikWaypoint *a = (VikWaypoint *)x;
+ VikWaypoint *b = (VikWaypoint *)y;
+ return strcmp(a->name,b->name);
+}
+
+static int gpx_track_compare_name(const void *x, const void *y)
+{
+ VikTrack *a = (VikTrack *)x;
+ VikTrack *b = (VikTrack *)y;
+ return strcmp(a->name,b->name);
+}
+
+void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
+{
+ GpxWritingContext context = { options, f };
+
+ gpx_write_header ( f );
+
+ gchar *tmp;
+ const gchar *name = vik_layer_get_name(VIK_LAYER(vtl));
+ if ( name ) {
+ tmp = entitize ( name );
+ fprintf ( f, " <name>%s</name>\n", tmp );
+ g_free ( tmp );
+ }
+
+ VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
+ if ( md ) {
+ if ( md->author ) {
+ tmp = entitize ( md->author );
+ fprintf ( f, " <author>%s</author>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->description ) {
+ tmp = entitize ( md->description );
+ fprintf ( f, " <desc>%s</desc>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->timestamp ) {
+ tmp = entitize ( md->timestamp );
+ fprintf ( f, " <time>%s</time>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->keywords ) {
+ tmp = entitize ( md->keywords );
+ fprintf ( f, " <keywords>%s</keywords>\n", tmp );
+ g_free ( tmp );
+ }
+ }
+
+ if ( vik_trw_layer_get_waypoints_visibility(vtl) || (options && options->hidden) ) {
+ // gather waypoints in a list, then sort
+ GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
+ gl = g_list_sort ( gl, gpx_waypoint_compare );
+
+ for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+ gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
+ }
+ g_list_free ( gl );
+ }
+
+ GList *gl = NULL;
+ if ( vik_trw_layer_get_tracks_visibility(vtl) || (options && options->hidden) ) {
+ //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
+ // Forming the list manually seems to produce one that is more likely to be nearer to the creation order
+ gpointer key, value;
+ GHashTableIter ght_iter;
+ g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) );
+ while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) {
+ gl = g_list_prepend ( gl ,value );
+ }
+ gl = g_list_reverse ( gl );
+
+ // Sort method determined by preference
+ if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
+ gl = g_list_sort ( gl, vik_track_compare_timestamp );
+ else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA )
+ gl = g_list_sort ( gl, gpx_track_compare_name );
+ }
+
+ GList *glrte = NULL;
+ // Routes sorted by name
+ if ( vik_trw_layer_get_tracks_visibility(vtl) || (options && options->hidden) ) {
+ glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
+ glrte = g_list_sort ( glrte, gpx_track_compare_name );
+ }
+
+ // g_list_concat doesn't copy memory properly
+ // so process each list separately
+
+ GpxWritingContext context_tmp = context;
+ GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE, FALSE };
+ // Force trackpoints on tracks
+ if ( !context.options )
+ context_tmp.options = &opt_tmp;
+ context_tmp.options->is_route = FALSE;
+
+ // Loop around each list and write each one
+ for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+ gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
+ }
+
+ // Routes (to get routepoints)
+ context_tmp.options->is_route = TRUE;
+ for (GList *iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
+ gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
+ }
+
+ g_list_free ( gl );
+ g_list_free ( glrte );
+
+ gpx_write_footer ( f );
+}
+
+void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
+{
+ GpxWritingContext context = {options, f};
+ gpx_write_header ( f );
+ gpx_write_track ( trk, &context );
+ gpx_write_footer ( f );
+}
+
+/**
+ * Common write of a temporary GPX file
+ */
+static gchar* write_tmp_file ( VikTrwLayer *vtl, VikTrack *trk, GpxWritingOptions *options )
+{
+ gchar *tmp_filename = NULL;
+ GError *error = NULL;
+ // Opening temporary file
+ int fd = g_file_open_tmp("viking_XXXXXX.gpx", &tmp_filename, &error);
+ if (fd < 0) {
+ g_warning ( _("failed to open temporary file: %s"), error->message );
+ g_clear_error ( &error );
+ return NULL;
+ }
+ g_debug ("%s: temporary file = %s", __FUNCTION__, tmp_filename);
+
+ FILE *ff = fdopen (fd, "w");
+
+ if ( trk )
+ a_gpx_write_track_file ( trk, ff, options );
+ else
+ a_gpx_write_file ( vtl, ff, options );
+
+ fclose (ff);
+
+ return tmp_filename;
+}
+
+/*
+ * a_gpx_write_tmp_file:
+ * @vtl: The #VikTrwLayer to write
+ * @options: Possible ways of writing the file data (can be NULL)
+ *
+ * Returns: The name of newly created temporary GPX file
+ * This file should be removed once used and the string freed.
+ * If NULL then the process failed.
+ */
+gchar* a_gpx_write_tmp_file ( VikTrwLayer *vtl, GpxWritingOptions *options )
+{
+ return write_tmp_file ( vtl, NULL, options );
+}
+
+/*
+ * a_gpx_write_track_tmp_file:
+ * @trk: The #VikTrack to write
+ * @options: Possible ways of writing the file data (can be NULL)
+ *
+ * Returns: The name of newly created temporary GPX file
+ * This file should be removed once used and the string freed.
+ * If NULL then the process failed.
+ */
+gchar* a_gpx_write_track_tmp_file ( VikTrack *trk, GpxWritingOptions *options )
+{
+ return write_tmp_file ( NULL, trk, options );