]> git.street.me.uk Git - andy/viking.git/blobdiff - src/file.c
SF#356778: Download Map Tiles using F5
[andy/viking.git] / src / file.c
index 8e34a7e94177f8920b35a375c6d782a94ca09440..f185a4671b387593bc91765ec82abd32d6f0f91a 100644 (file)
@@ -267,7 +267,15 @@ static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp)
   vik_layer_set_param ( VIK_LAYER(layer_and_vp[0]), i, x, layer_and_vp[1], TRUE );
 }
 
-static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
+/**
+ * Read in a Viking file and return how successful the parsing was
+ * ATM this will always work, in that even if there are parsing problems
+ *  then there will be no new values to override the defaults
+ *
+ * TODO flow up line number(s) / error messages of problems encountered...
+ *
+ */
+static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
 {
   Stack *stack;
   struct LatLon ll = { 0.0, 0.0 };
@@ -281,6 +289,8 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
 
   GHashTable *string_lists = g_hash_table_new(g_direct_hash,g_direct_equal);
 
+  gboolean successful_read = TRUE;
+
   push(&stack);
   stack->under = NULL;
   stack->data = (gpointer) top;
@@ -317,6 +327,7 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
         int parent_type = VIK_LAYER(stack->data)->type;
         if ( ( ! stack->data ) || ((parent_type != VIK_LAYER_AGGREGATE) && (parent_type != VIK_LAYER_GPS)) )
         {
+          successful_read = FALSE;
           g_warning ( "Line %ld: Layer command inside non-Aggregate Layer (type %d)", line_num, parent_type );
           push(&stack); /* inside INVALID layer */
           stack->data = NULL;
@@ -328,6 +339,7 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
           push(&stack);
           if ( type == -1 )
           {
+            successful_read = FALSE;
             g_warning ( "Line %ld: Unknown type %s", line_num, line+6 );
             stack->data = NULL;
           }
@@ -347,8 +359,10 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
       }
       else if ( str_starts_with ( line, "EndLayer", 8, FALSE ) )
       {
-        if ( stack->under == NULL )
+        if ( stack->under == NULL ) {
+          successful_read = FALSE;
           g_warning ( "Line %ld: Mismatched ~EndLayer command", line_num );
+        }
         else
         {
           /* add any string lists we've accumulated */
@@ -367,8 +381,10 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
             else if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_GPS) {
               /* TODO: anything else needs to be done here ? */
             }
-            else
+            else {
+              successful_read = FALSE;
               g_warning ( "Line %ld: EndLayer command inside non-Aggregate Layer (type %d)", line_num, VIK_LAYER(stack->data)->type );
+            }
           }
           pop(&stack);
         }
@@ -376,9 +392,11 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
       else if ( str_starts_with ( line, "LayerData", 9, FALSE ) )
       {
         if ( stack->data && vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data )
-          vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f );
+        {
           /* must read until hits ~EndLayerData */
-
+          if ( ! vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f ) )
+            successful_read = FALSE;
+        }
         else
         { /* simply skip layer data over */
           while ( fgets ( buffer, 4096, f ) )
@@ -400,6 +418,7 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
       }
       else
       {
+        successful_read = FALSE;
         g_warning ( "Line %ld: Unknown tilde command", line_num );
       }
     }
@@ -428,10 +447,12 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
         vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_EXPEDIA );
       else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "google" ) == 0)
       {
+        successful_read = FALSE;
         g_warning ( _("Draw mode '%s' no more supported"), "google" );
       }
       else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "kh" ) == 0)
       {
+        successful_read = FALSE;
         g_warning ( _("Draw mode '%s' no more supported"), "kh" );
       }
       else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "mercator" ) == 0)
@@ -461,6 +482,7 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
 
         if ( ! params )
         {
+          successful_read = FALSE;
           g_warning ( "Line %ld: No options for this kind of layer", line_num );
           continue;
         }
@@ -493,11 +515,19 @@ static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
             found_match = TRUE;
             break;
           }
-        if ( ! found_match )
+        if ( ! found_match ) {
+          // ATM don't flow up this issue because at least one internal parameter has changed from version 1.3
+          //   and don't what to worry users about raising such issues
+          // TODO Maybe hold old values here - compare the line value against them and if a match
+          //       generate a different style of message in the GUI...
+          // successful_read = FALSE;
           g_warning ( "Line %ld: Unknown parameter. Line:\n%s", line_num, line );
+       }
       }
-      else
+      else {
+        successful_read = FALSE;
         g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num );
+      }
     }
 /* could be:
 [Layer Type=Bla]
@@ -527,6 +557,8 @@ name=this
   /* delete anything we've forgotten about -- should only happen when file ends before an EndLayer */
   g_hash_table_foreach ( string_lists, string_list_delete, NULL );
   g_hash_table_destroy ( string_lists );
+
+  return successful_read;
 }
 
 /*
@@ -577,61 +609,72 @@ gboolean check_file_magic_vik ( const gchar *filename )
 
 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)
     filename = filename + 7;
 
-  gboolean is_gpx_file = check_file_ext ( filename, ".gpx" );
   FILE *f = xfopen ( filename, "r" );
 
-  g_assert ( vp );
-
   if ( ! f )
     return LOAD_TYPE_READ_FAILURE;
 
-  if ( !is_gpx_file && check_magic ( f, VIK_MAGIC ) )
+  VikLoadType_t load_answer = LOAD_TYPE_OTHER_SUCCESS;
+
+  // Attempt loading the primary file type first - our internal .vik file:
+  if ( check_magic ( f, VIK_MAGIC ) )
   {
-    file_read ( top, f, vp );
-    if ( f != stdin )
-      xfclose(f);
-    return LOAD_TYPE_VIK_SUCCESS;
+    if ( file_read ( top, f, vp ) )
+      load_answer = LOAD_TYPE_VIK_SUCCESS;
+    else
+      load_answer = LOAD_TYPE_VIK_FAILURE_NON_FATAL;
   }
   else
   {
-    gboolean success = TRUE;
+       // For all other file types which consist of tracks, routes and/or waypoints,
+       //  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, NULL, FALSE );
 
     // 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 ( ! a_babel_convert_from ( VIK_TRW_LAYER(vtl), "-i kml", filename, NULL, NULL ) ) {
-       // Probably want to remove the vtl, but I'm not sure how yet...
-       xfclose(f);
-       return LOAD_TYPE_GPSBABEL_FAILURE;
+      if ( ! ( success = a_babel_convert_from ( VIK_TRW_LAYER(vtl), "-i kml", filename, 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 ( check_file_ext ( filename, ".gpx" ) || check_magic ( f, GPX_MAGIC ) ) {
+      if ( ! ( success = a_gpx_read_file ( VIK_TRW_LAYER(vtl), f ) ) ) {
+        load_answer = LOAD_TYPE_GPX_FAILURE;
       }
     }
-    else if ( is_gpx_file || check_magic ( f, GPX_MAGIC ) ) {
-      a_gpx_read_file ( VIK_TRW_LAYER(vtl), f );
+    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
+        load_answer = LOAD_TYPE_UNSUPPORTED_FAILURE;
+         }
     }
-    else
-      success = a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f );
 
-    // Refuse to load file types not supported
+    // Clean up when we can't handle the file
     if ( ! success ) {
       // free up layer
       g_object_unref ( vtl );
-      xfclose(f);
-      return LOAD_TYPE_UNSUPPORTED_FAILURE;
     }
-
-    vik_layer_rename ( vtl, a_file_basename ( filename ) );
-    vik_layer_post_read ( vtl, vp, TRUE );
-    vik_aggregate_layer_add_layer ( top, vtl );
-    vik_trw_layer_auto_set_view ( VIK_TRW_LAYER(vtl), vp );
-
-    xfclose(f);
-    return LOAD_TYPE_OTHER_SUCCESS;
+    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_trw_layer_auto_set_view ( VIK_TRW_LAYER(vtl), vp );
+    }
   }
+  xfclose(f);
+  return load_answer;
 }
 
 gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filename )
@@ -671,9 +714,9 @@ const gchar *a_file_basename ( const gchar *filename )
 */
 gboolean check_file_ext ( const gchar *filename, const gchar *fileext )
 {
+  g_return_val_if_fail ( filename != NULL, FALSE );
+  g_return_val_if_fail ( fileext && fileext[0]=='.', FALSE );
   const gchar *basename = a_file_basename(filename);
-  g_assert( filename );
-  g_assert( fileext && fileext[0]=='.' );
   if (!basename)
     return FALSE;
 
@@ -684,16 +727,15 @@ gboolean check_file_ext ( const gchar *filename, const gchar *fileext )
   return FALSE;
 }
 
-gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t file_type, const gchar *trackname )
+gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t file_type, VikTrack *trk )
 {
   FILE *f = g_fopen ( filename, "w" );
   if ( f )
   {
-    if (trackname) {
-      VikTrack *vt = vik_trw_layer_get_track ( vtl, trackname );
+    if ( trk ) {
       switch ( file_type ) {
         case FILE_TYPE_GPX:
-          a_gpx_write_track_file ( trackname, vt, f );
+          a_gpx_write_track_file ( trk, f );
           break;
         default:
           g_critical("Houston, we've had a problem. file_type=%d", file_type);