Generated files should not appears on CVS
[viking.git] / src / file.c
blob0d72bf6f46847da33c6f0e946b0459e984c6db40
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "viking.h"
24 #include "gpx.h"
26 #include <string.h>
27 #include <stdlib.h>
29 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
30 #define VIK_MAGIC "#VIK"
31 #define GPX_MAGIC "<?xm"
32 #define VIK_MAGIC_LEN 4
34 #ifdef WINDOWS
35 #define FILE_SEP '\\'
36 #else
37 #define FILE_SEP '/'
38 #endif
40 typedef struct _Stack Stack;
42 struct _Stack {
43 Stack *under;
44 gpointer *data;
47 static void pop(Stack **stack) {
48 Stack *tmp = (*stack)->under;
49 g_free ( *stack );
50 *stack = tmp;
53 static void push(Stack **stack)
55 Stack *tmp = g_malloc ( sizeof ( Stack ) );
56 tmp->under = *stack;
57 *stack = tmp;
60 static gboolean check_magic ( FILE *f, const gchar *magic_number )
62 gchar magic[VIK_MAGIC_LEN];
63 gboolean rv = FALSE;
64 gint8 i;
65 if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) &&
66 strncmp(magic, magic_number, sizeof(magic)) == 0 )
67 rv = TRUE;
68 for ( i = sizeof(magic)-1; i >= 0; i-- ) /* the ol' pushback */
69 ungetc(magic[i],f);
70 return rv;
74 static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, guint16 len_needle, gboolean must_be_longer )
76 if ( strlen(haystack) > len_needle - (!must_be_longer) && strncasecmp ( haystack, needle, len_needle ) == 0 )
77 return TRUE;
78 return FALSE;
81 static guint16 layer_type_from_string ( const gchar *str )
83 guint8 i;
84 for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
85 if ( strcasecmp ( str, vik_layer_get_interface(i)->name ) == 0 )
86 return i;
87 return -1;
90 static void write_layer_params_and_data ( VikLayer *l, FILE *f )
92 VikLayerParam *params = vik_layer_get_interface(l->type)->params;
93 VikLayerFuncGetParam get_param = vik_layer_get_interface(l->type)->get_param;
95 fprintf ( f, "name=%s\n", l->name ? l->name : "" );
96 if ( !l->visible )
97 fprintf ( f, "visible=f\n" );
99 if ( params && get_param )
101 VikLayerParamData data;
102 guint16 i, params_count = vik_layer_get_interface(l->type)->params_count;
103 for ( i = 0; i < params_count; i++ )
105 data = get_param(l,i);
106 fprintf ( f, "%s=", params[i].name );
107 switch ( params[i].type )
109 case VIK_LAYER_PARAM_DOUBLE: {
110 // char buf[15]; /* locale independent */
111 // fprintf ( f, "%s\n", (char *) g_dtostr (data.d, buf, sizeof (buf)) ); break;
112 fprintf ( f, "%f\n", data.d );
113 break;
115 case VIK_LAYER_PARAM_UINT: fprintf ( f, "%d\n", data.u ); break;
116 case VIK_LAYER_PARAM_INT: fprintf ( f, "%d\n", data.i ); break;
117 case VIK_LAYER_PARAM_BOOLEAN: fprintf ( f, "%c\n", data.b ? 't' : 'f' ); break;
118 case VIK_LAYER_PARAM_STRING: fprintf ( f, "%s\n", data.s ); break;
119 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;
123 if ( vik_layer_get_interface(l->type)->write_file_data )
125 fprintf ( f, "\n\n~LayerData\n" );
126 vik_layer_get_interface(l->type)->write_file_data ( l, f );
127 fprintf ( f, "~EndLayerData\n" );
129 /* foreach param:
130 write param, and get_value, etc.
131 then run layer data, and that's it.
135 static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp )
137 Stack *stack = NULL;
138 VikLayer *current_layer;
139 struct LatLon ll;
140 gchar *modestring;
142 push(&stack);
143 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top));
144 stack->under = NULL;
146 /* crazhy CRAZHY */
147 vik_coord_to_latlon ( vik_viewport_get_center ( VIK_VIEWPORT(vp) ), &ll );
149 switch ( vik_viewport_get_drawmode ( VIK_VIEWPORT(vp) ) ) {
150 case VIK_VIEWPORT_DRAWMODE_UTM: modestring = "utm"; break;
151 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: modestring = "expedia"; break;
152 case VIK_VIEWPORT_DRAWMODE_GOOGLE: modestring = "google"; break;
153 case VIK_VIEWPORT_DRAWMODE_KH: modestring = "kh"; break;
154 default: modestring = "mercator";
157 fprintf ( f, "#VIKING GPS Data file http://gpsmaps.org/viking/\n\nxmpp=%f\nympp=%f\nlat=%f\nlon=%f\nmode=%s\ncolor=%s\n",
158 vik_viewport_get_xmpp ( VIK_VIEWPORT(vp) ), vik_viewport_get_ympp ( VIK_VIEWPORT(vp) ), ll.lat, ll.lon,
159 modestring, vik_viewport_get_background_color(VIK_VIEWPORT(vp)) );
161 if ( ! VIK_LAYER(top)->visible )
162 fprintf ( f, "visible=f\n" );
164 while (stack && stack->data)
166 current_layer = VIK_LAYER(((GList *)stack->data)->data);
167 fprintf ( f, "\n~Layer %s\n", vik_layer_get_interface(current_layer->type)->name );
168 write_layer_params_and_data ( current_layer, f );
169 if ( current_layer->type == VIK_LAYER_AGGREGATE && !vik_aggregate_layer_is_empty(VIK_AGGREGATE_LAYER(current_layer)) )
171 push(&stack);
172 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(current_layer));
174 else
176 stack->data = (gpointer) ((GList *)stack->data)->next;
177 fprintf ( f, "~EndLayer\n\n" );
178 while ( stack && (!stack->data) )
180 pop(&stack);
181 if ( stack )
183 stack->data = (gpointer) ((GList *)stack->data)->next;
184 fprintf ( f, "~EndLayer\n\n" );
190 get vikaggregatelayer's children (?)
191 foreach write ALL params,
192 then layer data (IF function exists)
193 then endlayer
195 impl:
196 stack of layers (LIST) we are working on
197 when layer->next == NULL ...
198 we move on.
202 static void file_read ( VikAggregateLayer *top, FILE *f, gpointer vp )
204 Stack *stack;
205 struct LatLon ll = { 0.0, 0.0 };
206 gchar buffer[4096];
207 gchar *line;
208 guint16 len;
209 long line_num = 0;
211 VikLayerParam *params; /* for current layer, so we don't have to keep on looking up interface */
212 guint8 params_count;
214 push(&stack);
215 stack->under = NULL;
216 stack->data = (gpointer) top;
218 while ( fgets ( buffer, 4096, f ) )
220 line_num++;
222 line = buffer;
223 while ( *line == ' ' || *line =='\t' )
224 line++;
226 if ( line[0] == '#' )
227 continue;
230 len = strlen(line);
231 if ( len > 0 && line[len-1] == '\n' )
232 line[--len] = '\0';
233 if ( len > 0 && line[len-1] == '\r' )
234 line[--len] = '\0';
236 if ( len == 0 )
237 continue;
240 if ( line[0] == '~' )
242 line++; len--;
243 if ( *line == '\0' )
244 continue;
245 else if ( str_starts_with ( line, "Layer ", 6, TRUE ) )
247 if ( ( ! stack->data ) || VIK_LAYER(stack->data)->type != VIK_LAYER_AGGREGATE )
249 g_warning ( "Line %ld: Layer command inside non-Aggregate Layer", line_num );
250 push(&stack); /* inside INVALID layer */
251 stack->data = NULL;
252 continue;
254 else
256 gint16 type = layer_type_from_string ( line+6 );
257 push(&stack);
258 if ( type == -1 )
260 g_warning ( "Line %ld: Unknown type %s\n", line_num, line+6 );
261 stack->data = NULL;
263 else
265 stack->data = (gpointer) vik_layer_create ( type, vp, NULL, FALSE );
266 params = vik_layer_get_interface(type)->params;
267 params_count = vik_layer_get_interface(type)->params_count;
271 else if ( str_starts_with ( line, "EndLayer", 8, FALSE ) )
273 if ( stack->under == NULL )
274 g_warning ( "Line %ld: Mismatched ~EndLayer command", line_num );
275 else
277 if ( stack->data && stack->under->data )
279 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
280 vik_layer_post_read ( VIK_LAYER(stack->data), vp );
282 pop(&stack);
285 else if ( str_starts_with ( line, "LayerData", 9, FALSE ) )
287 if ( stack->data && vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data )
288 vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f );
289 /* must read until hits ~EndLayerData */
291 else
292 { /* simply skip layer data over */
293 while ( fgets ( buffer, 4096, f ) )
295 line_num++;
297 line = buffer;
299 len = strlen(line);
300 if ( len > 0 && line[len-1] == '\n' )
301 line[--len] = '\0';
302 if ( len > 0 && line[len-1] == '\r' )
303 line[--len] = '\0';
304 if ( strcasecmp ( line, "~EndLayerData" ) == 0 )
305 break;
307 continue;
310 else
312 g_warning ( "Line %ld: Unknown tilde command", line_num );
315 else
317 gint32 eq_pos = -1;
318 guint16 i;
319 if ( ! stack->data )
320 continue;
322 for ( i = 0; i < len; i++ )
323 if ( line[i] == '=' )
324 eq_pos = i;
326 if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "xmpp", eq_pos ) == 0) /* "hard coded" params: global & for all layer-types */
327 vik_viewport_set_xmpp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
328 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "ympp", eq_pos ) == 0)
329 vik_viewport_set_ympp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
330 else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lat", eq_pos ) == 0 )
331 ll.lat = strtod ( line+4, NULL );
332 else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lon", eq_pos ) == 0 )
333 ll.lon = strtod ( line+4, NULL );
334 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "utm" ) == 0)
335 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_UTM);
336 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "expedia" ) == 0)
337 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_EXPEDIA );
338 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "google" ) == 0)
339 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_GOOGLE );
340 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "kh" ) == 0)
341 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_KH );
342 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "mercator" ) == 0)
343 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_MERCATOR );
344 else if ( stack->under == NULL && eq_pos == 5 && strncasecmp ( line, "color", eq_pos ) == 0 )
345 vik_viewport_set_background_color ( VIK_VIEWPORT(vp), line+6 );
346 else if ( stack->under && eq_pos == 4 && strncasecmp ( line, "name", eq_pos ) == 0 )
347 vik_layer_rename ( VIK_LAYER(stack->data), line+5 );
348 else if ( eq_pos == 7 && strncasecmp ( line, "visible", eq_pos ) == 0 )
349 VIK_LAYER(stack->data)->visible = TEST_BOOLEAN(line+8);
350 else if ( eq_pos != -1 && stack->under )
352 gboolean found_match = FALSE;
353 /* go thru layer params. if len == eq_pos && starts_with jazz, set it. */
354 /* also got to check for name and visible. */
356 if ( ! params )
358 g_warning ( "Line %ld: No options for this kind of layer", line_num );
359 continue;
362 for ( i = 0; i < params_count; i++ )
363 if ( strlen(params[i].name) == eq_pos && strncasecmp ( line, params[i].name, eq_pos ) == 0 )
365 VikLayerParamData x;
366 line += eq_pos+1;
367 switch ( params[i].type )
369 case VIK_LAYER_PARAM_DOUBLE: x.d = strtod(line, NULL); break;
370 case VIK_LAYER_PARAM_UINT: x.u = strtoul(line, NULL, 10); break;
371 case VIK_LAYER_PARAM_INT: x.i = strtol(line, NULL, 10); break;
372 case VIK_LAYER_PARAM_BOOLEAN: x.b = TEST_BOOLEAN(line); break;
373 case VIK_LAYER_PARAM_COLOR: memset(&(x.c), 0, sizeof(x.c)); /* default: black */
374 gdk_color_parse ( line, &(x.c) ); break;
375 default: x.s = line;
377 vik_layer_set_param ( VIK_LAYER(stack->data), i, x, vp );
378 found_match = TRUE;
379 break;
381 if ( ! found_match )
382 g_warning ( "Line %ld: Unknown parameter", line_num );
384 else
385 g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num );
387 /* could be:
388 [Layer Type=Bla]
389 [EndLayer]
390 [LayerData]
391 name=this
392 #comment
396 while ( stack )
398 if ( stack->under && stack->under->data && stack->data )
400 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
401 vik_layer_post_read ( VIK_LAYER(stack->data), vp );
403 pop(&stack);
406 if ( ll.lat != 0.0 || ll.lon != 0.0 )
407 vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll );
409 if ( ( ! VIK_LAYER(top)->visible ) && VIK_LAYER(top)->realized )
410 vik_treeview_item_set_visible ( VIK_LAYER(top)->vt, &(VIK_LAYER(top)->iter), FALSE );
414 read thru file
415 if "[Layer Type="
416 push(&stack)
417 new default layer of type (str_to_type) (check interface->name)
418 if "[EndLayer]"
419 VikLayer *vl = stack->data;
420 pop(&stack);
421 vik_aggregate_layer_add_layer(stack->data, vl);
422 if "[LayerData]"
423 vik_layer_data ( VIK_LAYER_DATA(stack->data), f, vp );
427 /* ---------------------------------------------------- */
429 static FILE *xfopen ( const char *fn, const char *mode )
431 if ( strcmp(fn,"-") == 0 )
432 return stdin;
433 else
434 return fopen(fn, "r");
437 static void xfclose ( FILE *f )
439 if ( f != stdin && f != stdout )
440 fclose ( f );
443 /* 0 on failure, 1 on success (vik file) 2 on success (other file) */
444 gshort a_file_load ( VikAggregateLayer *top, gpointer vp, const gchar *filename )
446 FILE *f = xfopen ( filename, "r" );
448 g_assert ( vp );
450 if ( ! f )
451 return 0;
453 if ( check_magic ( f, VIK_MAGIC ) )
455 file_read ( top, f, vp );
456 if ( f != stdin )
457 xfclose(f);
458 return 1;
460 else
462 VikCoord new_center;
463 VikLayer *vtl = vik_layer_create ( VIK_LAYER_TRW, vp, NULL, FALSE );
464 vik_layer_rename ( vtl, a_file_basename ( filename ) );
466 if ( check_magic ( f, GPX_MAGIC ) )
467 a_gpx_read_file ( VIK_TRW_LAYER(vtl), f );
468 else
469 a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f );
471 vik_layer_post_read ( vtl, vp );
473 vik_aggregate_layer_add_layer ( top, vtl );
475 if ( vik_trw_layer_find_center ( VIK_TRW_LAYER(vtl), &new_center ) )
476 vik_viewport_set_center_coord ( VIK_VIEWPORT(vp), &new_center );
477 xfclose(f);
478 return 2;
482 gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filename )
484 FILE *f = fopen(filename, "w");
486 if ( ! f )
487 return FALSE;
489 file_write ( top, f, vp );
491 fclose(f);
493 return TRUE;
497 const gchar *a_file_basename ( const gchar *filename )
499 const gchar *t = filename + strlen(filename) - 1;
500 while ( --t > filename )
501 if ( *(t-1) == FILE_SEP )
502 break;
503 if ( t >= filename )
504 return t;
505 return filename;
508 gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, gshort file_type )
510 FILE *f = fopen ( filename, "w" );
511 if ( f )
513 if ( file_type == FILE_TYPE_GPSMAPPER )
514 a_gpsmapper_write_file ( vtl, f );
515 else if ( file_type == FILE_TYPE_GPX )
516 a_gpx_write_file ( vtl, f );
517 else
518 a_gpspoint_write_file ( vtl, f );
519 fclose ( f );
520 return TRUE;
522 return FALSE;