]>
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> | |
1b14d0d2 | 5 | * Copyright (C) 2012-2013, Rob Norris <rw_norris@hotmail.com> |
50a14534 EB |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
8c00358d GB |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
50a14534 | 25 | |
8c00358d | 26 | #ifdef HAVE_MATH_H |
a2817d3c | 27 | #include <math.h> |
8c00358d GB |
28 | #endif |
29 | ||
50a14534 EB |
30 | #include "viking.h" |
31 | ||
32 | #include <ctype.h> | |
8c00358d | 33 | #ifdef HAVE_STRING_H |
50a14534 | 34 | #include <string.h> |
8c00358d | 35 | #endif |
50a14534 EB |
36 | |
37 | #include <stdlib.h> | |
38 | /* strtod */ | |
39 | ||
0d2b891f RN |
40 | typedef struct { |
41 | FILE *f; | |
42 | gboolean is_route; | |
43 | } TP_write_info_type; | |
50a14534 | 44 | |
ce4bd1cf | 45 | static void a_gpspoint_write_track ( const gpointer id, const VikTrack *t, FILE *f ); |
0d2b891f | 46 | static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info ); |
c9570f86 | 47 | static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f ); |
50a14534 | 48 | |
50a14534 EB |
49 | /* outline for file gpspoint.c |
50 | ||
51 | reading file: | |
52 | ||
53 | take a line. | |
54 | get first tag, if not type, skip it. | |
55 | if type, record type. if waypoint list, etc move on. if track, make a new track, make it current track, add it, etc. | |
56 | if waypoint, read on and store to the waypoint. | |
57 | if trackpoint, make trackpoint, store to current track (error / skip if none) | |
58 | ||
59 | */ | |
60 | ||
61 | /* Thanks to etrex-cache's gpsbabel's gpspoint.c for starting me off! */ | |
3bdcb42e RN |
62 | #define VIKING_LINE_SIZE 4096 |
63 | static char line_buffer[VIKING_LINE_SIZE]; | |
50a14534 EB |
64 | |
65 | #define GPSPOINT_TYPE_NONE 0 | |
66 | #define GPSPOINT_TYPE_WAYPOINT 1 | |
67 | #define GPSPOINT_TYPE_TRACKPOINT 2 | |
0d2b891f | 68 | #define GPSPOINT_TYPE_ROUTEPOINT 3 |
50a14534 | 69 | #define GPSPOINT_TYPE_TRACK 4 |
0d2b891f | 70 | #define GPSPOINT_TYPE_ROUTE 5 |
50a14534 EB |
71 | |
72 | static VikTrack *current_track; /* pointer to pointer to first GList */ | |
73 | ||
74 | static gint line_type = GPSPOINT_TYPE_NONE; | |
75 | static struct LatLon line_latlon; | |
76 | static gchar *line_name; | |
77 | static gchar *line_comment; | |
6b2f262e | 78 | static gchar *line_description; |
be455472 | 79 | static gchar *line_source; |
01cfda98 | 80 | static gchar *line_xtype; |
b1453c16 | 81 | static gchar *line_color; |
387ff7ac RN |
82 | static gint line_name_label = 0; |
83 | static gint line_dist_label = 0; | |
50a14534 | 84 | static gchar *line_image; |
acaf7113 | 85 | static gchar *line_symbol; |
50a14534 EB |
86 | static gboolean line_newsegment = FALSE; |
87 | static gboolean line_has_timestamp = FALSE; | |
88 | static time_t line_timestamp = 0; | |
89 | static gdouble line_altitude = VIK_DEFAULT_ALTITUDE; | |
90 | static gboolean line_visible = TRUE; | |
a2817d3c QT |
91 | |
92 | static gboolean line_extended = FALSE; | |
93 | static gdouble line_speed = NAN; | |
94 | static gdouble line_course = NAN; | |
95 | static gint line_sat = 0; | |
96 | static gint line_fix = 0; | |
9d342fdd RN |
97 | static gdouble line_hdop = VIK_DEFAULT_DOP; |
98 | static gdouble line_vdop = VIK_DEFAULT_DOP; | |
99 | static gdouble line_pdop = VIK_DEFAULT_DOP; | |
50a14534 EB |
100 | /* other possible properties go here */ |
101 | ||
102 | ||
7dfdf988 RN |
103 | static void gpspoint_process_tag ( const gchar *tag, guint len ); |
104 | static void gpspoint_process_key_and_value ( const gchar *key, guint key_len, const gchar *value, guint value_len ); | |
50a14534 EB |
105 | |
106 | static gchar *slashdup(const gchar *str) | |
107 | { | |
7dfdf988 RN |
108 | size_t len = strlen(str); |
109 | size_t need_bs_count, i, j; | |
50a14534 EB |
110 | gchar *rv; |
111 | for ( i = 0, need_bs_count = 0; i < len; i++ ) | |
112 | if ( str[i] == '\\' || str[i] == '"' ) | |
113 | need_bs_count++; | |
114 | rv = g_malloc ( (len+need_bs_count+1) * sizeof(gchar) ); | |
115 | for ( i = 0, j = 0; i < len; i++, j++ ) | |
116 | { | |
117 | if ( str[i] == '\\' || str[i] == '"' ) | |
118 | rv[j++] = '\\'; | |
119 | rv[j] = str[i]; | |
56dbc6f4 RN |
120 | // Basic normalization of strings - replace Linefeed and Carriage returns as blanks. |
121 | // although allowed in GPX Spec - Viking file format can't handle multi-line strings yet... | |
122 | if ( str[i] == '\n' || str[i] == '\r' ) | |
123 | rv[j] = ' '; | |
50a14534 EB |
124 | } |
125 | rv[j] = '\0'; | |
126 | return rv; | |
127 | } | |
128 | ||
129 | static gchar *deslashndup ( const gchar *str, guint16 len ) | |
130 | { | |
131 | guint16 i,j, bs_count, new_len; | |
132 | gboolean backslash = FALSE; | |
133 | gchar *rv; | |
134 | ||
135 | if ( len < 1 ) | |
136 | return NULL; | |
137 | ||
138 | for ( i = 0, bs_count = 0; i < len; i++ ) | |
139 | if ( str[i] == '\\' ) | |
140 | { | |
141 | bs_count++; | |
142 | i++; | |
143 | } | |
144 | ||
145 | if ( str[i-1] == '\\' && (len == 1 || str[i-2] != '\\') ) | |
146 | bs_count--; | |
147 | ||
148 | new_len = len - bs_count; | |
149 | rv = g_malloc ( (new_len+1) * sizeof(gchar) ); | |
150 | for ( i = 0, j = 0; i < len && j < new_len; i++ ) | |
151 | if ( str[i] == '\\' && !backslash ) | |
152 | backslash = TRUE; | |
153 | else | |
154 | { | |
155 | rv[j++] = str[i]; | |
156 | backslash = FALSE; | |
157 | } | |
158 | ||
159 | rv[new_len] = '\0'; | |
160 | return rv; | |
161 | } | |
162 | ||
cb5ec7a8 RN |
163 | /* |
164 | * Returns whether file read was a success | |
165 | * No obvious way to test for a 'gpspoint' file, | |
166 | * thus set a flag if any actual tag found during processing of the file | |
167 | */ | |
1b14d0d2 | 168 | gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f, const gchar *dirpath ) { |
50a14534 EB |
169 | VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw ); |
170 | gchar *tag_start, *tag_end; | |
50a14534 EB |
171 | g_assert ( f != NULL && trw != NULL ); |
172 | line_type = 0; | |
173 | line_timestamp = 0; | |
174 | line_newsegment = FALSE; | |
acaf7113 AF |
175 | line_image = NULL; |
176 | line_symbol = NULL; | |
50a14534 | 177 | current_track = NULL; |
cb5ec7a8 RN |
178 | gboolean have_read_something = FALSE; |
179 | ||
3bdcb42e | 180 | while (fgets(line_buffer, VIKING_LINE_SIZE, f)) |
50a14534 EB |
181 | { |
182 | gboolean inside_quote = 0; | |
183 | gboolean backslash = 0; | |
184 | ||
185 | line_buffer[strlen(line_buffer)-1] = '\0'; /* chop off newline */ | |
186 | ||
187 | /* for gpspoint files wrapped inside */ | |
119ada4c RN |
188 | if ( strlen(line_buffer) >= 13 && strncmp ( line_buffer, "~EndLayerData", 13 ) == 0 ) { |
189 | // Even just a blank TRW is ok when in a .vik file | |
190 | have_read_something = TRUE; | |
50a14534 | 191 | break; |
119ada4c | 192 | } |
50a14534 | 193 | |
cb5ec7a8 | 194 | /* each line: nullify stuff, make thing if nes, free name if ness */ |
50a14534 EB |
195 | tag_start = line_buffer; |
196 | for (;;) | |
197 | { | |
198 | /* my addition: find first non-whitespace character. if the null, skip line. */ | |
199 | while (*tag_start != '\0' && isspace(*tag_start)) | |
200 | tag_start++; | |
5934bd21 | 201 | if (*tag_start == '\0') |
50a14534 EB |
202 | break; |
203 | ||
204 | if (*tag_start == '#') | |
205 | break; | |
206 | ||
207 | tag_end = tag_start; | |
208 | if (*tag_end == '"') | |
209 | inside_quote = !inside_quote; | |
210 | while (*tag_end != '\0' && (!isspace(*tag_end) || inside_quote)) { | |
211 | tag_end++; | |
212 | if (*tag_end == '\\' && !backslash) | |
213 | backslash = TRUE; | |
214 | else if (backslash) | |
215 | backslash = FALSE; | |
216 | else if (*tag_end == '"') | |
217 | inside_quote = !inside_quote; | |
218 | } | |
219 | ||
7dfdf988 RN |
220 | // Won't have super massively long strings, so potential truncation in cast is acceptable. |
221 | guint len = (guint)(tag_end - tag_start); | |
ce7ec497 | 222 | gpspoint_process_tag ( tag_start, len ); |
50a14534 EB |
223 | |
224 | if (*tag_end == '\0' ) | |
225 | break; | |
226 | else | |
227 | tag_start = tag_end+1; | |
228 | } | |
229 | if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name) | |
230 | { | |
cb5ec7a8 | 231 | have_read_something = TRUE; |
50a14534 | 232 | VikWaypoint *wp = vik_waypoint_new(); |
50a14534 EB |
233 | wp->visible = line_visible; |
234 | wp->altitude = line_altitude; | |
a2d207d7 RN |
235 | wp->has_timestamp = line_has_timestamp; |
236 | wp->timestamp = line_timestamp; | |
50a14534 EB |
237 | |
238 | vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon ); | |
239 | ||
805d282e EB |
240 | vik_trw_layer_filein_add_waypoint ( trw, line_name, wp ); |
241 | g_free ( line_name ); | |
242 | line_name = NULL; | |
50a14534 EB |
243 | |
244 | if ( line_comment ) | |
50a14534 | 245 | vik_waypoint_set_comment ( wp, line_comment ); |
50a14534 | 246 | |
6b2f262e | 247 | if ( line_description ) |
6b2f262e | 248 | vik_waypoint_set_description ( wp, line_description ); |
6b2f262e | 249 | |
be455472 RN |
250 | if ( line_source ) |
251 | vik_waypoint_set_source ( wp, line_source ); | |
252 | ||
01cfda98 RN |
253 | if ( line_xtype ) |
254 | vik_waypoint_set_type ( wp, line_xtype ); | |
255 | ||
1b14d0d2 RN |
256 | if ( line_image ) { |
257 | // Ensure the filename is absolute | |
258 | if ( g_path_is_absolute ( line_image ) ) | |
259 | vik_waypoint_set_image ( wp, line_image ); | |
260 | else { | |
261 | // Otherwise create the absolute filename from the directory of the .vik file & and the relative filename | |
262 | gchar *full = g_strconcat(dirpath, G_DIR_SEPARATOR_S, line_image, NULL); | |
263 | gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name | |
264 | vik_waypoint_set_image ( wp, absolute ); | |
265 | g_free ( absolute ); | |
266 | g_free ( full ); | |
267 | } | |
268 | } | |
50a14534 | 269 | |
acaf7113 | 270 | if ( line_symbol ) |
acaf7113 | 271 | vik_waypoint_set_symbol ( wp, line_symbol ); |
50a14534 | 272 | } |
0d2b891f | 273 | else if ((line_type == GPSPOINT_TYPE_TRACK || line_type == GPSPOINT_TYPE_ROUTE) && line_name) |
50a14534 | 274 | { |
cb5ec7a8 | 275 | have_read_something = TRUE; |
50a14534 | 276 | VikTrack *pl = vik_track_new(); |
387ff7ac RN |
277 | // NB don't set defaults here as all properties are stored in the GPS_POINT format |
278 | //vik_track_set_defaults ( pl ); | |
50a14534 EB |
279 | |
280 | /* Thanks to Peter Jones for this Fix */ | |
281 | if (!line_name) line_name = g_strdup("UNK"); | |
282 | ||
283 | pl->visible = line_visible; | |
0d2b891f | 284 | pl->is_route = (line_type == GPSPOINT_TYPE_ROUTE); |
50a14534 | 285 | |
50a14534 | 286 | if ( line_comment ) |
50a14534 | 287 | vik_track_set_comment ( pl, line_comment ); |
50a14534 | 288 | |
6b2f262e | 289 | if ( line_description ) |
6b2f262e | 290 | vik_track_set_description ( pl, line_description ); |
6b2f262e | 291 | |
be455472 RN |
292 | if ( line_source ) |
293 | vik_track_set_source ( pl, line_source ); | |
294 | ||
01cfda98 RN |
295 | if ( line_xtype ) |
296 | vik_track_set_type ( pl, line_xtype ); | |
297 | ||
b1453c16 RN |
298 | if ( line_color ) |
299 | { | |
300 | if ( gdk_color_parse ( line_color, &(pl->color) ) ) | |
301 | pl->has_color = TRUE; | |
302 | } | |
303 | ||
387ff7ac RN |
304 | pl->draw_name_mode = line_name_label; |
305 | pl->max_number_dist_labels = line_dist_label; | |
306 | ||
50a14534 | 307 | pl->trackpoints = NULL; |
805d282e EB |
308 | vik_trw_layer_filein_add_track ( trw, line_name, pl ); |
309 | g_free ( line_name ); | |
310 | line_name = NULL; | |
50a14534 EB |
311 | |
312 | current_track = pl; | |
313 | } | |
0d2b891f | 314 | else if ((line_type == GPSPOINT_TYPE_TRACKPOINT || line_type == GPSPOINT_TYPE_ROUTEPOINT) && current_track) |
50a14534 | 315 | { |
cb5ec7a8 | 316 | have_read_something = TRUE; |
a2817d3c | 317 | VikTrackpoint *tp = vik_trackpoint_new(); |
50a14534 EB |
318 | vik_coord_load_from_latlon ( &(tp->coord), coord_mode, &line_latlon ); |
319 | tp->newsegment = line_newsegment; | |
320 | tp->has_timestamp = line_has_timestamp; | |
321 | tp->timestamp = line_timestamp; | |
322 | tp->altitude = line_altitude; | |
b45865b4 | 323 | vik_trackpoint_set_name ( tp, line_name ); |
a2817d3c | 324 | if (line_extended) { |
a2817d3c QT |
325 | tp->speed = line_speed; |
326 | tp->course = line_course; | |
327 | tp->nsats = line_sat; | |
328 | tp->fix_mode = line_fix; | |
9d342fdd RN |
329 | tp->hdop = line_hdop; |
330 | tp->vdop = line_vdop; | |
331 | tp->pdop = line_pdop; | |
a2817d3c | 332 | } |
50a14534 EB |
333 | current_track->trackpoints = g_list_append ( current_track->trackpoints, tp ); |
334 | } | |
335 | ||
336 | if (line_name) | |
337 | g_free ( line_name ); | |
338 | line_name = NULL; | |
6b2f262e | 339 | if (line_comment) |
50a14534 | 340 | g_free ( line_comment ); |
6b2f262e RN |
341 | if (line_description) |
342 | g_free ( line_description ); | |
be455472 RN |
343 | if (line_source) |
344 | g_free ( line_source ); | |
01cfda98 RN |
345 | if (line_xtype) |
346 | g_free ( line_xtype ); | |
b1453c16 RN |
347 | if (line_color) |
348 | g_free ( line_color ); | |
50a14534 EB |
349 | if (line_image) |
350 | g_free ( line_image ); | |
acaf7113 AF |
351 | if (line_symbol) |
352 | g_free ( line_symbol ); | |
50a14534 | 353 | line_comment = NULL; |
6b2f262e | 354 | line_description = NULL; |
be455472 | 355 | line_source = NULL; |
01cfda98 | 356 | line_xtype = NULL; |
b1453c16 | 357 | line_color = NULL; |
50a14534 | 358 | line_image = NULL; |
acaf7113 | 359 | line_symbol = NULL; |
50a14534 EB |
360 | line_type = GPSPOINT_TYPE_NONE; |
361 | line_newsegment = FALSE; | |
362 | line_has_timestamp = FALSE; | |
363 | line_timestamp = 0; | |
364 | line_altitude = VIK_DEFAULT_ALTITUDE; | |
365 | line_visible = TRUE; | |
acaf7113 | 366 | line_symbol = NULL; |
a2817d3c QT |
367 | |
368 | line_extended = FALSE; | |
369 | line_speed = NAN; | |
370 | line_course = NAN; | |
371 | line_sat = 0; | |
372 | line_fix = 0; | |
9d342fdd RN |
373 | line_hdop = VIK_DEFAULT_DOP; |
374 | line_vdop = VIK_DEFAULT_DOP; | |
375 | line_pdop = VIK_DEFAULT_DOP; | |
387ff7ac RN |
376 | line_name_label = 0; |
377 | line_dist_label = 0; | |
50a14534 | 378 | } |
cb5ec7a8 RN |
379 | |
380 | return have_read_something; | |
50a14534 EB |
381 | } |
382 | ||
383 | /* Tag will be of a few defined forms: | |
384 | ^[:alpha:]*=".*"$ | |
385 | ^[:alpha:]*=.*$ | |
386 | ||
387 | <invalid tag> | |
388 | ||
389 | So we must determine end of tag name, start of value, end of value. | |
390 | */ | |
7dfdf988 | 391 | static void gpspoint_process_tag ( const gchar *tag, guint len ) |
50a14534 EB |
392 | { |
393 | const gchar *key_end, *value_start, *value_end; | |
394 | ||
395 | /* Searching for key end */ | |
396 | key_end = tag; | |
397 | ||
398 | while (++key_end - tag < len) | |
399 | if (*key_end == '=') | |
400 | break; | |
401 | ||
402 | if (key_end - tag == len) | |
403 | return; /* no good */ | |
404 | ||
405 | if (key_end - tag == len + 1) | |
406 | value_start = value_end = 0; /* size = 0 */ | |
407 | else | |
408 | { | |
409 | value_start = key_end + 1; /* equal_sign plus one */ | |
410 | ||
411 | if (*value_start == '"') | |
412 | { | |
413 | value_start++; | |
414 | if (*value_start == '"') | |
415 | value_start = value_end = 0; /* size = 0 */ | |
416 | else | |
417 | { | |
418 | if (*(tag+len-1) == '"') | |
a81479e1 | 419 | value_end = tag + len - 1; |
50a14534 EB |
420 | else |
421 | return; /* bogus */ | |
422 | } | |
423 | } | |
424 | else | |
425 | value_end = tag + len; /* value start really IS value start. */ | |
426 | ||
a81479e1 RN |
427 | // Detect broken lines which end without any text or the enclosing ". i.e. like: comment=" |
428 | if ( (value_end - value_start) < 0 ) | |
429 | return; | |
430 | ||
50a14534 EB |
431 | gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start); |
432 | } | |
433 | } | |
434 | ||
435 | /* | |
436 | value = NULL for none | |
437 | */ | |
7dfdf988 | 438 | static void gpspoint_process_key_and_value ( const gchar *key, guint key_len, const gchar *value, guint value_len ) |
50a14534 EB |
439 | { |
440 | if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 ) | |
441 | { | |
442 | if (value == NULL) | |
443 | line_type = GPSPOINT_TYPE_NONE; | |
444 | else if (value_len == 5 && strncasecmp( value, "track", value_len ) == 0 ) | |
445 | line_type = GPSPOINT_TYPE_TRACK; | |
446 | else if (value_len == 10 && strncasecmp( value, "trackpoint", value_len ) == 0 ) | |
447 | line_type = GPSPOINT_TYPE_TRACKPOINT; | |
448 | else if (value_len == 8 && strncasecmp( value, "waypoint", value_len ) == 0 ) | |
449 | line_type = GPSPOINT_TYPE_WAYPOINT; | |
0d2b891f RN |
450 | else if (value_len == 5 && strncasecmp( value, "route", value_len ) == 0 ) |
451 | line_type = GPSPOINT_TYPE_ROUTE; | |
452 | else if (value_len == 10 && strncasecmp( value, "routepoint", value_len ) == 0 ) | |
453 | line_type = GPSPOINT_TYPE_ROUTEPOINT; | |
50a14534 EB |
454 | else |
455 | /* all others are ignored */ | |
456 | line_type = GPSPOINT_TYPE_NONE; | |
457 | } | |
458 | else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL) | |
459 | { | |
460 | if (line_name == NULL) | |
461 | { | |
81eb2211 | 462 | line_name = deslashndup ( value, value_len ); |
50a14534 EB |
463 | } |
464 | } | |
465 | else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL) | |
466 | { | |
467 | if (line_comment == NULL) | |
468 | line_comment = deslashndup ( value, value_len ); | |
469 | } | |
6b2f262e RN |
470 | else if (key_len == 11 && strncasecmp( key, "description", key_len ) == 0 && value != NULL) |
471 | { | |
472 | if (line_description == NULL) | |
473 | line_description = deslashndup ( value, value_len ); | |
474 | } | |
be455472 RN |
475 | else if (key_len == 6 && strncasecmp( key, "source", key_len ) == 0 && value != NULL) |
476 | { | |
477 | if (line_source == NULL) | |
478 | line_source = deslashndup ( value, value_len ); | |
479 | } | |
01cfda98 RN |
480 | // NB using 'xtype' to differentiate from our own 'type' key |
481 | else if (key_len == 5 && strncasecmp( key, "xtype", key_len ) == 0 && value != NULL) | |
482 | { | |
483 | if (line_xtype == NULL) | |
484 | line_xtype = deslashndup ( value, value_len ); | |
485 | } | |
b1453c16 RN |
486 | else if (key_len == 5 && strncasecmp( key, "color", key_len ) == 0 && value != NULL) |
487 | { | |
488 | if (line_color == NULL) | |
489 | line_color = deslashndup ( value, value_len ); | |
490 | } | |
387ff7ac RN |
491 | else if (key_len == 14 && strncasecmp( key, "draw_name_mode", key_len ) == 0 && value != NULL) |
492 | { | |
493 | line_name_label = atoi(value); | |
494 | } | |
495 | else if (key_len == 18 && strncasecmp( key, "number_dist_labels", key_len ) == 0 && value != NULL) | |
496 | { | |
497 | line_dist_label = atoi(value); | |
498 | } | |
50a14534 EB |
499 | else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL) |
500 | { | |
501 | if (line_image == NULL) | |
502 | line_image = deslashndup ( value, value_len ); | |
503 | } | |
504 | else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL) | |
505 | { | |
d1cd12c0 | 506 | line_latlon.lat = g_ascii_strtod(value, NULL); |
50a14534 EB |
507 | } |
508 | else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL) | |
509 | { | |
d1cd12c0 | 510 | line_latlon.lon = g_ascii_strtod(value, NULL); |
50a14534 EB |
511 | } |
512 | else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL) | |
513 | { | |
d1cd12c0 | 514 | line_altitude = g_ascii_strtod(value, NULL); |
50a14534 | 515 | } |
e69ac989 | 516 | else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value != NULL && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T') |
50a14534 EB |
517 | { |
518 | line_visible = FALSE; | |
519 | } | |
acaf7113 AF |
520 | else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL) |
521 | { | |
522 | line_symbol = g_strndup ( value, value_len ); | |
523 | } | |
50a14534 EB |
524 | else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL) |
525 | { | |
d1cd12c0 | 526 | line_timestamp = g_ascii_strtod(value, NULL); |
50a14534 EB |
527 | if ( line_timestamp != 0x80000000 ) |
528 | line_has_timestamp = TRUE; | |
529 | } | |
530 | else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL) | |
531 | { | |
532 | line_newsegment = TRUE; | |
533 | } | |
a2817d3c QT |
534 | else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL) |
535 | { | |
536 | line_extended = TRUE; | |
537 | } | |
538 | else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL) | |
539 | { | |
d1cd12c0 | 540 | line_speed = g_ascii_strtod(value, NULL); |
a2817d3c QT |
541 | } |
542 | else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL) | |
543 | { | |
d1cd12c0 | 544 | line_course = g_ascii_strtod(value, NULL); |
a2817d3c QT |
545 | } |
546 | else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL) | |
547 | { | |
548 | line_sat = atoi(value); | |
549 | } | |
550 | else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL) | |
551 | { | |
552 | line_fix = atoi(value); | |
553 | } | |
9d342fdd RN |
554 | else if (key_len == 4 && strncasecmp( key, "hdop", key_len ) == 0 && value != NULL) |
555 | { | |
556 | line_hdop = g_ascii_strtod(value, NULL); | |
557 | } | |
558 | else if (key_len == 4 && strncasecmp( key, "vdop", key_len ) == 0 && value != NULL) | |
559 | { | |
560 | line_vdop = g_ascii_strtod(value, NULL); | |
561 | } | |
562 | else if (key_len == 4 && strncasecmp( key, "pdop", key_len ) == 0 && value != NULL) | |
563 | { | |
564 | line_pdop = g_ascii_strtod(value, NULL); | |
565 | } | |
50a14534 EB |
566 | } |
567 | ||
c9570f86 | 568 | static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f ) |
50a14534 | 569 | { |
e798750d | 570 | struct LatLon ll; |
e778b260 RN |
571 | gchar s_lat[COORDS_STR_BUFFER_SIZE]; |
572 | gchar s_lon[COORDS_STR_BUFFER_SIZE]; | |
e69ac989 RN |
573 | // Sanity clauses |
574 | if ( !wp ) | |
c9570f86 | 575 | return; |
e69ac989 RN |
576 | if ( !(wp->name) ) |
577 | return; | |
578 | ||
50a14534 | 579 | vik_coord_to_latlon ( &(wp->coord), &ll ); |
e778b260 RN |
580 | a_coords_dtostr_buffer ( ll.lat, s_lat ); |
581 | a_coords_dtostr_buffer ( ll.lon, s_lon ); | |
81eb2211 RN |
582 | gchar *tmp_name = slashdup(wp->name); |
583 | fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, tmp_name ); | |
584 | g_free ( tmp_name ); | |
161aa492 EB |
585 | |
586 | if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) { | |
e778b260 RN |
587 | gchar s_alt[COORDS_STR_BUFFER_SIZE]; |
588 | a_coords_dtostr_buffer ( wp->altitude, s_alt ); | |
161aa492 | 589 | fprintf ( f, " altitude=\"%s\"", s_alt ); |
161aa492 | 590 | } |
a2d207d7 RN |
591 | if ( wp->has_timestamp ) |
592 | fprintf ( f, " unixtime=\"%ld\"", wp->timestamp ); | |
50a14534 EB |
593 | if ( wp->comment ) |
594 | { | |
595 | gchar *tmp_comment = slashdup(wp->comment); | |
596 | fprintf ( f, " comment=\"%s\"", tmp_comment ); | |
597 | g_free ( tmp_comment ); | |
598 | } | |
6b2f262e RN |
599 | if ( wp->description ) |
600 | { | |
601 | gchar *tmp_description = slashdup(wp->description); | |
602 | fprintf ( f, " description=\"%s\"", tmp_description ); | |
603 | g_free ( tmp_description ); | |
604 | } | |
be455472 RN |
605 | if ( wp->source ) |
606 | { | |
607 | gchar *tmp_source = slashdup(wp->source); | |
608 | fprintf ( f, " source=\"%s\"", tmp_source ); | |
609 | g_free ( tmp_source ); | |
610 | } | |
01cfda98 RN |
611 | if ( wp->type ) |
612 | { | |
613 | gchar *tmp_type = slashdup(wp->type); | |
614 | fprintf ( f, " xtype=\"%s\"", tmp_type ); | |
615 | g_free ( tmp_type ); | |
616 | } | |
50a14534 EB |
617 | if ( wp->image ) |
618 | { | |
88542aa9 RN |
619 | gchar *tmp_image = NULL; |
620 | gchar *cwd = NULL; | |
621 | if ( a_vik_get_file_ref_format() == VIK_FILE_REF_FORMAT_RELATIVE ) { | |
622 | cwd = g_get_current_dir(); | |
623 | if ( cwd ) | |
624 | tmp_image = g_strdup ( file_GetRelativeFilename ( cwd, wp->image ) ); | |
625 | } | |
626 | ||
627 | // if cwd not available - use image filename as is | |
628 | // this should be an absolute path as set in thumbnails | |
629 | if ( !cwd ) | |
630 | tmp_image = slashdup(wp->image); | |
631 | ||
632 | if ( tmp_image ) | |
633 | fprintf ( f, " image=\"%s\"", tmp_image ); | |
634 | ||
635 | g_free ( cwd ); | |
50a14534 EB |
636 | g_free ( tmp_image ); |
637 | } | |
acaf7113 AF |
638 | if ( wp->symbol ) |
639 | { | |
f8c92775 RN |
640 | // Due to changes in garminsymbols - the symbol name is now in Title Case |
641 | // However to keep newly generated .vik files better compatible with older Viking versions | |
642 | // The symbol names will always be lowercase | |
643 | gchar *tmp_symbol = g_utf8_strdown(wp->symbol, -1); | |
644 | fprintf ( f, " symbol=\"%s\"", tmp_symbol ); | |
645 | g_free ( tmp_symbol ); | |
acaf7113 | 646 | } |
50a14534 EB |
647 | if ( ! wp->visible ) |
648 | fprintf ( f, " visible=\"n\"" ); | |
649 | fprintf ( f, "\n" ); | |
650 | } | |
651 | ||
0d2b891f | 652 | static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info ) |
50a14534 | 653 | { |
e798750d | 654 | struct LatLon ll; |
e778b260 RN |
655 | gchar s_lat[COORDS_STR_BUFFER_SIZE]; |
656 | gchar s_lon[COORDS_STR_BUFFER_SIZE]; | |
657 | gchar s_alt[COORDS_STR_BUFFER_SIZE]; | |
50a14534 EB |
658 | vik_coord_to_latlon ( &(tp->coord), &ll ); |
659 | ||
0d2b891f RN |
660 | FILE *f = write_info->f; |
661 | ||
e778b260 RN |
662 | a_coords_dtostr_buffer ( ll.lat, s_lat ); |
663 | a_coords_dtostr_buffer ( ll.lon, s_lon ); | |
0d2b891f | 664 | fprintf ( f, "type=\"%spoint\" latitude=\"%s\" longitude=\"%s\"", write_info->is_route ? "route" : "track", s_lat, s_lon ); |
50a14534 | 665 | |
b45865b4 RN |
666 | if ( tp->name ) { |
667 | gchar *name = slashdup(tp->name); | |
668 | fprintf ( f, " name=\"%s\"", name ); | |
669 | g_free(name); | |
670 | } | |
671 | ||
161aa492 | 672 | if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) { |
e778b260 | 673 | a_coords_dtostr_buffer ( tp->altitude, s_alt ); |
161aa492 | 674 | fprintf ( f, " altitude=\"%s\"", s_alt ); |
161aa492 | 675 | } |
50a14534 EB |
676 | if ( tp->has_timestamp ) |
677 | fprintf ( f, " unixtime=\"%ld\"", tp->timestamp ); | |
678 | if ( tp->newsegment ) | |
679 | fprintf ( f, " newsegment=\"yes\"" ); | |
a2817d3c | 680 | |
376c9177 | 681 | if (!isnan(tp->speed) || !isnan(tp->course) || tp->nsats > 0) { |
a2817d3c QT |
682 | fprintf ( f, " extended=\"yes\"" ); |
683 | if (!isnan(tp->speed)) { | |
e778b260 RN |
684 | gchar s_speed[COORDS_STR_BUFFER_SIZE]; |
685 | a_coords_dtostr_buffer ( tp->speed, s_speed ); | |
a2817d3c | 686 | fprintf ( f, " speed=\"%s\"", s_speed ); |
a2817d3c QT |
687 | } |
688 | if (!isnan(tp->course)) { | |
e778b260 RN |
689 | gchar s_course[COORDS_STR_BUFFER_SIZE]; |
690 | a_coords_dtostr_buffer ( tp->course, s_course ); | |
a2817d3c | 691 | fprintf ( f, " course=\"%s\"", s_course ); |
a2817d3c QT |
692 | } |
693 | if (tp->nsats > 0) | |
694 | fprintf ( f, " sat=\"%d\"", tp->nsats ); | |
695 | if (tp->fix_mode > 0) | |
696 | fprintf ( f, " fix=\"%d\"", tp->fix_mode ); | |
9d342fdd RN |
697 | |
698 | if ( tp->hdop != VIK_DEFAULT_DOP ) { | |
e778b260 RN |
699 | gchar ss[COORDS_STR_BUFFER_SIZE]; |
700 | a_coords_dtostr_buffer ( tp->hdop, ss ); | |
9d342fdd | 701 | fprintf ( f, " hdop=\"%s\"", ss ); |
9d342fdd RN |
702 | } |
703 | if ( tp->vdop != VIK_DEFAULT_DOP ) { | |
e778b260 RN |
704 | gchar ss[COORDS_STR_BUFFER_SIZE]; |
705 | a_coords_dtostr_buffer ( tp->vdop, ss ); | |
9d342fdd | 706 | fprintf ( f, " vdop=\"%s\"", ss ); |
9d342fdd RN |
707 | } |
708 | if ( tp->pdop != VIK_DEFAULT_DOP ) { | |
e778b260 RN |
709 | gchar ss[COORDS_STR_BUFFER_SIZE]; |
710 | a_coords_dtostr_buffer ( tp->pdop, ss ); | |
9d342fdd | 711 | fprintf ( f, " pdop=\"%s\"", ss ); |
9d342fdd | 712 | } |
a2817d3c | 713 | } |
50a14534 EB |
714 | fprintf ( f, "\n" ); |
715 | } | |
716 | ||
717 | ||
6b2f262e | 718 | static void a_gpspoint_write_track ( const gpointer id, const VikTrack *trk, FILE *f ) |
50a14534 | 719 | { |
e979bdab | 720 | // Sanity clauses |
6b2f262e | 721 | if ( !trk ) |
ce4bd1cf | 722 | return; |
6b2f262e | 723 | if ( !(trk->name) ) |
e979bdab RN |
724 | return; |
725 | ||
81eb2211 RN |
726 | gchar *tmp_name = slashdup(trk->name); |
727 | fprintf ( f, "type=\"%s\" name=\"%s\"", trk->is_route ? "route" : "track", tmp_name ); | |
728 | g_free ( tmp_name ); | |
6b2f262e RN |
729 | |
730 | if ( trk->comment ) { | |
731 | gchar *tmp = slashdup(trk->comment); | |
732 | fprintf ( f, " comment=\"%s\"", tmp ); | |
733 | g_free ( tmp ); | |
50a14534 | 734 | } |
6b2f262e RN |
735 | |
736 | if ( trk->description ) { | |
737 | gchar *tmp = slashdup(trk->description); | |
738 | fprintf ( f, " description=\"%s\"", tmp ); | |
739 | g_free ( tmp ); | |
740 | } | |
741 | ||
be455472 RN |
742 | if ( trk->source ) { |
743 | gchar *tmp = slashdup(trk->source); | |
744 | fprintf ( f, " source=\"%s\"", tmp ); | |
745 | g_free ( tmp ); | |
746 | } | |
747 | ||
01cfda98 RN |
748 | if ( trk->type ) { |
749 | gchar *tmp = slashdup(trk->type); | |
750 | fprintf ( f, " xtype=\"%s\"", tmp ); | |
751 | g_free ( tmp ); | |
752 | } | |
753 | ||
b1453c16 RN |
754 | if ( trk->has_color ) { |
755 | fprintf ( f, " color=#%.2x%.2x%.2x", (int)(trk->color.red/256),(int)(trk->color.green/256),(int)(trk->color.blue/256)); | |
756 | } | |
757 | ||
387ff7ac RN |
758 | if ( trk->draw_name_mode > 0 ) |
759 | fprintf ( f, " draw_name_mode=\"%d\"", trk->draw_name_mode ); | |
760 | ||
761 | if ( trk->max_number_dist_labels > 0 ) | |
762 | fprintf ( f, " number_dist_labels=\"%d\"", trk->max_number_dist_labels ); | |
763 | ||
6b2f262e RN |
764 | if ( ! trk->visible ) { |
765 | fprintf ( f, " visible=\"n\"" ); | |
766 | } | |
767 | fprintf ( f, "\n" ); | |
768 | ||
0d2b891f RN |
769 | TP_write_info_type tp_write_info = { f, trk->is_route }; |
770 | g_list_foreach ( trk->trackpoints, (GFunc) a_gpspoint_write_trackpoint, &tp_write_info ); | |
771 | fprintf ( f, "type=\"%send\"\n", trk->is_route ? "route" : "track" ); | |
50a14534 EB |
772 | } |
773 | ||
774 | void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f ) | |
775 | { | |
776 | GHashTable *tracks = vik_trw_layer_get_tracks ( trw ); | |
0d2b891f | 777 | GHashTable *routes = vik_trw_layer_get_routes ( trw ); |
50a14534 EB |
778 | GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw ); |
779 | ||
780 | fprintf ( f, "type=\"waypointlist\"\n" ); | |
781 | g_hash_table_foreach ( waypoints, (GHFunc) a_gpspoint_write_waypoint, f ); | |
782 | fprintf ( f, "type=\"waypointlistend\"\n" ); | |
783 | g_hash_table_foreach ( tracks, (GHFunc) a_gpspoint_write_track, f ); | |
0d2b891f | 784 | g_hash_table_foreach ( routes, (GHFunc) a_gpspoint_write_track, f ); |
50a14534 | 785 | } |