]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
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 | ||
eb93fa95 GB |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
50a14534 EB |
25 | #include "viking.h" |
26 | ||
bf35388d EB |
27 | #include "gpx.h" |
28 | ||
50a14534 EB |
29 | #include <string.h> |
30 | #include <stdlib.h> | |
d5728c65 GB |
31 | #include <glib.h> |
32 | #include <glib/gstdio.h> | |
50a14534 | 33 | |
cf38e8c7 EB |
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 | |
39 | ||
50a14534 EB |
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" | |
bf35388d | 42 | #define GPX_MAGIC "<?xm" |
50a14534 EB |
43 | #define VIK_MAGIC_LEN 4 |
44 | ||
45 | #ifdef WINDOWS | |
46 | #define FILE_SEP '\\' | |
47 | #else | |
48 | #define FILE_SEP '/' | |
49 | #endif | |
50 | ||
51 | typedef struct _Stack Stack; | |
52 | ||
53 | struct _Stack { | |
54 | Stack *under; | |
55 | gpointer *data; | |
56 | }; | |
57 | ||
58 | static void pop(Stack **stack) { | |
59 | Stack *tmp = (*stack)->under; | |
60 | g_free ( *stack ); | |
61 | *stack = tmp; | |
62 | } | |
63 | ||
64 | static void push(Stack **stack) | |
65 | { | |
66 | Stack *tmp = g_malloc ( sizeof ( Stack ) ); | |
67 | tmp->under = *stack; | |
68 | *stack = tmp; | |
69 | } | |
70 | ||
bf35388d | 71 | static gboolean check_magic ( FILE *f, const gchar *magic_number ) |
50a14534 EB |
72 | { |
73 | gchar magic[VIK_MAGIC_LEN]; | |
74 | gboolean rv = FALSE; | |
75 | gint8 i; | |
76 | if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) && | |
bf35388d | 77 | strncmp(magic, magic_number, sizeof(magic)) == 0 ) |
50a14534 EB |
78 | rv = TRUE; |
79 | for ( i = sizeof(magic)-1; i >= 0; i-- ) /* the ol' pushback */ | |
80 | ungetc(magic[i],f); | |
81 | return rv; | |
82 | } | |
83 | ||
84 | ||
85 | static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, guint16 len_needle, gboolean must_be_longer ) | |
86 | { | |
87 | if ( strlen(haystack) > len_needle - (!must_be_longer) && strncasecmp ( haystack, needle, len_needle ) == 0 ) | |
88 | return TRUE; | |
89 | return FALSE; | |
90 | } | |
91 | ||
92 | static guint16 layer_type_from_string ( const gchar *str ) | |
93 | { | |
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; | |
99 | } | |
100 | ||
101 | static void write_layer_params_and_data ( VikLayer *l, FILE *f ) | |
102 | { | |
103 | VikLayerParam *params = vik_layer_get_interface(l->type)->params; | |
104 | VikLayerFuncGetParam get_param = vik_layer_get_interface(l->type)->get_param; | |
105 | ||
e9d005c9 | 106 | fprintf ( f, "name=%s\n", l->name ? l->name : "" ); |
50a14534 EB |
107 | if ( !l->visible ) |
108 | fprintf ( f, "visible=f\n" ); | |
109 | ||
110 | if ( params && get_param ) | |
111 | { | |
112 | VikLayerParamData data; | |
113 | guint16 i, params_count = vik_layer_get_interface(l->type)->params_count; | |
114 | for ( i = 0; i < params_count; i++ ) | |
115 | { | |
116 | data = get_param(l,i); | |
ad0a8c2d EB |
117 | |
118 | /* string lists are handled differently. We get a GList (that shouldn't | |
119 | * be freed) back for get_param and if it is null we shouldn't write | |
120 | * anything at all (otherwise we'd read in a list with an empty string, | |
121 |