Typos
[viking.git] / src / file.c
blob5379c0ee52df1dc33d0424ededbe92d0f6f5cf0c
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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "viking.h"
27 #include "gpx.h"
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <glib.h>
36 #include <glib/gstdio.h>
38 /* Relax some dependencies */
39 #if ! GLIB_CHECK_VERSION(2,12,0)
40 static gboolean return_true (gpointer a, gpointer b, gpointer c) { return TRUE; }
41 static g_hash_table_remove_all (GHashTable *ght) { g_hash_table_foreach_remove ( ght, (GHRFunc) return_true, FALSE ); }
42 #endif
44 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
45 #define VIK_MAGIC "#VIK"
46 #define GPX_MAGIC "<?xm"
47 #define VIK_MAGIC_LEN 4
49 #ifdef WINDOWS
50 #define FILE_SEP '\\'
51 #else
52 #define FILE_SEP '/'
53 #endif
55 typedef struct _Stack Stack;
57 struct _Stack {
58 Stack *under;
59 gpointer *data;
62 static void pop(Stack **stack) {
63 Stack *tmp = (*stack)->under;
64 g_free ( *stack );
65 *stack = tmp;
68 static void push(Stack **stack)
70 Stack *tmp = g_malloc ( sizeof ( Stack ) );
71 tmp->under = *stack;
72 *stack = tmp;
75 static gboolean check_magic ( FILE *f, const gchar *magic_number )
77 gchar magic[VIK_MAGIC_LEN];
78 gboolean rv = FALSE;
79 gint8 i;
80 if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) &&
81 strncmp(magic, magic_number, sizeof(magic)) == 0 )
82 rv = TRUE;
83 for ( i = sizeof(magic)-1; i >= 0; i-- ) /* the ol' pushback */
84 ungetc(magic[i],f);
85 return rv;
89 static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, guint16 len_needle, gboolean must_be_longer )
91 if ( strlen(haystack) > len_needle - (!must_be_longer) && strncasecmp ( haystack, needle, len_needle ) == 0 )
92 return TRUE;
93 return FALSE;
96 static guint16 layer_type_from_string ( const gchar *str )
98 guint8 i;
99 for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
100 if ( strcasecmp ( str, vik_layer_get_interface(i)->name ) == 0 )
101 return i;
102 return -1;
105 void file_write_layer_param ( FILE *f, const gchar *name, guint8 type, VikLayerParamData data ) {
106 /* string lists are handled differently. We get a GList (that shouldn't
107 * be freed) back for get_param and if it is null we shouldn't write
108 * anything at all (otherwise we'd read in a list with an empty string,
109 * not an empty string list.
111 if ( type == VIK_LAYER_PARAM_STRING_LIST ) {
112 if ( data.sl ) {
113 GList *iter = (GList *)data.sl;
114 while ( iter ) {
115 fprintf ( f, "%s=", name );
116 fprintf ( f, "%s\n", (gchar *)(iter->data) );
117 iter = iter->next;
120 } else {
121 fprintf ( f, "%s=", name );
122 switch ( type )
124 case VIK_LAYER_PARAM_DOUBLE: {
125 // char buf[15]; /* locale independent */
126 // fprintf ( f, "%s\n", (char *) g_dtostr (data.d, buf, sizeof (buf)) ); break;
127 fprintf ( f, "%f\n", data.d );
128 break;
130 case VIK_LAYER_PARAM_UINT: fprintf ( f, "%d\n", data.u ); break;
131 case VIK_LAYER_PARAM_INT: fprintf ( f, "%d\n", data.i ); break;
132 case VIK_LAYER_PARAM_BOOLEAN: fprintf ( f, "%c\n", data.b ? 't' : 'f' ); break;
133 case VIK_LAYER_PARAM_STRING: fprintf ( f, "%s\n", data.s ); break;
134 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;
139 static void write_layer_params_and_data ( VikLayer *l, FILE *f )
141 VikLayerParam *params = vik_layer_get_interface(l->type)->params;
142 VikLayerFuncGetParam get_param = vik_layer_get_interface(l->type)->get_param;
144 fprintf ( f, "name=%s\n", l->name ? l->name : "" );
145 if ( !l->visible )
146 fprintf ( f, "visible=f\n" );
148 if ( params && get_param )
150 VikLayerParamData data;
151 guint16 i, params_count = vik_layer_get_interface(l->type)->params_count;
152 for ( i = 0; i < params_count; i++ )
154 data = get_param(l,i);
155 file_write_layer_param(f, params[i].name, params[i].type, data);
158 if ( vik_layer_get_interface(l->type)->write_file_data )
160 fprintf ( f, "\n\n~LayerData\n" );
161 vik_layer_get_interface(l->type)->write_file_data ( l, f );
162 fprintf ( f, "~EndLayerData\n" );
164 /* foreach param:
165 write param, and get_value, etc.
166 then run layer data, and that's it.
170 static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp )
172 Stack *stack = NULL;
173 VikLayer *current_layer;
174 struct LatLon ll;
175 VikViewportDrawMode mode;
176 gchar *modestring;
178 push(&stack);
179 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top));
180 stack->under = NULL;
182 /* crazhy CRAZHY */
183 vik_coord_to_latlon ( vik_viewport_get_center ( VIK_VIEWPORT(vp) ), &ll );
185 mode = vik_viewport_get_drawmode ( VIK_VIEWPORT(vp) );
186 switch ( mode ) {
187 case VIK_VIEWPORT_DRAWMODE_UTM: modestring = "utm"; break;
188 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: modestring = "expedia"; break;
189 case VIK_VIEWPORT_DRAWMODE_GOOGLE: modestring = "google"; break;
190 case VIK_VIEWPORT_DRAWMODE_KH: modestring = "kh"; break;
191 case VIK_VIEWPORT_DRAWMODE_MERCATOR: modestring = "mercator"; break;
192 default:
193 g_critical("Houston, we've had a problem. mode=%d", mode);
196 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",
197 vik_viewport_get_xmpp ( VIK_VIEWPORT(vp) ), vik_viewport_get_ympp ( VIK_VIEWPORT(vp) ), ll.lat, ll.lon,
198 modestring, vik_viewport_get_background_color(VIK_VIEWPORT(vp)),
199 vik_viewport_get_draw_scale(VIK_VIEWPORT(vp)) ? "t" : "f",
200 vik_viewport_get_draw_centermark(VIK_VIEWPORT(vp)) ? "t" : "f" );
202 if ( ! VIK_LAYER(top)->visible )
203 fprintf ( f, "visible=f\n" );
205 while (stack && stack->data)
207 current_layer = VIK_LAYER(((GList *)stack->data)->data);
208 fprintf ( f, "\n~Layer %s\n", vik_layer_get_interface(current_layer->type)->name );
209 write_layer_params_and_data ( current_layer, f );
210 if ( current_layer->type == VIK_LAYER_AGGREGATE && !vik_aggregate_layer_is_empty(VIK_AGGREGATE_LAYER(current_layer)) )
212 push(&stack);
213 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(current_layer));
215 else if ( current_layer->type == VIK_LAYER_GPS && !vik_gps_layer_is_empty(VIK_GPS_LAYER(current_layer)) )
217 push(&stack);
218 stack->data = (gpointer) vik_gps_layer_get_children(VIK_GPS_LAYER(current_layer));
220 else
222 stack->data = (gpointer) ((GList *)stack->data)->next;
223 fprintf ( f, "~EndLayer\n\n" );
224 while ( stack && (!stack->data) )
226 pop(&stack);
227 if ( stack )
229 stack->data = (gpointer) ((GList *)stack->data)->next;
230 fprintf ( f, "~EndLayer\n\n" );
236 get vikaggregatelayer's children (?)
237 foreach write ALL params,
238 then layer data (IF function exists)
239 then endlayer
241 impl:
242 stack of layers (LIST) we are working on
243 when layer->next == NULL ...
244 we move on.
248 static void string_list_delete ( gpointer key, gpointer l, gpointer user_data )
250 /* 20071021 bugfix */
251 GList *iter = (GList *) l;
252 while ( iter ) {
253 g_free ( iter->data );
254 iter = iter->next;
256 g_list_free ( (GList *) l );
259 static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp)
261 VikLayerParamData x;
262 x.sl = list;
263 vik_layer_set_param ( VIK_LAYER(layer_and_vp[0]), i, x, layer_and_vp[1] );
266 static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
268 Stack *stack;
269 struct LatLon ll = { 0.0, 0.0 };
270 gchar buffer[4096];
271 gchar *line;
272 guint16 len;
273 long line_num = 0;
275 VikLayerParam *params = NULL; /* for current layer, so we don't have to keep on looking up interface */
276 guint8 params_count = 0;
278 GHashTable *string_lists = g_hash_table_new(g_direct_hash,g_direct_equal);
280 push(&stack);
281 stack->under = NULL;
282 stack->data = (gpointer) top;
284 while ( fgets ( buffer, 4096, f ) )
286 line_num++;
288 line = buffer;
289 while ( *line == ' ' || *line =='\t' )
290 line++;
292 if ( line[0] == '#' )
293 continue;
296 len = strlen(line);
297 if ( len > 0 && line[len-1] == '\n' )
298 line[--len] = '\0';
299 if ( len > 0 && line[len-1] == '\r' )
300 line[--len] = '\0';
302 if ( len == 0 )
303 continue;
306 if ( line[0] == '~' )
308 line++; len--;
309 if ( *line == '\0' )
310 continue;
311 else if ( str_starts_with ( line, "Layer ", 6, TRUE ) )
313 int parent_type = VIK_LAYER(stack->data)->type;
314 if ( ( ! stack->data ) || ((parent_type != VIK_LAYER_AGGREGATE) && (parent_type != VIK_LAYER_GPS)) )
316 g_warning ( "Line %ld: Layer command inside non-Aggregate Layer (type %d)", line_num, parent_type );
317 push(&stack); /* inside INVALID layer */
318 stack->data = NULL;
319 continue;
321 else
323 gint16 type = layer_type_from_string ( line+6 );
324 push(&stack);
325 if ( type == -1 )
327 g_warning ( "Line %ld: Unknown type %s", line_num, line+6 );
328 stack->data = NULL;
330 else if (parent_type == VIK_LAYER_GPS)
332 stack->data = (gpointer) vik_gps_layer_get_a_child(VIK_GPS_LAYER(stack->under->data));
333 params = vik_layer_get_interface(type)->params;
334 params_count = vik_layer_get_interface(type)->params_count;
336 else
338 stack->data = (gpointer) vik_layer_create ( type, vp, NULL, FALSE );
339 params = vik_layer_get_interface(type)->params;
340 params_count = vik_layer_get_interface(type)->params_count;
344 else if ( str_starts_with ( line, "EndLayer", 8, FALSE ) )
346 if ( stack->under == NULL )
347 g_warning ( "Line %ld: Mismatched ~EndLayer command", line_num );
348 else
350 /* add any string lists we've accumulated */
351 gpointer layer_and_vp[2];
352 layer_and_vp[0] = stack->data;
353 layer_and_vp[1] = vp;
354 g_hash_table_foreach ( string_lists, (GHFunc) string_list_set_param, layer_and_vp );
355 g_hash_table_remove_all ( string_lists );
357 if ( stack->data && stack->under->data )
359 if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_AGGREGATE) {
360 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
361 vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE );
363 else if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_GPS) {
364 /* TODO: anything else needs to be done here ? */
366 else
367 g_warning ( "Line %ld: EndLayer command inside non-Aggregate Layer (type %d)", line_num, VIK_LAYER(stack->data)->type );
369 pop(&stack);
372 else if ( str_starts_with ( line, "LayerData", 9, FALSE ) )
374 if ( stack->data && vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data )
375 vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f );
376 /* must read until hits ~EndLayerData */
378 else
379 { /* simply skip layer data over */
380 while ( fgets ( buffer, 4096, f ) )
382 line_num++;
384 line = buffer;
386 len = strlen(line);
387 if ( len > 0 && line[len-1] == '\n' )
388 line[--len] = '\0';
389 if ( len > 0 && line[len-1] == '\r' )
390 line[--len] = '\0';
391 if ( strcasecmp ( line, "~EndLayerData" ) == 0 )
392 break;
394 continue;
397 else
399 g_warning ( "Line %ld: Unknown tilde command", line_num );
402 else
404 gint32 eq_pos = -1;
405 guint16 i;
406 if ( ! stack->data )
407 continue;
409 for ( i = 0; i < len; i++ )
410 if ( line[i] == '=' )
411 eq_pos = i;
413 if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "xmpp", eq_pos ) == 0) /* "hard coded" params: global & for all layer-types */
414 vik_viewport_set_xmpp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
415 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "ympp", eq_pos ) == 0)
416 vik_viewport_set_ympp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
417 else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lat", eq_pos ) == 0 )
418 ll.lat = strtod ( line+4, NULL );
419 else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lon", eq_pos ) == 0 )
420 ll.lon = strtod ( line+4, NULL );
421 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "utm" ) == 0)
422 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_UTM);
423 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "expedia" ) == 0)
424 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_EXPEDIA );
425 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "google" ) == 0)
426 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_GOOGLE );
427 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "kh" ) == 0)
428 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_KH );
429 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "mercator" ) == 0)
430 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_MERCATOR );
431 else if ( stack->under == NULL && eq_pos == 5 && strncasecmp ( line, "color", eq_pos ) == 0 )
432 vik_viewport_set_background_color ( VIK_VIEWPORT(vp), line+6 );
433 else if ( stack->under == NULL && eq_pos == 9 && strncasecmp ( line, "drawscale", eq_pos ) == 0 )
434 vik_viewport_set_draw_scale ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+10) );
435 else if ( stack->under == NULL && eq_pos == 14 && strncasecmp ( line, "drawcentermark", eq_pos ) == 0 )
436 vik_viewport_set_draw_centermark ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+15) );
437 else if ( stack->under && eq_pos == 4 && strncasecmp ( line, "name", eq_pos ) == 0 )
438 vik_layer_rename ( VIK_LAYER(stack->data), line+5 );
439 else if ( eq_pos == 7 && strncasecmp ( line, "visible", eq_pos ) == 0 )
440 VIK_LAYER(stack->data)->visible = TEST_BOOLEAN(line+8);
441 else if ( eq_pos != -1 && stack->under )
443 gboolean found_match = FALSE;
445 /* go thru layer params. if len == eq_pos && starts_with jazz, set it. */
446 /* also got to check for name and visible. */
448 if ( ! params )
450 g_warning ( "Line %ld: No options for this kind of layer", line_num );
451 continue;
454 for ( i = 0; i < params_count; i++ )
455 if ( strlen(params[i].name) == eq_pos && strncasecmp ( line, params[i].name, eq_pos ) == 0 )
457 VikLayerParamData x;
458 line += eq_pos+1;
459 if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST ) {
460 GList *l = g_list_append ( g_hash_table_lookup ( string_lists, GINT_TO_POINTER ((gint) i) ),
461 g_strdup(line) );
462 g_hash_table_replace ( string_lists, GINT_TO_POINTER ((gint)i), l );
463 /* add the value to a list, possibly making a new list.
464 * this will be passed to the layer when we read an ~EndLayer */
465 } else {
466 switch ( params[i].type )
468 case VIK_LAYER_PARAM_DOUBLE: x.d = strtod(line, NULL); break;
469 case VIK_LAYER_PARAM_UINT: x.u = strtoul(line, NULL, 10); break;
470 case VIK_LAYER_PARAM_INT: x.i = strtol(line, NULL, 10); break;
471 case VIK_LAYER_PARAM_BOOLEAN: x.b = TEST_BOOLEAN(line); break;
472 case VIK_LAYER_PARAM_COLOR: memset(&(x.c), 0, sizeof(x.c)); /* default: black */
473 gdk_color_parse ( line, &(x.c) ); break;
474 /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */
475 default: x.s = line;
477 vik_layer_set_param ( VIK_LAYER(stack->data), i, x, vp );
479 found_match = TRUE;
480 break;
482 if ( ! found_match )
483 g_warning ( "Line %ld: Unknown parameter. Line:\n%s", line_num, line );
485 else
486 g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num );
488 /* could be:
489 [Layer Type=Bla]
490 [EndLayer]
491 [LayerData]
492 name=this
493 #comment
497 while ( stack )
499 if ( stack->under && stack->under->data && stack->data )
501 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
502 vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE );
504 pop(&stack);
507 if ( ll.lat != 0.0 || ll.lon != 0.0 )
508 vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll );
510 if ( ( ! VIK_LAYER(top)->visible ) && VIK_LAYER(top)->realized )
511 vik_treeview_item_set_visible ( VIK_LAYER(top)->vt, &(VIK_LAYER(top)->iter), FALSE );
513 /* delete anything we've forgotten about -- should only happen when file ends before an EndLayer */
514 g_hash_table_foreach ( string_lists, string_list_delete, NULL );
515 g_hash_table_destroy ( string_lists );
519 read thru file
520 if "[Layer Type="
521 push(&stack)
522 new default layer of type (str_to_type) (check interface->name)
523 if "[EndLayer]"
524 VikLayer *vl = stack->data;
525 pop(&stack);
526 vik_aggregate_layer_add_layer(stack->data, vl);
527 if "[LayerData]"
528 vik_layer_data ( VIK_LAYER_DATA(stack->data), f, vp );
532 /* ---------------------------------------------------- */
534 static FILE *xfopen ( const char *fn, const char *mode )
536 if ( strcmp(fn,"-") == 0 )
537 return stdin;
538 else
539 return g_fopen(fn, "r");
542 static void xfclose ( FILE *f )
544 if ( f != stdin && f != stdout ) {
545 fclose ( f );
546 f = NULL;
550 /* 0 on failure, 1 on success (vik file) 2 on success (other file) */
551 gshort a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename )
553 FILE *f = xfopen ( filename, "r" );
555 g_assert ( vp );
557 if ( ! f )
558 return 0;
560 if ( check_magic ( f, VIK_MAGIC ) )
562 file_read ( top, f, vp );
563 if ( f != stdin )
564 xfclose(f);
565 return 1;
567 else
569 VikCoord new_center;
570 VikLayer *vtl = vik_layer_create ( VIK_LAYER_TRW, vp, NULL, FALSE );
571 vik_layer_rename ( vtl, a_file_basename ( filename ) );
573 if ( check_magic ( f, GPX_MAGIC ) )
574 a_gpx_read_file ( VIK_TRW_LAYER(vtl), f );
575 else
576 a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f );
578 vik_layer_post_read ( vtl, vp, TRUE );
580 vik_aggregate_layer_add_layer ( top, vtl );
582 if ( vik_trw_layer_find_center ( VIK_TRW_LAYER(vtl), &new_center ) )
583 vik_viewport_set_center_coord ( VIK_VIEWPORT(vp), &new_center );
584 xfclose(f);
585 return 2;
589 gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filename )
591 FILE *f = g_fopen(filename, "w");
593 if ( ! f )
594 return FALSE;
596 file_write ( top, f, vp );
598 fclose(f);
599 f = NULL;
601 return TRUE;
605 const gchar *a_file_basename ( const gchar *filename )
607 const gchar *t = filename + strlen(filename) - 1;
608 while ( --t > filename )
609 if ( *(t-1) == FILE_SEP )
610 break;
611 if ( t >= filename )
612 return t;
613 return filename;
616 gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, gshort file_type )
618 FILE *f = g_fopen ( filename, "w" );
619 if ( f )
621 if ( file_type == FILE_TYPE_GPSMAPPER )
622 a_gpsmapper_write_file ( vtl, f );
623 else if ( file_type == FILE_TYPE_GPX )
624 a_gpx_write_file ( vtl, f );
625 else
626 a_gpspoint_write_file ( vtl, f );
627 fclose ( f );
628 f = NULL;
629 return TRUE;
631 return FALSE;
634 const gchar *a_get_viking_dir()
636 static gchar *viking_dir = NULL;
638 // TODO: use g_get_user_config_dir ?
640 if (!viking_dir) {
641 const gchar *home = g_getenv("HOME");
642 if (!home || g_access(home, W_OK))
643 home = g_get_home_dir ();
644 #ifdef HAVE_MKDTEMP
645 if (!home || g_access(home, W_OK))
647 static gchar temp[] = {"/tmp/vikXXXXXX"};
648 home = mkdtemp(temp);
650 #endif
651 if (!home || g_access(home, W_OK))
652 /* Fatal error */
653 g_critical("Unable to find a base directory");
655 /* Build the name of the directory */
656 viking_dir = g_build_filename(home, ".viking", NULL);
657 if (g_file_test(viking_dir, G_FILE_TEST_EXISTS) == FALSE)
658 g_mkdir(viking_dir, 0755);
661 return viking_dir;