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