]> git.street.me.uk Git - andy/viking.git/blobdiff - src/file.c
Add 'Advanced' preferences tab.
[andy/viking.git] / src / file.c
index e473228d5ed206805e891aa4432be2aec2e32b07..b2be51f1448fa750e8f2f44547bf927200711231 100644 (file)
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef WINDOWS
+#define realpath(X,Y) _fullpath(Y,X,MAX_PATH)
+#endif
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
 
-/* Relax some dependencies */
-#if ! GLIB_CHECK_VERSION(2,12,0)
-static gboolean return_true (gpointer a, gpointer b, gpointer c) { return TRUE; }
-static g_hash_table_remove_all (GHashTable *ght) { g_hash_table_foreach_remove ( ght, (GHRFunc) return_true, FALSE ); }
-#endif
-
 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
 #define VIK_MAGIC "#VIK"
 #define GPX_MAGIC "<?xm"
@@ -55,6 +52,8 @@ static g_hash_table_remove_all (GHashTable *ght) { g_hash_table_foreach_remove (
 #define FILE_SEP '/'
 #endif
 
+#define VIKING_FILE_VERSION 1
+
 typedef struct _Stack Stack;
 
 struct _Stack {
@@ -96,16 +95,7 @@ static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, gu
   return FALSE;
 }
 
-static guint16 layer_type_from_string ( const gchar *str )
-{
-  guint8 i;
-  for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
-    if ( strcasecmp ( str, vik_layer_get_interface(i)->name ) == 0 )
-      return i;
-  return -1;
-}
-
-void file_write_layer_param ( FILE *f, const gchar *name, guint8 type, VikLayerParamData data ) {
+void file_write_layer_param ( FILE *f, const gchar *name, VikLayerParamType type, VikLayerParamData data ) {
       /* string lists are handled differently. We get a GList (that shouldn't
        * be freed) back for get_param and if it is null we shouldn't write
        * anything at all (otherwise we'd read in a list with an empty string,
@@ -135,6 +125,7 @@ void file_write_layer_param ( FILE *f, const gchar *name, guint8 type, VikLayerP
           case VIK_LAYER_PARAM_BOOLEAN: fprintf ( f, "%c\n", data.b ? 't' : 'f' ); break;
           case VIK_LAYER_PARAM_STRING: fprintf ( f, "%s\n", data.s ); break;
           case VIK_LAYER_PARAM_COLOR: fprintf ( f, "#%.2x%.2x%.2x\n", (int)(data.c.red/256),(int)(data.c.green/256),(int)(data.c.blue/256)); break;
+          default: break;
         }
       }
 }
@@ -195,7 +186,9 @@ static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp )
       g_critical("Houston, we've had a problem. mode=%d", mode);
   }
 
-  fprintf ( f, "#VIKING GPS Data file " VIKING_URL "\n\nxmpp=%f\nympp=%f\nlat=%f\nlon=%f\nmode=%s\ncolor=%s\nhighlightcolor=%s\ndrawscale=%s\ndrawcentermark=%s\ndrawhighlight=%s\n",
+  fprintf ( f, "#VIKING GPS Data file " VIKING_URL "\n" );
+  fprintf ( f, "FILE_VERSION=%d\n", VIKING_FILE_VERSION );
+  fprintf ( f, "\nxmpp=%f\nympp=%f\nlat=%f\nlon=%f\nmode=%s\ncolor=%s\nhighlightcolor=%s\ndrawscale=%s\ndrawcentermark=%s\ndrawhighlight=%s\n",
       vik_viewport_get_xmpp ( VIK_VIEWPORT(vp) ), vik_viewport_get_ympp ( VIK_VIEWPORT(vp) ), ll.lat, ll.lon,
       modestring, vik_viewport_get_background_color(VIK_VIEWPORT(vp)),
       vik_viewport_get_highlight_color(VIK_VIEWPORT(vp)),
@@ -209,7 +202,7 @@ static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp )
   while (stack && stack->data)
   {
     current_layer = VIK_LAYER(((GList *)stack->data)->data);
-    fprintf ( f, "\n~Layer %s\n", vik_layer_get_interface(current_layer->type)->name );
+    fprintf ( f, "\n~Layer %s\n", vik_layer_get_interface(current_layer->type)->fixed_layer_name );
     write_layer_params_and_data ( current_layer, f );
     if ( current_layer->type == VIK_LAYER_AGGREGATE && !vik_aggregate_layer_is_empty(VIK_AGGREGATE_LAYER(current_layer)) )
     {
@@ -335,9 +328,9 @@ static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
         }
         else
         {
-          gint16 type = layer_type_from_string ( line+6 );
+          VikLayerTypeEnum type = vik_layer_type_from_string ( line+6 );
           push(&stack);
-          if ( type == -1 )
+          if ( type == VIK_LAYER_NUM_TYPES )
           {
             successful_read = FALSE;
             g_warning ( "Line %ld: Unknown type %s", line_num, line+6 );
@@ -375,7 +368,7 @@ static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
           if ( stack->data && stack->under->data )
           {
             if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_AGGREGATE) {
-              vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
+              vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data), FALSE );
               vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE );
             }
             else if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_GPS) {
@@ -433,7 +426,14 @@ static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
         if ( line[i] == '=' )
           eq_pos = i;
 
-      if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "xmpp", eq_pos ) == 0) /* "hard coded" params: global & for all layer-types */
+      if ( stack->under == NULL && eq_pos == 12 && strncasecmp ( line, "FILE_VERSION", eq_pos ) == 0) {
+        gint version = strtol(line+13, NULL, 10);
+        g_debug ( "%s: reading file version %d", __FUNCTION__, version );
+        if ( version > VIKING_FILE_VERSION )
+          successful_read = FALSE;
+        // However we'll still carry and attempt to read whatever we can
+      }
+      else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "xmpp", eq_pos ) == 0) /* "hard coded" params: global & for all layer-types */
         vik_viewport_set_xmpp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
       else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "ympp", eq_pos ) == 0)
         vik_viewport_set_ympp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
@@ -542,7 +542,7 @@ name=this
   {
     if ( stack->under && stack->under->data && stack->data )
     {
-      vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
+      vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data), FALSE );
       vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE );
     }
     pop(&stack);
@@ -620,6 +620,16 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
   if ( ! f )
     return LOAD_TYPE_READ_FAILURE;
 
+  // Enables relative paths in a .vik file to work
+  // Also allows us to remember what directory we where using
+  gchar *dir = g_path_get_dirname ( filename );
+  if ( dir ) {
+    if ( g_chdir ( dir ) ) {
+      g_warning ( "Could not change directory to %s", dir );
+    }
+    g_free (dir);
+  }
+
   VikLoadType_t load_answer = LOAD_TYPE_OTHER_SUCCESS;
 
   // Attempt loading the primary file type first - our internal .vik file:
@@ -641,7 +651,7 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
     // In fact both kml & gpx files start the same as they are in xml
     if ( check_file_ext ( filename, ".kml" ) && check_magic ( f, GPX_MAGIC ) ) {
       // Implicit Conversion
-      if ( ! ( success = a_babel_convert_from ( VIK_TRW_LAYER(vtl), "-i kml", filename, NULL, NULL ) ) ) {
+      if ( ! ( success = a_babel_convert_from ( VIK_TRW_LAYER(vtl), "-i kml", filename, NULL, NULL, NULL ) ) ) {
         load_answer = LOAD_TYPE_GPSBABEL_FAILURE;
       }
     }
@@ -669,7 +679,7 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
       // Complete the setup from the successful load
       vik_layer_rename ( vtl, a_file_basename ( filename ) );
       vik_layer_post_read ( vtl, vp, TRUE );
-      vik_aggregate_layer_add_layer ( top, vtl );
+      vik_aggregate_layer_add_layer ( top, vtl, FALSE );
       vik_trw_layer_auto_set_view ( VIK_TRW_LAYER(vtl), vp );
     }
   }
@@ -689,6 +699,16 @@ gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filenam
   if ( ! f )
     return FALSE;
 
+  // Enable relative paths in .vik files to work
+  // Also allows us to remember what directory we where using
+  gchar *dir = g_path_get_dirname ( filename );
+  if ( dir ) {
+    if ( g_chdir ( dir ) ) {
+      g_warning ( "Could not change directory to %s", dir );
+    }
+    g_free (dir);
+  }
+
   file_write ( top, f, vp );
 
   fclose(f);
@@ -740,13 +760,15 @@ gboolean check_file_ext ( const gchar *filename, const gchar *fileext )
  */
 gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t file_type, VikTrack *trk, gboolean write_hidden )
 {
-  GpxWritingOptions options = { FALSE, FALSE, write_hidden };
+  GpxWritingOptions options = { FALSE, FALSE, write_hidden, FALSE };
   FILE *f = g_fopen ( filename, "w" );
   if ( f )
   {
     if ( trk ) {
       switch ( file_type ) {
         case FILE_TYPE_GPX:
+          // trk defined so can set the option
+          options.is_route = trk->is_route;
           a_gpx_write_track_file ( trk, f, &options );
           break;
         default:
@@ -790,3 +812,11 @@ gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t
   return FALSE;
 }
 
+/**
+ * Just a wrapper around realpath, which itself is platform dependent
+ */
+char *file_realpath ( const char *path, char *real )
+{
+  return realpath ( path, real );
+}
+