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