]> git.street.me.uk Git - andy/viking.git/blame - src/file.c
[QA] Tidy up vikgoto header usage.
[andy/viking.git] / src / file.c
CommitLineData
50a14534
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
29f1598c 5 * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
50a14534
EB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
eb93fa95
GB
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
50a14534
EB
26#include "viking.h"
27
bf35388d 28#include "gpx.h"
ba9d0a00 29#include "babel.h"
bf35388d 30
50a14534
EB
31#include <string.h>
32#include <stdlib.h>
8c060406 33#include <stdio.h>
45acf79e
MA
34#ifdef HAVE_UNISTD_H
35#include <unistd.h>
36#endif
d5728c65
GB
37#include <glib.h>
38#include <glib/gstdio.h>
fd98b156 39#include <glib/gi18n.h>
50a14534
EB
40
41#define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
42#define VIK_MAGIC "#VIK"
bf35388d 43#define GPX_MAGIC "<?xm"
50a14534
EB
44#define VIK_MAGIC_LEN 4
45
46#ifdef WINDOWS
47#define FILE_SEP '\\'
48#else
49#define FILE_SEP '/'
50#endif
51
52typedef struct _Stack Stack;
53
54struct _Stack {
55 Stack *under;
56 gpointer *data;
57};
58
59static void pop(Stack **stack) {
60 Stack *tmp = (*stack)->under;
61 g_free ( *stack );
62 *stack = tmp;
63}
64
65static void push(Stack **stack)
66{
67 Stack *tmp = g_malloc ( sizeof ( Stack ) );
68 tmp->under = *stack;
69 *stack = tmp;
70}
71
bf35388d 72static gboolean check_magic ( FILE *f, const gchar *magic_number )
50a14534
EB
73{
74 gchar magic[VIK_MAGIC_LEN];
75 gboolean rv = FALSE;
76 gint8 i;
77 if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) &&
bf35388d 78 strncmp(magic, magic_number, sizeof(magic)) == 0 )
50a14534
EB
79 rv = TRUE;
80 for ( i = sizeof(magic)-1; i >= 0; i-- ) /* the ol' pushback */
81 ungetc(magic[i],f);
82 return rv;
83}
84
85
86static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, guint16 len_needle, gboolean must_be_longer )
87{
88 if ( strlen(haystack) > len_needle - (!must_be_longer) && strncasecmp ( haystack, needle, len_needle ) == 0 )
89 return TRUE;
90 return FALSE;
91}
92
93static guint16 layer_type_from_string ( const gchar *str )
94{
95 guint8 i;
96 for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
db386630 97 if ( strcasecmp ( str, vik_layer_get_interface(i)->fixed_layer_name ) == 0 )
50a14534
EB
98 return i;
99 return -1;
100}
101
17a1f8f9 102void file_write_layer_param ( FILE *f, const gchar *name, guint8 type, VikLayerParamData data ) {
ad0a8c2d
EB
103 /* string lists are handled differently. We get a GList (that shouldn't
104 * be freed) back for get_param and if it is null we shouldn't write
105 * anything at all (otherwise we'd read in a list with an empty string,
17a1f8f9 106 * not an empty string list.
ad0a8c2d 107 */
17a1f8f9 108 if ( type == VIK_LAYER_PARAM_STRING_LIST ) {
ad0a8c2d
EB
109 if ( data.sl ) {
110 GList *iter = (GList *)data.sl;
111 while ( iter ) {
17a1f8f9 112 fprintf ( f, "%s=", name );
ad0a8c2d
EB
113 fprintf ( f, "%s\n", (gchar *)(iter->data) );
114 iter = iter->next;
115 }
116 }
117 } else {
17a1f8f9
EB
118 fprintf ( f, "%s=", name );
119 switch ( type )
ad0a8c2d
EB
120 {
121 case VIK_LAYER_PARAM_DOUBLE: {
122 // char buf[15]; /* locale independent */
123 // fprintf ( f, "%s\n", (char *) g_dtostr (data.d, buf, sizeof (buf)) ); break;
124 fprintf ( f, "%f\n", data.d );
125 break;
126 }
127 case VIK_LAYER_PARAM_UINT: fprintf ( f, "%d\n", data.u ); break;
128 case VIK_LAYER_PARAM_INT: fprintf ( f, "%d\n", data.i ); break;
129 case VIK_LAYER_PARAM_BOOLEAN: fprintf ( f, "%c\n", data.b ? 't' : 'f' ); break;
130 case VIK_LAYER_PARAM_STRING: fprintf ( f, "%s\n", data.s ); break;
131 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;
132 }
50a14534 133 }
17a1f8f9
EB
134}
135
136static void write_layer_params_and_data ( VikLayer *l, FILE *f )
137{
138 VikLayerParam *params = vik_layer_get_interface(l->type)->params;
139 VikLayerFuncGetParam get_param = vik_layer_get_interface(l->type)->get_param;
140
141 fprintf ( f, "name=%s\n", l->name ? l->name : "" );
142 if ( !l->visible )
143 fprintf ( f, "visible=f\n" );
144
145 if ( params && get_param )
146 {
147 VikLayerParamData data;
148 guint16 i, params_count = vik_layer_get_interface(l->type)->params_count;
149 for ( i = 0; i < params_count; i++ )
150 {
158b3642 151 data = get_param(l, i, TRUE);
17a1f8f9 152 file_write_layer_param(f, params[i].name, params[i].type, data);
50a14534
EB
153 }
154 }
155 if ( vik_layer_get_interface(l->type)->write_file_data )
156 {
157 fprintf ( f, "\n\n~LayerData\n" );
158 vik_layer_get_interface(l->type)->write_file_data ( l, f );
159 fprintf ( f, "~EndLayerData\n" );
160 }
161 /* foreach param:
162 write param, and get_value, etc.
163 then run layer data, and that's it.
164 */
165}
166
167static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp )
168{
169 Stack *stack = NULL;
170 VikLayer *current_layer;
171 struct LatLon ll;
c5f9245d 172 VikViewportDrawMode mode;
4d872dde 173 gchar *modestring = NULL;
50a14534
EB
174
175 push(&stack);
176 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top));
177 stack->under = NULL;
178
179 /* crazhy CRAZHY */
180 vik_coord_to_latlon ( vik_viewport_get_center ( VIK_VIEWPORT(vp) ), &ll );
181
c5f9245d
GB
182 mode = vik_viewport_get_drawmode ( VIK_VIEWPORT(vp) );
183 switch ( mode ) {
50a14534
EB
184 case VIK_VIEWPORT_DRAWMODE_UTM: modestring = "utm"; break;
185 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: modestring = "expedia"; break;
c5f9245d 186 case VIK_VIEWPORT_DRAWMODE_MERCATOR: modestring = "mercator"; break;
d587678a 187 case VIK_VIEWPORT_DRAWMODE_LATLON: modestring = "latlon"; break;
c5f9245d
GB
188 default:
189 g_critical("Houston, we've had a problem. mode=%d", mode);
50a14534
EB
190 }
191
480fb7e1 192 fprintf ( f, "#VIKING GPS Data file " VIKING_URL "\n\nxmpp=%f\nympp=%f\nlat=%f\nlon=%f\nmode=%s\ncolor=%s\nhighlightcolor=%s\ndrawscale=%s\ndrawcentermark=%s\ndrawhighlight=%s\n",
50a14534 193 vik_viewport_get_xmpp ( VIK_VIEWPORT(vp) ), vik_viewport_get_ympp ( VIK_VIEWPORT(vp) ), ll.lat, ll.lon,
35c7c0ba 194 modestring, vik_viewport_get_background_color(VIK_VIEWPORT(vp)),
480fb7e1 195 vik_viewport_get_highlight_color(VIK_VIEWPORT(vp)),
c933487f 196 vik_viewport_get_draw_scale(VIK_VIEWPORT(vp)) ? "t" : "f",
2afcef36
RN
197 vik_viewport_get_draw_centermark(VIK_VIEWPORT(vp)) ? "t" : "f",
198 vik_viewport_get_draw_highlight(VIK_VIEWPORT(vp)) ? "t" : "f" );
50a14534
EB
199
200 if ( ! VIK_LAYER(top)->visible )
201 fprintf ( f, "visible=f\n" );
202
203 while (stack && stack->data)
204 {
205 current_layer = VIK_LAYER(((GList *)stack->data)->data);
db386630 206 fprintf ( f, "\n~Layer %s\n", vik_layer_get_interface(current_layer->type)->fixed_layer_name );
50a14534
EB
207 write_layer_params_and_data ( current_layer, f );
208 if ( current_layer->type == VIK_LAYER_AGGREGATE && !vik_aggregate_layer_is_empty(VIK_AGGREGATE_LAYER(current_layer)) )
209 {
210 push(&stack);
211 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(current_layer));
212 }
f253a6a6 213 else if ( current_layer->type == VIK_LAYER_GPS && !vik_gps_layer_is_empty(VIK_GPS_LAYER(current_layer)) )
7886de28
QT
214 {
215 push(&stack);
216 stack->data = (gpointer) vik_gps_layer_get_children(VIK_GPS_LAYER(current_layer));
217 }
50a14534
EB
218 else
219 {
220 stack->data = (gpointer) ((GList *)stack->data)->next;
221 fprintf ( f, "~EndLayer\n\n" );
222 while ( stack && (!stack->data) )
223 {
224 pop(&stack);
225 if ( stack )
226 {
227 stack->data = (gpointer) ((GList *)stack->data)->next;
228 fprintf ( f, "~EndLayer\n\n" );
229 }
230 }
231 }
232 }
233/*
234 get vikaggregatelayer's children (?)
235 foreach write ALL params,
236 then layer data (IF function exists)
237 then endlayer
238
239 impl:
240 stack of layers (LIST) we are working on
241 when layer->next == NULL ...
242 we move on.
243*/
244}
245
ad0a8c2d
EB
246static void string_list_delete ( gpointer key, gpointer l, gpointer user_data )
247{
28c82d8b
EB
248 /* 20071021 bugfix */
249 GList *iter = (GList *) l;
ad0a8c2d
EB
250 while ( iter ) {
251 g_free ( iter->data );
252 iter = iter->next;
253 }
28c82d8b 254 g_list_free ( (GList *) l );
ad0a8c2d
EB
255}
256
257static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp)
258{
259 VikLayerParamData x;
260 x.sl = list;
158b3642 261 vik_layer_set_param ( VIK_LAYER(layer_and_vp[0]), i, x, layer_and_vp[1], TRUE );
ad0a8c2d
EB
262}
263
327a2533
RN
264/**
265 * Read in a Viking file and return how successful the parsing was
266 * ATM this will always work, in that even if there are parsing problems
267 * then there will be no new values to override the defaults
268 *
269 * TODO flow up line number(s) / error messages of problems encountered...
270 *
271 */
272static gboolean file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
50a14534
EB
273{
274 Stack *stack;
275 struct LatLon ll = { 0.0, 0.0 };
276 gchar buffer[4096];
277 gchar *line;
278 guint16 len;
279 long line_num = 0;
280
37518034
QT
281 VikLayerParam *params = NULL; /* for current layer, so we don't have to keep on looking up interface */
282 guint8 params_count = 0;
50a14534 283
ad0a8c2d
EB
284 GHashTable *string_lists = g_hash_table_new(g_direct_hash,g_direct_equal);
285
327a2533
RN
286 gboolean successful_read = TRUE;
287
50a14534
EB
288 push(&stack);
289 stack->under = NULL;
290 stack->data = (gpointer) top;
291
292 while ( fgets ( buffer, 4096, f ) )
293 {
294 line_num++;
295
296 line = buffer;
297 while ( *line == ' ' || *line =='\t' )
298 line++;
299
300 if ( line[0] == '#' )
301 continue;
302
303
304 len = strlen(line);
305 if ( len > 0 && line[len-1] == '\n' )
306 line[--len] = '\0';
307 if ( len > 0 && line[len-1] == '\r' )
308 line[--len] = '\0';
309
310 if ( len == 0 )
311 continue;
312
313
314 if ( line[0] == '~' )
315 {
316 line++; len--;
317 if ( *line == '\0' )
318 continue;
319 else if ( str_starts_with ( line, "Layer ", 6, TRUE ) )
320 {
f253a6a6
QT
321 int parent_type = VIK_LAYER(stack->data)->type;
322 if ( ( ! stack->data ) || ((parent_type != VIK_LAYER_AGGREGATE) && (parent_type != VIK_LAYER_GPS)) )
50a14534 323 {
327a2533 324 successful_read = FALSE;
f253a6a6 325 g_warning ( "Line %ld: Layer command inside non-Aggregate Layer (type %d)", line_num, parent_type );
50a14534
EB
326 push(&stack); /* inside INVALID layer */
327 stack->data = NULL;
328 continue;
329 }
330 else
331 {
332 gint16 type = layer_type_from_string ( line+6 );
333 push(&stack);
334 if ( type == -1 )
335 {
327a2533 336 successful_read = FALSE;
4258f4e2 337 g_warning ( "Line %ld: Unknown type %s", line_num, line+6 );
50a14534
EB
338 stack->data = NULL;
339 }
f253a6a6
QT
340 else if (parent_type == VIK_LAYER_GPS)
341 {
342 stack->data = (gpointer) vik_gps_layer_get_a_child(VIK_GPS_LAYER(stack->under->data));
343 params = vik_layer_get_interface(type)->params;
344 params_count = vik_layer_get_interface(type)->params_count;
345 }
50a14534
EB
346 else
347 {
348 stack->data = (gpointer) vik_layer_create ( type, vp, NULL, FALSE );
349 params = vik_layer_get_interface(type)->params;
350 params_count = vik_layer_get_interface(type)->params_count;
351 }
352 }
353 }
354 else if ( str_starts_with ( line, "EndLayer", 8, FALSE ) )
355 {
327a2533
RN
356 if ( stack->under == NULL ) {
357 successful_read = FALSE;
50a14534 358 g_warning ( "Line %ld: Mismatched ~EndLayer command", line_num );
327a2533 359 }
50a14534
EB
360 else
361 {
ad0a8c2d
EB
362 /* add any string lists we've accumulated */
363 gpointer layer_and_vp[2];
364 layer_and_vp[0] = stack->data;
365 layer_and_vp[1] = vp;
366 g_hash_table_foreach ( string_lists, (GHFunc) string_list_set_param, layer_and_vp );
367 g_hash_table_remove_all ( string_lists );
368
50a14534
EB
369 if ( stack->data && stack->under->data )
370 {
f253a6a6
QT
371 if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_AGGREGATE) {
372 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
dcb51e52 373 vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE );
f253a6a6
QT
374 }
375 else if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_GPS) {
376 /* TODO: anything else needs to be done here ? */
377 }
327a2533
RN
378 else {
379 successful_read = FALSE;
f253a6a6 380 g_warning ( "Line %ld: EndLayer command inside non-Aggregate Layer (type %d)", line_num, VIK_LAYER(stack->data)->type );
327a2533 381 }
50a14534
EB
382 }
383 pop(&stack);
384 }
385 }
386 else if ( str_starts_with ( line, "LayerData", 9, FALSE ) )
387 {
388 if ( stack->data && vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data )
119ada4c 389 {
50a14534 390 /* must read until hits ~EndLayerData */
119ada4c
RN
391 if ( ! vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f ) )
392 successful_read = FALSE;
393 }
50a14534
EB
394 else
395 { /* simply skip layer data over */
396 while ( fgets ( buffer, 4096, f ) )
397 {
398 line_num++;
399
400 line = buffer;
401
402 len = strlen(line);
403 if ( len > 0 && line[len-1] == '\n' )
404 line[--len] = '\0';
405 if ( len > 0 && line[len-1] == '\r' )
406 line[--len] = '\0';
407 if ( strcasecmp ( line, "~EndLayerData" ) == 0 )
408 break;
409 }
410 continue;
411 }
412 }
413 else
414 {
327a2533 415 successful_read = FALSE;
50a14534
EB
416 g_warning ( "Line %ld: Unknown tilde command", line_num );
417 }
418 }
419 else
420 {
421 gint32 eq_pos = -1;
422 guint16 i;
423 if ( ! stack->data )
424 continue;
425
426 for ( i = 0; i < len; i++ )
427 if ( line[i] == '=' )
428 eq_pos = i;
429
430 if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "xmpp", eq_pos ) == 0) /* "hard coded" params: global & for all layer-types */
431 vik_viewport_set_xmpp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
432 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "ympp", eq_pos ) == 0)
433 vik_viewport_set_ympp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
434 else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lat", eq_pos ) == 0 )
435 ll.lat = strtod ( line+4, NULL );
436 else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lon", eq_pos ) == 0 )
437 ll.lon = strtod ( line+4, NULL );
438 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "utm" ) == 0)
439 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_UTM);
440 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "expedia" ) == 0)
441 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_EXPEDIA );
442 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "google" ) == 0)
fd98b156 443 {
327a2533 444 successful_read = FALSE;
fd98b156
GB
445 g_warning ( _("Draw mode '%s' no more supported"), "google" );
446 }
50a14534 447 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "kh" ) == 0)
fd98b156 448 {
327a2533 449 successful_read = FALSE;
fd98b156
GB
450 g_warning ( _("Draw mode '%s' no more supported"), "kh" );
451 }
50a14534
EB
452 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "mercator" ) == 0)
453 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_MERCATOR );
d587678a
GB
454 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "latlon" ) == 0)
455 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_LATLON );
50a14534
EB
456 else if ( stack->under == NULL && eq_pos == 5 && strncasecmp ( line, "color", eq_pos ) == 0 )
457 vik_viewport_set_background_color ( VIK_VIEWPORT(vp), line+6 );
480fb7e1
RN
458 else if ( stack->under == NULL && eq_pos == 14 && strncasecmp ( line, "highlightcolor", eq_pos ) == 0 )
459 vik_viewport_set_highlight_color ( VIK_VIEWPORT(vp), line+15 );
35c7c0ba
EB
460 else if ( stack->under == NULL && eq_pos == 9 && strncasecmp ( line, "drawscale", eq_pos ) == 0 )
461 vik_viewport_set_draw_scale ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+10) );
de66b263 462 else if ( stack->under == NULL && eq_pos == 14 && strncasecmp ( line, "drawcentermark", eq_pos ) == 0 )
1657065a 463 vik_viewport_set_draw_centermark ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+15) );
2afcef36
RN
464 else if ( stack->under == NULL && eq_pos == 13 && strncasecmp ( line, "drawhighlight", eq_pos ) == 0 )
465 vik_viewport_set_draw_highlight ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+14) );
50a14534
EB
466 else if ( stack->under && eq_pos == 4 && strncasecmp ( line, "name", eq_pos ) == 0 )
467 vik_layer_rename ( VIK_LAYER(stack->data), line+5 );
468 else if ( eq_pos == 7 && strncasecmp ( line, "visible", eq_pos ) == 0 )
469 VIK_LAYER(stack->data)->visible = TEST_BOOLEAN(line+8);
470 else if ( eq_pos != -1 && stack->under )
471 {
472 gboolean found_match = FALSE;
ad0a8c2d 473
50a14534
EB
474 /* go thru layer params. if len == eq_pos && starts_with jazz, set it. */
475 /* also got to check for name and visible. */
476
477 if ( ! params )
478 {
327a2533 479 successful_read = FALSE;
50a14534
EB
480 g_warning ( "Line %ld: No options for this kind of layer", line_num );
481 continue;
482 }
483
484 for ( i = 0; i < params_count; i++ )
485 if ( strlen(params[i].name) == eq_pos && strncasecmp ( line, params[i].name, eq_pos ) == 0 )
486 {
487 VikLayerParamData x;
488 line += eq_pos+1;
ad0a8c2d 489 if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST ) {
dc2c040e
RN
490 GList *l = g_list_append ( g_hash_table_lookup ( string_lists, GINT_TO_POINTER ((gint) i) ),
491 g_strdup(line) );
492 g_hash_table_replace ( string_lists, GINT_TO_POINTER ((gint)i), l );
ad0a8c2d
EB
493 /* add the value to a list, possibly making a new list.
494 * this will be passed to the layer when we read an ~EndLayer */
495 } else {
496 switch ( params[i].type )
497 {
498 case VIK_LAYER_PARAM_DOUBLE: x.d = strtod(line, NULL); break;
499 case VIK_LAYER_PARAM_UINT: x.u = strtoul(line, NULL, 10); break;
500 case VIK_LAYER_PARAM_INT: x.i = strtol(line, NULL, 10); break;
501 case VIK_LAYER_PARAM_BOOLEAN: x.b = TEST_BOOLEAN(line); break;
502 case VIK_LAYER_PARAM_COLOR: memset(&(x.c), 0, sizeof(x.c)); /* default: black */
50a14534 503 gdk_color_parse ( line, &(x.c) ); break;
ad0a8c2d
EB
504 /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */
505 default: x.s = line;
506 }
158b3642 507 vik_layer_set_param ( VIK_LAYER(stack->data), i, x, vp, TRUE );
50a14534 508 }
50a14534
EB
509 found_match = TRUE;
510 break;
511 }
327a2533
RN
512 if ( ! found_match ) {
513 // ATM don't flow up this issue because at least one internal parameter has changed from version 1.3
514 // and don't what to worry users about raising such issues
515 // TODO Maybe hold old values here - compare the line value against them and if a match
516 // generate a different style of message in the GUI...
517 // successful_read = FALSE;
ea3933fc 518 g_warning ( "Line %ld: Unknown parameter. Line:\n%s", line_num, line );
327a2533 519 }
50a14534 520 }
327a2533
RN
521 else {
522 successful_read = FALSE;
50a14534 523 g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num );
327a2533 524 }
50a14534
EB
525 }
526/* could be:
527[Layer Type=Bla]
528[EndLayer]
529[LayerData]
530name=this
531#comment
532*/
533 }
534
535 while ( stack )
536 {
537 if ( stack->under && stack->under->data && stack->data )
538 {
539 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
dcb51e52 540 vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE );
50a14534
EB
541 }
542 pop(&stack);
543 }
544
545 if ( ll.lat != 0.0 || ll.lon != 0.0 )
546 vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll );
547
548 if ( ( ! VIK_LAYER(top)->visible ) && VIK_LAYER(top)->realized )
549 vik_treeview_item_set_visible ( VIK_LAYER(top)->vt, &(VIK_LAYER(top)->iter), FALSE );
ad0a8c2d
EB
550
551 /* delete anything we've forgotten about -- should only happen when file ends before an EndLayer */
552 g_hash_table_foreach ( string_lists, string_list_delete, NULL );
553 g_hash_table_destroy ( string_lists );
327a2533
RN
554
555 return successful_read;
50a14534
EB
556}
557
558/*
559read thru file
560if "[Layer Type="
561 push(&stack)
562 new default layer of type (str_to_type) (check interface->name)
563if "[EndLayer]"
564 VikLayer *vl = stack->data;
565 pop(&stack);
566 vik_aggregate_layer_add_layer(stack->data, vl);
567if "[LayerData]"
568 vik_layer_data ( VIK_LAYER_DATA(stack->data), f, vp );
569
570*/
571
572/* ---------------------------------------------------- */
573
574static FILE *xfopen ( const char *fn, const char *mode )
575{
576 if ( strcmp(fn,"-") == 0 )
577 return stdin;
578 else
8c060406 579 return g_fopen(fn, "r");
50a14534
EB
580}
581
582static void xfclose ( FILE *f )
583{
8c060406 584 if ( f != stdin && f != stdout ) {
50a14534 585 fclose ( f );
8c060406
MA
586 f = NULL;
587 }
50a14534
EB
588}
589
ba9d0a00
RN
590/*
591 * Function to determine if a filename is a 'viking' type file
592 */
593gboolean check_file_magic_vik ( const gchar *filename )
594{
a3180051 595 gboolean result = FALSE;
ba9d0a00 596 FILE *ff = xfopen ( filename, "r" );
a3180051
RN
597 if ( ff ) {
598 result = check_magic ( ff, VIK_MAGIC );
599 xfclose ( ff );
600 }
ba9d0a00
RN
601 return result;
602}
603
604VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename_or_uri )
50a14534 605{
0a514451
RN
606 g_return_val_if_fail ( vp != NULL, LOAD_TYPE_READ_FAILURE );
607
11f427b8 608 char *filename = (char *)filename_or_uri;
db32b809
MR
609 if (strncmp(filename, "file://", 7) == 0)
610 filename = filename + 7;
611
50a14534
EB
612 FILE *f = xfopen ( filename, "r" );
613
50a14534 614 if ( ! f )
ba9d0a00 615 return LOAD_TYPE_READ_FAILURE;
50a14534 616
327a2533 617 VikLoadType_t load_answer = LOAD_TYPE_OTHER_SUCCESS;
0a514451
RN
618
619 // Attempt loading the primary file type first - our internal .vik file:
620 if ( check_magic ( f, VIK_MAGIC ) )
50a14534 621 {
327a2533
RN
622 if ( file_read ( top, f, vp ) )
623 load_answer = LOAD_TYPE_VIK_SUCCESS;
624 else
625 load_answer = LOAD_TYPE_VIK_FAILURE_NON_FATAL;
50a14534
EB
626 }
627 else
628 {
0a514451
RN
629 // For all other file types which consist of tracks, routes and/or waypoints,
630 // must be loaded into a new TrackWaypoint layer (hence it be created)
631 gboolean success = TRUE; // Detect load failures - mainly to remove the layer created as it's not required
632
50a14534 633 VikLayer *vtl = vik_layer_create ( VIK_LAYER_TRW, vp, NULL, FALSE );
bf35388d 634
ba9d0a00
RN
635 // In fact both kml & gpx files start the same as they are in xml
636 if ( check_file_ext ( filename, ".kml" ) && check_magic ( f, GPX_MAGIC ) ) {
637 // Implicit Conversion
ed691ed1 638 if ( ! ( success = a_babel_convert_from ( VIK_TRW_LAYER(vtl), "-i kml", filename, NULL, NULL, NULL ) ) ) {
0a514451 639 load_answer = LOAD_TYPE_GPSBABEL_FAILURE;
ba9d0a00
RN
640 }
641 }
0a514451
RN
642 // NB use a extension check first, as a GPX file header may have a Byte Order Mark (BOM) in it
643 // - which currently confuses our check_magic function
644 else if ( check_file_ext ( filename, ".gpx" ) || check_magic ( f, GPX_MAGIC ) ) {
645 if ( ! ( success = a_gpx_read_file ( VIK_TRW_LAYER(vtl), f ) ) ) {
646 load_answer = LOAD_TYPE_GPX_FAILURE;
54b84792 647 }
ba9d0a00 648 }
0a514451
RN
649 else {
650 // Try final supported file type
651 if ( ! ( success = a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f ) ) ) {
652 // Failure here means we don't know how to handle the file
653 load_answer = LOAD_TYPE_UNSUPPORTED_FAILURE;
654 }
655 }
50a14534 656
0a514451 657 // Clean up when we can't handle the file
cb5ec7a8
RN
658 if ( ! success ) {
659 // free up layer
660 g_object_unref ( vtl );
cb5ec7a8 661 }
0a514451
RN
662 else {
663 // Complete the setup from the successful load
664 vik_layer_rename ( vtl, a_file_basename ( filename ) );
665 vik_layer_post_read ( vtl, vp, TRUE );
666 vik_aggregate_layer_add_layer ( top, vtl );
667 vik_trw_layer_auto_set_view ( VIK_TRW_LAYER(vtl), vp );
668 }
50a14534 669 }
0a514451
RN
670 xfclose(f);
671 return load_answer;
50a14534
EB
672}
673
674gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filename )
675{
bf1c0ae3
SM
676 FILE *f;
677
678 if (strncmp(filename, "file://", 7) == 0)
679 filename = filename + 7;
680
681 f = g_fopen(filename, "w");
50a14534
EB
682
683 if ( ! f )
684 return FALSE;
685
686 file_write ( top, f, vp );
687
688 fclose(f);
8c060406 689 f = NULL;
50a14534
EB
690
691 return TRUE;
692}
693
694
695const gchar *a_file_basename ( const gchar *filename )
696{
697 const gchar *t = filename + strlen(filename) - 1;
698 while ( --t > filename )
699 if ( *(t-1) == FILE_SEP )
700 break;
701 if ( t >= filename )
702 return t;
703 return filename;
704}
705
b0252b2e
T
706/* example:
707 gboolean is_gpx = check_file_ext ( "a/b/c.gpx", ".gpx" );
708*/
709gboolean check_file_ext ( const gchar *filename, const gchar *fileext )
710{
0a514451
RN
711 g_return_val_if_fail ( filename != NULL, FALSE );
712 g_return_val_if_fail ( fileext && fileext[0]=='.', FALSE );
b0252b2e 713 const gchar *basename = a_file_basename(filename);
b0252b2e
T
714 if (!basename)
715 return FALSE;
716
717 const char * dot = strrchr(basename, '.');
718 if (dot && !strcmp(dot, fileext))
719 return TRUE;
720
721 return FALSE;
722}
723
208d2084
RN
724/**
725 * a_file_export:
726 * @vtl: The TrackWaypoint to export data from
727 * @filename: The name of the file to be written
728 * @file_type: Choose one of the supported file types for the export
729 * @trk: If specified then only export this track rather than the whole layer
730 * @write_hidden: Whether to write invisible items
731 *
732 * A general export command to convert from Viking TRW layer data to an external supported format.
733 * The write_hidden option is provided mainly to be able to transfer selected items when uploading to a GPS
734 */
735gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t file_type, VikTrack *trk, gboolean write_hidden )
50a14534 736{
0d2b891f 737 GpxWritingOptions options = { FALSE, FALSE, write_hidden, FALSE };
8c060406 738 FILE *f = g_fopen ( filename, "w" );
50a14534
EB
739 if ( f )
740 {
ce4bd1cf 741 if ( trk ) {
f7f8a0a6
GB
742 switch ( file_type ) {
743 case FILE_TYPE_GPX:
0d2b891f
RN
744 // trk defined so can set the option
745 options.is_route = trk->is_route;
208d2084 746 a_gpx_write_track_file ( trk, f, &options );
f7f8a0a6
GB
747 break;
748 default:
749 g_critical("Houston, we've had a problem. file_type=%d", file_type);
750 }
751 } else {
752 switch ( file_type ) {
753 case FILE_TYPE_GPSMAPPER:
754 a_gpsmapper_write_file ( vtl, f );
755 break;
756 case FILE_TYPE_GPX:
208d2084 757 a_gpx_write_file ( vtl, f, &options );
f7f8a0a6
GB
758 break;
759 case FILE_TYPE_GPSPOINT:
760 a_gpspoint_write_file ( vtl, f );
761 break;
ba9d0a00
RN
762 case FILE_TYPE_KML:
763 fclose ( f );
764 f = NULL;
60dbd0ad
RN
765 switch ( a_vik_get_kml_export_units () ) {
766 case VIK_KML_EXPORT_UNITS_STATUTE:
6f94db6d 767 return a_babel_convert_to ( vtl, NULL, "-o kml", filename, NULL, NULL );
60dbd0ad
RN
768 break;
769 case VIK_KML_EXPORT_UNITS_NAUTICAL:
6f94db6d 770 return a_babel_convert_to ( vtl, NULL, "-o kml,units=n", filename, NULL, NULL );
60dbd0ad
RN
771 break;
772 default:
773 // VIK_KML_EXPORT_UNITS_METRIC:
6f94db6d 774 return a_babel_convert_to ( vtl, NULL, "-o kml,units=m", filename, NULL, NULL );
60dbd0ad
RN
775 break;
776 }
777 break;
f7f8a0a6
GB
778 default:
779 g_critical("Houston, we've had a problem. file_type=%d", file_type);
780 }
7f6757c4 781 }
50a14534 782 fclose ( f );
8c060406 783 f = NULL;
50a14534
EB
784 return TRUE;
785 }
786 return FALSE;
787}
80d0ff2b 788