]> git.street.me.uk Git - andy/viking.git/blobdiff - src/gpx.c
Enable option to not write invisible tracks or waypoints in GPX file and don't write...
[andy/viking.git] / src / gpx.c
index 3138dce6aa272ca8e475719cfe284a714c0a998f..63dd68f99dd14f284c7976a1febc7364356bab7b 100644 (file)
--- a/src/gpx.c
+++ b/src/gpx.c
@@ -418,9 +418,10 @@ static void gpx_cdata(void *dta, const XML_Char *s, int len)
 // make like a "stack" of tag names
 // like gpspoint's separated like /gpx/wpt/whatever
 
-void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
+gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
   XML_Parser parser = XML_ParserCreate(NULL);
   int done=0, len;
+  enum XML_Status status = XML_STATUS_ERROR;
 
   XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
   XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
@@ -439,12 +440,14 @@ void a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
   while (!done) {
     len = fread(buf, 1, sizeof(buf)-7, f);
     done = feof(f) || !len;
-    XML_Parse(parser, buf, len, done);
+    status = XML_Parse(parser, buf, len, done);
   }
  
   XML_ParserFree (parser);
   g_string_free ( xpath, TRUE );
   g_string_free ( c_cdata, TRUE );
+
+  return status != XML_STATUS_ERROR;
 }
 
 /**** entitize from GPSBabel ****/
@@ -618,6 +621,10 @@ entitize(const char * str)
 
 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;
@@ -625,6 +632,8 @@ static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
   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 );
@@ -766,6 +775,10 @@ static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context
 
 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. */
@@ -776,6 +789,8 @@ static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
   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, "<trk%s>\n  <name>%s</name>\n", t->visible ? "" : " hidden=\"hidden\"", tmp );
   g_free ( tmp );
 
@@ -785,7 +800,6 @@ static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
     fprintf ( f, "  <desc>%s</desc>\n", tmp );
     g_free ( tmp );
   }
-
   fprintf ( f, "  <trkseg>\n" );
 
   if ( t->trackpoints && t->trackpoints->data ) {
@@ -866,12 +880,7 @@ static int gpx_track_and_timestamp_compare(const void *x, const void *y)
   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 )
+void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
 {
   GpxWritingContext context = { options, f };
 
@@ -908,12 +917,7 @@ void a_gpx_write_file_options ( GpxWritingOptions *options, VikTrwLayer *vtl, FI
   gpx_write_footer ( f );
 }
 
-void a_gpx_write_track_file ( VikTrack *trk, FILE *f )
-{
-  a_gpx_write_track_file_options ( NULL, trk, f );
-}
-
-void a_gpx_write_track_file_options ( GpxWritingOptions *options, VikTrack *trk, FILE *f )
+void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
 {
   GpxWritingContext context = {options, f};
   gpx_write_header ( f );