]>
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> | |
c9570f86 | 5 | * Copyright (C) 2012, 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 EB |
48 | |
49 | ||
50 | /* outline for file gpspoint.c | |
51 | ||
52 | reading file: | |
53 | ||
54 | take a line. | |
55 | get first tag, if not type, skip it. | |
56 | if type, record type. if waypoint list, etc move on. if track, make a new track, make it current track, add it, etc. | |
57 | if waypoint, read on and store to the waypoint. | |
58 | if trackpoint, make trackpoint, store to current track (error / skip if none) | |
59 | ||
60 | */ | |
61 | ||
62 | /* Thanks to etrex-cache's gpsbabel's gpspoint.c for starting me off! */ | |
63 | ||
64 | static char line_buffer[2048]; | |
65 | ||
66 | #define GPSPOINT_TYPE_NONE 0 | |
67 | #define GPSPOINT_TYPE_WAYPOINT 1 | |
68 | #define GPSPOINT_TYPE_TRACKPOINT 2 | |
0d2b891f | 69 | #define GPSPOINT_TYPE_ROUTEPOINT 3 |
50a14534 | 70 | #define GPSPOINT_TYPE_TRACK 4 |
0d2b891f | 71 | #define GPSPOINT_TYPE_ROUTE 5 |
50a14534 EB |
72 | |
73 | static VikTrack *current_track; /* pointer to pointer to first GList */ | |
74 | ||
75 | static gint line_type = GPSPOINT_TYPE_NONE; | |
76 | static struct LatLon line_latlon; | |
77 | static gchar *line_name; | |
78 | static gchar *line_comment; | |
6b2f262e | 79 | static gchar *line_description; |
b1453c16 | 80 | static gchar *line_color; |
50a14534 | 81 | static gchar *line_image; |
acaf7113 | 82 | static gchar *line_symbol; |
50a14534 EB |
83 | static gboolean line_newsegment = FALSE; |
84 | static gboolean line_has_timestamp = FALSE; | |
85 | static time_t line_timestamp = 0; | |
86 | static gdouble line_altitude = VIK_DEFAULT_ALTITUDE; | |
87 | static gboolean line_visible = TRUE; | |
a2817d3c QT |
88 | |
89 | static gboolean line_extended = FALSE; | |
90 | static gdouble line_speed = NAN; | |
91 | static gdouble line_course = NAN; | |
92 | static gint line_sat = 0; | |
93 | static gint line_fix = 0; | |
50a14534 EB |
94 | /* other possible properties go here */ |
95 | ||
96 | ||
97 | static void gpspoint_process_tag ( const gchar *tag, gint len ); | |
98 | static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len ); | |
99 | ||
100 | static gchar *slashdup(const gchar *str) | |
101 | { | |
102 | guint16 len = strlen(str); | |
103 | guint16 need_bs_count, i, j; | |
104 | gchar *rv; | |
105 | for ( i = 0, need_bs_count = 0; i < len; i++ ) | |
106 | if ( str[i] == '\\' || str[i] == '"' ) | |
107 | need_bs_count++; | |
108 | rv = g_malloc ( (len+need_bs_count+1) * sizeof(gchar) ); | |
109 | for ( i = 0, j = 0; i < len; i++, j++ ) | |
110 | { | |
111 | if ( str[i] == '\\' || str[i] == '"' ) | |
112 | rv[j++] = '\\'; | |
113 | rv[j] = str[i]; | |
114 | } | |
115 | rv[j] = '\0'; | |
116 | return rv; | |
117 | } | |
118 | ||
119 | static gchar *deslashndup ( const gchar *str, guint16 len ) | |
120 | { | |
121 | guint16 i,j, bs_count, new_len; | |
122 | gboolean backslash = FALSE; | |
123 | gchar *rv; | |
124 | ||
125 | if ( len < 1 ) | |
126 | return NULL; | |
127 | ||
128 | for ( i = 0, bs_count = 0; i < len; i++ ) | |
129 | if ( str[i] == '\\' ) | |
130 | { | |
131 | bs_count++; | |
132 | i++; | |
133 | } | |
134 | ||
135 | if ( str[i-1] == '\\' && (len == 1 || str[i-2] != '\\') ) | |
136 | bs_count--; | |
137 | ||
138 | new_len = len - bs_count; | |
139 | rv = g_malloc ( (new_len+1) * sizeof(gchar) ); | |
140 | for ( i = 0, j = 0; i < len && j < new_len; i++ ) | |
141 | if ( str[i] == '\\' && !backslash ) | |
142 | backslash = TRUE; | |
143 | else | |
144 | { | |
145 | rv[j++] = str[i]; | |
146 | backslash = FALSE; | |
147 | } | |
148 | ||
149 | rv[new_len] = '\0'; | |
150 | return rv; | |
151 | } | |
152 | ||
cb5ec7a8 RN |
153 | /* |
154 | * Returns whether file read was a success | |
155 | * No obvious way to test for a 'gpspoint' file, | |
156 | * thus set a flag if any actual tag found during processing of the file | |
157 | */ | |
158 | gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) { | |
50a14534 EB |
159 | VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw ); |
160 | gchar *tag_start, *tag_end; | |
50a14534 EB |
161 | g_assert ( f != NULL && trw != NULL ); |
162 | line_type = 0; | |
163 | line_timestamp = 0; | |
164 | line_newsegment = FALSE; | |
acaf7113 AF |
165 | line_image = NULL; |
166 | line_symbol = NULL; | |
50a14534 | 167 | current_track = NULL; |
cb5ec7a8 RN |
168 | gboolean have_read_something = FALSE; |
169 | ||
50a14534 EB |
170 | while (fgets(line_buffer, 2048, f)) |
171 | { | |
172 | gboolean inside_quote = 0; | |
173 | gboolean backslash = 0; | |
174 | ||
175 | line_buffer[strlen(line_buffer)-1] = '\0'; /* chop off newline */ | |
176 | ||
177 | /* for gpspoint files wrapped inside */ | |
119ada4c RN |
178 | if ( strlen(line_buffer) >= 13 && strncmp ( line_buffer, "~EndLayerData", 13 ) == 0 ) { |
179 | // Even just a blank TRW is ok when in a .vik file | |
180 | have_read_something = TRUE; | |
50a14534 | 181 | break; |
119ada4c | 182 | } |
50a14534 | 183 | |
cb5ec7a8 | 184 | /* each line: nullify stuff, make thing if nes, free name if ness */ |
50a14534 EB |
185 | tag_start = line_buffer; |
186 | for (;;) | |
187 | { | |
188 | /* my addition: find first non-whitespace character. if the null, skip line. */ | |
189 | while (*tag_start != '\0' && isspace(*tag_start)) | |
190 | tag_start++; | |
191 | if (tag_start == '\0') | |
192 | break; | |
193 | ||
194 | if (*tag_start == '#') | |
195 | break; | |
196 | ||
197 | tag_end = tag_start; | |
198 | if (*tag_end == '"') | |
199 | inside_quote = !inside_quote; | |
200 | while (*tag_end != '\0' && (!isspace(*tag_end) || inside_quote)) { | |
201 | tag_end++; | |
202 | if (*tag_end == '\\' && !backslash) | |
203 | backslash = TRUE; | |
204 | else if (backslash) | |
205 | backslash = FALSE; | |
206 | else if (*tag_end == '"') | |
207 | inside_quote = !inside_quote; | |
208 | } | |
209 | ||
210 | gpspoint_process_tag ( tag_start, tag_end - tag_start ); | |
211 | ||
212 | if (*tag_end == '\0' ) | |
213 | break; | |
214 | else | |
215 | tag_start = tag_end+1; | |
216 | } | |
217 | if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name) | |
218 | { | |
cb5ec7a8 | 219 | have_read_something = TRUE; |
50a14534 | 220 | VikWaypoint *wp = vik_waypoint_new(); |
50a14534 EB |
221 | wp->visible = line_visible; |
222 | wp->altitude = line_altitude; | |
50a14534 EB |
223 | |
224 | vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon ); | |
225 | ||
805d282e EB |
226 | vik_trw_layer_filein_add_waypoint ( trw, line_name, wp ); |
227 | g_free ( line_name ); | |
228 | line_name = NULL; | |
50a14534 EB |
229 | |
230 | if ( line_comment ) | |
231 | { | |
232 | vik_waypoint_set_comment ( wp, line_comment ); | |
233 | line_comment = NULL; | |
234 | } | |
235 | ||
6b2f262e RN |
236 | if ( line_description ) |
237 | { | |
238 | vik_waypoint_set_description ( wp, line_description ); | |
239 | line_description = NULL; | |
240 | } | |
241 | ||
50a14534 EB |
242 | if ( line_image ) |
243 | { | |
244 | vik_waypoint_set_image ( wp, line_image ); | |
245 | line_image = NULL; | |
246 | } | |
247 | ||
acaf7113 AF |
248 | if ( line_symbol ) |
249 | { | |
250 | vik_waypoint_set_symbol ( wp, line_symbol ); | |
251 | line_symbol = NULL; | |
252 | } | |
50a14534 | 253 | } |
0d2b891f | 254 | else if ((line_type == GPSPOINT_TYPE_TRACK || line_type == GPSPOINT_TYPE_ROUTE) && line_name) |
50a14534 | 255 | { |
cb5ec7a8 | 256 | have_read_something = TRUE; |
50a14534 | 257 | VikTrack *pl = vik_track_new(); |
50a14534 EB |
258 | |
259 | /* Thanks to Peter Jones for this Fix */ | |
260 | if (!line_name) line_name = g_strdup("UNK"); | |
261 | ||
262 | pl->visible = line_visible; | |
0d2b891f | 263 | pl->is_route = (line_type == GPSPOINT_TYPE_ROUTE); |
50a14534 | 264 | |
50a14534 EB |
265 | if ( line_comment ) |
266 | { | |
267 | vik_track_set_comment ( pl, line_comment ); | |
268 | line_comment = NULL; | |
269 | } | |
270 | ||
6b2f262e RN |
271 | if ( line_description ) |
272 | { | |
273 | vik_track_set_description ( pl, line_description ); | |
274 | line_description = NULL; | |
275 | } | |
276 | ||
b1453c16 RN |
277 | if ( line_color ) |
278 | { | |
279 | if ( gdk_color_parse ( line_color, &(pl->color) ) ) | |
280 | pl->has_color = TRUE; | |
281 | } | |
282 | ||
50a14534 | 283 | pl->trackpoints = NULL; |
805d282e EB |
284 | vik_trw_layer_filein_add_track ( trw, line_name, pl ); |
285 | g_free ( line_name ); | |
286 | line_name = NULL; | |
50a14534 EB |
287 | |
288 | current_track = pl; | |
289 | } | |
0d2b891f | 290 | else if ((line_type == GPSPOINT_TYPE_TRACKPOINT || line_type == GPSPOINT_TYPE_ROUTEPOINT) && current_track) |
50a14534 | 291 | { |
cb5ec7a8 | 292 | have_read_something = TRUE; |
a2817d3c | 293 | VikTrackpoint *tp = vik_trackpoint_new(); |
50a14534 EB |
294 | vik_coord_load_from_latlon ( &(tp->coord), coord_mode, &line_latlon ); |
295 | tp->newsegment = line_newsegment; | |
296 | tp->has_timestamp = line_has_timestamp; | |
297 | tp->timestamp = line_timestamp; | |
298 | tp->altitude = line_altitude; | |
a2817d3c | 299 | if (line_extended) { |
a2817d3c QT |
300 | tp->speed = line_speed; |
301 | tp->course = line_course; | |
302 | tp->nsats = line_sat; | |
303 | tp->fix_mode = line_fix; | |
304 | } | |
50a14534 EB |
305 | current_track->trackpoints = g_list_append ( current_track->trackpoints, tp ); |
306 | } | |
307 | ||
308 | if (line_name) | |
309 | g_free ( line_name ); | |
310 | line_name = NULL; | |
6b2f262e | 311 | if (line_comment) |
50a14534 | 312 | g_free ( line_comment ); |
6b2f262e RN |
313 | if (line_description) |
314 | g_free ( line_description ); | |
b1453c16 RN |
315 | if (line_color) |
316 | g_free ( line_color ); | |
50a14534 EB |
317 | if (line_image) |
318 | g_free ( line_image ); | |
acaf7113 AF |
319 | if (line_symbol) |
320 | g_free ( line_symbol ); | |
50a14534 | 321 | line_comment = NULL; |
6b2f262e | 322 | line_description = NULL; |
b1453c16 | 323 | line_color = NULL; |
50a14534 | 324 | line_image = NULL; |
acaf7113 | 325 | line_symbol = NULL; |
50a14534 EB |
326 | line_type = GPSPOINT_TYPE_NONE; |
327 | line_newsegment = FALSE; | |
328 | line_has_timestamp = FALSE; | |
329 | line_timestamp = 0; | |
330 | line_altitude = VIK_DEFAULT_ALTITUDE; | |
331 | line_visible = TRUE; | |
acaf7113 | 332 | line_symbol = NULL; |
a2817d3c QT |
333 | |
334 | line_extended = FALSE; | |
335 | line_speed = NAN; | |
336 | line_course = NAN; | |
337 | line_sat = 0; | |
338 | line_fix = 0; | |
50a14534 | 339 | } |
cb5ec7a8 RN |
340 | |
341 | return have_read_something; | |
50a14534 EB |
342 | } |
343 | ||
344 | /* Tag will be of a few defined forms: | |
345 | ^[:alpha:]*=".*"$ | |
346 | ^[:alpha:]*=.*$ | |
347 | ||
348 | <invalid tag> | |
349 | ||
350 | So we must determine end of tag name, start of value, end of value. | |
351 | */ | |
352 | static void gpspoint_process_tag ( const gchar *tag, gint len ) | |
353 | { | |
354 | const gchar *key_end, *value_start, *value_end; | |
355 | ||
356 | /* Searching for key end */ | |
357 | key_end = tag; | |
358 | ||
359 | while (++key_end - tag < len) | |
360 | if (*key_end == '=') | |
361 | break; | |
362 | ||
363 | if (key_end - tag == len) | |
364 | return; /* no good */ | |
365 | ||
366 | if (key_end - tag == len + 1) | |
367 | value_start = value_end = 0; /* size = 0 */ | |
368 | else | |
369 | { | |
370 | value_start = key_end + 1; /* equal_sign plus one */ | |
371 | ||
372 | if (*value_start == '"') | |
373 | { | |
374 | value_start++; | |
375 | if (*value_start == '"') | |
376 | value_start = value_end = 0; /* size = 0 */ | |
377 | else | |
378 | { | |
379 | if (*(tag+len-1) == '"') | |
380 | value_end = tag + len - 1; | |
381 | else | |
382 | return; /* bogus */ | |
383 | } | |
384 | } | |
385 | else | |
386 | value_end = tag + len; /* value start really IS value start. */ | |
387 | ||
388 | gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start); | |
389 | } | |
390 | } | |
391 | ||
392 | /* | |
393 | value = NULL for none | |
394 | */ | |
395 | static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len ) | |
396 | { | |
397 | if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 ) | |
398 | { | |
399 | if (value == NULL) | |
400 | line_type = GPSPOINT_TYPE_NONE; | |
401 | else if (value_len == 5 && strncasecmp( value, "track", value_len ) == 0 ) | |
402 | line_type = GPSPOINT_TYPE_TRACK; | |
403 | else if (value_len == 10 && strncasecmp( value, "trackpoint", value_len ) == 0 ) | |
404 | line_type = GPSPOINT_TYPE_TRACKPOINT; | |
405 | else if (value_len == 8 && strncasecmp( value, "waypoint", value_len ) == 0 ) | |
406 | line_type = GPSPOINT_TYPE_WAYPOINT; | |
0d2b891f RN |
407 | else if (value_len == 5 && strncasecmp( value, "route", value_len ) == 0 ) |
408 | line_type = GPSPOINT_TYPE_ROUTE; | |
409 | else if (value_len == 10 && strncasecmp( value, "routepoint", value_len ) == 0 ) | |
410 | line_type = GPSPOINT_TYPE_ROUTEPOINT; | |
50a14534 EB |
411 | else |
412 | /* all others are ignored */ | |
413 | line_type = GPSPOINT_TYPE_NONE; | |
414 | } | |
415 | else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL) | |
416 | { | |
417 | if (line_name == NULL) | |
418 | { | |
81eb2211 | 419 | line_name = deslashndup ( value, value_len ); |
50a14534 EB |
420 | } |
421 | } | |
422 | else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL) | |
423 | { | |
424 | if (line_comment == NULL) | |
425 | line_comment = deslashndup ( value, value_len ); | |
426 | } | |
6b2f262e RN |
427 | else if (key_len == 11 && strncasecmp( key, "description", key_len ) == 0 && value != NULL) |
428 | { | |
429 | if (line_description == NULL) | |
430 | line_description = deslashndup ( value, value_len ); | |
431 | } | |
b1453c16 RN |
432 | else if (key_len == 5 && strncasecmp( key, "color", key_len ) == 0 && value != NULL) |
433 | { | |
434 | if (line_color == NULL) | |
435 | line_color = deslashndup ( value, value_len ); | |
436 | } | |
50a14534 EB |
437 | else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL) |
438 | { | |
439 | if (line_image == NULL) | |
440 | line_image = deslashndup ( value, value_len ); | |
441 | } | |
442 | else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL) | |
443 | { | |
d1cd12c0 | 444 | line_latlon.lat = g_ascii_strtod(value, NULL); |
50a14534 EB |
445 | } |
446 | else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL) | |
447 | { | |
d1cd12c0 | 448 | line_latlon.lon = g_ascii_strtod(value, NULL); |
50a14534 EB |
449 | } |
450 | else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL) | |
451 | { | |
d1cd12c0 | 452 | line_altitude = g_ascii_strtod(value, NULL); |
50a14534 EB |
453 | } |
454 | else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T') | |
455 | { | |
456 | line_visible = FALSE; | |
457 | } | |
acaf7113 AF |
458 | else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL) |
459 | { | |
460 | line_symbol = g_strndup ( value, value_len ); | |
461 | } | |
50a14534 EB |
462 | else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL) |
463 | { | |
d1cd12c0 | 464 | line_timestamp = g_ascii_strtod(value, NULL); |
50a14534 EB |
465 | if ( line_timestamp != 0x80000000 ) |
466 | line_has_timestamp = TRUE; | |
467 | } | |
468 | else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL) | |
469 | { | |
470 | line_newsegment = TRUE; | |
471 | } | |
a2817d3c QT |
472 | else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL) |
473 | { | |
474 | line_extended = TRUE; | |
475 | } | |
476 | else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL) | |
477 | { | |
d1cd12c0 | 478 | line_speed = g_ascii_strtod(value, NULL); |
a2817d3c QT |
479 | } |
480 | else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL) | |
481 | { | |
d1cd12c0 | 482 | line_course = g_ascii_strtod(value, NULL); |
a2817d3c QT |
483 | } |
484 | else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL) | |
485 | { | |
486 | line_sat = atoi(value); | |
487 | } | |
488 | else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL) | |
489 | { | |
490 | line_fix = atoi(value); | |
491 | } | |
50a14534 EB |
492 | } |
493 | ||
c9570f86 | 494 | static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f ) |
50a14534 EB |
495 | { |
496 | static struct LatLon ll; | |
161aa492 | 497 | gchar *s_lat, *s_lon; |
c9570f86 RN |
498 | // Sanity clause |
499 | if ( wp && !(wp->name) ) { | |
500 | return; | |
501 | } | |
50a14534 | 502 | vik_coord_to_latlon ( &(wp->coord), &ll ); |
161aa492 EB |
503 | s_lat = a_coords_dtostr(ll.lat); |
504 | s_lon = a_coords_dtostr(ll.lon); | |
81eb2211 RN |
505 | gchar *tmp_name = slashdup(wp->name); |
506 | fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, tmp_name ); | |
507 | g_free ( tmp_name ); | |
161aa492 EB |
508 | g_free ( s_lat ); |
509 | g_free ( s_lon ); | |
510 | ||
511 | if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) { | |
512 | gchar *s_alt = a_coords_dtostr(wp->altitude); | |
513 | fprintf ( f, " altitude=\"%s\"", s_alt ); | |
514 | g_free(s_alt); | |
515 | } | |
50a14534 EB |
516 | if ( wp->comment ) |
517 | { | |
518 | gchar *tmp_comment = slashdup(wp->comment); | |
519 | fprintf ( f, " comment=\"%s\"", tmp_comment ); | |
520 | g_free ( tmp_comment ); | |
521 | } | |
6b2f262e RN |
522 | if ( wp->description ) |
523 | { | |
524 | gchar *tmp_description = slashdup(wp->description); | |
525 | fprintf ( f, " description=\"%s\"", tmp_description ); | |
526 | g_free ( tmp_description ); | |
527 | } | |
50a14534 EB |
528 | if ( wp->image ) |
529 | { | |
88542aa9 RN |
530 | gchar *tmp_image = NULL; |
531 | gchar *cwd = NULL; | |
532 | if ( a_vik_get_file_ref_format() == VIK_FILE_REF_FORMAT_RELATIVE ) { | |
533 | cwd = g_get_current_dir(); | |
534 | if ( cwd ) | |
535 | tmp_image = g_strdup ( file_GetRelativeFilename ( cwd, wp->image ) ); | |
536 | } | |
537 | ||
538 | // if cwd not available - use image filename as is | |
539 | // this should be an absolute path as set in thumbnails | |
540 | if ( !cwd ) | |
541 | tmp_image = slashdup(wp->image); | |
542 | ||
543 | if ( tmp_image ) | |
544 | fprintf ( f, " image=\"%s\"", tmp_image ); | |
545 | ||
546 | g_free ( cwd ); | |
50a14534 EB |
547 | g_free ( tmp_image ); |
548 | } | |
acaf7113 AF |
549 | if ( wp->symbol ) |
550 | { | |
f8c92775 RN |
551 | // Due to changes in garminsymbols - the symbol name is now in Title Case |
552 | // However to keep newly generated .vik files better compatible with older Viking versions | |
553 | // The symbol names will always be lowercase | |
554 | gchar *tmp_symbol = g_utf8_strdown(wp->symbol, -1); | |
555 | fprintf ( f, " symbol=\"%s\"", tmp_symbol ); | |
556 | g_free ( tmp_symbol ); | |
acaf7113 | 557 | } |
50a14534 EB |
558 | if ( ! wp->visible ) |
559 | fprintf ( f, " visible=\"n\"" ); | |
560 | fprintf ( f, "\n" ); | |
561 | } | |
562 | ||
0d2b891f | 563 | static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info ) |
50a14534 EB |
564 | { |
565 | static struct LatLon ll; | |
161aa492 | 566 | gchar *s_lat, *s_lon; |
50a14534 EB |
567 | vik_coord_to_latlon ( &(tp->coord), &ll ); |
568 | ||
0d2b891f RN |
569 | FILE *f = write_info->f; |
570 | ||
a2817d3c QT |
571 | /* TODO: modify a_coords_dtostr() to accept (optional) buffer |
572 | * instead of doing malloc/free everytime */ | |
161aa492 EB |
573 | s_lat = a_coords_dtostr(ll.lat); |
574 | s_lon = a_coords_dtostr(ll.lon); | |
0d2b891f | 575 | fprintf ( f, "type=\"%spoint\" latitude=\"%s\" longitude=\"%s\"", write_info->is_route ? "route" : "track", s_lat, s_lon ); |
161aa492 EB |
576 | g_free ( s_lat ); |
577 | g_free ( s_lon ); | |
50a14534 | 578 | |
161aa492 EB |
579 | if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) { |
580 | gchar *s_alt = a_coords_dtostr(tp->altitude); | |
581 | fprintf ( f, " altitude=\"%s\"", s_alt ); | |
582 | g_free(s_alt); | |
583 | } | |
50a14534 EB |
584 | if ( tp->has_timestamp ) |
585 | fprintf ( f, " unixtime=\"%ld\"", tp->timestamp ); | |
586 | if ( tp->newsegment ) | |
587 | fprintf ( f, " newsegment=\"yes\"" ); | |
a2817d3c | 588 | |
376c9177 | 589 | if (!isnan(tp->speed) || !isnan(tp->course) || tp->nsats > 0) { |
a2817d3c QT |
590 | fprintf ( f, " extended=\"yes\"" ); |
591 | if (!isnan(tp->speed)) { | |
592 | gchar *s_speed = a_coords_dtostr(tp->speed); | |
593 | fprintf ( f, " speed=\"%s\"", s_speed ); | |
594 | g_free(s_speed); | |
595 | } | |
596 | if (!isnan(tp->course)) { | |
597 | gchar *s_course = a_coords_dtostr(tp->course); | |
598 | fprintf ( f, " course=\"%s\"", s_course ); | |
599 | g_free(s_course); | |
600 | } | |
601 | if (tp->nsats > 0) | |
602 | fprintf ( f, " sat=\"%d\"", tp->nsats ); | |
603 | if (tp->fix_mode > 0) | |
604 | fprintf ( f, " fix=\"%d\"", tp->fix_mode ); | |
605 | } | |
50a14534 EB |
606 | fprintf ( f, "\n" ); |
607 | } | |
608 | ||
609 | ||
6b2f262e | 610 | static void a_gpspoint_write_track ( const gpointer id, const VikTrack *trk, FILE *f ) |
50a14534 | 611 | { |
e979bdab | 612 | // Sanity clauses |
6b2f262e | 613 | if ( !trk ) |
ce4bd1cf | 614 | return; |
6b2f262e | 615 | if ( !(trk->name) ) |
e979bdab RN |
616 | return; |
617 | ||
81eb2211 RN |
618 | gchar *tmp_name = slashdup(trk->name); |
619 | fprintf ( f, "type=\"%s\" name=\"%s\"", trk->is_route ? "route" : "track", tmp_name ); | |
620 | g_free ( tmp_name ); | |
6b2f262e RN |
621 | |
622 | if ( trk->comment ) { | |
623 | gchar *tmp = slashdup(trk->comment); | |
624 | fprintf ( f, " comment=\"%s\"", tmp ); | |
625 | g_free ( tmp ); | |
50a14534 | 626 | } |
6b2f262e RN |
627 | |
628 | if ( trk->description ) { | |
629 | gchar *tmp = slashdup(trk->description); | |
630 | fprintf ( f, " description=\"%s\"", tmp ); | |
631 | g_free ( tmp ); | |
632 | } | |
633 | ||
b1453c16 RN |
634 | if ( trk->has_color ) { |
635 | fprintf ( f, " color=#%.2x%.2x%.2x", (int)(trk->color.red/256),(int)(trk->color.green/256),(int)(trk->color.blue/256)); | |
636 | } | |
637 | ||
6b2f262e RN |
638 | if ( ! trk->visible ) { |
639 | fprintf ( f, " visible=\"n\"" ); | |
640 | } | |
641 | fprintf ( f, "\n" ); | |
642 | ||
0d2b891f RN |
643 | TP_write_info_type tp_write_info = { f, trk->is_route }; |
644 | g_list_foreach ( trk->trackpoints, (GFunc) a_gpspoint_write_trackpoint, &tp_write_info ); | |
645 | fprintf ( f, "type=\"%send\"\n", trk->is_route ? "route" : "track" ); | |
50a14534 EB |
646 | } |
647 | ||
648 | void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f ) | |
649 | { | |
650 | GHashTable *tracks = vik_trw_layer_get_tracks ( trw ); | |
0d2b891f | 651 | GHashTable *routes = vik_trw_layer_get_routes ( trw ); |
50a14534 EB |
652 | GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw ); |
653 | ||
654 | fprintf ( f, "type=\"waypointlist\"\n" ); | |
655 | g_hash_table_foreach ( waypoints, (GHFunc) a_gpspoint_write_waypoint, f ); | |
656 | fprintf ( f, "type=\"waypointlistend\"\n" ); | |
657 | g_hash_table_foreach ( tracks, (GHFunc) a_gpspoint_write_track, f ); | |
0d2b891f | 658 | g_hash_table_foreach ( routes, (GHFunc) a_gpspoint_write_track, f ); |
50a14534 | 659 | } |