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