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