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
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
40 typedef struct _Stack Stack
;
47 static void pop(Stack
**stack
) {
48 Stack
*tmp
= (*stack
)->under
;
53 static void push(Stack
**stack
)
55 Stack
*tmp
= g_malloc ( sizeof ( Stack
) );
60 static gboolean
check_magic ( FILE *f
, const gchar
*magic_number
)
62 gchar magic
[VIK_MAGIC_LEN
];
65 if ( fread(magic
, 1, sizeof(magic
), f
) == sizeof(magic
) &&
66 strncmp(magic
, magic_number
, sizeof(magic
)) == 0 )
68 for ( i
= sizeof(magic
)-1; i
>= 0; i
-- ) /* the ol' pushback */
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 )
81 static guint16
layer_type_from_string ( const gchar
*str
)
84 for ( i
= 0; i
< VIK_LAYER_NUM_TYPES
; i
++ )
85 if ( strcasecmp ( str
, vik_layer_get_interface(i
)->name
) == 0 )
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
: "" );
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
);
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" );
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
)
138 VikLayer
*current_layer
;
143 stack
->data
= (gpointer
) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top
));
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
)) )
172 stack
->data
= (gpointer
) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(current_layer
));
176 stack
->data
= (gpointer
) ((GList
*)stack
->data
)->next
;
177 fprintf ( f
, "~EndLayer\n\n" );
178 while ( stack
&& (!stack
->data
) )
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)
196 stack of layers (LIST) we are working on
197 when layer->next == NULL ...
202 static void file_read ( VikAggregateLayer
*top
, FILE *f
, gpointer vp
)
205 struct LatLon ll
= { 0.0, 0.0 };
211 VikLayerParam
*params
; /* for current layer, so we don't have to keep on looking up interface */
216 stack
->data
= (gpointer
) top
;
218 while ( fgets ( buffer
, 4096, f
) )
223 while ( *line
== ' ' || *line
=='\t' )
226 if ( line
[0] == '#' )
231 if ( len
> 0 && line
[len
-1] == '\n' )
233 if ( len
> 0 && line
[len
-1] == '\r' )
240 if ( line
[0] == '~' )
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 */
256 gint16 type
= layer_type_from_string ( line
+6 );
260 g_warning ( "Line %ld: Unknown type %s\n", line_num
, line
+6 );
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
);
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
);
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 */
292 { /* simply skip layer data over */
293 while ( fgets ( buffer
, 4096, f
) )
300 if ( len
> 0 && line
[len
-1] == '\n' )
302 if ( len
> 0 && line
[len
-1] == '\r' )
304 if ( strcasecmp ( line
, "~EndLayerData" ) == 0 )
312 g_warning ( "Line %ld: Unknown tilde command", line_num
);
322 for ( i
= 0; i
< len
; i
++ )
323 if ( line
[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. */
358 g_warning ( "Line %ld: No options for this kind of layer", line_num
);
362 for ( i
= 0; i
< params_count
; i
++ )
363 if ( strlen(params
[i
].name
) == eq_pos
&& strncasecmp ( line
, params
[i
].name
, eq_pos
) == 0 )
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;
377 vik_layer_set_param ( VIK_LAYER(stack
->data
), i
, x
, vp
);
382 g_warning ( "Line %ld: Unknown parameter", line_num
);
385 g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num
);
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
);
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
);
417 new default layer of type (str_to_type) (check interface->name)
419 VikLayer *vl = stack->data;
421 vik_aggregate_layer_add_layer(stack->data, vl);
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 )
434 return fopen(fn
, "r");
437 static void xfclose ( FILE *f
)
439 if ( f
!= stdin
&& f
!= stdout
)
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" );
453 if ( check_magic ( f
, VIK_MAGIC
) )
455 file_read ( top
, f
, vp
);
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
);
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
);
482 gboolean
a_file_save ( VikAggregateLayer
*top
, gpointer vp
, const gchar
*filename
)
484 FILE *f
= fopen(filename
, "w");
489 file_write ( top
, f
, vp
);
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
)
508 gboolean
a_file_export ( VikTrwLayer
*vtl
, const gchar
*filename
, gshort file_type
)
510 FILE *f
= fopen ( filename
, "w" );
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
);
518 a_gpspoint_write_file ( vtl
, f
);