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
36 #include <glib/gstdio.h>
37 #include <glib/gi18n.h>
39 /* Relax some dependencies */
40 #if ! GLIB_CHECK_VERSION(2,12,0)
41 static gboolean
return_true (gpointer a
, gpointer b
, gpointer c
) { return TRUE
; }
42 static g_hash_table_remove_all (GHashTable
*ght
) { g_hash_table_foreach_remove ( ght
, (GHRFunc
) return_true
, FALSE
); }
45 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
46 #define VIK_MAGIC "#VIK"
47 #define GPX_MAGIC "<?xm"
48 #define VIK_MAGIC_LEN 4
56 typedef struct _Stack Stack
;
63 static void pop(Stack
**stack
) {
64 Stack
*tmp
= (*stack
)->under
;
69 static void push(Stack
**stack
)
71 Stack
*tmp
= g_malloc ( sizeof ( Stack
) );
76 static gboolean
check_magic ( FILE *f
, const gchar
*magic_number
)
78 gchar magic
[VIK_MAGIC_LEN
];
81 if ( fread(magic
, 1, sizeof(magic
), f
) == sizeof(magic
) &&
82 strncmp(magic
, magic_number
, sizeof(magic
)) == 0 )
84 for ( i
= sizeof(magic
)-1; i
>= 0; i
-- ) /* the ol' pushback */
90 static gboolean
str_starts_with ( const gchar
*haystack
, const gchar
*needle
, guint16 len_needle
, gboolean must_be_longer
)
92 if ( strlen(haystack
) > len_needle
- (!must_be_longer
) && strncasecmp ( haystack
, needle
, len_needle
) == 0 )
97 static guint16
layer_type_from_string ( const gchar
*str
)
100 for ( i
= 0; i
< VIK_LAYER_NUM_TYPES
; i
++ )
101 if ( strcasecmp ( str
, vik_layer_get_interface(i
)->name
) == 0 )
106 void file_write_layer_param ( FILE *f
, const gchar
*name
, guint8 type
, VikLayerParamData data
) {
107 /* string lists are handled differently. We get a GList (that shouldn't
108 * be freed) back for get_param and if it is null we shouldn't write
109 * anything at all (otherwise we'd read in a list with an empty string,
110 * not an empty string list.
112 if ( type
== VIK_LAYER_PARAM_STRING_LIST
) {
114 GList
*iter
= (GList
*)data
.sl
;
116 fprintf ( f
, "%s=", name
);
117 fprintf ( f
, "%s\n", (gchar
*)(iter
->data
) );
122 fprintf ( f
, "%s=", name
);
125 case VIK_LAYER_PARAM_DOUBLE
: {
126 // char buf[15]; /* locale independent */
127 // fprintf ( f, "%s\n", (char *) g_dtostr (data.d, buf, sizeof (buf)) ); break;
128 fprintf ( f
, "%f\n", data
.d
);
131 case VIK_LAYER_PARAM_UINT
: fprintf ( f
, "%d\n", data
.u
); break;
132 case VIK_LAYER_PARAM_INT
: fprintf ( f
, "%d\n", data
.i
); break;
133 case VIK_LAYER_PARAM_BOOLEAN
: fprintf ( f
, "%c\n", data
.b
? 't' : 'f' ); break;
134 case VIK_LAYER_PARAM_STRING
: fprintf ( f
, "%s\n", data
.s
); break;
135 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;
140 static void write_layer_params_and_data ( VikLayer
*l
, FILE *f
)
142 VikLayerParam
*params
= vik_layer_get_interface(l
->type
)->params
;
143 VikLayerFuncGetParam get_param
= vik_layer_get_interface(l
->type
)->get_param
;
145 fprintf ( f
, "name=%s\n", l
->name
? l
->name
: "" );
147 fprintf ( f
, "visible=f\n" );
149 if ( params
&& get_param
)
151 VikLayerParamData data
;
152 guint16 i
, params_count
= vik_layer_get_interface(l
->type
)->params_count
;
153 for ( i
= 0; i
< params_count
; i
++ )
155 data
= get_param(l
,i
);
156 file_write_layer_param(f
, params
[i
].name
, params
[i
].type
, data
);
159 if ( vik_layer_get_interface(l
->type
)->write_file_data
)
161 fprintf ( f
, "\n\n~LayerData\n" );
162 vik_layer_get_interface(l
->type
)->write_file_data ( l
, f
);
163 fprintf ( f
, "~EndLayerData\n" );
166 write param, and get_value, etc.
167 then run layer data, and that's it.
171 static void file_write ( VikAggregateLayer
*top
, FILE *f
, gpointer vp
)
174 VikLayer
*current_layer
;
176 VikViewportDrawMode mode
;
180 stack
->data
= (gpointer
) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top
));
184 vik_coord_to_latlon ( vik_viewport_get_center ( VIK_VIEWPORT(vp
) ), &ll
);
186 mode
= vik_viewport_get_drawmode ( VIK_VIEWPORT(vp
) );
188 case VIK_VIEWPORT_DRAWMODE_UTM
: modestring
= "utm"; break;
189 case VIK_VIEWPORT_DRAWMODE_EXPEDIA
: modestring
= "expedia"; break;
190 case VIK_VIEWPORT_DRAWMODE_MERCATOR
: modestring
= "mercator"; break;
192 g_critical("Houston, we've had a problem. mode=%d", mode
);
195 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",
196 vik_viewport_get_xmpp ( VIK_VIEWPORT(vp
) ), vik_viewport_get_ympp ( VIK_VIEWPORT(vp
) ), ll
.lat
, ll
.lon
,
197 modestring
, vik_viewport_get_background_color(VIK_VIEWPORT(vp
)),
198 vik_viewport_get_draw_scale(VIK_VIEWPORT(vp
)) ? "t" : "f",
199 vik_viewport_get_draw_centermark(VIK_VIEWPORT(vp
)) ? "t" : "f" );
201 if ( ! VIK_LAYER(top
)->visible
)
202 fprintf ( f
, "visible=f\n" );
204 while (stack
&& stack
->data
)
206 current_layer
= VIK_LAYER(((GList
*)stack
->data
)->data
);
207 fprintf ( f
, "\n~Layer %s\n", vik_layer_get_interface(current_layer
->type
)->name
);
208 write_layer_params_and_data ( current_layer
, f
);
209 if ( current_layer
->type
== VIK_LAYER_AGGREGATE
&& !vik_aggregate_layer_is_empty(VIK_AGGREGATE_LAYER(current_layer
)) )
212 stack
->data
= (gpointer
) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(current_layer
));
214 else if ( current_layer
->type
== VIK_LAYER_GPS
&& !vik_gps_layer_is_empty(VIK_GPS_LAYER(current_layer
)) )
217 stack
->data
= (gpointer
) vik_gps_layer_get_children(VIK_GPS_LAYER(current_layer
));
221 stack
->data
= (gpointer
) ((GList
*)stack
->data
)->next
;
222 fprintf ( f
, "~EndLayer\n\n" );
223 while ( stack
&& (!stack
->data
) )
228 stack
->data
= (gpointer
) ((GList
*)stack
->data
)->next
;
229 fprintf ( f
, "~EndLayer\n\n" );
235 get vikaggregatelayer's children (?)
236 foreach write ALL params,
237 then layer data (IF function exists)
241 stack of layers (LIST) we are working on
242 when layer->next == NULL ...
247 static void string_list_delete ( gpointer key
, gpointer l
, gpointer user_data
)
249 /* 20071021 bugfix */
250 GList
*iter
= (GList
*) l
;
252 g_free ( iter
->data
);
255 g_list_free ( (GList
*) l
);
258 static void string_list_set_param (gint i
, GList
*list
, gpointer
*layer_and_vp
)
262 vik_layer_set_param ( VIK_LAYER(layer_and_vp
[0]), i
, x
, layer_and_vp
[1] );
265 static void file_read ( VikAggregateLayer
*top
, FILE *f
, VikViewport
*vp
)
268 struct LatLon ll
= { 0.0, 0.0 };
274 VikLayerParam
*params
= NULL
; /* for current layer, so we don't have to keep on looking up interface */
275 guint8 params_count
= 0;
277 GHashTable
*string_lists
= g_hash_table_new(g_direct_hash
,g_direct_equal
);
281 stack
->data
= (gpointer
) top
;
283 while ( fgets ( buffer
, 4096, f
) )
288 while ( *line
== ' ' || *line
=='\t' )
291 if ( line
[0] == '#' )
296 if ( len
> 0 && line
[len
-1] == '\n' )
298 if ( len
> 0 && line
[len
-1] == '\r' )
305 if ( line
[0] == '~' )
310 else if ( str_starts_with ( line
, "Layer ", 6, TRUE
) )
312 int parent_type
= VIK_LAYER(stack
->data
)->type
;
313 if ( ( ! stack
->data
) || ((parent_type
!= VIK_LAYER_AGGREGATE
) && (parent_type
!= VIK_LAYER_GPS
)) )
315 g_warning ( "Line %ld: Layer command inside non-Aggregate Layer (type %d)", line_num
, parent_type
);
316 push(&stack
); /* inside INVALID layer */
322 gint16 type
= layer_type_from_string ( line
+6 );
326 g_warning ( "Line %ld: Unknown type %s", line_num
, line
+6 );
329 else if (parent_type
== VIK_LAYER_GPS
)
331 stack
->data
= (gpointer
) vik_gps_layer_get_a_child(VIK_GPS_LAYER(stack
->under
->data
));
332 params
= vik_layer_get_interface(type
)->params
;
333 params_count
= vik_layer_get_interface(type
)->params_count
;
337 stack
->data
= (gpointer
) vik_layer_create ( type
, vp
, NULL
, FALSE
);
338 params
= vik_layer_get_interface(type
)->params
;
339 params_count
= vik_layer_get_interface(type
)->params_count
;
343 else if ( str_starts_with ( line
, "EndLayer", 8, FALSE
) )
345 if ( stack
->under
== NULL
)
346 g_warning ( "Line %ld: Mismatched ~EndLayer command", line_num
);
349 /* add any string lists we've accumulated */
350 gpointer layer_and_vp
[2];
351 layer_and_vp
[0] = stack
->data
;
352 layer_and_vp
[1] = vp
;
353 g_hash_table_foreach ( string_lists
, (GHFunc
) string_list_set_param
, layer_and_vp
);
354 g_hash_table_remove_all ( string_lists
);
356 if ( stack
->data
&& stack
->under
->data
)
358 if (VIK_LAYER(stack
->under
->data
)->type
== VIK_LAYER_AGGREGATE
) {
359 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack
->under
->data
), VIK_LAYER(stack
->data
) );
360 vik_layer_post_read ( VIK_LAYER(stack
->data
), vp
, TRUE
);
362 else if (VIK_LAYER(stack
->under
->data
)->type
== VIK_LAYER_GPS
) {
363 /* TODO: anything else needs to be done here ? */
366 g_warning ( "Line %ld: EndLayer command inside non-Aggregate Layer (type %d)", line_num
, VIK_LAYER(stack
->data
)->type
);
371 else if ( str_starts_with ( line
, "LayerData", 9, FALSE
) )
373 if ( stack
->data
&& vik_layer_get_interface(VIK_LAYER(stack
->data
)->type
)->read_file_data
)
374 vik_layer_get_interface(VIK_LAYER(stack
->data
)->type
)->read_file_data ( VIK_LAYER(stack
->data
), f
);
375 /* must read until hits ~EndLayerData */
378 { /* simply skip layer data over */
379 while ( fgets ( buffer
, 4096, f
) )
386 if ( len
> 0 && line
[len
-1] == '\n' )
388 if ( len
> 0 && line
[len
-1] == '\r' )
390 if ( strcasecmp ( line
, "~EndLayerData" ) == 0 )
398 g_warning ( "Line %ld: Unknown tilde command", line_num
);
408 for ( i
= 0; i
< len
; i
++ )
409 if ( line
[i
] == '=' )
412 if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "xmpp", eq_pos
) == 0) /* "hard coded" params: global & for all layer-types */
413 vik_viewport_set_xmpp ( VIK_VIEWPORT(vp
), strtod ( line
+5, NULL
) );
414 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "ympp", eq_pos
) == 0)
415 vik_viewport_set_ympp ( VIK_VIEWPORT(vp
), strtod ( line
+5, NULL
) );
416 else if ( stack
->under
== NULL
&& eq_pos
== 3 && strncasecmp ( line
, "lat", eq_pos
) == 0 )
417 ll
.lat
= strtod ( line
+4, NULL
);
418 else if ( stack
->under
== NULL
&& eq_pos
== 3 && strncasecmp ( line
, "lon", eq_pos
) == 0 )
419 ll
.lon
= strtod ( line
+4, NULL
);
420 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "utm" ) == 0)
421 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp
), VIK_VIEWPORT_DRAWMODE_UTM
);
422 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "expedia" ) == 0)
423 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp
), VIK_VIEWPORT_DRAWMODE_EXPEDIA
);
424 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "google" ) == 0)
426 g_warning ( _("Draw mode '%s' no more supported"), "google" );
428 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "kh" ) == 0)
430 g_warning ( _("Draw mode '%s' no more supported"), "kh" );
432 else if ( stack
->under
== NULL
&& eq_pos
== 4 && strncasecmp ( line
, "mode", eq_pos
) == 0 && strcasecmp ( line
+5, "mercator" ) == 0)
433 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp
), VIK_VIEWPORT_DRAWMODE_MERCATOR
);
434 else if ( stack
->under
== NULL
&& eq_pos
== 5 && strncasecmp ( line
, "color", eq_pos
) == 0 )
435 vik_viewport_set_background_color ( VIK_VIEWPORT(vp
), line
+6 );
436 else if ( stack
->under
== NULL
&& eq_pos
== 9 && strncasecmp ( line
, "drawscale", eq_pos
) == 0 )
437 vik_viewport_set_draw_scale ( VIK_VIEWPORT(vp
), TEST_BOOLEAN(line
+10) );
438 else if ( stack
->under
== NULL
&& eq_pos
== 14 && strncasecmp ( line
, "drawcentermark", eq_pos
) == 0 )
439 vik_viewport_set_draw_centermark ( VIK_VIEWPORT(vp
), TEST_BOOLEAN(line
+15) );
440 else if ( stack
->under
&& eq_pos
== 4 && strncasecmp ( line
, "name", eq_pos
) == 0 )
441 vik_layer_rename ( VIK_LAYER(stack
->data
), line
+5 );
442 else if ( eq_pos
== 7 && strncasecmp ( line
, "visible", eq_pos
) == 0 )
443 VIK_LAYER(stack
->data
)->visible
= TEST_BOOLEAN(line
+8);
444 else if ( eq_pos
!= -1 && stack
->under
)
446 gboolean found_match
= FALSE
;
448 /* go thru layer params. if len == eq_pos && starts_with jazz, set it. */
449 /* also got to check for name and visible. */
453 g_warning ( "Line %ld: No options for this kind of layer", line_num
);
457 for ( i
= 0; i
< params_count
; i
++ )
458 if ( strlen(params
[i
].name
) == eq_pos
&& strncasecmp ( line
, params
[i
].name
, eq_pos
) == 0 )
462 if ( params
[i
].type
== VIK_LAYER_PARAM_STRING_LIST
) {
463 GList
*l
= g_list_append ( g_hash_table_lookup ( string_lists
, GINT_TO_POINTER ((gint
) i
) ),
465 g_hash_table_replace ( string_lists
, GINT_TO_POINTER ((gint
)i
), l
);
466 /* add the value to a list, possibly making a new list.
467 * this will be passed to the layer when we read an ~EndLayer */
469 switch ( params
[i
].type
)
471 case VIK_LAYER_PARAM_DOUBLE
: x
.d
= strtod(line
, NULL
); break;
472 case VIK_LAYER_PARAM_UINT
: x
.u
= strtoul(line
, NULL
, 10); break;
473 case VIK_LAYER_PARAM_INT
: x
.i
= strtol(line
, NULL
, 10); break;
474 case VIK_LAYER_PARAM_BOOLEAN
: x
.b
= TEST_BOOLEAN(line
); break;
475 case VIK_LAYER_PARAM_COLOR
: memset(&(x
.c
), 0, sizeof(x
.c
)); /* default: black */
476 gdk_color_parse ( line
, &(x
.c
) ); break;
477 /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */
480 vik_layer_set_param ( VIK_LAYER(stack
->data
), i
, x
, vp
);
486 g_warning ( "Line %ld: Unknown parameter. Line:\n%s", line_num
, line
);
489 g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num
);
502 if ( stack
->under
&& stack
->under
->data
&& stack
->data
)
504 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack
->under
->data
), VIK_LAYER(stack
->data
) );
505 vik_layer_post_read ( VIK_LAYER(stack
->data
), vp
, TRUE
);
510 if ( ll
.lat
!= 0.0 || ll
.lon
!= 0.0 )
511 vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp
), &ll
);
513 if ( ( ! VIK_LAYER(top
)->visible
) && VIK_LAYER(top
)->realized
)
514 vik_treeview_item_set_visible ( VIK_LAYER(top
)->vt
, &(VIK_LAYER(top
)->iter
), FALSE
);
516 /* delete anything we've forgotten about -- should only happen when file ends before an EndLayer */
517 g_hash_table_foreach ( string_lists
, string_list_delete
, NULL
);
518 g_hash_table_destroy ( string_lists
);
525 new default layer of type (str_to_type) (check interface->name)
527 VikLayer *vl = stack->data;
529 vik_aggregate_layer_add_layer(stack->data, vl);
531 vik_layer_data ( VIK_LAYER_DATA(stack->data), f, vp );
535 /* ---------------------------------------------------- */
537 static FILE *xfopen ( const char *fn
, const char *mode
)
539 if ( strcmp(fn
,"-") == 0 )
542 return g_fopen(fn
, "r");
545 static void xfclose ( FILE *f
)
547 if ( f
!= stdin
&& f
!= stdout
) {
553 /* 0 on failure, 1 on success (vik file) 2 on success (other file) */
554 gshort
a_file_load ( VikAggregateLayer
*top
, VikViewport
*vp
, const gchar
*filename
)
556 FILE *f
= xfopen ( filename
, "r" );
563 if ( check_magic ( f
, VIK_MAGIC
) )
565 file_read ( top
, f
, vp
);
573 VikLayer
*vtl
= vik_layer_create ( VIK_LAYER_TRW
, vp
, NULL
, FALSE
);
574 vik_layer_rename ( vtl
, a_file_basename ( filename
) );
576 if ( check_magic ( f
, GPX_MAGIC
) )
577 a_gpx_read_file ( VIK_TRW_LAYER(vtl
), f
);
579 a_gpspoint_read_file ( VIK_TRW_LAYER(vtl
), f
);
581 vik_layer_post_read ( vtl
, vp
, TRUE
);
583 vik_aggregate_layer_add_layer ( top
, vtl
);
585 if ( vik_trw_layer_find_center ( VIK_TRW_LAYER(vtl
), &new_center
) )
586 vik_viewport_set_center_coord ( VIK_VIEWPORT(vp
), &new_center
);
592 gboolean
a_file_save ( VikAggregateLayer
*top
, gpointer vp
, const gchar
*filename
)
594 FILE *f
= g_fopen(filename
, "w");
599 file_write ( top
, f
, vp
);
608 const gchar
*a_file_basename ( const gchar
*filename
)
610 const gchar
*t
= filename
+ strlen(filename
) - 1;
611 while ( --t
> filename
)
612 if ( *(t
-1) == FILE_SEP
)
619 gboolean
a_file_export ( VikTrwLayer
*vtl
, const gchar
*filename
, gshort file_type
)
621 FILE *f
= g_fopen ( filename
, "w" );
624 if ( file_type
== FILE_TYPE_GPSMAPPER
)
625 a_gpsmapper_write_file ( vtl
, f
);
626 else if ( file_type
== FILE_TYPE_GPX
)
627 a_gpx_write_file ( vtl
, f
);
629 a_gpspoint_write_file ( vtl
, f
);
637 const gchar
*a_get_viking_dir()
639 static gchar
*viking_dir
= NULL
;
641 // TODO: use g_get_user_config_dir ?
644 const gchar
*home
= g_getenv("HOME");
645 if (!home
|| g_access(home
, W_OK
))
646 home
= g_get_home_dir ();
648 if (!home
|| g_access(home
, W_OK
))
650 static gchar temp
[] = {"/tmp/vikXXXXXX"};
651 home
= mkdtemp(temp
);
654 if (!home
|| g_access(home
, W_OK
))
656 g_critical("Unable to find a base directory");
658 /* Build the name of the directory */
659 viking_dir
= g_build_filename(home
, ".viking", NULL
);
660 if (g_file_test(viking_dir
, G_FILE_TEST_EXISTS
) == FALSE
)
661 g_mkdir(viking_dir
, 0755);