X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/e270cd3d8aa62bca2630191b41958be0d0cbd430..64d557a287f0e695856d6bc579293b399b8a07ea:/src/file.c?ds=inline diff --git a/src/file.c b/src/file.c index 3c30c30f..f93f5efc 100644 --- a/src/file.c +++ b/src/file.c @@ -192,7 +192,7 @@ 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\ndrawscale=%s\ndrawcentermark=%s", + fprintf ( f, "#VIKING GPS Data file " VIKING_URL "\n\nxmpp=%f\nympp=%f\nlat=%f\nlon=%f\nmode=%s\ncolor=%s\ndrawscale=%s\ndrawcentermark=%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_draw_scale(VIK_VIEWPORT(vp)) ? "t" : "f", @@ -551,8 +551,13 @@ static void xfclose ( FILE *f ) } /* 0 on failure, 1 on success (vik file) 2 on success (other file) */ -gshort a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename ) +gshort a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename_or_uri ) { + char *filename = 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 ); @@ -560,7 +565,7 @@ gshort a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filen if ( ! f ) return 0; - if ( check_magic ( f, VIK_MAGIC ) ) + if ( !is_gpx_file && check_magic ( f, VIK_MAGIC ) ) { file_read ( top, f, vp ); if ( f != stdin ) @@ -573,7 +578,7 @@ gshort a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filen VikLayer *vtl = vik_layer_create ( VIK_LAYER_TRW, vp, NULL, FALSE ); vik_layer_rename ( vtl, a_file_basename ( filename ) ); - if ( check_magic ( f, GPX_MAGIC ) ) + if ( is_gpx_file || check_magic ( f, GPX_MAGIC ) ) a_gpx_read_file ( VIK_TRW_LAYER(vtl), f ); else a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f ); @@ -591,7 +596,12 @@ gshort a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filen gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filename ) { - FILE *f = g_fopen(filename, "w"); + FILE *f; + + if (strncmp(filename, "file://", 7) == 0) + filename = filename + 7; + + f = g_fopen(filename, "w"); if ( ! f ) return FALSE; @@ -616,6 +626,24 @@ const gchar *a_file_basename ( const gchar *filename ) return filename; } +/* example: + gboolean is_gpx = check_file_ext ( "a/b/c.gpx", ".gpx" ); +*/ +gboolean check_file_ext ( const gchar *filename, const gchar *fileext ) +{ + const gchar *basename = a_file_basename(filename); + g_assert( filename ); + g_assert( fileext && fileext[0]=='.' ); + if (!basename) + return FALSE; + + const char * dot = strrchr(basename, '.'); + if (dot && !strcmp(dot, fileext)) + return TRUE; + + return FALSE; +} + gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, gshort file_type ) { FILE *f = g_fopen ( filename, "w" );