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 };
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;
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;
push(&stack);
if ( type == -1 )
{
+ successful_read = FALSE;
g_warning ( "Line %ld: Unknown type %s", line_num, line+6 );
stack->data = NULL;
}
}
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 */
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);
}
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 ) )
}
else
{
+ successful_read = FALSE;
g_warning ( "Line %ld: Unknown tilde command", line_num );
}
}
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)
if ( ! params )
{
+ successful_read = FALSE;
g_warning ( "Line %ld: No options for this kind of layer", line_num );
continue;
}
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]
/* 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;
}
/*
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 ) ) {
- success = 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 )
*/
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;
f = NULL;
switch ( a_vik_get_kml_export_units () ) {
case VIK_KML_EXPORT_UNITS_STATUTE:
- return a_babel_convert_to ( vtl, "-o kml", filename, NULL, NULL );
+ return a_babel_convert_to ( vtl, NULL, "-o kml", filename, NULL, NULL );
break;
case VIK_KML_EXPORT_UNITS_NAUTICAL:
- return a_babel_convert_to ( vtl, "-o kml,units=n", filename, NULL, NULL );
+ return a_babel_convert_to ( vtl, NULL, "-o kml,units=n", filename, NULL, NULL );
break;
default:
// VIK_KML_EXPORT_UNITS_METRIC:
- return a_babel_convert_to ( vtl, "-o kml,units=m", filename, NULL, NULL );
+ return a_babel_convert_to ( vtl, NULL, "-o kml,units=m", filename, NULL, NULL );
break;
}
break;