Marking translatable string in main.c
[viking.git] / src / file.c
blobd7cc89d4c0802b5dc78379df0dcb492bcc4ecb4e
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 <glib.h>
32 #include <glib/gstdio.h>
34 /* Relax some dependencies */
35 #if ! GLIB_CHECK_VERSION(2,12,0)
36 static gboolean return_true (gpointer a, gpointer b, gpointer c) { return TRUE; }
37 static g_hash_table_remove_all (GHashTable *ght) { g_hash_table_foreach_remove ( ght, (GHRFunc) return_true, FALSE ); }
38 #endif
40 #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
41 #define VIK_MAGIC "#VIK"
42 #define GPX_MAGIC "<?xm"
43 #define VIK_MAGIC_LEN 4
45 #ifdef WINDOWS
46 #define FILE_SEP '\\'
47 #else
48 #define FILE_SEP '/'
49 #endif
51 typedef struct _Stack Stack;
53 struct _Stack {
54 Stack *under;
55 gpointer *data;
58 static void pop(Stack **stack) {
59 Stack *tmp = (*stack)->under;
60 g_free ( *stack );
61 *stack = tmp;
64 static void push(Stack **stack)
66 Stack *tmp = g_malloc ( sizeof ( Stack ) );
67 tmp->under = *stack;
68 *stack = tmp;
71 static gboolean check_magic ( FILE *f, const gchar *magic_number )
73 gchar magic[VIK_MAGIC_LEN];
74 gboolean rv = FALSE;
75 gint8 i;
76 if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) &&
77 strncmp(magic, magic_number, sizeof(magic)) == 0 )
78 rv = TRUE;
79 for ( i = sizeof(magic)-1; i >= 0; i-- ) /* the ol' pushback */
80 ungetc(magic[i],f);
81 return rv;
85 static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, guint16 len_needle, gboolean must_be_longer )
87 if ( strlen(haystack) > len_needle - (!must_be_longer) && strncasecmp ( haystack, needle, len_needle ) == 0 )
88 return TRUE;
89 return FALSE;
92 static guint16 layer_type_from_string ( const gchar *str )
94 guint8 i;
95 for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
96 if ( strcasecmp ( str, vik_layer_get_interface(i)->name ) == 0 )
97 return i;
98 return -1;
101 void file_write_layer_param ( FILE *f, const gchar *name, guint8 type, VikLayerParamData data ) {
102 /* string lists are handled differently. We get a GList (that shouldn't
103 * be freed) back for get_param and if it is null we shouldn't write
104 * anything at all (otherwise we'd read in a list with an empty string,
105 * not an empty string list.
107 if ( type == VIK_LAYER_PARAM_STRING_LIST ) {
108 if ( data.sl ) {
109 GList *iter = (GList *)data.sl;
110 while ( iter ) {
111 fprintf ( f, "%s=", name );
112 fprintf ( f, "%s\n", (gchar *)(iter->data) );
113 iter = iter->next;
116 } else {
117 fprintf ( f, "%s=", name );
118 switch ( type )
120 case VIK_LAYER_PARAM_DOUBLE: {
121 // char buf[15]; /* locale independent */
122 // fprintf ( f, "%s\n", (char *) g_dtostr (data.d, buf, sizeof (buf)) ); break;
123 fprintf ( f, "%f\n", data.d );
124 break;
126 case VIK_LAYER_PARAM_UINT: fprintf ( f, "%d\n", data.u ); break;
127 case VIK_LAYER_PARAM_INT: fprintf ( f, "%d\n", data.i ); break;
128 case VIK_LAYER_PARAM_BOOLEAN: fprintf ( f, "%c\n", data.b ? 't' : 'f' ); break;
129 case VIK_LAYER_PARAM_STRING: fprintf ( f, "%s\n", data.s ); break;
130 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;
135 static void write_layer_params_and_data ( VikLayer *l, FILE *f )
137 VikLayerParam *params = vik_layer_get_interface(l->type)->params;
138 VikLayerFuncGetParam get_param = vik_layer_get_interface(l->type)->get_param;
140 fprintf ( f, "name=%s\n", l->name ? l->name : "" );
141 if ( !l->visible )
142 fprintf ( f, "visible=f\n" );
144 if ( params && get_param )
146 VikLayerParamData data;
147 guint16 i, params_count = vik_layer_get_interface(l->type)->params_count;
148 for ( i = 0; i < params_count; i++ )
150 data = get_param(l,i);
151 file_write_layer_param(f, params[i].name, params[i].type, data);
154 if ( vik_layer_get_interface(l->type)->write_file_data )
156 fprintf ( f, "\n\n~LayerData\n" );
157 vik_layer_get_interface(l->type)->write_file_data ( l, f );
158 fprintf ( f, "~EndLayerData\n" );
160 /* foreach param:
161 write param, and get_value, etc.
162 then run layer data, and that's it.
166 static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp )
168 Stack *stack = NULL;
169 VikLayer *current_layer;
170 struct LatLon ll;
171 gchar *modestring;
173 push(&stack);
174 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top));
175 stack->under = NULL;
177 /* crazhy CRAZHY */
178 vik_coord_to_latlon ( vik_viewport_get_center ( VIK_VIEWPORT(vp) ), &ll );
180 switch ( vik_viewport_get_drawmode ( VIK_VIEWPORT(vp) ) ) {
181 case VIK_VIEWPORT_DRAWMODE_UTM: modestring = "utm"; break;
182 case VIK_VIEWPORT_DRAWMODE_EXPEDIA: modestring = "expedia"; break;
183 case VIK_VIEWPORT_DRAWMODE_GOOGLE: modestring = "google"; break;
184 case VIK_VIEWPORT_DRAWMODE_KH: modestring = "kh"; break;
185 default: modestring = "mercator";
188 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",
189 vik_viewport_get_xmpp ( VIK_VIEWPORT(vp) ), vik_viewport_get_ympp ( VIK_VIEWPORT(vp) ), ll.lat, ll.lon,
190 modestring, vik_viewport_get_background_color(VIK_VIEWPORT(vp)),
191 vik_viewport_get_draw_scale(VIK_VIEWPORT(vp)) ? "t" : "f",
192 vik_viewport_get_draw_centermark(VIK_VIEWPORT(vp)) ? "t" : "f" );
194 if ( ! VIK_LAYER(top)->visible )
195 fprintf ( f, "visible=f\n" );
197 while (stack && stack->data)
199 current_layer = VIK_LAYER(((GList *)stack->data)->data);
200 fprintf ( f, "\n~Layer %s\n", vik_layer_get_interface(current_layer->type)->name );
201 write_layer_params_and_data ( current_layer, f );
202 if ( current_layer->type == VIK_LAYER_AGGREGATE && !vik_aggregate_layer_is_empty(VIK_AGGREGATE_LAYER(current_layer)) )
204 push(&stack);
205 stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(current_layer));
207 else if ( current_layer->type == VIK_LAYER_GPS && !vik_gps_layer_is_empty(VIK_GPS_LAYER(current_layer)) )
209 push(&stack);
210 stack->data = (gpointer) vik_gps_layer_get_children(VIK_GPS_LAYER(current_layer));
212 else
214 stack->data = (gpointer) ((GList *)stack->data)->next;
215 fprintf ( f, "~EndLayer\n\n" );
216 while ( stack && (!stack->data) )
218 pop(&stack);
219 if ( stack )
221 stack->data = (gpointer) ((GList *)stack->data)->next;
222 fprintf ( f, "~EndLayer\n\n" );
228 get vikaggregatelayer's children (?)
229 foreach write ALL params,
230 then layer data (IF function exists)
231 then endlayer
233 impl:
234 stack of layers (LIST) we are working on
235 when layer->next == NULL ...
236 we move on.
240 static void string_list_delete ( gpointer key, gpointer l, gpointer user_data )
242 /* 20071021 bugfix */
243 GList *iter = (GList *) l;
244 while ( iter ) {
245 g_free ( iter->data );
246 iter = iter->next;
248 g_list_free ( (GList *) l );
251 static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp)
253 VikLayerParamData x;
254 x.sl = list;
255 vik_layer_set_param ( VIK_LAYER(layer_and_vp[0]), i, x, layer_and_vp[1] );
258 static void file_read ( VikAggregateLayer *top, FILE *f, VikViewport *vp )
260 Stack *stack;
261 struct LatLon ll = { 0.0, 0.0 };
262 gchar buffer[4096];
263 gchar *line;
264 guint16 len;
265 long line_num = 0;
267 VikLayerParam *params = NULL; /* for current layer, so we don't have to keep on looking up interface */
268 guint8 params_count = 0;
270 GHashTable *string_lists = g_hash_table_new(g_direct_hash,g_direct_equal);
272 push(&stack);
273 stack->under = NULL;
274 stack->data = (gpointer) top;
276 while ( fgets ( buffer, 4096, f ) )
278 line_num++;
280 line = buffer;
281 while ( *line == ' ' || *line =='\t' )
282 line++;
284 if ( line[0] == '#' )
285 continue;
288 len = strlen(line);
289 if ( len > 0 && line[len-1] == '\n' )
290 line[--len] = '\0';
291 if ( len > 0 && line[len-1] == '\r' )
292 line[--len] = '\0';
294 if ( len == 0 )
295 continue;
298 if ( line[0] == '~' )
300 line++; len--;
301 if ( *line == '\0' )
302 continue;
303 else if ( str_starts_with ( line, "Layer ", 6, TRUE ) )
305 int parent_type = VIK_LAYER(stack->data)->type;
306 if ( ( ! stack->data ) || ((parent_type != VIK_LAYER_AGGREGATE) && (parent_type != VIK_LAYER_GPS)) )
308 g_warning ( "Line %ld: Layer command inside non-Aggregate Layer (type %d)", line_num, parent_type );
309 push(&stack); /* inside INVALID layer */
310 stack->data = NULL;
311 continue;
313 else
315 gint16 type = layer_type_from_string ( line+6 );
316 push(&stack);
317 if ( type == -1 )
319 g_warning ( "Line %ld: Unknown type %s\n", line_num, line+6 );
320 stack->data = NULL;
322 else if (parent_type == VIK_LAYER_GPS)
324 stack->data = (gpointer) vik_gps_layer_get_a_child(VIK_GPS_LAYER(stack->under->data));
325 params = vik_layer_get_interface(type)->params;
326 params_count = vik_layer_get_interface(type)->params_count;
328 else
330 stack->data = (gpointer) vik_layer_create ( type, vp, NULL, FALSE );
331 params = vik_layer_get_interface(type)->params;
332 params_count = vik_layer_get_interface(type)->params_count;
336 else if ( str_starts_with ( line, "EndLayer", 8, FALSE ) )
338 if ( stack->under == NULL )
339 g_warning ( "Line %ld: Mismatched ~EndLayer command", line_num );
340 else
342 /* add any string lists we've accumulated */
343 gpointer layer_and_vp[2];
344 layer_and_vp[0] = stack->data;
345 layer_and_vp[1] = vp;
346 g_hash_table_foreach ( string_lists, (GHFunc) string_list_set_param, layer_and_vp );
347 g_hash_table_remove_all ( string_lists );
349 if ( stack->data && stack->under->data )
351 if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_AGGREGATE) {
352 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
353 vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE );
355 else if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_GPS) {
356 /* TODO: anything else needs to be done here ? */
358 else
359 g_warning ( "Line %ld: EndLayer command inside non-Aggregate Layer (type %d)", line_num, VIK_LAYER(stack->data)->type );
361 pop(&stack);
364 else if ( str_starts_with ( line, "LayerData", 9, FALSE ) )
366 if ( stack->data && vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data )
367 vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f );
368 /* must read until hits ~EndLayerData */
370 else
371 { /* simply skip layer data over */
372 while ( fgets ( buffer, 4096, f ) )
374 line_num++;
376 line = buffer;
378 len = strlen(line);
379 if ( len > 0 && line[len-1] == '\n' )
380 line[--len] = '\0';
381 if ( len > 0 && line[len-1] == '\r' )
382 line[--len] = '\0';
383 if ( strcasecmp ( line, "~EndLayerData" ) == 0 )
384 break;
386 continue;
389 else
391 g_warning ( "Line %ld: Unknown tilde command", line_num );
394 else
396 gint32 eq_pos = -1;
397 guint16 i;
398 if ( ! stack->data )
399 continue;
401 for ( i = 0; i < len; i++ )
402 if ( line[i] == '=' )
403 eq_pos = i;
405 if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "xmpp", eq_pos ) == 0) /* "hard coded" params: global & for all layer-types */
406 vik_viewport_set_xmpp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
407 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "ympp", eq_pos ) == 0)
408 vik_viewport_set_ympp ( VIK_VIEWPORT(vp), strtod ( line+5, NULL ) );
409 else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lat", eq_pos ) == 0 )
410 ll.lat = strtod ( line+4, NULL );
411 else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lon", eq_pos ) == 0 )
412 ll.lon = strtod ( line+4, NULL );
413 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "utm" ) == 0)
414 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_UTM);
415 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "expedia" ) == 0)
416 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_EXPEDIA );
417 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "google" ) == 0)
418 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_GOOGLE );
419 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "kh" ) == 0)
420 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_KH );
421 else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "mercator" ) == 0)
422 vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_MERCATOR );
423 else if ( stack->under == NULL && eq_pos == 5 && strncasecmp ( line, "color", eq_pos ) == 0 )
424 vik_viewport_set_background_color ( VIK_VIEWPORT(vp), line+6 );
425 else if ( stack->under == NULL && eq_pos == 9 && strncasecmp ( line, "drawscale", eq_pos ) == 0 )
426 vik_viewport_set_draw_scale ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+10) );
427 else if ( stack->under == NULL && eq_pos == 14 && strncasecmp ( line, "drawcentermark", eq_pos ) == 0 )
428 vik_viewport_set_draw_centermark ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+15) );
429 else if ( stack->under && eq_pos == 4 && strncasecmp ( line, "name", eq_pos ) == 0 )
430 vik_layer_rename ( VIK_LAYER(stack->data), line+5 );
431 else if ( eq_pos == 7 && strncasecmp ( line, "visible", eq_pos ) == 0 )
432 VIK_LAYER(stack->data)->visible = TEST_BOOLEAN(line+8);
433 else if ( eq_pos != -1 && stack->under )
435 gboolean found_match = FALSE;
437 /* go thru layer params. if len == eq_pos && starts_with jazz, set it. */
438 /* also got to check for name and visible. */
440 if ( ! params )
442 g_warning ( "Line %ld: No options for this kind of layer", line_num );
443 continue;
446 for ( i = 0; i < params_count; i++ )
447 if ( strlen(params[i].name) == eq_pos && strncasecmp ( line, params[i].name, eq_pos ) == 0 )
449 VikLayerParamData x;
450 line += eq_pos+1;
451 if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST ) {
452 GList *l = g_list_append ( g_hash_table_lookup ( string_lists, (gpointer) ((gint) i) ), g_strdup(line) );
453 g_hash_table_replace ( string_lists, (gpointer) ((gint)i), l );
454 /* add the value to a list, possibly making a new list.
455 * this will be passed to the layer when we read an ~EndLayer */
456 } else {
457 switch ( params[i].type )
459 case VIK_LAYER_PARAM_DOUBLE: x.d = strtod(line, NULL); break;
460 case VIK_LAYER_PARAM_UINT: x.u = strtoul(line, NULL, 10); break;
461 case VIK_LAYER_PARAM_INT: x.i = strtol(line, NULL, 10); break;
462 case VIK_LAYER_PARAM_BOOLEAN: x.b = TEST_BOOLEAN(line); break;
463 case VIK_LAYER_PARAM_COLOR: memset(&(x.c), 0, sizeof(x.c)); /* default: black */
464 gdk_color_parse ( line, &(x.c) ); break;
465 /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */
466 default: x.s = line;
468 vik_layer_set_param ( VIK_LAYER(stack->data), i, x, vp );
470 found_match = TRUE;
471 break;
473 if ( ! found_match )
474 g_warning ( "Line %ld: Unknown parameter. Line:\n%s", line_num, line );
476 else
477 g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num );
479 /* could be:
480 [Layer Type=Bla]
481 [EndLayer]
482 [LayerData]
483 name=this
484 #comment
488 while ( stack )
490 if ( stack->under && stack->under->data && stack->data )
492 vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data) );
493 vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE );
495 pop(&stack);
498 if ( ll.lat != 0.0 || ll.lon != 0.0 )
499 vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll );
501 if ( ( ! VIK_LAYER(top)->visible ) && VIK_LAYER(top)->realized )
502 vik_treeview_item_set_visible ( VIK_LAYER(top)->vt, &(VIK_LAYER(top)->iter), FALSE );
504 /* delete anything we've forgotten about -- should only happen when file ends before an EndLayer */
505 g_hash_table_foreach ( string_lists, string_list_delete, NULL );
506 g_hash_table_destroy ( string_lists );
510 read thru file
511 if "[Layer Type="
512 push(&stack)
513 new default layer of type (str_to_type) (check interface->name)
514 if "[EndLayer]"
515 VikLayer *vl = stack->data;
516 pop(&stack);
517 vik_aggregate_layer_add_layer(stack->data, vl);
518 if "[LayerData]"
519 vik_layer_data ( VIK_LAYER_DATA(stack->data), f, vp );
523 /* ---------------------------------------------------- */
525 static FILE *xfopen ( const char *fn, const char *mode )
527 if ( strcmp(fn,"-") == 0 )
528 return stdin;
529 else
530 return fopen(fn, "r");
533 static void xfclose ( FILE *f )
535 if ( f != stdin && f != stdout )
536 fclose ( f );
539 /* 0 on failure, 1 on success (vik file) 2 on success (other file) */
540 gshort a_file_load ( VikAggregateLayer *top, VikViewport *vp, const gchar *filename )
542 FILE *f = xfopen ( filename, "r" );
544 g_assert ( vp );
546 if ( ! f )
547 return 0;
549 if ( check_magic ( f, VIK_MAGIC ) )
551 file_read ( top, f, vp );
552 if ( f != stdin )
553 xfclose(f);
554 return 1;
556 else
558 VikCoord new_center;
559 VikLayer *vtl = vik_layer_create ( VIK_LAYER_TRW, vp, NULL, FALSE );
560 vik_layer_rename ( vtl, a_file_basename ( filename ) );
562 if ( check_magic ( f, GPX_MAGIC ) )
563 a_gpx_read_file ( VIK_TRW_LAYER(vtl), f );
564 else
565 a_gpspoint_read_file ( VIK_TRW_LAYER(vtl), f );
567 vik_layer_post_read ( vtl, vp, TRUE );
569 vik_aggregate_layer_add_layer ( top, vtl );
571 if ( vik_trw_layer_find_center ( VIK_TRW_LAYER(vtl), &new_center ) )
572 vik_viewport_set_center_coord ( VIK_VIEWPORT(vp), &new_center );
573 xfclose(f);
574 return 2;
578 gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filename )
580 FILE *f = fopen(filename, "w");
582 if ( ! f )
583 return FALSE;
585 file_write ( top, f, vp );
587 fclose(f);
589 return TRUE;
593 const gchar *a_file_basename ( const gchar *filename )
595 const gchar *t = filename + strlen(filename) - 1;
596 while ( --t > filename )
597 if ( *(t-1) == FILE_SEP )
598 break;
599 if ( t >= filename )
600 return t;
601 return filename;
604 gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, gshort file_type )
606 FILE *f = fopen ( filename, "w" );
607 if ( f )
609 if ( file_type == FILE_TYPE_GPSMAPPER )
610 a_gpsmapper_write_file ( vtl, f );
611 else if ( file_type == FILE_TYPE_GPX )
612 a_gpx_write_file ( vtl, f );
613 else
614 a_gpspoint_write_file ( vtl, f );
615 fclose ( f );
616 return TRUE;
618 return FALSE;
621 const gchar *a_get_viking_dir()
623 static gchar *viking_dir = NULL;
625 // TODO: use g_get_user_config_dir ?
627 if (!viking_dir) {
628 const gchar *home = g_getenv("HOME");
629 if (!home || access(home, W_OK))
630 home = g_get_home_dir ();
631 #ifdef HAVE_MKDTEMP
632 if (!home || access(home, W_OK))
634 static gchar temp[] = {"/tmp/vikXXXXXX"};
635 home = mkdtemp(temp);
637 #endif
638 if (!home || access(home, W_OK))
639 /* Fatal error */
640 g_critical("Unable to find a base directory");
642 /* Build the name of the directory */
643 viking_dir = g_build_filename(home, ".viking", NULL);
644 if (access(viking_dir, F_OK))
645 g_mkdir(viking_dir, 0755);
648 return viking_dir;