]>
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> | |
29f1598c | 5 | * Copyright (C) 2012, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> |
1b14d0d2 | 6 | * Copyright (C) 2012-2013, Rob Norris <rw_norris@hotmail.com> |
50a14534 EB |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | */ | |
23 | ||
eb93fa95 GB |
24 | #ifdef HAVE_CONFIG_H |
25 | #include "config.h" | |
26 | #endif | |
50a14534 EB |
27 | #include "viking.h" |
28 | ||
5bed0ef6 | 29 | #include "jpg.h" |
bf35388d | 30 | #include "gpx.h" |
1ca082d7 | 31 | #include "geojson.h" |
ba9d0a00 | 32 | #include "babel.h" |
113c9b1b | 33 | #include "gpsmapper.h" |
bf35388d | 34 | |
50a14534 EB |
35 | #include <string.h> |
36 | #include <stdlib.h> | |
8c060406 | 37 | #include <stdio.h> |
45acf79e MA |
38 | #ifdef HAVE_UNISTD_H |
39 | #include <unistd.h> | |
40 | #endif | |
0925261e RN |
41 | #ifdef WINDOWS |
42 | #define realpath(X,Y) _fullpath(Y,X,MAX_PATH) | |
43 | #endif | |
d5728c65 GB |
44 | #include <glib.h> |
45 | #include <glib/gstdio.h> | |
fd98b156 | 46 | #include <glib/gi18n.h> |
50a14534 | 47 | |
694b6d08 | 48 | #include "file.h" |
81687a73 | 49 | #include "misc/strtod.h" |
694b6d08 | 50 | |
50a14534 EB |
51 | #define TEST_BOOLEAN(str) (! ((str)[0] == '\0' || (str)[0] == '0' || (str)[0] == 'n' || (str)[0] == 'N' || (str)[0] == 'f' || (str)[0] == 'F') ) |
52 | #define VIK_MAGIC "#VIK" | |
bf35388d | 53 | #define GPX_MAGIC "<?xm" |
50a14534 | 54 | #define VIK_MAGIC_LEN 4 |
f8ecf9c2 | 55 | #define GPX_MAGIC_LEN 4 |
50a14534 | 56 | |
e087b0d9 RN |
57 | #define VIKING_FILE_VERSION 1 |
58 | ||
50a14534 EB |
59 | typedef struct _Stack Stack; |
60 | ||
61 | struct _Stack { | |
62 | Stack *under; | |
ab8c8dbf | 63 | gpointer data; |
50a14534 EB |
64 | }; |
65 | ||
66 | static void pop(Stack **stack) { | |
67 | Stack *tmp = (*stack)->under; | |
68 | g_free ( *stack ); | |
69 | *stack = tmp; | |
70 | } | |
71 | ||
72 | static void push(Stack **stack) | |
73 | { | |
74 | Stack *tmp = g_malloc ( sizeof ( Stack ) ); | |
75 | tmp->under = *stack; | |
76 | *stack = tmp; | |
77 | } | |
78 | ||
f8ecf9c2 | 79 | static gboolean check_magic ( FILE *f, const gchar *magic_number, guint magic_length ) |
50a14534 | 80 | { |
f8ecf9c2 | 81 | gchar magic[magic_length]; |
50a14534 EB |
82 | gboolean rv = FALSE; |
83 | gint8 i; | |
84 | if ( fread(magic, 1, sizeof(magic), f) == sizeof(magic) && | |
bf35388d | 85 | strncmp(magic, magic_number, sizeof(magic)) == 0 ) |
50a14534 EB |
86 | rv = TRUE; |
87 | for ( i = sizeof(magic)-1; i >= 0; i-- ) /* the ol' pushback */ | |
88 | ungetc(magic[i],f); | |
89 | return rv; | |
90 | } | |
91 | ||
92 | ||
93 | static gboolean str_starts_with ( const gchar *haystack, const gchar *needle, guint16 len_needle, gboolean must_be_longer ) | |
94 | { | |
95 | if ( strlen(haystack) > len_needle - (!must_be_longer) && strncasecmp ( haystack, needle, len_needle ) == 0 ) | |
96 | return TRUE; | |
97 | return FALSE; | |
98 | } | |
99 | ||
b5926b35 | 100 | void file_write_layer_param ( FILE *f, const gchar *name, VikLayerParamType type, VikLayerParamData data ) { |
ad0a8c2d EB |
101 | /* string lists are handled differently. We get a GList (that shouldn't |
102 | * be freed) back for get_param and if it is null we shouldn't write | |
103 | * anything at all (otherwise we'd read in a list with an empty string, | |
17a1f8f9 | 104 | * not an empty string list. |
ad0a8c2d | 105 | */ |
17a1f8f9 | 106 | if ( type == VIK_LAYER_PARAM_STRING_LIST ) { |
ad0a8c2d EB |
107 | if ( data.sl ) { |
108 | GList *iter = (GList *)data.sl; | |
109 | while ( iter ) { | |
17a1f8f9 | 110 | fprintf ( f, "%s=", name ); |
ad0a8c2d EB |
111 | fprintf ( f, "%s\n", (gchar *)(iter->data) ); |
112 | iter = iter->next; | |
113 | } | |
114 | } | |
115 | } else { | |
17a1f8f9 EB |
116 | fprintf ( f, "%s=", name ); |
117 | switch ( type ) | |
ad0a8c2d EB |
118 | { |
119 | case VIK_LAYER_PARAM_DOUBLE: { | |
120 | // char buf[15]; /* locale independent */ | |
121 | // fprintf ( f, "%s\n", (char *) g_dtostr (data.d, buf, sizeof (buf)) ); break; | |
122 | fprintf ( f, "%f\n", data.d ); | |
123 | break; | |
124 | } | |
125 | case VIK_LAYER_PARAM_UINT: fprintf ( f, "%d\n", data.u ); break; | |
126 | case VIK_LAYER_PARAM_INT: fprintf ( f, "%d\n", data.i ); break; | |
127 | case VIK_LAYER_PARAM_BOOLEAN: fprintf ( f, "%c\n", data.b ? 't' : 'f' ); break; | |
fd74cdb9 | 128 | case VIK_LAYER_PARAM_STRING: fprintf ( f, "%s\n", data.s ? data.s : "" ); break; |
ad0a8c2d | 129 | 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; |
b5926b35 | 130 | default: break; |
ad0a8c2d | 131 | } |
50a14534 | 132 | } |
17a1f8f9 EB |
133 | } |
134 | ||
135 | static void write_layer_params_and_data ( VikLayer *l, FILE *f ) | |
136 | { | |
137 | VikLayerParam *params = vik_layer_get_interface(l->type)->params; | |
138 | VikLayerFuncGetParam get_param = vik_layer_get_interface(l->type)->get_param; | |
139 | ||
140 | fprintf ( f, "name=%s\n", l->name ? l->name : "" ); | |
141 | if ( !l->visible ) | |
142 | fprintf ( f, "visible=f\n" ); | |
143 | ||
144 | if ( params && get_param ) | |
145 | { | |
146 | VikLayerParamData data; | |
147 | guint16 i, params_count = vik_layer_get_interface(l->type)->params_count; | |
148 | for ( i = 0; i < params_count; i++ ) | |
149 | { | |
158b3642 | 150 | data = get_param(l, i, TRUE); |
17a1f8f9 | 151 | file_write_layer_param(f, params[i].name, params[i].type, data); |
50a14534 EB |
152 | } |
153 | } | |
154 | if ( vik_layer_get_interface(l->type)->write_file_data ) | |
155 | { | |
156 | fprintf ( f, "\n\n~LayerData\n" ); | |
157 | vik_layer_get_interface(l->type)->write_file_data ( l, f ); | |
158 | fprintf ( f, "~EndLayerData\n" ); | |
159 | } | |
160 | /* foreach param: | |
161 | write param, and get_value, etc. | |
162 | then run layer data, and that's it. | |
163 | */ | |
164 | } | |
165 | ||
166 | static void file_write ( VikAggregateLayer *top, FILE *f, gpointer vp ) | |
167 | { | |
168 | Stack *stack = NULL; | |
169 | VikLayer *current_layer; | |
170 | struct LatLon ll; | |
c5f9245d | 171 | VikViewportDrawMode mode; |
4d872dde | 172 | gchar *modestring = NULL; |
50a14534 EB |
173 | |
174 | push(&stack); | |
175 | stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(top)); | |
176 | stack->under = NULL; | |
177 | ||
178 | /* crazhy CRAZHY */ | |
179 | vik_coord_to_latlon ( vik_viewport_get_center ( VIK_VIEWPORT(vp) ), &ll ); | |
180 | ||
c5f9245d GB |
181 | mode = vik_viewport_get_drawmode ( VIK_VIEWPORT(vp) ); |
182 | switch ( mode ) { | |
50a14534 EB |
183 | case VIK_VIEWPORT_DRAWMODE_UTM: modestring = "utm"; break; |
184 | case VIK_VIEWPORT_DRAWMODE_EXPEDIA: modestring = "expedia"; break; | |
c5f9245d | 185 | case VIK_VIEWPORT_DRAWMODE_MERCATOR: modestring = "mercator"; break; |
d587678a | 186 | case VIK_VIEWPORT_DRAWMODE_LATLON: modestring = "latlon"; break; |
c5f9245d GB |
187 | default: |
188 | g_critical("Houston, we've had a problem. mode=%d", mode); | |
50a14534 EB |
189 | } |
190 | ||
e087b0d9 RN |
191 | fprintf ( f, "#VIKING GPS Data file " VIKING_URL "\n" ); |
192 | fprintf ( f, "FILE_VERSION=%d\n", VIKING_FILE_VERSION ); | |
193 | fprintf ( f, "\nxmpp=%f\nympp=%f\nlat=%f\nlon=%f\nmode=%s\ncolor=%s\nhighlightcolor=%s\ndrawscale=%s\ndrawcentermark=%s\ndrawhighlight=%s\n", | |
50a14534 | 194 | vik_viewport_get_xmpp ( VIK_VIEWPORT(vp) ), vik_viewport_get_ympp ( VIK_VIEWPORT(vp) ), ll.lat, ll.lon, |
35c7c0ba | 195 | modestring, vik_viewport_get_background_color(VIK_VIEWPORT(vp)), |
480fb7e1 | 196 | vik_viewport_get_highlight_color(VIK_VIEWPORT(vp)), |
c933487f | 197 | vik_viewport_get_draw_scale(VIK_VIEWPORT(vp)) ? "t" : "f", |
2afcef36 RN |
198 | vik_viewport_get_draw_centermark(VIK_VIEWPORT(vp)) ? "t" : "f", |
199 | vik_viewport_get_draw_highlight(VIK_VIEWPORT(vp)) ? "t" : "f" ); | |
50a14534 EB |
200 | |
201 | if ( ! VIK_LAYER(top)->visible ) | |
202 | fprintf ( f, "visible=f\n" ); | |
203 | ||
204 | while (stack && stack->data) | |
205 | { | |
206 | current_layer = VIK_LAYER(((GList *)stack->data)->data); | |
db386630 | 207 | fprintf ( f, "\n~Layer %s\n", vik_layer_get_interface(current_layer->type)->fixed_layer_name ); |
50a14534 EB |
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)) ) | |
210 | { | |
211 | push(&stack); | |
212 | stack->data = (gpointer) vik_aggregate_layer_get_children(VIK_AGGREGATE_LAYER(current_layer)); | |
213 | } | |
f253a6a6 | 214 | else if ( current_layer->type == VIK_LAYER_GPS && !vik_gps_layer_is_empty(VIK_GPS_LAYER(current_layer)) ) |
7886de28 QT |
215 | { |
216 | push(&stack); | |
217 | stack->data = (gpointer) vik_gps_layer_get_children(VIK_GPS_LAYER(current_layer)); | |
218 | } | |
50a14534 EB |
219 | else |
220 | { | |
221 | stack->data = (gpointer) ((GList *)stack->data)->next; | |
222 | fprintf ( f, "~EndLayer\n\n" ); | |
223 | while ( stack && (!stack->data) ) | |
224 | { | |
225 | pop(&stack); | |
226 | if ( stack ) | |
227 | { | |
228 | stack->data = (gpointer) ((GList *)stack->data)->next; | |
229 | fprintf ( f, "~EndLayer\n\n" ); | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | /* | |
235 | get vikaggregatelayer's children (?) | |
236 | foreach write ALL params, | |
237 | then layer data (IF function exists) | |
238 | then endlayer | |
239 | ||
240 | impl: | |
241 | stack of layers (LIST) we are working on | |
242 | when layer->next == NULL ... | |
243 | we move on. | |
244 | */ | |
245 | } | |
246 | ||
ad0a8c2d EB |
247 | static void string_list_delete ( gpointer key, gpointer l, gpointer user_data ) |
248 | { | |
28c82d8b EB |
249 | /* 20071021 bugfix */ |
250 | GList *iter = (GList *) l; | |
ad0a8c2d EB |
251 | while ( iter ) { |
252 | g_free ( iter->data ); | |
253 | iter = iter->next; | |
254 | } | |
28c82d8b | 255 | g_list_free ( (GList *) l ); |
ad0a8c2d EB |
256 | } |
257 | ||
258 | static void string_list_set_param (gint i, GList *list, gpointer *layer_and_vp) | |
259 | { | |
260 | VikLayerParamData x; | |
261 | x.sl = list; | |
158b3642 | 262 | vik_layer_set_param ( VIK_LAYER(layer_and_vp[0]), i, x, layer_and_vp[1], TRUE ); |
ad0a8c2d EB |
263 | } |
264 | ||
327a2533 RN |
265 | /** |
266 | * Read in a Viking file and return how successful the parsing was | |
267 | * ATM this will always work, in that even if there are parsing problems | |
268 | * then there will be no new values to override the defaults | |
269 | * | |
270 | * TODO flow up line number(s) / error messages of problems encountered... | |
271 | * | |
272 | */ | |
1b14d0d2 | 273 | static gboolean file_read ( VikAggregateLayer *top, FILE *f, const gchar *dirpath, VikViewport *vp ) |
50a14534 | 274 | { |
d99f5da9 | 275 | Stack *stack = NULL; |
50a14534 EB |
276 | struct LatLon ll = { 0.0, 0.0 }; |
277 | gchar buffer[4096]; | |
278 | gchar *line; | |
279 | guint16 len; | |
280 | long line_num = 0; | |
281 | ||
37518034 QT |
282 | VikLayerParam *params = NULL; /* for current layer, so we don't have to keep on looking up interface */ |
283 | guint8 params_count = 0; | |
50a14534 | 284 | |
ad0a8c2d EB |
285 | GHashTable *string_lists = g_hash_table_new(g_direct_hash,g_direct_equal); |
286 | ||
327a2533 RN |
287 | gboolean successful_read = TRUE; |
288 | ||
50a14534 EB |
289 | push(&stack); |
290 | stack->under = NULL; | |
291 | stack->data = (gpointer) top; | |
292 | ||
293 | while ( fgets ( buffer, 4096, f ) ) | |
294 | { | |
295 | line_num++; | |
296 | ||
297 | line = buffer; | |
298 | while ( *line == ' ' || *line =='\t' ) | |
299 | line++; | |
300 | ||
301 | if ( line[0] == '#' ) | |
302 | continue; | |
303 | ||
304 | ||
305 | len = strlen(line); | |
306 | if ( len > 0 && line[len-1] == '\n' ) | |
307 | line[--len] = '\0'; | |
308 | if ( len > 0 && line[len-1] == '\r' ) | |
309 | line[--len] = '\0'; | |
310 | ||
311 | if ( len == 0 ) | |
312 | continue; | |
313 | ||
314 | ||
315 | if ( line[0] == '~' ) | |
316 | { | |
317 | line++; len--; | |
318 | if ( *line == '\0' ) | |
319 | continue; | |
320 | else if ( str_starts_with ( line, "Layer ", 6, TRUE ) ) | |
321 | { | |
f253a6a6 QT |
322 | int parent_type = VIK_LAYER(stack->data)->type; |
323 | if ( ( ! stack->data ) || ((parent_type != VIK_LAYER_AGGREGATE) && (parent_type != VIK_LAYER_GPS)) ) | |
50a14534 | 324 | { |
327a2533 | 325 | successful_read = FALSE; |
f253a6a6 | 326 | g_warning ( "Line %ld: Layer command inside non-Aggregate Layer (type %d)", line_num, parent_type ); |
50a14534 EB |
327 | push(&stack); /* inside INVALID layer */ |
328 | stack->data = NULL; | |
329 | continue; | |
330 | } | |
331 | else | |
332 | { | |
44b84795 | 333 | VikLayerTypeEnum type = vik_layer_type_from_string ( line+6 ); |
50a14534 | 334 | push(&stack); |
b5926b35 | 335 | if ( type == VIK_LAYER_NUM_TYPES ) |
50a14534 | 336 | { |
327a2533 | 337 | successful_read = FALSE; |
4258f4e2 | 338 | g_warning ( "Line %ld: Unknown type %s", line_num, line+6 ); |
50a14534 EB |
339 | stack->data = NULL; |
340 | } | |
f253a6a6 QT |
341 | else if (parent_type == VIK_LAYER_GPS) |
342 | { | |
343 | stack->data = (gpointer) vik_gps_layer_get_a_child(VIK_GPS_LAYER(stack->under->data)); | |
344 | params = vik_layer_get_interface(type)->params; | |
345 | params_count = vik_layer_get_interface(type)->params_count; | |
346 | } | |
50a14534 EB |
347 | else |
348 | { | |
0ab35525 | 349 | stack->data = (gpointer) vik_layer_create ( type, vp, FALSE ); |
50a14534 EB |
350 | params = vik_layer_get_interface(type)->params; |
351 | params_count = vik_layer_get_interface(type)->params_count; | |
352 | } | |
353 | } | |
354 | } | |
355 | else if ( str_starts_with ( line, "EndLayer", 8, FALSE ) ) | |
356 | { | |
327a2533 RN |
357 | if ( stack->under == NULL ) { |
358 | successful_read = FALSE; | |
50a14534 | 359 | g_warning ( "Line %ld: Mismatched ~EndLayer command", line_num ); |
327a2533 | 360 | } |
50a14534 EB |
361 | else |
362 | { | |
ad0a8c2d EB |
363 | /* add any string lists we've accumulated */ |
364 | gpointer layer_and_vp[2]; | |
365 | layer_and_vp[0] = stack->data; | |
366 | layer_and_vp[1] = vp; | |
367 | g_hash_table_foreach ( string_lists, (GHFunc) string_list_set_param, layer_and_vp ); | |
368 | g_hash_table_remove_all ( string_lists ); | |
369 | ||
50a14534 EB |
370 | if ( stack->data && stack->under->data ) |
371 | { | |
f253a6a6 | 372 | if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_AGGREGATE) { |
c3a02429 | 373 | vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data), FALSE ); |
dcb51e52 | 374 | vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE ); |
f253a6a6 QT |
375 | } |
376 | else if (VIK_LAYER(stack->under->data)->type == VIK_LAYER_GPS) { | |
377 | /* TODO: anything else needs to be done here ? */ | |
378 | } | |
327a2533 RN |
379 | else { |
380 | successful_read = FALSE; | |
f253a6a6 | 381 | g_warning ( "Line %ld: EndLayer command inside non-Aggregate Layer (type %d)", line_num, VIK_LAYER(stack->data)->type ); |
327a2533 | 382 | } |
50a14534 EB |
383 | } |
384 | pop(&stack); | |
385 | } | |
386 | } | |
387 | else if ( str_starts_with ( line, "LayerData", 9, FALSE ) ) | |
388 | { | |
389 | if ( stack->data && vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ) | |
119ada4c | 390 | { |
50a14534 | 391 | /* must read until hits ~EndLayerData */ |
1b14d0d2 | 392 | if ( ! vik_layer_get_interface(VIK_LAYER(stack->data)->type)->read_file_data ( VIK_LAYER(stack->data), f, dirpath ) ) |
119ada4c RN |
393 | successful_read = FALSE; |
394 | } | |
50a14534 EB |
395 | else |
396 | { /* simply skip layer data over */ | |
397 | while ( fgets ( buffer, 4096, f ) ) | |
398 | { | |
399 | line_num++; | |
400 | ||
401 | line = buffer; | |
402 | ||
403 | len = strlen(line); | |
404 | if ( len > 0 && line[len-1] == '\n' ) | |
405 | line[--len] = '\0'; | |
406 | if ( len > 0 && line[len-1] == '\r' ) | |
407 | line[--len] = '\0'; | |
408 | if ( strcasecmp ( line, "~EndLayerData" ) == 0 ) | |
409 | break; | |
410 | } | |
411 | continue; | |
412 | } | |
413 | } | |
414 | else | |
415 | { | |
327a2533 | 416 | successful_read = FALSE; |
50a14534 EB |
417 | g_warning ( "Line %ld: Unknown tilde command", line_num ); |
418 | } | |
419 | } | |
420 | else | |
421 | { | |
422 | gint32 eq_pos = -1; | |
423 | guint16 i; | |
424 | if ( ! stack->data ) | |
425 | continue; | |
426 | ||
427 | for ( i = 0; i < len; i++ ) | |
428 | if ( line[i] == '=' ) | |
429 | eq_pos = i; | |
430 | ||
e087b0d9 RN |
431 | if ( stack->under == NULL && eq_pos == 12 && strncasecmp ( line, "FILE_VERSION", eq_pos ) == 0) { |
432 | gint version = strtol(line+13, NULL, 10); | |
433 | g_debug ( "%s: reading file version %d", __FUNCTION__, version ); | |
434 | if ( version > VIKING_FILE_VERSION ) | |
435 | successful_read = FALSE; | |
436 | // However we'll still carry and attempt to read whatever we can | |
437 | } | |
438 | else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "xmpp", eq_pos ) == 0) /* "hard coded" params: global & for all layer-types */ | |
81687a73 | 439 | vik_viewport_set_xmpp ( VIK_VIEWPORT(vp), strtod_i8n ( line+5, NULL ) ); |
50a14534 | 440 | else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "ympp", eq_pos ) == 0) |
81687a73 | 441 | vik_viewport_set_ympp ( VIK_VIEWPORT(vp), strtod_i8n ( line+5, NULL ) ); |
50a14534 | 442 | else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lat", eq_pos ) == 0 ) |
81687a73 | 443 | ll.lat = strtod_i8n ( line+4, NULL ); |
50a14534 | 444 | else if ( stack->under == NULL && eq_pos == 3 && strncasecmp ( line, "lon", eq_pos ) == 0 ) |
81687a73 | 445 | ll.lon = strtod_i8n ( line+4, NULL ); |
50a14534 EB |
446 | else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "utm" ) == 0) |
447 | vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_UTM); | |
448 | else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "expedia" ) == 0) | |
449 | vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_EXPEDIA ); | |
450 | else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "google" ) == 0) | |
fd98b156 | 451 | { |
327a2533 | 452 | successful_read = FALSE; |
fd98b156 GB |
453 | g_warning ( _("Draw mode '%s' no more supported"), "google" ); |
454 | } | |
50a14534 | 455 | else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "kh" ) == 0) |
fd98b156 | 456 | { |
327a2533 | 457 | successful_read = FALSE; |
fd98b156 GB |
458 | g_warning ( _("Draw mode '%s' no more supported"), "kh" ); |
459 | } | |
50a14534 EB |
460 | else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "mercator" ) == 0) |
461 | vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_MERCATOR ); | |
d587678a GB |
462 | else if ( stack->under == NULL && eq_pos == 4 && strncasecmp ( line, "mode", eq_pos ) == 0 && strcasecmp ( line+5, "latlon" ) == 0) |
463 | vik_viewport_set_drawmode ( VIK_VIEWPORT(vp), VIK_VIEWPORT_DRAWMODE_LATLON ); | |
50a14534 EB |
464 | else if ( stack->under == NULL && eq_pos == 5 && strncasecmp ( line, "color", eq_pos ) == 0 ) |
465 | vik_viewport_set_background_color ( VIK_VIEWPORT(vp), line+6 ); | |
480fb7e1 RN |
466 | else if ( stack->under == NULL && eq_pos == 14 && strncasecmp ( line, "highlightcolor", eq_pos ) == 0 ) |
467 | vik_viewport_set_highlight_color ( VIK_VIEWPORT(vp), line+15 ); | |
35c7c0ba EB |
468 | else if ( stack->under == NULL && eq_pos == 9 && strncasecmp ( line, "drawscale", eq_pos ) == 0 ) |
469 | vik_viewport_set_draw_scale ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+10) ); | |
de66b263 | 470 | else if ( stack->under == NULL && eq_pos == 14 && strncasecmp ( line, "drawcentermark", eq_pos ) == 0 ) |
1657065a | 471 | vik_viewport_set_draw_centermark ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+15) ); |
2afcef36 RN |
472 | else if ( stack->under == NULL && eq_pos == 13 && strncasecmp ( line, "drawhighlight", eq_pos ) == 0 ) |
473 | vik_viewport_set_draw_highlight ( VIK_VIEWPORT(vp), TEST_BOOLEAN(line+14) ); | |
50a14534 EB |
474 | else if ( stack->under && eq_pos == 4 && strncasecmp ( line, "name", eq_pos ) == 0 ) |
475 | vik_layer_rename ( VIK_LAYER(stack->data), line+5 ); | |
476 | else if ( eq_pos == 7 && strncasecmp ( line, "visible", eq_pos ) == 0 ) | |
477 | VIK_LAYER(stack->data)->visible = TEST_BOOLEAN(line+8); | |
478 | else if ( eq_pos != -1 && stack->under ) | |
479 | { | |
480 | gboolean found_match = FALSE; | |
ad0a8c2d | 481 | |
50a14534 EB |
482 | /* go thru layer params. if len == eq_pos && starts_with jazz, set it. */ |
483 | /* also got to check for name and visible. */ | |
484 | ||
485 | if ( ! params ) | |
486 | { | |
327a2533 | 487 | successful_read = FALSE; |
50a14534 EB |
488 | g_warning ( "Line %ld: No options for this kind of layer", line_num ); |
489 | continue; | |
490 | } | |
491 | ||
492 | for ( i = 0; i < params_count; i++ ) | |
493 | if ( strlen(params[i].name) == eq_pos && strncasecmp ( line, params[i].name, eq_pos ) == 0 ) | |
494 | { | |
495 | VikLayerParamData x; | |
496 | line += eq_pos+1; | |
ad0a8c2d | 497 | if ( params[i].type == VIK_LAYER_PARAM_STRING_LIST ) { |
dc2c040e RN |
498 | GList *l = g_list_append ( g_hash_table_lookup ( string_lists, GINT_TO_POINTER ((gint) i) ), |
499 | g_strdup(line) ); | |
500 | g_hash_table_replace ( string_lists, GINT_TO_POINTER ((gint)i), l ); | |
ad0a8c2d EB |
501 | /* add the value to a list, possibly making a new list. |
502 | * this will be passed to the layer when we read an ~EndLayer */ | |
503 | } else { | |
504 | switch ( params[i].type ) | |
505 | { | |
81687a73 | 506 | case VIK_LAYER_PARAM_DOUBLE: x.d = strtod_i8n(line, NULL); break; |
ad0a8c2d EB |
507 | case VIK_LAYER_PARAM_UINT: x.u = strtoul(line, NULL, 10); break; |
508 | case VIK_LAYER_PARAM_INT: x.i = strtol(line, NULL, 10); break; | |
509 | case VIK_LAYER_PARAM_BOOLEAN: x.b = TEST_BOOLEAN(line); break; | |
510 | case VIK_LAYER_PARAM_COLOR: memset(&(x.c), 0, sizeof(x.c)); /* default: black */ | |
50a14534 | 511 | gdk_color_parse ( line, &(x.c) ); break; |
ad0a8c2d EB |
512 | /* STRING or STRING_LIST -- if STRING_LIST, just set param to add a STRING */ |
513 | default: x.s = line; | |
514 | } | |
158b3642 | 515 | vik_layer_set_param ( VIK_LAYER(stack->data), i, x, vp, TRUE ); |
50a14534 | 516 | } |
50a14534 EB |
517 | found_match = TRUE; |
518 | break; | |
519 | } | |
327a2533 RN |
520 | if ( ! found_match ) { |
521 | // ATM don't flow up this issue because at least one internal parameter has changed from version 1.3 | |
522 | // and don't what to worry users about raising such issues | |
523 | // TODO Maybe hold old values here - compare the line value against them and if a match | |
524 | // generate a different style of message in the GUI... | |
525 | // successful_read = FALSE; | |
ea3933fc | 526 | g_warning ( "Line %ld: Unknown parameter. Line:\n%s", line_num, line ); |
327a2533 | 527 | } |
50a14534 | 528 | } |
327a2533 RN |
529 | else { |
530 | successful_read = FALSE; | |
50a14534 | 531 | g_warning ( "Line %ld: Invalid parameter or parameter outside of layer.", line_num ); |
327a2533 | 532 | } |
50a14534 EB |
533 | } |
534 | /* could be: | |
535 | [Layer Type=Bla] | |
536 | [EndLayer] | |
537 | [LayerData] | |
538 | name=this | |
539 | #comment | |
540 | */ | |
541 | } | |
542 | ||
543 | while ( stack ) | |
544 | { | |
545 | if ( stack->under && stack->under->data && stack->data ) | |
546 | { | |
c3a02429 | 547 | vik_aggregate_layer_add_layer ( VIK_AGGREGATE_LAYER(stack->under->data), VIK_LAYER(stack->data), FALSE ); |
dcb51e52 | 548 | vik_layer_post_read ( VIK_LAYER(stack->data), vp, TRUE ); |
50a14534 EB |
549 | } |
550 | pop(&stack); | |
551 | } | |
552 | ||
553 | if ( ll.lat != 0.0 || ll.lon != 0.0 ) | |
be5554c5 | 554 | vik_viewport_set_center_latlon ( VIK_VIEWPORT(vp), &ll, TRUE ); |
50a14534 EB |
555 | |
556 | if ( ( ! VIK_LAYER(top)->visible ) && VIK_LAYER(top)->realized ) | |
557 | vik_treeview_item_set_visible ( VIK_LAYER(top)->vt, &(VIK_LAYER(top)->iter), FALSE ); | |
ad0a8c2d EB |
558 | |
559 | /* delete anything we've forgotten about -- should only happen when file ends before an EndLayer */ | |
560 | g_hash_table_foreach ( string_lists, string_list_delete, NULL ); | |
561 | g_hash_table_destroy ( string_lists ); | |
327a2533 RN |
562 | |
563 | return successful_read; | |
50a14534 EB |
564 | } |
565 | ||
566 | /* | |
567 | read thru file | |
568 | if "[Layer Type=" | |
569 | push(&stack) | |
570 | new default layer of type (str_to_type) (check interface->name) | |
571 | if "[EndLayer]" | |
572 | VikLayer *vl = stack->data; | |
573 | pop(&stack); | |
574 | vik_aggregate_layer_add_layer(stack->data, vl); | |
575 | if "[LayerData]" | |
576 | vik_layer_data ( VIK_LAYER_DATA(stack->data), f, vp ); | |
577 | ||
578 | */ | |
579 | ||
580 | /* ---------------------------------------------------- */ | |
581 | ||
0ba6e0b9 | 582 | static FILE *xfopen ( const char *fn ) |
50a14534 EB |
583 | { |
584 | if ( strcmp(fn,"-") == 0 ) | |
585 | return stdin; | |
586 | else | |
8c060406 | 587 | return g_fopen(fn, "r"); |
50a14534 EB |
588 | } |
589 | ||
590 | static void xfclose ( FILE *f ) | |
591 | { | |
8c060406 | 592 | if ( f != stdin && f != stdout ) { |
50a14534 | 593 | fclose ( f ); |
8c060406 MA |
594 | f = NULL; |
595 | } | |
50a14534 EB |
596 | } |
597 | ||
ba9d0a00 RN |
598 | /* |
599 | * Function to determine if a filename is a 'viking' type file | |
600 | */ | |
601 | gboolean check_file_magic_vik ( const gchar *filename ) | |
602 | { | |
a3180051 | 603 | gboolean result = FALSE; |
0ba6e0b9 | 604 | FILE *ff = xfopen ( filename ); |
a3180051 | 605 | if ( ff ) { |
f8ecf9c2 | 606 | result = check_magic ( ff, VIK_MAGIC, VIK_MAGIC_LEN ); |
a3180051 RN |
607 | xfclose ( ff ); |
608 | } | |
ba9d0a00 RN |
609 | return result; |
610 | } | |
611 | ||
e4a11fbe GB |
612 | /** |
613 | * append_file_ext: | |
614 | * | |
615 | * Append a file extension, if not already present. | |
616 | * | |
617 | * Returns: a newly allocated string | |
618 | */ | |
bb3c38af | 619 | gchar *append_file_ext ( const gchar *filename, VikFileType_t type ) |
e4a11fbe GB |
620 | { |
621 | gchar *new_name = NULL; | |
622 | const gchar *ext = NULL; | |
623 | ||
624 | /* Select an extension */ | |
625 | switch (type) | |
626 | { | |
627 | case FILE_TYPE_GPX: | |
628 | ext = ".gpx"; | |
629 | break; | |
630 | case FILE_TYPE_KML: | |
631 | ext = ".kml"; | |
632 | break; | |
1ca082d7 RN |
633 | case FILE_TYPE_GEOJSON: |
634 | ext = ".geojson"; | |
635 | break; | |
e4a11fbe GB |
636 | case FILE_TYPE_GPSMAPPER: |
637 | case FILE_TYPE_GPSPOINT: | |
638 | default: | |
639 | /* Do nothing, ext already set to NULL */ | |
640 | break; | |
641 | } | |
642 | ||
643 | /* Do */ | |
140d0d67 | 644 | if ( ext != NULL && ! a_file_check_ext ( filename, ext ) ) |
e4a11fbe GB |
645 | new_name = g_strconcat ( filename, ext, NULL ); |
646 | else | |
647 | /* Simply duplicate */ | |
648 | new_name = g_strdup ( filename ); | |
649 | ||
650 | return new_name; | |
651 | } | |
652 | ||
718696d6 | 653 | VikLoadType_t a_file_load ( VikAggregateLayer *top, VikViewport *vp, VikTrwLayer *vtl, const gchar *filename_or_uri ) |
50a14534 | 654 | { |
0a514451 RN |
655 | g_return_val_if_fail ( vp != NULL, LOAD_TYPE_READ_FAILURE ); |
656 | ||
11f427b8 | 657 | char *filename = (char *)filename_or_uri; |
df390922 | 658 | if (strncmp(filename, "file://", 7) == 0) { |
07c78458 RN |
659 | // Consider replacing this with: |
660 | // filename = g_filename_from_uri ( entry, NULL, NULL ); | |
661 | // Since this doesn't support URIs properly (i.e. will failure if is it has %20 characters in it) | |
db32b809 | 662 | filename = filename + 7; |
df390922 RN |
663 | g_debug ( "Loading file %s from URI %s", filename, filename_or_uri ); |
664 | } | |
0ba6e0b9 | 665 | FILE *f = xfopen ( filename ); |
50a14534 | 666 | |
50a14534 | 667 | if ( ! f ) |
ba9d0a00 | 668 | return LOAD_TYPE_READ_FAILURE; |
50a14534 | 669 | |
327a2533 | 670 | VikLoadType_t load_answer = LOAD_TYPE_OTHER_SUCCESS; |
0a514451 | 671 | |
1b14d0d2 | 672 | gchar *dirpath = g_path_get_dirname ( filename ); |
0a514451 | 673 | // Attempt loading the primary file type first - our internal .vik file: |
f8ecf9c2 | 674 | if ( check_magic ( f, VIK_MAGIC, VIK_MAGIC_LEN ) ) |
50a14534 | 675 | { |
1b14d0d2 | 676 | if ( file_read ( top, f, dirpath, vp ) ) |
327a2533 RN |
677 | load_answer = LOAD_TYPE_VIK_SUCCESS; |
678 | else | |
679 | load_answer = LOAD_TYPE_VIK_FAILURE_NON_FATAL; | |
50a14534 | 680 | } |
5bed0ef6 RN |
681 | else if ( a_jpg_magic_check ( filename ) ) { |
682 | if ( ! a_jpg_load_file ( top, filename, vp ) ) | |
683 | load_answer = LOAD_TYPE_UNSUPPORTED_FAILURE; | |
684 | } | |
50a14534 EB |
685 | else |
686 | { | |
0a514451 RN |
687 | // For all other file types which consist of tracks, routes and/or waypoints, |
688 | // must be loaded into a new TrackWaypoint layer (hence it be created) | |
689 | gboolean success = TRUE; // Detect load failures - mainly to remove the layer created as it's not required | |
690 | ||
718696d6 | 691 | // Add to specified layer |
692 | gboolean add_new = !IS_VIK_TRW_LAYER(vtl); | |
693 | if (add_new) { | |
3f350544 RN |
694 | vtl = VIK_TRW_LAYER (vik_layer_create ( VIK_LAYER_TRW, vp, FALSE )); |
695 | vik_layer_rename ( VIK_LAYER(vtl), a_file_basename ( filename ) ); | |
718696d6 | 696 | } |
bf35388d | 697 | |
ba9d0a00 | 698 | // In fact both kml & gpx files start the same as they are in xml |
f8ecf9c2 | 699 | if ( a_file_check_ext ( filename, ".kml" ) && check_magic ( f, GPX_MAGIC, GPX_MAGIC_LEN ) ) { |
ba9d0a00 | 700 | // Implicit Conversion |
17acdaec | 701 | ProcessOptions po = { "-i kml", filename, NULL, NULL, NULL, NULL }; |
3f350544 | 702 | if ( ! ( success = a_babel_convert_from ( vtl, &po, NULL, NULL, NULL ) ) ) { |
0a514451 | 703 | load_answer = LOAD_TYPE_GPSBABEL_FAILURE; |
ba9d0a00 RN |
704 | } |
705 | } | |
0a514451 RN |
706 | // NB use a extension check first, as a GPX file header may have a Byte Order Mark (BOM) in it |
707 | // - which currently confuses our check_magic function | |
f8ecf9c2 | 708 | else if ( a_file_check_ext ( filename, ".gpx" ) || check_magic ( f, GPX_MAGIC, GPX_MAGIC_LEN ) ) { |
3f350544 | 709 | if ( ! ( success = a_gpx_read_file ( vtl, f ) ) ) { |
0a514451 | 710 | load_answer = LOAD_TYPE_GPX_FAILURE; |
54b84792 | 711 | } |
ba9d0a00 | 712 | } |
0a514451 RN |
713 | else { |
714 | // Try final supported file type | |
3f350544 | 715 | if ( ! ( success = a_gpspoint_read_file ( vtl, f, dirpath ) ) ) { |
1b14d0d2 | 716 | // Failure here means we don't know how to handle the file |
0a514451 | 717 | load_answer = LOAD_TYPE_UNSUPPORTED_FAILURE; |
1b14d0d2 | 718 | } |
0a514451 | 719 | } |
0a514451 | 720 | // Clean up when we can't handle the file |
cb5ec7a8 RN |
721 | if ( ! success ) { |
722 | // free up layer | |
723 | g_object_unref ( vtl ); | |
cb5ec7a8 | 724 | } |
0a514451 RN |
725 | else { |
726 | // Complete the setup from the successful load | |
3f350544 | 727 | vik_layer_post_read ( VIK_LAYER(vtl), vp, TRUE ); |
718696d6 | 728 | if (add_new) { |
3f350544 | 729 | vik_aggregate_layer_add_layer ( top, VIK_LAYER(vtl), FALSE ); |
718696d6 | 730 | } |
3f350544 | 731 | vik_trw_layer_auto_set_view ( vtl, vp ); |
0a514451 | 732 | } |
50a14534 | 733 | } |
70b96cd5 | 734 | g_free ( dirpath ); |
0a514451 RN |
735 | xfclose(f); |
736 | return load_answer; | |
50a14534 EB |
737 | } |
738 | ||
739 | gboolean a_file_save ( VikAggregateLayer *top, gpointer vp, const gchar *filename ) | |
740 | { | |
bf1c0ae3 SM |
741 | FILE *f; |
742 | ||
743 | if (strncmp(filename, "file://", 7) == 0) | |
744 | filename = filename + 7; | |
745 | ||
746 | f = g_fopen(filename, "w"); | |
50a14534 EB |
747 | |
748 | if ( ! f ) | |
749 | return FALSE; | |
750 | ||
31729835 | 751 | // Enable relative paths in .vik files to work |
1dfb1691 | 752 | gchar *cwd = g_get_current_dir(); |
31729835 RN |
753 | gchar *dir = g_path_get_dirname ( filename ); |
754 | if ( dir ) { | |
755 | if ( g_chdir ( dir ) ) { | |
756 | g_warning ( "Could not change directory to %s", dir ); | |
757 | } | |
758 | g_free (dir); | |
759 | } | |
760 | ||
50a14534 EB |
761 | file_write ( top, f, vp ); |
762 | ||
1dfb1691 RN |
763 | // Restore previous working directory |
764 | if ( cwd ) { | |
765 | if ( g_chdir ( cwd ) ) { | |
766 | g_warning ( "Could not return to directory %s", cwd ); | |
767 | } | |
768 | g_free (cwd); | |
769 | } | |
770 | ||
50a14534 | 771 | fclose(f); |
8c060406 | 772 | f = NULL; |
50a14534 EB |
773 | |
774 | return TRUE; | |
775 | } | |
776 | ||
777 | ||
b0252b2e | 778 | /* example: |
140d0d67 | 779 | gboolean is_gpx = a_file_check_ext ( "a/b/c.gpx", ".gpx" ); |
b0252b2e | 780 | */ |
140d0d67 | 781 | gboolean a_file_check_ext ( const gchar *filename, const gchar *fileext ) |
b0252b2e | 782 | { |
0a514451 RN |
783 | g_return_val_if_fail ( filename != NULL, FALSE ); |
784 | g_return_val_if_fail ( fileext && fileext[0]=='.', FALSE ); | |
b0252b2e | 785 | const gchar *basename = a_file_basename(filename); |
b0252b2e T |
786 | if (!basename) |
787 | return FALSE; | |
788 | ||
789 | const char * dot = strrchr(basename, '.'); | |
790 | if (dot && !strcmp(dot, fileext)) | |
791 | return TRUE; | |
792 | ||
793 | return FALSE; | |
794 | } | |
795 | ||
208d2084 RN |
796 | /** |
797 | * a_file_export: | |
798 | * @vtl: The TrackWaypoint to export data from | |
799 | * @filename: The name of the file to be written | |
800 | * @file_type: Choose one of the supported file types for the export | |
801 | * @trk: If specified then only export this track rather than the whole layer | |
802 | * @write_hidden: Whether to write invisible items | |
803 | * | |
804 | * A general export command to convert from Viking TRW layer data to an external supported format. | |
805 | * The write_hidden option is provided mainly to be able to transfer selected items when uploading to a GPS | |
806 | */ | |
807 | gboolean a_file_export ( VikTrwLayer *vtl, const gchar *filename, VikFileType_t file_type, VikTrack *trk, gboolean write_hidden ) | |
50a14534 | 808 | { |
0d2b891f | 809 | GpxWritingOptions options = { FALSE, FALSE, write_hidden, FALSE }; |
8c060406 | 810 | FILE *f = g_fopen ( filename, "w" ); |
50a14534 EB |
811 | if ( f ) |
812 | { | |
1ca082d7 RN |
813 | gboolean result = TRUE; |
814 | ||
ce4bd1cf | 815 | if ( trk ) { |
f7f8a0a6 GB |
816 | switch ( file_type ) { |
817 | case FILE_TYPE_GPX: | |
0d2b891f RN |
818 | // trk defined so can set the option |
819 | options.is_route = trk->is_route; | |
208d2084 | 820 | a_gpx_write_track_file ( trk, f, &options ); |
f7f8a0a6 GB |
821 | break; |
822 | default: | |
823 | g_critical("Houston, we've had a problem. file_type=%d", file_type); | |
824 | } | |
825 | } else { | |
826 | switch ( file_type ) { | |
827 | case FILE_TYPE_GPSMAPPER: | |
828 | a_gpsmapper_write_file ( vtl, f ); | |
829 | break; | |
830 | case FILE_TYPE_GPX: | |
208d2084 | 831 | a_gpx_write_file ( vtl, f, &options ); |
f7f8a0a6 GB |
832 | break; |
833 | case FILE_TYPE_GPSPOINT: | |
834 | a_gpspoint_write_file ( vtl, f ); | |
835 | break; | |
1ca082d7 RN |
836 | case FILE_TYPE_GEOJSON: |
837 | result = a_geojson_write_file ( vtl, f ); | |
838 | break; | |
ba9d0a00 RN |
839 | case FILE_TYPE_KML: |
840 | fclose ( f ); | |
60dbd0ad RN |
841 | switch ( a_vik_get_kml_export_units () ) { |
842 | case VIK_KML_EXPORT_UNITS_STATUTE: | |
6f94db6d | 843 | return a_babel_convert_to ( vtl, NULL, "-o kml", filename, NULL, NULL ); |
60dbd0ad RN |
844 | break; |
845 | case VIK_KML_EXPORT_UNITS_NAUTICAL: | |
6f94db6d | 846 | return a_babel_convert_to ( vtl, NULL, "-o kml,units=n", filename, NULL, NULL ); |
60dbd0ad RN |
847 | break; |
848 | default: | |
849 | // VIK_KML_EXPORT_UNITS_METRIC: | |
6f94db6d | 850 | return a_babel_convert_to ( vtl, NULL, "-o kml,units=m", filename, NULL, NULL ); |
60dbd0ad RN |
851 | break; |
852 | } | |
853 | break; | |
f7f8a0a6 GB |
854 | default: |
855 | g_critical("Houston, we've had a problem. file_type=%d", file_type); | |
856 | } | |
7f6757c4 | 857 | } |
50a14534 | 858 | fclose ( f ); |
1ca082d7 | 859 | return result; |
50a14534 EB |
860 | } |
861 | return FALSE; | |
862 | } | |
80d0ff2b | 863 | |
17720e63 GB |
864 | /** |
865 | * a_file_export_babel: | |
866 | */ | |
867 | gboolean a_file_export_babel ( VikTrwLayer *vtl, const gchar *filename, const gchar *format, | |
868 | gboolean tracks, gboolean routes, gboolean waypoints ) | |
869 | { | |
870 | gchar *args = g_strdup_printf("%s %s %s -o %s", | |
871 | tracks ? "-t" : "", | |
872 | routes ? "-r" : "", | |
873 | waypoints ? "-w" : "", | |
874 | format); | |
875 | gboolean result = a_babel_convert_to ( vtl, NULL, args, filename, NULL, NULL ); | |
876 | g_free(args); | |
877 | return result; | |
878 | } | |
879 | ||
0925261e RN |
880 | /** |
881 | * Just a wrapper around realpath, which itself is platform dependent | |
882 | */ | |
883 | char *file_realpath ( const char *path, char *real ) | |
884 | { | |
885 | return realpath ( path, real ); | |
886 | } | |
887 | ||
1b14d0d2 RN |
888 | #ifndef MAXPATHLEN |
889 | #define MAXPATHLEN 1024 | |
890 | #endif | |
891 | /** | |
892 | * Always return the canonical filename in a newly allocated string | |
893 | */ | |
894 | char *file_realpath_dup ( const char *path ) | |
895 | { | |
896 | char real[MAXPATHLEN]; | |
897 | ||
898 | g_return_val_if_fail(path != NULL, NULL); | |
899 | ||
900 | if (file_realpath(path, real)) | |
901 | return g_strdup(real); | |
902 | ||
903 | return g_strdup(path); | |
904 | } | |
905 | ||
88542aa9 RN |
906 | /** |
907 | * Permission granted to use this code after personal correspondance | |
908 | * Slightly reworked for better cross platform use, glibisms, function rename and a compacter format | |
909 | * | |
910 | * FROM http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263 | |
911 | */ | |
912 | ||
913 | // GetRelativeFilename(), by Rob Fisher. | |
914 | // rfisher@iee.org | |
915 | // http://come.to/robfisher | |
916 | ||
88542aa9 RN |
917 | // The number of characters at the start of an absolute filename. e.g. in DOS, |
918 | // absolute filenames start with "X:\" so this value should be 3, in UNIX they start | |
919 | // with "\" so this value should be 1. | |
920 | #ifdef WINDOWS | |
921 | #define ABSOLUTE_NAME_START 3 | |
922 | #else | |
923 | #define ABSOLUTE_NAME_START 1 | |
924 | #endif | |
925 | ||
926 | // Given the absolute current directory and an absolute file name, returns a relative file name. | |
927 | // For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given, | |
928 | // GetRelativeFilename will return ..\whee\text.txt. | |
929 | ||
930 | const gchar *file_GetRelativeFilename ( gchar *currentDirectory, gchar *absoluteFilename ) | |
931 | { | |
932 | gint afMarker = 0, rfMarker = 0; | |
933 | gint cdLen = 0, afLen = 0; | |
934 | gint i = 0; | |
935 | gint levels = 0; | |
936 | static gchar relativeFilename[MAXPATHLEN+1]; | |
937 | ||
938 | cdLen = strlen(currentDirectory); | |
939 | afLen = strlen(absoluteFilename); | |
940 | ||
941 | // make sure the names are not too long or too short | |
942 | if (cdLen > MAXPATHLEN || cdLen < ABSOLUTE_NAME_START+1 || | |
943 | afLen > MAXPATHLEN || afLen < ABSOLUTE_NAME_START+1) { | |
944 | return NULL; | |
945 | } | |
946 | ||
947 | // Handle DOS names that are on different drives: | |
948 | if (currentDirectory[0] != absoluteFilename[0]) { | |
949 | // not on the same drive, so only absolute filename will do | |
ed486892 | 950 | g_strlcpy(relativeFilename, absoluteFilename, MAXPATHLEN+1); |
88542aa9 RN |
951 | return relativeFilename; |
952 | } | |
953 | ||
954 | // they are on the same drive, find out how much of the current directory | |
955 | // is in the absolute filename | |
956 | i = ABSOLUTE_NAME_START; | |
957 | while (i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i]) { | |
958 | i++; | |
959 | } | |
960 | ||
961 | if (i == cdLen && (absoluteFilename[i] == G_DIR_SEPARATOR || absoluteFilename[i-1] == G_DIR_SEPARATOR)) { | |
962 | // the whole current directory name is in the file name, | |
963 | // so we just trim off the current directory name to get the | |
964 | // current file name. | |
965 | if (absoluteFilename[i] == G_DIR_SEPARATOR) { | |
966 | // a directory name might have a trailing slash but a relative | |
967 | // file name should not have a leading one... | |
968 | i++; | |
969 | } | |
970 | ||
ed486892 | 971 | g_strlcpy(relativeFilename, &absoluteFilename[i], MAXPATHLEN+1); |
88542aa9 RN |
972 | return relativeFilename; |
973 | } | |
974 | ||
975 | // The file is not in a child directory of the current directory, so we | |
976 | // need to step back the appropriate number of parent directories by | |
977 | // using "..\"s. First find out how many levels deeper we are than the | |
978 | // common directory | |
979 | afMarker = i; | |
980 | levels = 1; | |
981 | ||
982 | // count the number of directory levels we have to go up to get to the | |
983 | // common directory | |
984 | while (i < cdLen) { | |
985 | i++; | |
986 | if (currentDirectory[i] == G_DIR_SEPARATOR) { | |
987 | // make sure it's not a trailing slash | |
988 | i++; | |
989 | if (currentDirectory[i] != '\0') { | |
990 | levels++; | |
991 | } | |
992 | } | |
993 | } | |
994 | ||
995 | // move the absolute filename marker back to the start of the directory name | |
996 | // that it has stopped in. | |
997 | while (afMarker > 0 && absoluteFilename[afMarker-1] != G_DIR_SEPARATOR) { | |
998 | afMarker--; | |
999 | } | |
1000 | ||
1001 | // check that the result will not be too long | |
1002 | if (levels * 3 + afLen - afMarker > MAXPATHLEN) { | |
1003 | return NULL; | |
1004 | } | |
1005 | ||
1006 | // add the appropriate number of "..\"s. | |
1007 | rfMarker = 0; | |
1008 | for (i = 0; i < levels; i++) { | |
1009 | relativeFilename[rfMarker++] = '.'; | |
1010 | relativeFilename[rfMarker++] = '.'; | |
1011 | relativeFilename[rfMarker++] = G_DIR_SEPARATOR; | |
1012 | } | |
1013 | ||
1014 | // copy the rest of the filename into the result string | |
1015 | strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]); | |
1016 | ||
1017 | return relativeFilename; | |
1018 | } | |
1019 | /* END http://www.codeguru.com/cpp/misc/misc/fileanddirectorynaming/article.php/c263 */ |