]> git.street.me.uk Git - andy/viking.git/blame_incremental - src/file.c
Cursors!
[andy/viking.git] / src / file.c
... / ...
CommitLineData
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25#include "viking.h"
26
27#include "gpx.h"
28
29#include <string.h>
30#include <stdlib.h>
31#include <glib.h>
32#include <glib/gstdio.h>
33
34#define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') )
35#define VIK_MAGIC "#VIK"
36#define GPX_MAGIC "<?xm"
37#define VIK_MAGIC_LEN 4
38
39#ifdef WINDOWS
40#define FILE_SEP '\\'
41#else
42#define FILE_SEP '/'
43#endif
44
45typedef struct _Stack Stack;
46
47struct _Stack {
48 Stack *under;
49 gpointer *data;
50};
51
52static void pop(Stack **stack) {
53 Stack *tmp = (*stack)->under;
54 g_free ( *stack );
55 *stack = tmp;
56}
57
58static void push(Stack **stack)
59{
60 Stack *tmp = g_malloc ( sizeof ( Stack ) );
61 tmp->under = *stack;
62 *stack = tmp;
63}
64
65static gboolean check_magic ( FILE *f, const gchar *magic_number )
66{
67 gchar magic[VIK_MAGIC_LEN];
68 gboolean rv = FALSE;
69 gint8 i;
70 if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) &&
71 strncmp(magic, magic_number, sizeof(magic)) == 0 )
72 rv = TRUE;
73 for ( i = sizeof(magic)-1; i >= 0; i-- ) /* the ol' pushback */
74 ungetc(magic[i],f);
75 return rv;
76}
77
78
79static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, guint16 len_needle, gboolean must_be_longer )
80{
81 if ( strlen(haystack) > len_needle - (!must_be_longer) && strncasecmp ( haystack, needle, len_needle ) == 0 )
82 return TRUE;
83 return FALSE;
84}
85
86static guint16 layer_type_from_string ( const gchar *str )
87{
88 guint8 i;
89 for ( i = 0; i < VIK_LAYER_NUM_TYPES; i++ )
90 if ( strcasecmp ( str, vik_layer_get_interface(i)->name ) == 0 )
91 return i;
92 return -1;
93}
94
95static void write_layer_params_and_data ( VikLayer *l, FILE *f )
96{
97 VikLayerParam *params = vik_layer_get_interface(l->type)->params;
98 VikLayerFuncGetParam get_param = vik_layer_get_interface(l->type)->get_param;
99
100 fprintf ( f, "name=%s\n", l->name ? l->name : "" );
101 if ( !l->visible )
102 fprintf ( f, "visible=f\n" );
103
104 if ( params && get_param )
105 {
106 VikLayerParamData data;
107 guint16 i, params_count = vik_layer_get_interface(l->type)->params_count;
108 for ( i = 0; i < params_count; i++ )
109 {
110 data = get_param(l,i);
111
112 /* string lists are handled differently. We get a GList (that shouldn't
113 * be freed) back for get_param and if it is null we shouldn't write
114 * anything at all (otherwise we'd read in a list with an empty string,
115