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