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
32 #include <glib/gstdio.h>
34 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
35 #define VIK_MAGIC "#VIK"
36 #define GPX_MAGIC "<?xm"
37 #define VIK_MAGIC_LEN 4
45 typedef struct _Stack Stack
;
52 static void pop(Stack
**stack
) {
53 Stack
*tmp
= (*stack
)->under
;
58 static void push(Stack
**stack
)
60 Stack
*tmp
= g_malloc ( sizeof ( Stack
) );
65 static gboolean
check_magic ( FILE *f
, const gchar
*magic_number
)
67 gchar magic
[VIK_MAGIC_LEN
];
70 if ( fread(magic
, 1, sizeof(magic
), f
) == sizeof(magic
) &&
71 strncmp(magic
, magic_number
, sizeof(magic
)) == 0 )
73 for ( i
= sizeof(magic
)-1; i
>= 0; i
-- ) /* the ol' pushback */
79 static gboolean
str_starts_with ( const gchar
*haystack
, const gchar
*needle
, guint16 len_needle
, gboolean must_be_longer
)
81 if ( strlen(haystack
) > len_needle
- (!must_be_longer
) && strncasecmp ( haystack
, needle
, len_needle
) == 0 )
86 static guint16
layer_type_from_string ( const gchar
*str
)
89 for ( i
= 0; i
< VIK_LAYER_NUM_TYPES
; i
++ )
90 if ( strcasecmp ( str
, vik_layer_get_interface(i
)->name
) == 0 )
95 static void write_layer_params_and_data ( VikLayer
*l
, FILE *f
)
97 VikLayerParam
*params
= vik_layer_get_interface(l
->type
)->params
;
98 VikLayerFuncGetParam get_param
= vik_layer_get_interface(l
->type
)->get_param
;
100 fprintf ( f
, "name=%s\n", l
->name
? l
->name
: "" );
102 fprintf ( f
, "visible=f\n" );
104 if ( params
&& get_param
)
106 VikLayerParamData data
;
107 guint16 i
, params_count
= vik_layer_get_interface(l
->type
)->params_count
;
108 for ( i
= 0; i
< params_count
; i
++ )
110 data
= get_param(l
,i
);
112 /* string lists are handled differently. We get a GList (that shouldn't
113 * be freed) back for get_param and if it is null we shouldn't write
114 * anything at all (otherwise we'd read in a list with an empty string,
115 * not an empty string list.
117 if ( params
[i
].type
== VIK_LAYER_PARAM_STRING_LIST
) {
119 GList
*iter
= (GList
*)data
.sl
;
121 fprintf ( f
, "%s=", params
[i
].name
);
122 fprintf ( f
, "%s\n", (gchar
*)(iter
->data
) );
127 fprintf ( f
, "%s=", params
[i
].name
);
128 switch ( params
[i
].type
)
130 case VIK_LAYER_PARAM_DOUBLE
: {
131 // char buf[15]; /* locale independent */
132 // fprintf ( f, "%s\n", (char *) g_dtostr (data.d, buf, sizeof (buf)) ); break;
133 fprintf ( f
, "%f\n", data
.d
);
136 case VIK_LAYER_PARAM_UINT
: fprintf ( f
, "%d\n", data
.u
); break;
137 case VIK_LAYER_PARAM_INT
: fprintf ( f
, "%d\n", data
.i
); break;
138 case VIK_LAYER_PARAM_BOOLEAN
: fprintf ( f
, "%c\n", data
.b
? 't' : 'f' ); break;
139 case VIK_LAYER_PARAM_STRING
: fprintf ( f
, "%s\n", data
.s
); break;
140 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;
145 if ( vik_layer_get_interface(l
->type
)->write_file_data
)
147 fprintf ( f
, "\n\n~LayerData\n" );
148 vik_layer_get_interface(l
->type
)->write_file_data ( l
, f
);
149 fprintf ( f
, "~EndLayerData\n" );
152 write param, and get_value, etc.
153 then run layer data, and that's it.
157 static void file_write ( VikAggregateLayer
*top
, FILE *f
, gpointer vp
)
160 VikLayer
*current_layer
;
165 stack
->data
= (gpointer
) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top
));
169 vik_coord_to_latlon ( vik_viewport_get_center ( VIK_VIEWPORT(vp
) ), &ll
);
171 switch ( vik_viewport_get_drawmode ( VIK_VIEWPORT(vp
) ) ) {
172 case VIK_VIEWPORT_DRAWMODE_UTM
: modestring
= "utm"; break;
173 case VIK_VIEWPORT_DRAWMODE_EXPEDIA
: modestring
= "expedia"; break;
174 case VIK_VIEWPORT_DRAWMODE_GOOGLE
: modestring
= "google"; break;
175 case VIK_VIEWPORT_DRAWMODE_KH
: modestring
= "kh"; break;
176 default: modestring
= "mercator";
179 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",
180 vik_viewport_get_xmpp ( VIK_VIEWPORT(vp
) ), vik_viewport_get_ympp ( VIK_VIEWPORT(vp
) ), ll
.lat
, ll
.lon
,
181 modestring
, vik_viewport_get_background_color(VIK_VIEWPORT(vp
)),
182 vik_viewport_get_draw_scale(VIK_VIEWPORT(vp
)) ? "t" : "f",
183 vik_viewport_get_draw_centermark(VIK_VIEWPORT(vp
)) ? "t" : "f" );
185 if ( ! VIK_LAYER(top
)->visible
)
186 fprintf ( f
, "visible=f\n" );
188 while (stack
&& stack
->data
)
190 current_layer
= VIK_LAYER(((GList
*)stack
->data
)->data
);
191 fprintf ( f
, "\n~Layer %s\n", vik_layer_get_interface(current_layer
->type
)->name
);
192 write_layer_params_and_data ( current_layer
, f
);
193 if ( current_layer
->type
== VIK_LAYER_AGGREGATE
&& !vik_aggregate_layer_is_empty(VIK_AGGREGATE_LAYER(current_layer
)) )
196 stack
->data
= (gpointer
) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(current_layer
));
198 else if ( current_layer
->type
== VIK_LAYER_GPS
&& !vik_gps_layer_is_empty(VIK_GPS_LAYER(current_layer
)) )
201 stack
->data
= (gpointer
) vik_gps_layer_get_children(VIK_GPS_LAYER(current_layer
));
205 stack
->data
= (gpointer
) ((GList
*)stack
->data
)->next
;
206 fprintf ( f
, "~EndLayer\n\n" );
207 while ( stack
&& (!stack
->data
) )
212 stack
->data
= (gpointer
) ((GList
*)stack
->data
)->next
;
213 fprintf ( f
, "~EndLayer\n\n" );
219 get vikaggregatelayer's children (?)
220 foreach write ALL params,
221 then layer data (IF function exists)
225 stack of layers (LIST) we are working on
226 when layer->next == NULL ...
231 static void string_list_delete ( gpointer key
, gpointer l
, gpointer user_data
)
233 GList
*iter
= (GList
*) iter
;
235 g_free ( iter
->data
);
238 g_list_free ( (GList
*) iter
);
241 static void string_list_set_param (gint i
, GList
*list
, gpointer
*layer_and_vp
)
245 vik_layer_set_param ( VIK_LAYER(layer_and_vp
[0]), i
, x
, layer_and_vp
[1] );
248 static void file_read ( VikAggregateLayer
*top
, FILE *f
, VikViewport
*vp
)
251 struct LatLon ll
= { 0.0, 0.0 };
257 VikLayerParam
*params
= NULL
; /* for current layer, so we don't have to keep on looking up interface */
258 guint8 params_count
= 0;
260 GHashTable
*string_lists
= g_hash_table_new(g_direct_hash
,g_direct_equal
);
264 stack
->data
= (gpointer
) top
;
266 while ( fgets ( buffer
, 4096, f
) )
271 while ( *line
== ' ' || *line
=='\t' )
274 if ( line
[0] == '#' )
279 if ( len
> 0 && line
[len
-1] == '\n' )
281 if ( len
> 0 && line
[len
-1] == '\r' )
288 if ( line
[0] == '~' )
293 else if ( str_starts_with ( line
, "Layer ", 6, TRUE
) )
295 int parent_type
= VIK_LAYER(stack
->data
)->type
;
296 if ( ( ! stack
->data
) || ((parent_type
!= VIK_LAYER_AGGREGATE
) && (parent_type
!= VIK_LAYER_GPS
)) )
298 g_warning ( "Line %ld: Layer command inside non-Aggregate Layer (type %d)", line_num
, parent_type
);
299 push(&stack
); /* inside INVALID layer */
305 gint16 type
= layer_type_from_string ( line
+6 );
309 g_warning ( "Line %ld: Unknown type %s\n", line_num
, line
+6 );
312 else if (parent_type
== VIK_LAYER_GPS
)
314 stack
->data
= (gpointer
) vik_gps_layer_get_a_child(VIK_GPS_LAYER(stack
->under
->data
));
315 params
= vik_layer_get_interface(type
)->params
;
316 params_count
= vik_layer_get_interface(type
)->params_count
;
320 stack
->data
= (gpointer
) vik_layer_create ( type
, vp
, NULL
, FALSE
);
321 params
= vik_layer_get_interface(type
)->params
;
322 params_count
= vik_layer_get_interface(type
)->params_count
;
326 else if ( str_starts_with ( line
, "EndLayer", 8, FALSE
) )
328 if ( stack
->under
== NULL
)
329 g_warning ( "Line %ld: Mismatched ~EndLayer command", line_num
);
332 /* add any string lists we've accumulated */
333 gpointer layer_and_vp
[2];
334 layer_and_vp
[0] = stack
->data
;
335 layer_and_vp
[1] = vp
;
336 g_hash_table_foreach ( string_lists
, (GHFunc
) string_list_set_param
, layer_and_vp
);
337 g_hash_table_remove_all ( string_lists
);
339 if ( stack
->data
&& stack
->under
->data
)
341 if (VIK_LAYER(stack
->under
->data
)->type
== VIK_LAYER_AGGREGATE
) {
342 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack
->under
->data
), VIK_LAYER(stack
->data
) );
343 vik_layer_post_read ( VIK_LAYER(stack
->data
), vp
, TRUE
);
345 else if (VIK_LAYER(stack
->under
->data
)->type
== VIK_LAYER_GPS
) {
346 /* TODO: anything else needs to be done here ? */
349 g_warning ( "Line %ld: EndLayer command inside non-Aggregate Layer (type %d)", line_num
, VIK_LAYER(stack
->data
)->type
);
354 else if ( str_starts_with ( line
, "LayerData", 9, FALSE
) )
356 if ( stack
->data
&& vik_layer_get_interface(VIK_LAYER(stack
->data
)->type
)->read_file_data
)
357 vik_layer_get_interface(VIK_LAYER(stack
->data
)->type
)->read_file_data ( VIK_LAYER(stack
->data
), f
);
358 /* must read until hits ~EndLayerData */
361 { /* simply skip layer data over */
362 while ( fgets ( buffer
, 4096, f
) )
369 if ( len
> 0 && line
[len
-1] == '\n' )
371 if ( len
> 0 && line
[len
-1] == '\r' )
373 if ( strcasecmp ( line
, "~EndLayerData" ) == 0 )
381 g_warning ( "Line %ld: Unknown tilde command", line_num
);
391 for ( i
= 0; i
< len
; i
++ )
392 if ( line
[i
] == '=' )
395 if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "xmpp", eq_pos
) == 0) /* "hard coded" params: global & for all layer-types */
396 vik_viewport_set_xmpp ( VIK_VIEWPORT(vp
), strtod ( line
+5, NULL
) );
397 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "ympp", eq_pos
) == 0)
398 vik_viewport_set_ympp ( VIK_VIEWPORT(vp
), strtod ( line
+5, NULL
) );
399 else if ( stack
->under
== NULL
&& eq_pos
== 3 && strncasecmp ( line
, "lat", eq_pos
) == 0 )
400 ll
.lat
= strtod ( line
+4, NULL
);
401 else if ( stack
->under
== NULL
&& eq_pos
== 3 && strncasecmp ( line
, "lon", eq_pos
) == 0 )
402 ll
.lon
= strtod ( line
+4, NULL
);
403 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "utm" ) == 0)
404 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp
), VIK_VIEWPORT_DRAWMODE_UTM
);
405 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "expedia" ) == 0)
406 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp
), VIK_VIEWPORT_DRAWMODE_EXPEDIA
);
407 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "google" ) == 0)
408 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp
), VIK_VIEWPORT_DRAWMODE_GOOGLE
);
409 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "kh" ) == 0)
410 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp
), VIK_VIEWPORT_DRAWMODE_KH
);
411 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "mercator" ) == 0)
412 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp
), VIK_VIEWPORT_DRAWMODE_MERCATOR
);
413 else if ( stack
->under
== NULL
&& eq_pos
== 5 && strncasecmp ( line
, "color", eq_pos
) == 0 )
414 vik_viewport_set_background_color ( VIK_VIEWPORT(vp
), line
+6 );
415 else if ( stack
->under
== NULL
&& eq_pos
== 9 && strncasecmp ( line
, "drawscale", eq_pos
) == 0 )
416 vik_viewport_set_draw_scale ( VIK_VIEWPORT(vp
), TEST_BOOLEAN(line
+10) );
417 else if ( stack
->under
== NULL
&& eq_pos
== 14 && strncasecmp ( line
, "drawcentermark", eq_pos
) == 0 )
418 vik_viewport_set_draw_centermark ( VIK_VIEWPORT(vp
), TEST_BOOLEAN(line
+15) );
419 else if ( stack
->under
&& eq_pos
== 4 && strncasecmp ( line
, "name", eq_pos
) == 0 )
420 vik_layer_rename ( VIK_LAYER(stack
->data
), line
+5 );
421 else if ( eq_pos
== 7 && strncasecmp ( line
, "visible", eq_pos
) == 0 )
422 VIK_LAYER(stack
->data
)->visible
= TEST_BOOLEAN(line
+8);
423 else if ( eq_pos
!= -1 && stack
->under
)
425 gboolean found_match
= FALSE
;
427 /* go thru layer params. if len == eq_pos && starts_with jazz, set it. */
428 /* also got to check for name and visible. */
432 g_warning ( "Line %ld: No options for this kind of layer", line_num
);
436 for ( i
= 0; i
< params_count
; i
++ )
437 if ( strlen(params
[i
].name
) == eq_pos
&& strncasecmp ( line
, params
[i
].name
, eq_pos
) == 0 )
441 if ( params
[i
].type
== VIK_LAYER_PARAM_STRING_LIST
) {
442 GList
*l
= g_list_append ( g_hash_table_lookup ( string_lists
, (gpointer
) ((gint
) i
) ), g_strdup(line
) );
443 g_hash_table_replace ( string_lists
, (gpointer
) ((gint
)i
), l
);
444 /* add the value to a list, possibly making a new list.
445 * this will be passed to the layer when we read an ~EndLayer */
447 switch ( params
[i
].type
)
449 case VIK_LAYER_PARAM_DOUBLE
: x
.d
= strtod(line
, NULL
); break;
450 case VIK_LAYER_PARAM_UINT
: x
.u
= strtoul(line
, NULL
, 10); break;
451 case VIK_LAYER_PARAM_INT
: x
.i
= strtol(line
, NULL
, 10); break;
452 case VIK_LAYER_PARAM_BOOLEAN
: x
.b
= TEST_BOOLEAN(line
); break;
453 case VIK_LAYER_PARAM_COLOR
: memset(&(x
.c
), 0, sizeof(x
.c
)); /* default: black */
454 gdk_color_parse ( line
, &(x
.c
) ); break;
455 /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */
458 vik_layer_set_param ( VIK_LAYER(stack
->data
), i
, x
, vp
);
464 g_warning ( "Line %ld: Unknown parameter. Line:\n%s", line_num
, line
);
467 g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num
);
480 if ( stack
->under
&& stack
->under
->data
&& stack
->data
)
482 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack
->under
->data
), VIK_LAYER(stack
->data
) );
483 vik_layer_post_read ( VIK_LAYER(stack
->data
), vp
, TRUE
);
488 if ( ll
.lat
!= 0.0 || ll
.lon
!= 0.0 )
489 vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp
), &ll
);
491 if ( ( ! VIK_LAYER(top
)->visible
) && VIK_LAYER(top
)->realized
)
492 vik_treeview_item_set_visible ( VIK_LAYER(top
)->vt
, &(VIK_LAYER(top
)->iter
), FALSE
);
494 /* delete anything we've forgotten about -- should only happen when file ends before an EndLayer */
495 g_hash_table_foreach ( string_lists
, string_list_delete
, NULL
);
496 g_hash_table_destroy ( string_lists
);
503 new default layer of type (str_to_type) (check interface->name)
505 VikLayer *vl = stack->data;
507 vik_aggregate_layer_add_layer(stack->data, vl);
509 vik_layer_data ( VIK_LAYER_DATA(stack->data), f, vp );
513 /* ---------------------------------------------------- */
515 static FILE *xfopen ( const char *fn
, const char *mode
)
517 if ( strcmp(fn
,"-") == 0 )
520 return fopen(fn
, "r");
523 static void xfclose ( FILE *f
)
525 if ( f
!= stdin
&& f
!= stdout
)
529 /* 0 on failure, 1 on success (vik file) 2 on success (other file) */
530 gshort
a_file_load ( VikAggregateLayer
*top
, VikViewport
*vp
, const gchar
*filename
)
532 FILE *f
= xfopen ( filename
, "r" );
539 if ( check_magic ( f
, VIK_MAGIC
) )
541 file_read ( top
, f
, vp
);
549 VikLayer
*vtl
= vik_layer_create ( VIK_LAYER_TRW
, vp
, NULL
, FALSE
);
550 vik_layer_rename ( vtl
, a_file_basename ( filename
) );
552 if ( check_magic ( f
, GPX_MAGIC
) )
553 a_gpx_read_file ( VIK_TRW_LAYER(vtl
), f
);
555 a_gpspoint_read_file ( VIK_TRW_LAYER(vtl
), f
);
557 vik_layer_post_read ( vtl
, vp
, TRUE
);
559 vik_aggregate_layer_add_layer ( top
, vtl
);
561 if ( vik_trw_layer_find_center ( VIK_TRW_LAYER(vtl
), &new_center
) )
562 vik_viewport_set_center_coord ( VIK_VIEWPORT(vp
), &new_center
);
568 gboolean
a_file_save ( VikAggregateLayer
*top
, gpointer vp
, const gchar
*filename
)
570 FILE *f
= fopen(filename
, "w");
575 file_write ( top
, f
, vp
);
583 const gchar
*a_file_basename ( const gchar
*filename
)
585 const gchar
*t
= filename
+ strlen(filename
) - 1;
586 while ( --t
> filename
)
587 if ( *(t
-1) == FILE_SEP
)
594 gboolean
a_file_export ( VikTrwLayer
*vtl
, const gchar
*filename
, gshort file_type
)
596 FILE *f
= fopen ( filename
, "w" );
599 if ( file_type
== FILE_TYPE_GPSMAPPER
)
600 a_gpsmapper_write_file ( vtl
, f
);
601 else if ( file_type
== FILE_TYPE_GPX
)
602 a_gpx_write_file ( vtl
, f
);
604 a_gpspoint_write_file ( vtl
, f
);
611 const gchar
*a_get_viking_dir()
613 static gchar
*viking_dir
= NULL
;
616 const gchar
*home
= g_getenv("HOME");
617 if (!home
|| access(home
, W_OK
))
618 home
= g_get_home_dir ();
620 if (!home
|| access(home
, W_OK
))
622 static gchar temp
[] = {"/tmp/vikXXXXXX"};
623 home
= mkdtemp(temp
);
626 if (!home
|| access(home
, W_OK
))
628 g_critical("Unable to find a base directory");
630 /* Build the name of the directory */
631 viking_dir
= g_build_filename(home
, ".viking", NULL
);
632 if (access(viking_dir
, F_OK
))
633 g_mkdir(viking_dir
, 0755);