]> git.street.me.uk Git - andy/viking.git/blobdiff - src/file.c
Remove definition of a non existant function
[andy/viking.git] / src / file.c
index ff4d0f73561884fc92d3e879473e8c6b99b67a8f..6334676b87aab3682a2a3cc93eb017ddfd5407a3 100644 (file)
@@ -30,6 +30,7 @@
 #include "gpx.h"
 #include "geojson.h"
 #include "babel.h"
+#include "gpsmapper.h"
 
 #include <string.h>
 #include <stdlib.h>
@@ -37,9 +38,6 @@
 #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>
@@ -51,6 +49,7 @@
 #define VIK_MAGIC "#VIK"
 #define GPX_MAGIC "<?xm"
 #define VIK_MAGIC_LEN 4
+#define GPX_MAGIC_LEN 4
 
 #define VIKING_FILE_VERSION 1
 
@@ -58,7 +57,7 @@ typedef struct _Stack Stack;
 
 struct _Stack {
   Stack *under;
-  gpointer *data;
+  gpointer data;
 };
 
 static void pop(Stack **stack) {
@@ -74,9 +73,9 @@ static void push(Stack **stack)
   *stack = tmp;
 }
 
-static gboolean check_magic ( FILE *f, const gchar *magic_number )
+static gboolean check_magic ( FILE *f, const gchar *magic_number, guint magic_length )
 {
-  gchar magic[VIK_MAGIC_LEN];
+  gchar magic[magic_length];
   gboolean rv = FALSE;
   gint8 i;
   if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) &&
@@ -130,7 +129,7 @@ void file_write_layer_param ( FILE *f, const gchar *name, VikLayerParamType type
       }
 }
 
-static void write_layer_params_and_data ( VikLayer *l, FILE *f )
+static void write_layer_params_and_data ( VikLayer *l, FILE *f, const gchar *dirpath )
 {
   VikLayerParam *params = vik_layer_get_interface(l->type)->params;
   VikLayerFuncGetParam get_param = vik_layer_get_interface(l->type)->get_param;
@@ -152,7 +151,7 @@ static void write_layer_params_and_data ( VikLayer *l, FILE *f )
   if ( vik_layer_get_interface(l->type)->write_file_data )
   {
     fprintf ( f, "\n\n~LayerData\n" );
-    vik_layer_get_interface(l->type)->write_file_data ( l, f );
+    vik_layer_get_interface(l->type)->write_file_data ( l, f, dirpath );
     fprintf ( f, "~EndLayerData\n" );
   }
   /* foreach param:
@@ -161,7 +160,7 @@ static void write_layer_params_and_data ( VikLayer *l, FILE *f )
   */
 }
 
-static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp )
+static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp, const gchar *dirpath )
 {
   Stack *stack = NULL;
   VikLayer *current_layer;
@@ -203,7 +202,7 @@ static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp )
   {
     current_layer = VIK_LAYER(((GList *)stack->data)->data);
     fprintf ( f, "\n~Layer %s\n", vik_layer_get_interface(current_layer->type)->fixed_layer_name );
-    write_layer_params_and_data ( current_layer, f );
+    write_layer_params_and_data ( current_layer, f, dirpath );
     if ( current_layer->type == VIK_LAYER_AGGREGATE && !vik_aggregate_layer_is_empty(VIK_AGGREGATE_LAYER(current_layer)) )
     {
       push(&stack);
@@ -255,9 +254,14 @@ static void string_list_delete ( gpointer key, gpointer l, gpointer user_data )
 
 static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp)
 {
-  VikLayerParamData x;
-  x.sl = list;
-  vik_layer_set_param ( VIK_LAYER(layer_and_vp[0]), i, x, layer_and_vp[1], TRUE );
+  VikLayerSetParam vlsp;
+  vlsp.id                  = i;
+  vlsp.data.sl             = list;
+  vlsp.vp                  = layer_and_vp[1];
+  vlsp.is_file_operation   = TRUE;
+  vlsp.dirpath             = (const gchar*)layer_and_vp[2];
+
+  vik_layer_set_param ( VIK_LAYER(layer_and_vp[0]), &vlsp );
 }
 
 /**
@@ -359,9 +363,10 @@ static gboolean file_read ( VikAggregateLayer *top, FILE *f, const gchar *dirpat
         else
         {
           /* add any string lists we've accumulated */
-          gpointer layer_and_vp[2];
+          gpointer layer_and_vp[3];
           layer_and_vp[0] = stack->data;
           layer_and_vp[1] = vp;
+          layer_and_vp[2] = (gpointer)dirpath;
           g_hash_table_foreach ( string_lists, (GHFunc) string_list_set_param, layer_and_vp );
           g_hash_table_remove_all ( string_lists );
 
@@ -510,7 +515,14 @@ static gboolean file_read ( VikAggregateLayer *top, FILE *f, const gchar *dirpat
                 /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */
                 default: x.s = line;
               }
-              vik_layer_set_param ( VIK_LAYER(stack->data), i, x, vp, TRUE );
+
+              VikLayerSetParam vlsp;
+              vlsp.id                  = i;
+              vlsp.data                = x;
+              vlsp.vp                  = vp;
+              vlsp.is_file_operation   = TRUE;
+              vlsp.dirpath             = dirpath;
+              vik_layer_set_param ( VIK_LAYER(stack->data), &vlsp );
             }
             found_match = TRUE;
             break;
@@ -601,7 +613,7 @@ gboolean check_file_magic_vik ( const gchar *filename )
   gboolean result = FALSE;
   FILE *ff = xfopen ( filename );
   if ( ff ) {
-    result = check_magic ( ff, VIK_MAGIC );
+    result = check_magic ( ff, VIK_MAGIC, VIK_MAGIC_LEN );
     xfclose ( ff );
   }
   return result;
@@ -648,7 +660,7 @@ gchar *append_file_ext ( const gchar *filename, VikFileType_t type )
   return new_name;
 }
 
-VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename_or_uri )
+VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, VikTrwLayer *vtl, const gchar *filename_or_uri )
 {
   g_return_val_if_fail ( vp != NULL, LOAD_TYPE_READ_FAILURE );
 
@@ -667,9 +679,14 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
 
   VikLoadType_t load_answer = LOAD_TYPE_OTHER_SUCCESS;
 
-  gchar *dirpath = g_path_get_dirname ( filename );
+  gchar *absolute = file_realpath_dup ( filename );
+  gchar *dirpath = NULL;
+  if ( absolute )
+    dirpath = g_path_get_dirname ( absolute );
+  g_free ( absolute );
+
   // Attempt loading the primary file type first - our internal .vik file:
-  if ( check_magic ( f, VIK_MAGIC ) )
+  if ( check_magic ( f, VIK_MAGIC, VIK_MAGIC_LEN ) )
   {
     if ( file_read ( top, f, dirpath, vp ) )
       load_answer = LOAD_TYPE_VIK_SUCCESS;
@@ -686,33 +703,35 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
        //  must be loaded into a new TrackWaypoint layer (hence it be created)
     gboolean success = TRUE; // Detect load failures - mainly to remove the layer created as it's not required
 
-    VikLayer *vtl = vik_layer_create ( VIK_LAYER_TRW, vp, FALSE );
-    vik_layer_rename ( vtl, a_file_basename ( filename ) );
+    // Add to specified layer
+    gboolean add_new = !IS_VIK_TRW_LAYER(vtl);
+    if (add_new) {
+      vtl = VIK_TRW_LAYER (vik_layer_create ( VIK_LAYER_TRW, vp, FALSE ));
+      vik_layer_rename ( VIK_LAYER(vtl), a_file_basename ( filename ) );
+    }
 
     // In fact both kml & gpx files start the same as they are in xml
-    if ( a_file_check_ext ( filename, ".kml" ) && check_magic ( f, GPX_MAGIC ) ) {
+    if ( a_file_check_ext ( filename, ".kml" ) && check_magic ( f, GPX_MAGIC, GPX_MAGIC_LEN ) ) {
       // Implicit Conversion
       ProcessOptions po = { "-i kml", filename, NULL, NULL, NULL, NULL };
-      if ( ! ( success = a_babel_convert_from ( VIK_TRW_LAYER(vtl), &po, NULL, NULL, NULL ) ) ) {
+      if ( ! ( success = a_babel_convert_from ( vtl, &po, NULL, NULL, NULL ) ) ) {
         load_answer = LOAD_TYPE_GPSBABEL_FAILURE;
       }
     }
     // NB use a extension check first, as a GPX file header may have a Byte Order Mark (BOM) in it
     //    - which currently confuses our check_magic function
-    else if ( a_file_check_ext ( filename, ".gpx" ) || check_magic ( f, GPX_MAGIC ) ) {
-      if ( ! ( success = a_gpx_read_file ( VIK_TRW_LAYER(vtl), f ) ) ) {
+    else if ( a_file_check_ext ( filename, ".gpx" ) || check_magic ( f, GPX_MAGIC, GPX_MAGIC_LEN ) ) {
+      if ( ! ( success = a_gpx_read_file ( vtl, f, dirpath ) ) ) {
         load_answer = LOAD_TYPE_GPX_FAILURE;
       }
     }
     else {
       // Try final supported file type
-      if ( ! ( success = a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f, dirpath ) ) ) {
+      if ( ! ( success = a_gpspoint_read_file ( vtl, f, dirpath ) ) ) {
         // Failure here means we don't know how to handle the file
         load_answer = LOAD_TYPE_UNSUPPORTED_FAILURE;
       }
     }
-    g_free ( dirpath );
-
     // Clean up when we can't handle the file
     if ( ! success ) {
       // free up layer
@@ -720,11 +739,18 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
     }
     else {
       // Complete the setup from the successful load
-      vik_layer_post_read ( vtl, vp, TRUE );
-      vik_aggregate_layer_add_layer ( top, vtl, FALSE );
-      vik_trw_layer_auto_set_view ( VIK_TRW_LAYER(vtl), vp );
+      vik_layer_post_read ( VIK_LAYER(vtl), vp, TRUE );
+      if (add_new) {
+        vik_aggregate_layer_add_layer ( top, VIK_LAYER(vtl), FALSE );
+      }
+      else {
+       // Make it more accessible in layers panel
+       vik_layer_expand_tree ( VIK_LAYER(vtl) );
+      }
+      vik_trw_layer_auto_set_view ( vtl, vp );
     }
   }
+  g_free ( dirpath );
   xfclose(f);
   return load_answer;
 }
@@ -748,10 +774,10 @@ gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filenam
     if ( g_chdir ( dir ) ) {
       g_warning ( "Could not change directory to %s", dir );
     }
-    g_free (dir);
   }
 
-  file_write ( top, f, vp );
+  file_write ( top, f, vp, dir );
+  g_free (dir);
 
   // Restore previous working directory
   if ( cwd ) {
@@ -804,6 +830,7 @@ gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t
   if ( f )
   {
     gboolean result = TRUE;
+    gchar *dirpath = g_path_get_dirname ( filename );
 
     if ( trk ) {
       switch ( file_type ) {
@@ -821,17 +848,16 @@ gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t
           a_gpsmapper_write_file ( vtl, f );
           break;
         case FILE_TYPE_GPX:
-          a_gpx_write_file ( vtl, f, &options );
+          a_gpx_write_file ( vtl, f, &options, dirpath );
           break;
         case FILE_TYPE_GPSPOINT:
-          a_gpspoint_write_file ( vtl, f );
+          a_gpspoint_write_file ( vtl, f, dirpath );
           break;
         case FILE_TYPE_GEOJSON:
           result = a_geojson_write_file ( vtl, f );
           break;
         case FILE_TYPE_KML:
          fclose ( f );
-         f = NULL;
          switch ( a_vik_get_kml_export_units () ) {
            case VIK_KML_EXPORT_UNITS_STATUTE:
              return a_babel_convert_to ( vtl, NULL, "-o kml", filename, NULL, NULL );
@@ -849,8 +875,8 @@ gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t
           g_critical("Houston, we've had a problem. file_type=%d", file_type);
       }
     }
+    g_free ( dirpath );
     fclose ( f );
-    f = NULL;
     return result;
   }
   return FALSE;
@@ -871,144 +897,3 @@ gboolean a_file_export_babel ( VikTrwLayer *vtl, const gchar *filename, const gc
   g_free(args);
   return result;
 }
-
-/**
- * Just a wrapper around realpath, which itself is platform dependent
- */
-char *file_realpath ( const char *path, char *real )
-{
-  return realpath ( path, real );
-}
-
-#ifndef MAXPATHLEN
-#define MAXPATHLEN 1024
-#endif
-/**
- * Always return the canonical filename in a newly allocated string
- */
-char *file_realpath_dup ( const char *path )
-{
-       char real[MAXPATHLEN];
-
-       g_return_val_if_fail(path != NULL, NULL);
-
-       if (file_realpath(path, real))
-               return g_strdup(real);
-
-       return g_strdup(path);
-}
-
-/**
- * Permission granted to use this code after personal correspondance
- * Slightly reworked for better cross platform use, glibisms, function rename and a compacter format
- *
- * FROM http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263
- */
-
-// GetRelativeFilename(), by Rob Fisher.
-// rfisher@iee.org
-// http://come.to/robfisher
-
-// The number of characters at the start of an absolute filename.  e.g. in DOS,
-// absolute filenames start with "X:\" so this value should be 3, in UNIX they start
-// with "\" so this value should be 1.
-#ifdef WINDOWS
-#define ABSOLUTE_NAME_START 3
-#else
-#define ABSOLUTE_NAME_START 1
-#endif
-
-// Given the absolute current directory and an absolute file name, returns a relative file name.
-// For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given,
-// GetRelativeFilename will return ..\whee\text.txt.
-
-const gchar *file_GetRelativeFilename ( gchar *currentDirectory, gchar *absoluteFilename )
-{
-  gint afMarker = 0, rfMarker = 0;
-  gint cdLen = 0, afLen = 0;
-  gint i = 0;
-  gint levels = 0;
-  static gchar relativeFilename[MAXPATHLEN+1];
-
-  cdLen = strlen(currentDirectory);
-  afLen = strlen(absoluteFilename);
-
-  // make sure the names are not too long or too short
-  if (cdLen > MAXPATHLEN || cdLen < ABSOLUTE_NAME_START+1 ||
-      afLen > MAXPATHLEN || afLen < ABSOLUTE_NAME_START+1) {
-    return NULL;
-  }
-
-  // Handle DOS names that are on different drives:
-  if (currentDirectory[0] != absoluteFilename[0]) {
-    // not on the same drive, so only absolute filename will do
-    strcpy(relativeFilename, absoluteFilename);
-    return relativeFilename;
-  }
-
-  // they are on the same drive, find out how much of the current directory
-  // is in the absolute filename
-  i = ABSOLUTE_NAME_START;
-  while (i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i]) {
-    i++;
-  }
-
-  if (i == cdLen && (absoluteFilename[i] == G_DIR_SEPARATOR || absoluteFilename[i-1] == G_DIR_SEPARATOR)) {
-    // the whole current directory name is in the file name,
-    // so we just trim off the current directory name to get the
-    // current file name.
-    if (absoluteFilename[i] == G_DIR_SEPARATOR) {
-      // a directory name might have a trailing slash but a relative
-      // file name should not have a leading one...
-      i++;
-    }
-
-    strcpy(relativeFilename, &absoluteFilename[i]);
-    return relativeFilename;
-  }
-
-  // The file is not in a child directory of the current directory, so we
-  // need to step back the appropriate number of parent directories by
-  // using "..\"s.  First find out how many levels deeper we are than the
-  // common directory
-  afMarker = i;
-  levels = 1;
-
-  // count the number of directory levels we have to go up to get to the
-  // common directory
-  while (i < cdLen) {
-    i++;
-    if (currentDirectory[i] == G_DIR_SEPARATOR) {
-      // make sure it's not a trailing slash
-      i++;
-      if (currentDirectory[i] != '\0') {
-       levels++;
-      }
-    }
-  }
-
-  // move the absolute filename marker back to the start of the directory name
-  // that it has stopped in.
-  while (afMarker > 0 && absoluteFilename[afMarker-1] != G_DIR_SEPARATOR) {
-    afMarker--;
-  }
-
-  // check that the result will not be too long
-  if (levels * 3 + afLen - afMarker > MAXPATHLEN) {
-    return NULL;
-  }
-
-  // add the appropriate number of "..\"s.
-  rfMarker = 0;
-  for (i = 0; i < levels; i++) {
-    relativeFilename[rfMarker++] = '.';
-    relativeFilename[rfMarker++] = '.';
-    relativeFilename[rfMarker++] = G_DIR_SEPARATOR;
-  }
-
-  // copy the rest of the filename into the result string
-  strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]);
-
-  return relativeFilename;
-}
-/* END http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263 */