Use GtkBuilder to convert string to expected value's GType
[viking/guyou.git] / src / file.c
blob3c30c30f218c7a794923bd6e0f9edb2f3b7e36fa
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>
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 ); }
43 #endif
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
50 #ifdef WINDOWS
51 #define FILE_SEP '\\'
52 #else
53 #define FILE_SEP '/'
54 #endif
56 typedef struct _Stack Stack;
58 struct _Stack {
59 Stack *under;
60 gpointer *data;
63 static void pop(Stack **stack) {
64 Stack *tmp = (*stack)->under;
65 g_free ( *stack );
66 *stack = tmp;
69 static void push(Stack **stack)
71 Stack *tmp = g_malloc ( sizeof ( Stack ) );
72 tmp->under = *stack;
73 *stack = tmp;
76 static gboolean check_magic ( FILE *f, const gchar *magic_number )
78 gchar magic[VIK_MAGIC_LEN];
79 gboolean rv = FALSE;
80 gint8 i;
81 if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) &&
82 strncmp(magic, magic_number, sizeof(magic)) == 0 )
83 rv = TRUE;
84 for ( i = sizeof(magic)-1; i >= 0; i-- ) /* the ol' pushback */
85 ungetc(magic[i],f);
86 return rv;
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 )
93 return TRUE;
94 return FALSE;
97 static guint16 layer_type_from_string ( const gchar *str )
99 guint8 i;
100 for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
101 if ( strcasecmp ( str, vik_layer_get_interface(i)->name ) == 0 )
102 return i;
103 return -1;
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 ) {
113 if ( data.sl ) {
114 GList *iter = (GList *)data.sl;
115 while ( iter ) {
116 fprintf ( f, "%s=", name );
117 fprintf ( f, "%s\n", (gchar *)(iter->data) );
118 iter = iter->next;
121 } else {
122 fprintf ( f, "%s=", name );
123 switch ( type )
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 );
129 break;
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 : "" );
146 if ( !l->visible )
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" );
165 /* foreach param:
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 )
173 Stack *stack = NULL;
174 VikLayer *current_layer;
175 struct LatLon ll;
176 VikViewportDrawMode mode;
177 gchar *modestring;
179 push(&stack);
180 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top));
181 stack->under = NULL;
183 /* crazhy CRAZHY */
184 vik_coord_to_latlon ( vik_viewport_get_center ( VIK_VIEWPORT(vp) ), &ll );
186 mode = vik_viewport_get_drawmode ( VIK_VIEWPORT(vp) );
187 switch ( mode ) {
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;
191 default:
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)) )
211 push(&stack);
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)) )
216 push(&stack);
217 stack->data = (gpointer) vik_gps_layer_get_children(VIK_GPS_LAYER(current_layer));
219 else
221 stack->data = (gpointer) ((GList *)stack->data)->next;
222 fprintf ( f, "~EndLayer\n\n" );
223 while ( stack && (!stack->data) )
225 pop(&stack);
226 if ( stack )
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)
238 then endlayer
240 impl:
241 stack of layers (LIST) we are working on
242 when layer->next == NULL ...
243 we move on.
247 static void string_list_delete ( gpointer key, gpointer l, gpointer user_data )
249 /* 20071021 bugfix */
250 GList *iter = (GList *) l;
251 while ( iter ) {
252 g_free ( iter->data );
253 iter = iter->next;
255 g_list_free ( (GList *) l );
258 static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp)
260 VikLayerParamData x;
261 x.sl = list;
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 )
267 Stack *stack;
268 struct LatLon ll = { 0.0, 0.0 };
269 gchar buffer[4096];
270 gchar *line;
271 guint16 len;
272 long line_num = 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);
279 push(&stack);
280 stack->under = NULL;
281 stack->data = (gpointer) top;
283 while ( fgets ( buffer, 4096, f ) )
285 line_num++;
287 line = buffer;
288 while ( *line == ' ' || *line =='\t' )
289 line++;
291 if ( line[0] == '#' )
292 continue;
295 len = strlen(line);
296 if ( len > 0 && line[len-1] == '\n' )
297 line[--len] = '\0';
298 if ( len > 0 && line[len-1] == '\r' )
299 line[--len] = '\0';
301 if ( len == 0 )
302 continue;
305 if ( line[0] == '~' )
307 line++; len--;
308 if ( *line == '\0' )
309 continue;
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 */
317 stack->data = NULL;
318 continue;
320 else
322 gint16 type = layer_type_from_string ( line+6 );
323 push(&stack);
324 if ( type == -1 )
326 g_warning ( "Line %ld: Unknown type %s", line_num, line+6 );
327 stack->data = NULL;
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;
335 else
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 );
347 else
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 ? */
365 else
366 g_warning ( "Line %ld: EndLayer command inside non-Aggregate Layer (type %d)", line_num, VIK_LAYER(stack->data)->type );
368 pop(&stack);
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 */
377 else
378 { /* simply skip layer data over */
379 while ( fgets ( buffer, 4096, f ) )
381 line_num++;
383 line = buffer;
385 len = strlen(line);
386 if ( len > 0 && line[len-1] == '\n' )
387 line[--len] = '\0';
388 if ( len > 0 && line[len-1] == '\r' )
389 line[--len] = '\0';
390 if ( strcasecmp ( line, "~EndLayerData" ) == 0 )
391 break;
393 continue;
396 else
398 g_warning ( "Line %ld: Unknown tilde command", line_num );
401 else
403 gint32 eq_pos = -1;
404 guint16 i;
405 if ( ! stack->data )
406 continue;
408 for ( i = 0; i < len; i++ )
409 if ( line[i] == '=' )
410 eq_pos = 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. */
451 if ( ! params )
453 g_warning ( "Line %ld: No options for this kind of layer", line_num );
454 continue;
457 for ( i = 0; i < params_count; i++ )
458 if ( strlen(params[i].name) == eq_pos && strncasecmp ( line, params[i].name, eq_pos ) == 0 )
460 VikLayerParamData x;
461 line += eq_pos+1;
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) ),
464 g_strdup(line) );
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 */
468 } else {
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 */
478 default: x.s = line;
480 vik_layer_set_param ( VIK_LAYER(stack->data), i, x, vp );
482 found_match = TRUE;
483 break;
485 if ( ! found_match )
486 g_warning ( "Line %ld: Unknown parameter. Line:\n%s", line_num, line );
488 else
489 g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num );
491 /* could be:
492 [Layer Type=Bla]
493 [EndLayer]
494 [LayerData]
495 name=this
496 #comment
500 while ( stack )
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 );
507 pop(&stack);
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 );
522 read thru file
523 if "[Layer Type="
524 push(&stack)
525 new default layer of type (str_to_type) (check interface->name)
526 if "[EndLayer]"
527 VikLayer *vl = stack->data;
528 pop(&stack);
529 vik_aggregate_layer_add_layer(stack->data, vl);
530 if "[LayerData]"
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 )
540 return stdin;
541 else
542 return g_fopen(fn, "r");
545 static void xfclose ( FILE *f )
547 if ( f != stdin && f != stdout ) {
548 fclose ( f );
549 f = NULL;
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" );
558 g_assert ( vp );
560 if ( ! f )
561 return 0;
563 if ( check_magic ( f, VIK_MAGIC ) )
565 file_read ( top, f, vp );
566 if ( f != stdin )
567 xfclose(f);
568 return 1;
570 else
572 VikCoord new_center;
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 );
578 else
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 );
587 xfclose(f);
588 return 2;
592 gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filename )
594 FILE *f = g_fopen(filename, "w");
596 if ( ! f )
597 return FALSE;
599 file_write ( top, f, vp );
601 fclose(f);
602 f = NULL;
604 return TRUE;
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 )
613 break;
614 if ( t >= filename )
615 return t;
616 return filename;
619 gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, gshort file_type )
621 FILE *f = g_fopen ( filename, "w" );
622 if ( f )
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 );
628 else
629 a_gpspoint_write_file ( vtl, f );
630 fclose ( f );
631 f = NULL;
632 return TRUE;
634 return FALSE;
637 const gchar *a_get_viking_dir()
639 static gchar *viking_dir = NULL;
641 // TODO: use g_get_user_config_dir ?
643 if (!viking_dir) {
644 const gchar *home = g_getenv("HOME");
645 if (!home || g_access(home, W_OK))
646 home = g_get_home_dir ();
647 #ifdef HAVE_MKDTEMP
648 if (!home || g_access(home, W_OK))
650 static gchar temp[] = {"/tmp/vikXXXXXX"};
651 home = mkdtemp(temp);
653 #endif
654 if (!home || g_access(home, W_OK))
655 /* Fatal error */
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);
664 return viking_dir;