]> git.street.me.uk Git - andy/viking.git/blobdiff - src/file.c
[QA] Tools are enabled per active layer
[andy/viking.git] / src / file.c
index dbc7e84851bc8e7d9e5d3c4f14c3402154ea083d..ae22df2c1f726bd89b7f7d4b44ba8ddd018abb82 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
  * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
+ * Copyright (C) 2012-2013, Rob Norris <rw_norris@hotmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #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>
 
+#include "file.h"
+
 #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"
 #define VIK_MAGIC_LEN 4
 
-#ifdef WINDOWS
-#define FILE_SEP '\\'
-#else
-#define FILE_SEP '/'
-#endif
-
 #define VIKING_FILE_VERSION 1
 
 typedef struct _Stack Stack;
@@ -93,15 +93,6 @@ static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, gu
 }
 
 void file_write_layer_param ( FILE *f, const gchar *name, VikLayerParamType type, VikLayerParamData data ) {
-static VikLayerTypeEnum layer_type_from_string ( const gchar *str )
-{
-  VikLayerTypeEnum i;
-  for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
-    if ( strcasecmp ( str, vik_layer_get_interface(i)->fixed_layer_name ) == 0 )
-      return i;
-  return VIK_LAYER_NUM_TYPES;
-}
-
       /* 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,
@@ -129,7 +120,7 @@ static VikLayerTypeEnum layer_type_from_string ( const gchar *str )
           case VIK_LAYER_PARAM_UINT: fprintf ( f, "%d\n", data.u ); break;
           case VIK_LAYER_PARAM_INT: fprintf ( f, "%d\n", data.i ); break;
           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_STRING: fprintf ( f, "%s\n", data.s ? 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;
         }
@@ -274,7 +265,7 @@ static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp)
  * TODO flow up line number(s) / error messages of problems encountered...
  *
  */
-static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
+static gboolean file_read ( VikAggregateLayer *top, FILE *f, const gchar *dirpath, VikViewport *vp )
 {
   Stack *stack;
   struct LatLon ll = { 0.0, 0.0 };
@@ -334,7 +325,7 @@ static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
         }
         else
         {
-          VikLayerTypeEnum type = layer_type_from_string ( line+6 );
+          VikLayerTypeEnum type = vik_layer_type_from_string ( line+6 );
           push(&stack);
           if ( type == VIK_LAYER_NUM_TYPES )
           {
@@ -374,7 +365,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) {
@@ -393,7 +384,7 @@ static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
         if ( stack->data && vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data )
         {
           /* must read until hits ~EndLayerData */
-          if ( ! vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f ) )
+          if ( ! vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f, dirpath ) )
             successful_read = FALSE;
         }
         else
@@ -548,14 +539,14 @@ 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);
   }
 
   if ( ll.lat != 0.0 || ll.lon != 0.0 )
-    vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll );
+    vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll, TRUE );
 
   if ( ( ! VIK_LAYER(top)->visible ) && VIK_LAYER(top)->realized )
     vik_treeview_item_set_visible ( VIK_LAYER(top)->vt, &(VIK_LAYER(top)->iter), FALSE ); 
@@ -613,14 +604,56 @@ gboolean check_file_magic_vik ( const gchar *filename )
   return result;
 }
 
+/**
+ * append_file_ext:
+ *
+ * Append a file extension, if not already present.
+ *
+ * Returns: a newly allocated string
+ */
+gchar *append_file_ext ( const gchar *filename, VikLoadType_t type )
+{
+  gchar *new_name = NULL;
+  const gchar *ext = NULL;
+
+  /* Select an extension */
+  switch (type)
+  {
+  case FILE_TYPE_GPX:
+    ext = ".gpx";
+    break;
+  case FILE_TYPE_KML:
+    ext = ".kml";
+    break;
+  case FILE_TYPE_GPSMAPPER:
+  case FILE_TYPE_GPSPOINT:
+  default:
+    /* Do nothing, ext already set to NULL */
+    break;
+  }
+
+  /* Do */
+  if ( ext != NULL && ! check_file_ext ( filename, ext ) )
+    new_name = g_strconcat ( filename, ext, NULL );
+  else
+    /* Simply duplicate */
+    new_name = g_strdup ( filename );
+
+  return new_name;
+}
+
 VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename_or_uri )
 {
   g_return_val_if_fail ( vp != NULL, LOAD_TYPE_READ_FAILURE );
 
   char *filename = (char *)filename_or_uri;
-  if (strncmp(filename, "file://", 7) == 0)
+  if (strncmp(filename, "file://", 7) == 0) {
+    // Consider replacing this with:
+    // filename = g_filename_from_uri ( entry, NULL, NULL );
+    // Since this doesn't support URIs properly (i.e. will failure if is it has %20 characters in it)
     filename = filename + 7;
-
+    g_debug ( "Loading file %s from URI %s", filename, filename_or_uri );
+  }
   FILE *f = xfopen ( filename, "r" );
 
   if ( ! f )
@@ -628,10 +661,11 @@ 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 );
   // Attempt loading the primary file type first - our internal .vik file:
   if ( check_magic ( f, VIK_MAGIC ) )
   {
-    if ( file_read ( top, f, vp ) )
+    if ( file_read ( top, f, dirpath, vp ) )
       load_answer = LOAD_TYPE_VIK_SUCCESS;
     else
       load_answer = LOAD_TYPE_VIK_FAILURE_NON_FATAL;
@@ -643,6 +677,7 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
     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, NULL, FALSE );
+    vik_layer_rename ( vtl, a_file_basename ( filename ) );
 
     // 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 ) ) {
@@ -660,11 +695,12 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
     }
     else {
       // Try final supported file type
-      if ( ! ( success = a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f ) ) ) {
-               // Failure here means we don't know how to handle the file
+      if ( ! ( success = a_gpspoint_read_file ( VIK_TRW_LAYER(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 ) {
@@ -673,9 +709,8 @@ VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar
     }
     else {
       // 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 );
     }
   }
@@ -695,8 +730,26 @@ gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filenam
   if ( ! f )
     return FALSE;
 
+  // Enable relative paths in .vik files to work
+  gchar *cwd = g_get_current_dir();
+  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 );
 
+  // Restore previous working directory
+  if ( cwd ) {
+    if ( g_chdir ( cwd ) ) {
+      g_warning ( "Could not return to directory %s", cwd );
+    }
+    g_free (cwd);
+  }
+
   fclose(f);
   f = NULL;
 
@@ -708,7 +761,7 @@ const gchar *a_file_basename ( const gchar *filename )
 {
   const gchar *t = filename + strlen(filename) - 1;
   while ( --t > filename )
-    if ( *(t-1) == FILE_SEP )
+    if ( *(t-1) == G_DIR_SEPARATOR )
       break;
   if ( t >= filename )
     return t;
@@ -798,3 +851,159 @@ gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t
   return FALSE;
 }
 
+/**
+ * a_file_export_babel:
+ */
+gboolean a_file_export_babel ( VikTrwLayer *vtl, const gchar *filename, const gchar *format,
+                               gboolean tracks, gboolean routes, gboolean waypoints )
+{
+  gchar *args = g_strdup_printf("%s %s %s -o %s",
+                                tracks ? "-t" : "",
+                                routes ? "-r" : "",
+                                waypoints ? "-w" : "",
+                                format);
+  gboolean result = a_babel_convert_to ( vtl, NULL, args, filename, NULL, NULL );
+  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 */