]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * Copyright (C) 2012-2013, Rob Norris <rw_norris@hotmail.com> | |
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 | */ | |
22 | #ifdef HAVE_CONFIG_H | |
23 | #include "config.h" | |
24 | #endif | |
25 | ||
26 | #ifdef HAVE_MATH_H | |
27 | #include <math.h> | |
28 | #endif | |
29 | ||
30 | #include "viking.h" | |
31 | #include "vikutils.h" | |
32 | ||
33 | #include <ctype.h> | |
34 | #ifdef HAVE_STRING_H | |
35 | #include <string.h> | |
36 | #endif | |
37 | ||
38 | #include <stdlib.h> | |
39 | /* strtod */ | |
40 | ||
41 | typedef struct { | |
42 | FILE *f; | |
43 | gboolean is_route; | |
44 | } TP_write_info_type; | |
45 | ||
46 | static void a_gpspoint_write_track ( const gpointer id, const VikTrack *t, FILE *f ); | |
47 | static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info ); | |
48 | ||
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 | #define VIKING_LINE_SIZE 4096 | |
63 | static char line_buffer[VIKING_LINE_SIZE]; | |
64 | ||
65 | #define GPSPOINT_TYPE_NONE 0 | |
66 | #define GPSPOINT_TYPE_WAYPOINT 1 | |
67 | #define GPSPOINT_TYPE_TRACKPOINT 2 | |
68 | #define GPSPOINT_TYPE_ROUTEPOINT 3 | |
69 | #define GPSPOINT_TYPE_TRACK 4 | |
70 | #define GPSPOINT_TYPE_ROUTE 5 | |
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; | |
78 | static gchar *line_description; | |
79 | static gchar *line_source; | |
80 | static gchar *line_xtype; | |
81 | static gchar *line_color; | |
82 | static gint line_name_label = 0; | |
83 | static gint line_dist_label = 0; | |
84 | static gchar *line_image; | |
85 | static gchar *line_symbol; | |
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; | |
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; | |
97 | static gdouble line_hdop = VIK_DEFAULT_DOP; | |
98 | static gdouble line_vdop = VIK_DEFAULT_DOP; | |
99 | static gdouble line_pdop = VIK_DEFAULT_DOP; | |
100 | /* other possible properties go here */ | |
101 | ||
102 | ||
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 ); | |
105 | ||
106 | static gchar *slashdup(const gchar *str) | |
107 | { | |
108 | size_t len = strlen(str); | |
109 | size_t need_bs_count, i, j; | |
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]; | |
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] = ' '; | |
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 | ||
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 | */ | |
168 | gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f, const gchar *dirpath ) { | |
169 | VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw ); | |
170 | gchar *tag_start, *tag_end; | |
171 | g_assert ( f != NULL && trw != NULL ); | |
172 | line_type = 0; | |
173 | line_timestamp = 0; | |
174 | line_newsegment = FALSE; | |
175 | line_image = NULL; | |
176 | line_symbol = NULL; | |
177 | current_track = NULL; | |
178 | gboolean have_read_something = FALSE; | |
179 | ||
180 | while (fgets(line_buffer, VIKING_LINE_SIZE, f)) | |
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 */ | |
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; | |
191 | break; | |
192 | } | |
193 | ||
194 | /* each line: nullify stuff, make thing if nes, free name if ness */ | |
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++; | |
201 | if (*tag_start == '\0') | |
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 | ||
220 | // Won't have super massively long strings, so potential truncation in cast is acceptable. | |
221 | guint len = (guint)(tag_end - tag_start); | |
222 | gpspoint_process_tag ( tag_start, len ); | |
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 | { | |
231 | have_read_something = TRUE; | |
232 | VikWaypoint *wp = vik_waypoint_new(); | |
233 | wp->visible = line_visible; | |
234 | wp->altitude = line_altitude; | |
235 | wp->has_timestamp = line_has_timestamp; | |
236 | wp->timestamp = line_timestamp; | |
237 | ||
238 | vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon ); | |
239 | ||
240 | vik_trw_layer_filein_add_waypoint ( trw, line_name, wp ); | |
241 | g_free ( line_name ); | |
242 | line_name = NULL; | |
243 | ||
244 | if ( line_comment ) | |
245 | vik_waypoint_set_comment ( wp, line_comment ); | |
246 | ||
247 | if ( line_description ) | |
248 | vik_waypoint_set_description ( wp, line_description ); | |
249 | ||
250 | if ( line_source ) | |
251 | vik_waypoint_set_source ( wp, line_source ); | |
252 | ||
253 | if ( line_xtype ) | |
254 | vik_waypoint_set_type ( wp, line_xtype ); | |
255 | ||
256 | if ( line_image ) { | |
257 | gchar *fn = util_make_absolute_filename ( line_image, dirpath ); | |
258 | vik_waypoint_set_image ( wp, fn ? fn : line_image ); | |
259 | g_free ( fn ); | |
260 | } | |
261 | ||
262 | if ( line_symbol ) | |
263 | vik_waypoint_set_symbol ( wp, line_symbol ); | |
264 | } | |
265 | else if ((line_type == GPSPOINT_TYPE_TRACK || line_type == GPSPOINT_TYPE_ROUTE) && line_name) | |
266 | { | |
267 | have_read_something = TRUE; | |
268 | VikTrack *pl = vik_track_new(); | |
269 | // NB don't set defaults here as all properties are stored in the GPS_POINT format | |
270 | //vik_track_set_defaults ( pl ); | |
271 | ||
272 | /* Thanks to Peter Jones for this Fix */ | |
273 | if (!line_name) line_name = g_strdup("UNK"); | |
274 | ||
275 | pl->visible = line_visible; | |
276 | pl->is_route = (line_type == GPSPOINT_TYPE_ROUTE); | |
277 | ||
278 | if ( line_comment ) | |
279 | vik_track_set_comment ( pl, line_comment ); | |
280 | ||
281 | if ( line_description ) | |
282 | vik_track_set_description ( pl, line_description ); | |
283 | ||
284 | if ( line_source ) | |
285 | vik_track_set_source ( pl, line_source ); | |
286 | ||
287 | if ( line_xtype ) | |
288 | vik_track_set_type ( pl, line_xtype ); | |
289 | ||
290 | if ( line_color ) | |
291 | { | |
292 | if ( gdk_color_parse ( line_color, &(pl->color) ) ) | |
293 | pl->has_color = TRUE; | |
294 | } | |
295 | ||
296 | pl->draw_name_mode = line_name_label; | |
297 | pl->max_number_dist_labels = line_dist_label; | |
298 | ||
299 | pl->trackpoints = NULL; | |
300 | vik_trw_layer_filein_add_track ( trw, line_name, pl ); | |
301 | g_free ( line_name ); | |
302 | line_name = NULL; | |
303 | ||
304 | current_track = pl; | |
305 | } | |
306 | else if ((line_type == GPSPOINT_TYPE_TRACKPOINT || line_type == GPSPOINT_TYPE_ROUTEPOINT) && current_track) | |
307 | { | |
308 | have_read_something = TRUE; | |
309 | VikTrackpoint *tp = vik_trackpoint_new(); | |
310 | vik_coord_load_from_latlon ( &(tp->coord), coord_mode, &line_latlon ); | |
311 | tp->newsegment = line_newsegment; | |
312 | tp->has_timestamp = line_has_timestamp; | |
313 | tp->timestamp = line_timestamp; | |
314 | tp->altitude = line_altitude; | |
315 | vik_trackpoint_set_name ( tp, line_name ); | |
316 | if (line_extended) { | |
317 | tp->speed = line_speed; | |
318 | tp->course = line_course; | |
319 | tp->nsats = line_sat; | |
320 | tp->fix_mode = line_fix; | |
321 | tp->hdop = line_hdop; | |
322 | tp->vdop = line_vdop; | |
323 | tp->pdop = line_pdop; | |
324 | } | |
325 | current_track->trackpoints = g_list_append ( current_track->trackpoints, tp ); | |
326 | } | |
327 | ||
328 | if (line_name) | |
329 | g_free ( line_name ); | |
330 | line_name = NULL; | |
331 | if (line_comment) | |
332 | g_free ( line_comment ); | |
333 | if (line_description) | |
334 | g_free ( line_description ); | |
335 | if (line_source) | |
336 | g_free ( line_source ); | |
337 | if (line_xtype) | |
338 | g_free ( line_xtype ); | |
339 | if (line_color) | |
340 | g_free ( line_color ); | |
341 | if (line_image) | |
342 | g_free ( line_image ); | |
343 | if (line_symbol) | |
344 | g_free ( line_symbol ); | |
345 | line_comment = NULL; | |
346 | line_description = NULL; | |
347 | line_source = NULL; | |
348 | line_xtype = NULL; | |
349 | line_color = NULL; | |
350 | line_image = NULL; | |
351 | line_symbol = NULL; | |
352 | line_type = GPSPOINT_TYPE_NONE; | |
353 | line_newsegment = FALSE; | |
354 | line_has_timestamp = FALSE; | |
355 | line_timestamp = 0; | |
356 | line_altitude = VIK_DEFAULT_ALTITUDE; | |
357 | line_visible = TRUE; | |
358 | line_symbol = NULL; | |
359 | ||
360 | line_extended = FALSE; | |
361 | line_speed = NAN; | |
362 | line_course = NAN; | |
363 | line_sat = 0; | |
364 | line_fix = 0; | |
365 | line_hdop = VIK_DEFAULT_DOP; | |
366 | line_vdop = VIK_DEFAULT_DOP; | |
367 | line_pdop = VIK_DEFAULT_DOP; | |
368 | line_name_label = 0; | |
369 | line_dist_label = 0; | |
370 | } | |
371 | ||
372 | return have_read_something; | |
373 | } | |
374 | ||
375 | /* Tag will be of a few defined forms: | |
376 | ^[:alpha:]*=".*"$ | |
377 | ^[:alpha:]*=.*$ | |
378 | ||
379 | <invalid tag> | |
380 | ||
381 | So we must determine end of tag name, start of value, end of value. | |
382 | */ | |
383 | static void gpspoint_process_tag ( const gchar *tag, guint len ) | |
384 | { | |
385 | const gchar *key_end, *value_start, *value_end; | |
386 | ||
387 | /* Searching for key end */ | |
388 | key_end = tag; | |
389 | ||
390 | while (++key_end - tag < len) | |
391 | if (*key_end == '=') | |
392 | break; | |
393 | ||
394 | if (key_end - tag == len) | |
395 | return; /* no good */ | |
396 | ||
397 | if (key_end - tag == len + 1) | |
398 | value_start = value_end = 0; /* size = 0 */ | |
399 | else | |
400 | { | |
401 | value_start = key_end + 1; /* equal_sign plus one */ | |
402 | ||
403 | if (*value_start == '"') | |
404 | { | |
405 | value_start++; | |
406 | if (*value_start == '"') | |
407 | value_start = value_end = 0; /* size = 0 */ | |
408 | else | |
409 | { | |
410 | if (*(tag+len-1) == '"') | |
411 | value_end = tag + len - 1; | |
412 | else | |
413 | return; /* bogus */ | |
414 | } | |
415 | } | |
416 | else | |
417 | value_end = tag + len; /* value start really IS value start. */ | |
418 | ||
419 | // Detect broken lines which end without any text or the enclosing ". i.e. like: comment=" | |
420 | if ( (value_end - value_start) < 0 ) | |
421 | return; | |
422 | ||
423 | gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start); | |
424 | } | |
425 | } | |
426 | ||
427 | /* | |
428 | value = NULL for none | |
429 | */ | |
430 | static void gpspoint_process_key_and_value ( const gchar *key, guint key_len, const gchar *value, guint value_len ) | |
431 | { | |
432 | if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 ) | |
433 | { | |
434 | if (value == NULL) | |
435 | line_type = GPSPOINT_TYPE_NONE; | |
436 | else if (value_len == 5 && strncasecmp( value, "track", value_len ) == 0 ) | |
437 | line_type = GPSPOINT_TYPE_TRACK; | |
438 | else if (value_len == 10 && strncasecmp( value, "trackpoint", value_len ) == 0 ) | |
439 | line_type = GPSPOINT_TYPE_TRACKPOINT; | |
440 | else if (value_len == 8 && strncasecmp( value, "waypoint", value_len ) == 0 ) | |
441 | line_type = GPSPOINT_TYPE_WAYPOINT; | |
442 | else if (value_len == 5 && strncasecmp( value, "route", value_len ) == 0 ) | |
443 | line_type = GPSPOINT_TYPE_ROUTE; | |
444 | else if (value_len == 10 && strncasecmp( value, "routepoint", value_len ) == 0 ) | |
445 | line_type = GPSPOINT_TYPE_ROUTEPOINT; | |
446 | else | |
447 | /* all others are ignored */ | |
448 | line_type = GPSPOINT_TYPE_NONE; | |
449 | } | |
450 | else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL) | |
451 | { | |
452 | if (line_name == NULL) | |
453 | { | |
454 | line_name = deslashndup ( value, value_len ); | |
455 | } | |
456 | } | |
457 | else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL) | |
458 | { | |
459 | if (line_comment == NULL) | |
460 | line_comment = deslashndup ( value, value_len ); | |
461 | } | |
462 | else if (key_len == 11 && strncasecmp( key, "description", key_len ) == 0 && value != NULL) | |
463 | { | |
464 | if (line_description == NULL) | |
465 | line_description = deslashndup ( value, value_len ); | |
466 | } | |
467 | else if (key_len == 6 && strncasecmp( key, "source", key_len ) == 0 && value != NULL) | |
468 | { | |
469 | if (line_source == NULL) | |
470 | line_source = deslashndup ( value, value_len ); | |
471 | } | |
472 | // NB using 'xtype' to differentiate from our own 'type' key | |
473 | else if (key_len == 5 && strncasecmp( key, "xtype", key_len ) == 0 && value != NULL) | |
474 | { | |
475 | if (line_xtype == NULL) | |
476 | line_xtype = deslashndup ( value, value_len ); | |
477 | } | |
478 | else if (key_len == 5 && strncasecmp( key, "color", key_len ) == 0 && value != NULL) | |
479 | { | |
480 | if (line_color == NULL) | |
481 | line_color = deslashndup ( value, value_len ); | |
482 | } | |
483 | else if (key_len == 14 && strncasecmp( key, "draw_name_mode", key_len ) == 0 && value != NULL) | |
484 | { | |
485 | line_name_label = atoi(value); | |
486 | } | |
487 | else if (key_len == 18 && strncasecmp( key, "number_dist_labels", key_len ) == 0 && value != NULL) | |
488 | { | |
489 | line_dist_label = atoi(value); | |
490 | } | |
491 | else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL) | |
492 | { | |
493 | if (line_image == NULL) | |
494 | line_image = deslashndup ( value, value_len ); | |
495 | } | |
496 | else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL) | |
497 | { | |
498 | line_latlon.lat = g_ascii_strtod(value, NULL); | |
499 | } | |
500 | else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL) | |
501 | { | |
502 | line_latlon.lon = g_ascii_strtod(value, NULL); | |
503 | } | |
504 | else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL) | |
505 | { | |
506 | line_altitude = g_ascii_strtod(value, NULL); | |
507 | } | |
508 | 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') | |
509 | { | |
510 | line_visible = FALSE; | |
511 | } | |
512 | else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL) | |
513 | { | |
514 | line_symbol = g_strndup ( value, value_len ); | |
515 | } | |
516 | else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL) | |
517 | { | |
518 | line_timestamp = g_ascii_strtod(value, NULL); | |
519 | if ( line_timestamp != 0x80000000 ) | |
520 | line_has_timestamp = TRUE; | |
521 | } | |
522 | else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL) | |
523 | { | |
524 | line_newsegment = TRUE; | |
525 | } | |
526 | else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL) | |
527 | { | |
528 | line_extended = TRUE; | |
529 | } | |
530 | else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL) | |
531 | { | |
532 | line_speed = g_ascii_strtod(value, NULL); | |
533 | } | |
534 | else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL) | |
535 | { | |
536 | line_course = g_ascii_strtod(value, NULL); | |
537 | } | |
538 | else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL) | |
539 | { | |
540 | line_sat = atoi(value); | |
541 | } | |
542 | else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL) | |
543 | { | |
544 | line_fix = atoi(value); | |
545 | } | |
546 | else if (key_len == 4 && strncasecmp( key, "hdop", key_len ) == 0 && value != NULL) | |
547 | { | |
548 | line_hdop = g_ascii_strtod(value, NULL); | |
549 | } | |
550 | else if (key_len == 4 && strncasecmp( key, "vdop", key_len ) == 0 && value != NULL) | |
551 | { | |
552 | line_vdop = g_ascii_strtod(value, NULL); | |
553 | } | |
554 | else if (key_len == 4 && strncasecmp( key, "pdop", key_len ) == 0 && value != NULL) | |
555 | { | |
556 | line_pdop = g_ascii_strtod(value, NULL); | |
557 | } | |
558 | } | |
559 | ||
560 | typedef struct { | |
561 | FILE *file; | |
562 | const gchar *dirpath; | |
563 | } WritingContext; | |
564 | ||
565 | static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, WritingContext *wc ) | |
566 | { | |
567 | struct LatLon ll; | |
568 | gchar s_lat[COORDS_STR_BUFFER_SIZE]; | |
569 | gchar s_lon[COORDS_STR_BUFFER_SIZE]; | |
570 | // Sanity clauses | |
571 | if ( !wp ) | |
572 | return; | |
573 | if ( !(wp->name) ) | |
574 | return; | |
575 | if ( !wc ) | |
576 | return; | |
577 | ||
578 | FILE *f = wc->file; | |
579 | ||
580 | vik_coord_to_latlon ( &(wp->coord), &ll ); | |
581 | a_coords_dtostr_buffer ( ll.lat, s_lat ); | |
582 | a_coords_dtostr_buffer ( ll.lon, s_lon ); | |
583 | gchar *tmp_name = slashdup(wp->name); | |
584 | fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, tmp_name ); | |
585 | g_free ( tmp_name ); | |
586 | ||
587 | if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) { | |
588 | gchar s_alt[COORDS_STR_BUFFER_SIZE]; | |
589 | a_coords_dtostr_buffer ( wp->altitude, s_alt ); | |
590 | fprintf ( f, " altitude=\"%s\"", s_alt ); | |
591 | } | |
592 | if ( wp->has_timestamp ) | |
593 | fprintf ( f, " unixtime=\"%ld\"", wp->timestamp ); | |
594 | if ( wp->comment ) | |
595 | { | |
596 | gchar *tmp_comment = slashdup(wp->comment); | |
597 | fprintf ( f, " comment=\"%s\"", tmp_comment ); | |
598 | g_free ( tmp_comment ); | |
599 | } | |
600 | if ( wp->description ) | |
601 | { | |
602 | gchar *tmp_description = slashdup(wp->description); | |
603 | fprintf ( f, " description=\"%s\"", tmp_description ); | |
604 | g_free ( tmp_description ); | |
605 | } | |
606 | if ( wp->source ) | |
607 | { | |
608 | gchar *tmp_source = slashdup(wp->source); | |
609 | fprintf ( f, " source=\"%s\"", tmp_source ); | |
610 | g_free ( tmp_source ); | |
611 | } | |
612 | if ( wp->type ) | |
613 | { | |
614 | gchar *tmp_type = slashdup(wp->type); | |
615 | fprintf ( f, " xtype=\"%s\"", tmp_type ); | |
616 | g_free ( tmp_type ); | |
617 | } | |
618 | if ( wp->image ) | |
619 | { | |
620 | gchar *tmp_image = NULL; | |
621 | if ( a_vik_get_file_ref_format() == VIK_FILE_REF_FORMAT_RELATIVE ) { | |
622 | if ( wc->dirpath ) | |
623 | tmp_image = g_strdup ( file_GetRelativeFilename ( (gchar*)wc->dirpath, wp->image ) ); | |
624 | } | |
625 | ||
626 | // if tmp_image not available - use image filename as is | |
627 | // this should be an absolute path as set in thumbnails | |
628 | if ( !tmp_image ) | |
629 | tmp_image = slashdup(wp->image); | |
630 | ||
631 | if ( tmp_image ) | |
632 | fprintf ( f, " image=\"%s\"", tmp_image ); | |
633 | ||
634 | g_free ( tmp_image ); | |
635 | } | |
636 | if ( wp->symbol ) | |
637 | { | |
638 | // Due to changes in garminsymbols - the symbol name is now in Title Case | |
639 | // However to keep newly generated .vik files better compatible with older Viking versions | |
640 | // The symbol names will always be lowercase | |
641 | gchar *tmp_symbol = g_utf8_strdown(wp->symbol, -1); | |
642 | fprintf ( f, " symbol=\"%s\"", tmp_symbol ); | |
643 | g_free ( tmp_symbol ); | |
644 | } | |
645 | if ( ! wp->visible ) | |
646 | fprintf ( f, " visible=\"n\"" ); | |
647 | fprintf ( f, "\n" ); | |
648 | } | |
649 | ||
650 | static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info ) | |
651 | { | |
652 | struct LatLon ll; | |
653 | gchar s_lat[COORDS_STR_BUFFER_SIZE]; | |
654 | gchar s_lon[COORDS_STR_BUFFER_SIZE]; | |
655 | gchar s_alt[COORDS_STR_BUFFER_SIZE]; | |
656 | vik_coord_to_latlon ( &(tp->coord), &ll ); | |
657 | ||
658 | FILE *f = write_info->f; | |
659 | ||
660 | a_coords_dtostr_buffer ( ll.lat, s_lat ); | |
661 | a_coords_dtostr_buffer ( ll.lon, s_lon ); | |
662 | fprintf ( f, "type=\"%spoint\" latitude=\"%s\" longitude=\"%s\"", write_info->is_route ? "route" : "track", s_lat, s_lon ); | |
663 | ||
664 | if ( tp->name ) { | |
665 | gchar *name = slashdup(tp->name); | |
666 | fprintf ( f, " name=\"%s\"", name ); | |
667 | g_free(name); | |
668 | } | |
669 | ||
670 | if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) { | |
671 | a_coords_dtostr_buffer ( tp->altitude, s_alt ); | |
672 | fprintf ( f, " altitude=\"%s\"", s_alt ); | |
673 | } | |
674 | if ( tp->has_timestamp ) | |
675 | fprintf ( f, " unixtime=\"%ld\"", tp->timestamp ); | |
676 | if ( tp->newsegment ) | |
677 | fprintf ( f, " newsegment=\"yes\"" ); | |
678 | ||
679 | if (!isnan(tp->speed) || !isnan(tp->course) || tp->nsats > 0) { | |
680 | fprintf ( f, " extended=\"yes\"" ); | |
681 | if (!isnan(tp->speed)) { | |
682 | gchar s_speed[COORDS_STR_BUFFER_SIZE]; | |
683 | a_coords_dtostr_buffer ( tp->speed, s_speed ); | |
684 | fprintf ( f, " speed=\"%s\"", s_speed ); | |
685 | } | |
686 | if (!isnan(tp->course)) { | |
687 | gchar s_course[COORDS_STR_BUFFER_SIZE]; | |
688 | a_coords_dtostr_buffer ( tp->course, s_course ); | |
689 | fprintf ( f, " course=\"%s\"", s_course ); | |
690 | } | |
691 | if (tp->nsats > 0) | |
692 | fprintf ( f, " sat=\"%d\"", tp->nsats ); | |
693 | if (tp->fix_mode > 0) | |
694 | fprintf ( f, " fix=\"%d\"", tp->fix_mode ); | |
695 | ||
696 | if ( tp->hdop != VIK_DEFAULT_DOP ) { | |
697 | gchar ss[COORDS_STR_BUFFER_SIZE]; | |
698 | a_coords_dtostr_buffer ( tp->hdop, ss ); | |
699 | fprintf ( f, " hdop=\"%s\"", ss ); | |
700 | } | |
701 | if ( tp->vdop != VIK_DEFAULT_DOP ) { | |
702 | gchar ss[COORDS_STR_BUFFER_SIZE]; | |
703 | a_coords_dtostr_buffer ( tp->vdop, ss ); | |
704 | fprintf ( f, " vdop=\"%s\"", ss ); | |
705 | } | |
706 | if ( tp->pdop != VIK_DEFAULT_DOP ) { | |
707 | gchar ss[COORDS_STR_BUFFER_SIZE]; | |
708 | a_coords_dtostr_buffer ( tp->pdop, ss ); | |
709 | fprintf ( f, " pdop=\"%s\"", ss ); | |
710 | } | |
711 | } | |
712 | fprintf ( f, "\n" ); | |
713 | } | |
714 | ||
715 | ||
716 | static void a_gpspoint_write_track ( const gpointer id, const VikTrack *trk, FILE *f ) | |
717 | { | |
718 | // Sanity clauses | |
719 | if ( !trk ) | |
720 | return; | |
721 | if ( !(trk->name) ) | |
722 | return; | |
723 | ||
724 | gchar *tmp_name = slashdup(trk->name); | |
725 | fprintf ( f, "type=\"%s\" name=\"%s\"", trk->is_route ? "route" : "track", tmp_name ); | |
726 | g_free ( tmp_name ); | |
727 | ||
728 | if ( trk->comment ) { | |
729 | gchar *tmp = slashdup(trk->comment); | |
730 | fprintf ( f, " comment=\"%s\"", tmp ); | |
731 | g_free ( tmp ); | |
732 | } | |
733 | ||
734 | if ( trk->description ) { | |
735 | gchar *tmp = slashdup(trk->description); | |
736 | fprintf ( f, " description=\"%s\"", tmp ); | |
737 | g_free ( tmp ); | |
738 | } | |
739 | ||
740 | if ( trk->source ) { | |
741 | gchar *tmp = slashdup(trk->source); | |
742 | fprintf ( f, " source=\"%s\"", tmp ); | |
743 | g_free ( tmp ); | |
744 | } | |
745 | ||
746 | if ( trk->type ) { | |
747 | gchar *tmp = slashdup(trk->type); | |
748 | fprintf ( f, " xtype=\"%s\"", tmp ); | |
749 | g_free ( tmp ); | |
750 | } | |
751 | ||
752 | if ( trk->has_color ) { | |
753 | fprintf ( f, " color=#%.2x%.2x%.2x", (int)(trk->color.red/256),(int)(trk->color.green/256),(int)(trk->color.blue/256)); | |
754 | } | |
755 | ||
756 | if ( trk->draw_name_mode > 0 ) | |
757 | fprintf ( f, " draw_name_mode=\"%d\"", trk->draw_name_mode ); | |
758 | ||
759 | if ( trk->max_number_dist_labels > 0 ) | |
760 | fprintf ( f, " number_dist_labels=\"%d\"", trk->max_number_dist_labels ); | |
761 | ||
762 | if ( ! trk->visible ) { | |
763 | fprintf ( f, " visible=\"n\"" ); | |
764 | } | |
765 | fprintf ( f, "\n" ); | |
766 | ||
767 | TP_write_info_type tp_write_info = { f, trk->is_route }; | |
768 | g_list_foreach ( trk->trackpoints, (GFunc) a_gpspoint_write_trackpoint, &tp_write_info ); | |
769 | fprintf ( f, "type=\"%send\"\n", trk->is_route ? "route" : "track" ); | |
770 | } | |
771 | ||
772 | void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f, const gchar *dirpath ) | |
773 | { | |
774 | GHashTable *tracks = vik_trw_layer_get_tracks ( trw ); | |
775 | GHashTable *routes = vik_trw_layer_get_routes ( trw ); | |
776 | GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw ); | |
777 | ||
778 | WritingContext wc = { f, dirpath }; | |
779 | fprintf ( f, "type=\"waypointlist\"\n" ); | |
780 | g_hash_table_foreach ( waypoints, (GHFunc) a_gpspoint_write_waypoint, &wc ); | |
781 | fprintf ( f, "type=\"waypointlistend\"\n" ); | |
782 | g_hash_table_foreach ( tracks, (GHFunc) a_gpspoint_write_track, f ); | |
783 | g_hash_table_foreach ( routes, (GHFunc) a_gpspoint_write_track, f ); | |
784 | } |