]> git.street.me.uk Git - andy/viking.git/blame - src/gpx.c
Fix GPX symbol names for Garmin Oregon 450
[andy/viking.git] / src / gpx.c
CommitLineData
8c4f1350
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
a482007a
GB
4 * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
6 * Copyright (C) 2008, Hein Ragas <viking@ragas.nl>
7 * Copyright (C) 2009, Tal B <tal.bav@gmail.com>
0d2b891f 8 * Copyright (c) 2012, Rob Norris <rw_norris@hotmail.com>
8c4f1350 9 *
bf35388d
EB
10 * Some of the code adapted from GPSBabel 1.2.7
11 * http://gpsbabel.sf.net/
12 * Copyright (C) 2002, 2003, 2004, 2005 Robert Lipe, robertlipe@usa.net
13 *
8c4f1350
EB
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
8c00358d
GB
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
8c4f1350
EB
32
33#define _XOPEN_SOURCE /* glibc2 needs this */
34
0b72c435 35#include "gpx.h"
8c4f1350
EB
36#include "viking.h"
37#include <expat.h>
8c00358d 38#ifdef HAVE_STRING_H
8c4f1350 39#include <string.h>
8c00358d 40#endif
8904c540 41#include <glib.h>
8c00358d 42#ifdef HAVE_MATH_H
8904c540 43#include <math.h>
8c00358d 44#endif
e3449376 45#include <time.h>
8c4f1350 46
8c4f1350
EB
47typedef enum {
48 tt_unknown = 0,
49
50 tt_gpx,
51
52 tt_wpt,
6b2f262e 53 tt_wpt_cmt,
8c4f1350
EB
54 tt_wpt_desc,
55 tt_wpt_name,
56 tt_wpt_ele,
42c2ac3e 57 tt_wpt_sym,
8c4f1350
EB
58 tt_wpt_link, /* New in GPX 1.1 */
59
60 tt_trk,
6b2f262e 61 tt_trk_cmt,
8c4f1350
EB
62 tt_trk_desc,
63 tt_trk_name,
64
0d2b891f
RN
65 tt_rte,
66
8c4f1350
EB
67 tt_trk_trkseg,
68 tt_trk_trkseg_trkpt,
69 tt_trk_trkseg_trkpt_ele,
70 tt_trk_trkseg_trkpt_time,
a2817d3c
QT
71 /* extended */
72 tt_trk_trkseg_trkpt_course,
73 tt_trk_trkseg_trkpt_speed,
74 tt_trk_trkseg_trkpt_fix,
75 tt_trk_trkseg_trkpt_sat,
bf35388d 76
4a42b254
T
77 tt_trk_trkseg_trkpt_hdop,
78 tt_trk_trkseg_trkpt_vdop,
79 tt_trk_trkseg_trkpt_pdop,
80
bf35388d
EB
81 tt_waypoint,
82 tt_waypoint_coord,
83 tt_waypoint_name,
8c4f1350
EB
84} tag_type;
85
86typedef struct tag_mapping {
87 tag_type tag_type; /* enum from above for this tag */
88 const char *tag_name; /* xpath-ish tag name */
89} tag_mapping;
90
0b72c435
OK
91typedef struct {
92 GpxWritingOptions *options;
93 FILE *file;
94} GpxWritingContext;
95
8c4f1350
EB
96/*
97 * xpath(ish) mappings between full tag paths and internal identifers.
98 * These appear in the order they appear in the GPX specification.
99 * If it's not a tag we explictly handle, it doesn't go here.
100 */
101
102tag_mapping tag_path_map[] = {
8c4f1350
EB
103
104 { tt_wpt, "/gpx/wpt" },
bf35388d
EB
105
106 { tt_waypoint, "/loc/waypoint" },
107 { tt_waypoint_coord, "/loc/waypoint/coord" },
108 { tt_waypoint_name, "/loc/waypoint/name" },
109
8c4f1350
EB
110 { tt_wpt_ele, "/gpx/wpt/ele" },
111 { tt_wpt_name, "/gpx/wpt/name" },
6b2f262e 112 { tt_wpt_cmt, "/gpx/wpt/cmt" },
8c4f1350 113 { tt_wpt_desc, "/gpx/wpt/desc" },
42c2ac3e 114 { tt_wpt_sym, "/gpx/wpt/sym" },
08b251ec 115 { tt_wpt_sym, "/loc/waypoint/type" },
8c4f1350
EB
116 { tt_wpt_link, "/gpx/wpt/link" }, /* GPX 1.1 */
117
118 { tt_trk, "/gpx/trk" },
119 { tt_trk_name, "/gpx/trk/name" },
6b2f262e 120 { tt_trk_cmt, "/gpx/trk/cmt" },
8c4f1350
EB
121 { tt_trk_desc, "/gpx/trk/desc" },
122 { tt_trk_trkseg, "/gpx/trk/trkseg" },
8c4f1350
EB
123 { tt_trk_trkseg_trkpt, "/gpx/trk/trkseg/trkpt" },
124 { tt_trk_trkseg_trkpt_ele, "/gpx/trk/trkseg/trkpt/ele" },
125 { tt_trk_trkseg_trkpt_time, "/gpx/trk/trkseg/trkpt/time" },
a2817d3c
QT
126 /* extended */
127 { tt_trk_trkseg_trkpt_course, "/gpx/trk/trkseg/trkpt/course" },
128 { tt_trk_trkseg_trkpt_speed, "/gpx/trk/trkseg/trkpt/speed" },
129 { tt_trk_trkseg_trkpt_fix, "/gpx/trk/trkseg/trkpt/fix" },
130 { tt_trk_trkseg_trkpt_sat, "/gpx/trk/trkseg/trkpt/sat" },
8c4f1350 131
4a42b254
T
132 { tt_trk_trkseg_trkpt_hdop, "/gpx/trk/trkseg/trkpt/hdop" },
133 { tt_trk_trkseg_trkpt_vdop, "/gpx/trk/trkseg/trkpt/vdop" },
134 { tt_trk_trkseg_trkpt_pdop, "/gpx/trk/trkseg/trkpt/pdop" },
0d2b891f
RN
135
136 { tt_rte, "/gpx/rte" },
137 // NB Route reuses track point feature tags
138 { tt_trk_name, "/gpx/rte/name" },
139 { tt_trk_cmt, "/gpx/rte/cmt" },
140 { tt_trk_desc, "/gpx/rte/desc" },
141 { tt_trk_trkseg_trkpt, "/gpx/rte/rtept" },
142 { tt_trk_trkseg_trkpt_ele, "/gpx/rte/rtept/ele" },
143
8c4f1350
EB
144 {0}
145};
146
147static tag_type get_tag(const char *t)
148{
149 tag_mapping *tm;
150 for (tm = tag_path_map; tm->tag_type != 0; tm++)
151 if (0 == strcmp(tm->tag_name, t))
152 return tm->tag_type;
153 return tt_unknown;
154}
155
156/******************************************/
157
158tag_type current_tag = tt_unknown;
159GString *xpath = NULL;
160GString *c_cdata = NULL;
161
162/* current ("c_") objects */
163VikTrackpoint *c_tp = NULL;
164VikWaypoint *c_wp = NULL;
165VikTrack *c_tr = NULL;
166
167gchar *c_wp_name = NULL;
168gchar *c_tr_name = NULL;
169
170/* temporary things so we don't have to create them lots of times */
171const gchar *c_slat, *c_slon;
172struct LatLon c_ll;
173
174/* specialty flags / etc */
175gboolean f_tr_newseg;
176guint unnamed_waypoints = 0;
177guint unnamed_tracks = 0;
178
179
180static const char *get_attr ( const char **attr, const char *key )
181{
182 while ( *attr ) {
183 if ( strcmp(*attr,key) == 0 )
184 return *(attr + 1);
185 attr += 2;
186 }
187 return NULL;
188}
189
190static gboolean set_c_ll ( const char **attr )
191{
192 if ( (c_slat = get_attr ( attr, "lat" )) && (c_slon = get_attr ( attr, "lon" )) ) {
d1cd12c0
T
193 c_ll.lat = g_ascii_strtod(c_slat, NULL);
194 c_ll.lon = g_ascii_strtod(c_slon, NULL);
8c4f1350
EB
195 return TRUE;
196 }
197 return FALSE;
198}
199
200static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
201{
bf35388d
EB
202 static const gchar *tmp;
203
8c4f1350
EB
204 g_string_append_c ( xpath, '/' );
205 g_string_append ( xpath, el );
206 current_tag = get_tag ( xpath->str );
207
208 switch ( current_tag ) {
209
210 case tt_wpt:
211 if ( set_c_ll( attr ) ) {
212 c_wp = vik_waypoint_new ();
0b9f279d
RN
213 c_wp->visible = TRUE;
214 if ( get_attr ( attr, "hidden" ) )
215 c_wp->visible = FALSE;
8c4f1350
EB
216
217 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
218 }
219 break;
220
221 case tt_trk:
0d2b891f 222 case tt_rte:
8c4f1350 223 c_tr = vik_track_new ();
0d2b891f 224 c_tr->is_route = (current_tag == tt_rte) ? TRUE : FALSE;
0b9f279d
RN
225 c_tr->visible = TRUE;
226 if ( get_attr ( attr, "hidden" ) )
227 c_tr->visible = FALSE;
8c4f1350
EB
228 break;
229
230 case tt_trk_trkseg:
231 f_tr_newseg = TRUE;
232 break;
233
234 case tt_trk_trkseg_trkpt:
235 if ( set_c_ll( attr ) ) {
236 c_tp = vik_trackpoint_new ();
8c4f1350
EB
237 vik_coord_load_from_latlon ( &(c_tp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
238 if ( f_tr_newseg ) {
239 c_tp->newsegment = TRUE;
240 f_tr_newseg = FALSE;
241 }
242 c_tr->trackpoints = g_list_append ( c_tr->trackpoints, c_tp );
243 }
244 break;
245
246 case tt_trk_trkseg_trkpt_ele:
247 case tt_trk_trkseg_trkpt_time:
6b2f262e 248 case tt_wpt_cmt:
8c4f1350
EB
249 case tt_wpt_desc:
250 case tt_wpt_name:
251 case tt_wpt_ele:
252 case tt_wpt_link:
6b2f262e 253 case tt_trk_cmt:
8c4f1350
EB
254 case tt_trk_desc:
255 case tt_trk_name:
256 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer */
bf35388d 257 break;
8c4f1350 258
bf35388d
EB
259 case tt_waypoint:
260 c_wp = vik_waypoint_new ();
bf35388d
EB
261 c_wp->visible = TRUE;
262 break;
263
264 case tt_waypoint_coord:
265 if ( set_c_ll( attr ) )
266 vik_coord_load_from_latlon ( &(c_wp->coord), vik_trw_layer_get_coord_mode ( vtl ), &c_ll );
267 break;
268
269 case tt_waypoint_name:
270 if ( ( tmp = get_attr(attr, "id") ) ) {
271 if ( c_wp_name )
272 g_free ( c_wp_name );
273 c_wp_name = g_strdup ( tmp );
274 }
275 g_string_erase ( c_cdata, 0, -1 ); /* clear the cdata buffer for description */
276 break;
277
8c4f1350
EB
278 default: break;
279 }
280}
281
282static void gpx_end(VikTrwLayer *vtl, const char *el)
283{
dd81e762
GB
284 static GTimeVal tp_time;
285
8c4f1350
EB
286 g_string_truncate ( xpath, xpath->len - strlen(el) - 1 );
287
288 switch ( current_tag ) {
289
bf35388d 290 case tt_waypoint:
8c4f1350
EB
291 case tt_wpt:
292 if ( ! c_wp_name )
293 c_wp_name = g_strdup_printf("VIKING_WP%d", unnamed_waypoints++);
805d282e
EB
294 vik_trw_layer_filein_add_waypoint ( vtl, c_wp_name, c_wp );
295 g_free ( c_wp_name );
8c4f1350
EB
296 c_wp = NULL;
297 c_wp_name = NULL;
298 break;
299
300 case tt_trk:
0d2b891f 301 case tt_rte:
8c4f1350 302 if ( ! c_tr_name )
a20877b7 303 c_tr_name = g_strdup_printf("VIKING_TR%d", unnamed_tracks++);
805d282e
EB
304 vik_trw_layer_filein_add_track ( vtl, c_tr_name, c_tr );
305 g_free ( c_tr_name );
8c4f1350
EB
306 c_tr = NULL;
307 c_tr_name = NULL;
308 break;
309
310 case tt_wpt_name:
311 if ( c_wp_name )
312 g_free ( c_wp_name );
313 c_wp_name = g_strdup ( c_cdata->str );
314 g_string_erase ( c_cdata, 0, -1 );
315 break;
316
317 case tt_trk_name:
318 if ( c_tr_name )
319 g_free ( c_tr_name );
320 c_tr_name = g_strdup ( c_cdata->str );
321 g_string_erase ( c_cdata, 0, -1 );
322 break;
323
324 case tt_wpt_ele:
d1cd12c0 325 c_wp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
8c4f1350
EB
326 g_string_erase ( c_cdata, 0, -1 );
327 break;
328
329 case tt_trk_trkseg_trkpt_ele:
d1cd12c0 330 c_tp->altitude = g_ascii_strtod ( c_cdata->str, NULL );
8c4f1350
EB
331 g_string_erase ( c_cdata, 0, -1 );
332 break;
333
bf35388d 334 case tt_waypoint_name: /* .loc name is really description. */
8c4f1350 335 case tt_wpt_desc:
6b2f262e
RN
336 vik_waypoint_set_description ( c_wp, c_cdata->str );
337 g_string_erase ( c_cdata, 0, -1 );
338 break;
339
340 case tt_wpt_cmt:
8c4f1350
EB
341 vik_waypoint_set_comment ( c_wp, c_cdata->str );
342 g_string_erase ( c_cdata, 0, -1 );
343 break;
344
345 case tt_wpt_link:
346 vik_waypoint_set_image ( c_wp, c_cdata->str );
347 g_string_erase ( c_cdata, 0, -1 );
348 break;
349
ea3933fc 350 case tt_wpt_sym: {
fffcc269 351 vik_waypoint_set_symbol ( c_wp, c_cdata->str );
42c2ac3e
AF
352 g_string_erase ( c_cdata, 0, -1 );
353 break;
ea3933fc 354 }
42c2ac3e 355
8c4f1350 356 case tt_trk_desc:
6b2f262e
RN
357 vik_track_set_description ( c_tr, c_cdata->str );
358 g_string_erase ( c_cdata, 0, -1 );
359 break;
360
361 case tt_trk_cmt:
8c4f1350
EB
362 vik_track_set_comment ( c_tr, c_cdata->str );
363 g_string_erase ( c_cdata, 0, -1 );
364 break;
365
366 case tt_trk_trkseg_trkpt_time:
dd81e762
GB
367 if ( g_time_val_from_iso8601(c_cdata->str, &tp_time) ) {
368 c_tp->timestamp = tp_time.tv_sec;
8c4f1350
EB
369 c_tp->has_timestamp = TRUE;
370 }
371 g_string_erase ( c_cdata, 0, -1 );
bf35388d 372 break;
8c4f1350 373
a2817d3c 374 case tt_trk_trkseg_trkpt_course:
d1cd12c0 375 c_tp->course = g_ascii_strtod ( c_cdata->str, NULL );
a2817d3c
QT
376 g_string_erase ( c_cdata, 0, -1 );
377 break;
378
379 case tt_trk_trkseg_trkpt_speed:
d1cd12c0 380 c_tp->speed = g_ascii_strtod ( c_cdata->str, NULL );
a2817d3c
QT
381 g_string_erase ( c_cdata, 0, -1 );
382 break;
383
384 case tt_trk_trkseg_trkpt_fix:
a2817d3c
QT
385 if (!strcmp("2d", c_cdata->str))
386 c_tp->fix_mode = VIK_GPS_MODE_2D;
387 else if (!strcmp("3d", c_cdata->str))
388 c_tp->fix_mode = VIK_GPS_MODE_3D;
389 else /* TODO: more fix modes here */
390 c_tp->fix_mode = VIK_GPS_MODE_NOT_SEEN;
391 g_string_erase ( c_cdata, 0, -1 );
392 break;
393
394 case tt_trk_trkseg_trkpt_sat:
a2817d3c
QT
395 c_tp->nsats = atoi ( c_cdata->str );
396 g_string_erase ( c_cdata, 0, -1 );
397 break;
398
4a42b254 399 case tt_trk_trkseg_trkpt_hdop:
4a42b254
T
400 c_tp->hdop = g_strtod ( c_cdata->str, NULL );
401 g_string_erase ( c_cdata, 0, -1 );
402 break;
403
404 case tt_trk_trkseg_trkpt_vdop:
4a42b254
T
405 c_tp->vdop = g_strtod ( c_cdata->str, NULL );
406 g_string_erase ( c_cdata, 0, -1 );
407 break;
408
409 case tt_trk_trkseg_trkpt_pdop:
4a42b254
T
410 c_tp->pdop = g_strtod ( c_cdata->str, NULL );
411 g_string_erase ( c_cdata, 0, -1 );
412 break;
413
8c4f1350
EB
414 default: break;
415 }
416
417 current_tag = get_tag ( xpath->str );
418}
419
420static void gpx_cdata(void *dta, const XML_Char *s, int len)
421{
422 switch ( current_tag ) {
423 case tt_wpt_name:
424 case tt_trk_name:
425 case tt_wpt_ele:
426 case tt_trk_trkseg_trkpt_ele:
6b2f262e 427 case tt_wpt_cmt:
8c4f1350 428 case tt_wpt_desc:
42c2ac3e 429 case tt_wpt_sym:
8c4f1350 430 case tt_wpt_link:
6b2f262e 431 case tt_trk_cmt:
8c4f1350
EB
432 case tt_trk_desc:
433 case tt_trk_trkseg_trkpt_time:
a2817d3c
QT
434 case tt_trk_trkseg_trkpt_course:
435 case tt_trk_trkseg_trkpt_speed:
436 case tt_trk_trkseg_trkpt_fix:
437 case tt_trk_trkseg_trkpt_sat:
4a42b254
T
438 case tt_trk_trkseg_trkpt_hdop:
439 case tt_trk_trkseg_trkpt_vdop:
440 case tt_trk_trkseg_trkpt_pdop:
bf35388d 441 case tt_waypoint_name: /* .loc name is really description. */
8c4f1350
EB
442 g_string_append_len ( c_cdata, s, len );
443 break;
444
445 default: break; /* ignore cdata from other things */
446 }
447}
448
449// make like a "stack" of tag names
450// like gpspoint's separated like /gpx/wpt/whatever
451
efe9a9c3 452gboolean a_gpx_read_file( VikTrwLayer *vtl, FILE *f ) {
8c4f1350
EB
453 XML_Parser parser = XML_ParserCreate(NULL);
454 int done=0, len;
efe9a9c3 455 enum XML_Status status = XML_STATUS_ERROR;
8c4f1350
EB
456
457 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_start, (XML_EndElementHandler) gpx_end);
458 XML_SetUserData(parser, vtl); /* in the future we could remove all global variables */
459 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_cdata);
460
461 gchar buf[4096];
462
bf35388d 463 g_assert ( f != NULL && vtl != NULL );
8c4f1350
EB
464
465 xpath = g_string_new ( "" );
466 c_cdata = g_string_new ( "" );
467
468 unnamed_waypoints = 0;
469 unnamed_tracks = 0;
470
471 while (!done) {
472 len = fread(buf, 1, sizeof(buf)-7, f);
473 done = feof(f) || !len;
efe9a9c3 474 status = XML_Parse(parser, buf, len, done);
8c4f1350
EB
475 }
476
6278ccc6 477 XML_ParserFree (parser);
8c4f1350
EB
478 g_string_free ( xpath, TRUE );
479 g_string_free ( c_cdata, TRUE );
efe9a9c3
GB
480
481 return status != XML_STATUS_ERROR;
8c4f1350 482}
561e6ad0
EB
483
484/**** entitize from GPSBabel ****/
485typedef struct {
486 const char * text;
487 const char * entity;
488 int not_html;
489} entity_types;
490
491static
492entity_types stdentities[] = {
493 { "&", "&amp;", 0 },
494 { "'", "&apos;", 1 },
495 { "<", "&lt;", 0 },
496 { ">", "&gt;", 0 },
497 { "\"", "&quot;", 0 },
498 { NULL, NULL, 0 }
499};
500
501void utf8_to_int( const char *cp, int *bytes, int *value )
502{
503 if ( (*cp & 0xe0) == 0xc0 ) {
504 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
505 *bytes = 2;
506 *value = ((*cp & 0x1f) << 6) |
507 (*(cp+1) & 0x3f);
508 }
509 else if ( (*cp & 0xf0) == 0xe0 ) {
510 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
511 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
512 *bytes = 3;
513 *value = ((*cp & 0x0f) << 12) |
514 ((*(cp+1) & 0x3f) << 6) |
515 (*(cp+2) & 0x3f);
516 }
517 else if ( (*cp & 0xf8) == 0xf0 ) {
518 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
519 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
520 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
521 *bytes = 4;
522 *value = ((*cp & 0x07) << 18) |
523 ((*(cp+1) & 0x3f) << 12) |
524 ((*(cp+2) & 0x3f) << 6) |
525 (*(cp+3) & 0x3f);
526 }
527 else if ( (*cp & 0xfc) == 0xf8 ) {
528 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
529 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
530 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
531 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
532 *bytes = 5;
533 *value = ((*cp & 0x03) << 24) |
534 ((*(cp+1) & 0x3f) << 18) |
535 ((*(cp+2) & 0x3f) << 12) |
536 ((*(cp+3) & 0x3f) << 6) |
537 (*(cp+4) & 0x3f);
538 }
539 else if ( (*cp & 0xfe) == 0xfc ) {
540 if ( (*(cp+1) & 0xc0) != 0x80 ) goto dodefault;
541 if ( (*(cp+2) & 0xc0) != 0x80 ) goto dodefault;
542 if ( (*(cp+3) & 0xc0) != 0x80 ) goto dodefault;
543 if ( (*(cp+4) & 0xc0) != 0x80 ) goto dodefault;
544 if ( (*(cp+5) & 0xc0) != 0x80 ) goto dodefault;
545 *bytes = 6;
546 *value = ((*cp & 0x01) << 30) |
547 ((*(cp+1) & 0x3f) << 24) |
548 ((*(cp+2) & 0x3f) << 18) |
549 ((*(cp+3) & 0x3f) << 12) |
550 ((*(cp+4) & 0x3f) << 6) |
551 (*(cp+5) & 0x3f);
552 }
553 else {
554dodefault:
555 *bytes = 1;
556 *value = (unsigned char)*cp;
557 }
558}
559
560static
561char *
562entitize(const char * str)
563{
564 int elen, ecount, nsecount;
565 entity_types *ep;
566 const char * cp;
567 char * p, * tmp, * xstr;
568
569 char tmpsub[20];
570 int bytes = 0;
571 int value = 0;
572 ep = stdentities;
573 elen = ecount = nsecount = 0;
574
575 /* figure # of entity replacements and additional size. */
576 while (ep->text) {
577 cp = str;
578 while ((cp = strstr(cp, ep->text)) != NULL) {
579 elen += strlen(ep->entity) - strlen(ep->text);
580 ecount++;
581 cp += strlen(ep->text);
582 }
583 ep++;
584 }
585
586 /* figure the same for other than standard entities (i.e. anything
587 * that isn't in the range U+0000 to U+007F */
588 for ( cp = str; *cp; cp++ ) {
589 if ( *cp & 0x80 ) {
590
591 utf8_to_int( cp, &bytes, &value );
592 cp += bytes-1;
593 elen += sprintf( tmpsub, "&#x%x;", value ) - bytes;
594 nsecount++;
595 }
596 }
597
598 /* enough space for the whole string plus entity replacements, if any */
599 tmp = g_malloc((strlen(str) + elen + 1));
600 strcpy(tmp, str);
601
602 /* no entity replacements */
603 if (ecount == 0 && nsecount == 0)
604 return (tmp);
605
606 if ( ecount != 0 ) {
607 for (ep = stdentities; ep->text; ep++) {
608 p = tmp;
609 while ((p = strstr(p, ep->text)) != NULL) {
610 elen = strlen(ep->entity);
611
612 xstr = g_strdup(p + strlen(ep->text));
613
614 strcpy(p, ep->entity);
615 strcpy(p + elen, xstr);
616
617 g_free(xstr);
618
619 p += elen;
620 }
621 }
622 }
623
624 if ( nsecount != 0 ) {
625 p = tmp;
626 while (*p) {
627 if ( *p & 0x80 ) {
628 utf8_to_int( p, &bytes, &value );
629 if ( p[bytes] ) {
630 xstr = g_strdup( p + bytes );
631 }
632 else {
633 xstr = NULL;
634 }
635 sprintf( p, "&#x%x;", value );
636 p = p+strlen(p);
637 if ( xstr ) {
638 strcpy( p, xstr );
639 g_free(xstr);
640 }
641 }
642 else {
643 p++;
644 }
645 }
646 }
647 return (tmp);
648}
649/**** end GPSBabel code ****/
650
651/* export GPX */
8904c540 652
c9570f86 653static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
561e6ad0 654{
208d2084
RN
655 // Don't write invisible waypoints when specified
656 if (context->options && !context->options->hidden && !wp->visible)
657 return;
658
0b72c435 659 FILE *f = context->file;
561e6ad0 660 static struct LatLon ll;
8904c540 661 gchar *s_lat,*s_lon;
561e6ad0
EB
662 gchar *tmp;
663 vik_coord_to_latlon ( &(wp->coord), &ll );
161aa492
EB
664 s_lat = a_coords_dtostr( ll.lat );
665 s_lon = a_coords_dtostr( ll.lon );
208d2084
RN
666 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
667 // luckily most other GPX processing software ignores things they don't understand
8904c540
GB
668 fprintf ( f, "<wpt lat=\"%s\" lon=\"%s\"%s>\n",
669 s_lat, s_lon, wp->visible ? "" : " hidden=\"hidden\"" );
670 g_free ( s_lat );
671 g_free ( s_lon );
561e6ad0 672
c9570f86
RN
673 // Sanity clause
674 if ( wp->name )
675 tmp = entitize ( wp->name );
676 else
677 tmp = g_strdup ("waypoint");
678
561e6ad0
EB
679 fprintf ( f, " <name>%s</name>\n", tmp );
680 g_free ( tmp);
681
682 if ( wp->altitude != VIK_DEFAULT_ALTITUDE )
8904c540 683 {
161aa492 684 tmp = a_coords_dtostr ( wp->altitude );
8904c540
GB
685 fprintf ( f, " <ele>%s</ele>\n", tmp );
686 g_free ( tmp );
687 }
561e6ad0
EB
688 if ( wp->comment )
689 {
690 tmp = entitize(wp->comment);
6b2f262e
RN
691 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
692 g_free ( tmp );
693 }
694 if ( wp->description )
695 {
696 tmp = entitize(wp->description);
561e6ad0
EB
697 fprintf ( f, " <desc>%s</desc>\n", tmp );
698 g_free ( tmp );
699 }
700 if ( wp->image )
701 {
702 tmp = entitize(wp->image);
703 fprintf ( f, " <link>%s</link>\n", tmp );
704 g_free ( tmp );
705 }
42c2ac3e
AF
706 if ( wp->symbol )
707 {
708 tmp = entitize(wp->symbol);
709 fprintf ( f, " <sym>%s</sym>\n", tmp);
710 g_free ( tmp );
711 }
712
561e6ad0
EB
713 fprintf ( f, "</wpt>\n" );
714}
715
0b72c435 716static void gpx_write_trackpoint ( VikTrackpoint *tp, GpxWritingContext *context )
561e6ad0 717{
0b72c435 718 FILE *f = context->file;
561e6ad0 719 static struct LatLon ll;
4a42b254 720 gchar *s_lat,*s_lon, *s_alt, *s_dop;
dd81e762 721 gchar *time_iso8601;
561e6ad0
EB
722 vik_coord_to_latlon ( &(tp->coord), &ll );
723
0d2b891f
RN
724 // No such thing as a rteseg! So make sure we don't put them in
725 if ( context->options && !context->options->is_route && tp->newsegment )
561e6ad0
EB
726 fprintf ( f, " </trkseg>\n <trkseg>\n" );
727
161aa492
EB
728 s_lat = a_coords_dtostr( ll.lat );
729 s_lon = a_coords_dtostr( ll.lon );
0d2b891f 730 fprintf ( f, " <%spt lat=\"%s\" lon=\"%s\">\n", (context->options && context->options->is_route) ? "rte" : "trk", s_lat, s_lon );
0b72c435
OK
731 g_free ( s_lat ); s_lat = NULL;
732 g_free ( s_lon ); s_lon = NULL;
561e6ad0 733
0b72c435 734 s_alt = NULL;
561e6ad0 735 if ( tp->altitude != VIK_DEFAULT_ALTITUDE )
8904c540 736 {
161aa492 737 s_alt = a_coords_dtostr ( tp->altitude );
8904c540 738 }
0b72c435 739 else if ( context->options != NULL && context->options->force_ele )
36179a2e
OK
740 {
741 s_alt = a_coords_dtostr ( 0 );
742 }
0b72c435
OK
743 if (s_alt != NULL)
744 fprintf ( f, " <ele>%s</ele>\n", s_alt );
745 g_free ( s_alt ); s_alt = NULL;
36179a2e 746
dd81e762 747 time_iso8601 = NULL;
561e6ad0 748 if ( tp->has_timestamp ) {
dd81e762
GB
749 GTimeVal timestamp;
750 timestamp.tv_sec = tp->timestamp;
61f37ac9 751 timestamp.tv_usec = 0;
dd81e762
GB
752
753 time_iso8601 = g_time_val_to_iso8601 ( &timestamp );
561e6ad0 754 }
0b72c435 755 else if ( context->options != NULL && context->options->force_time )
36179a2e 756 {
dd81e762
GB
757 GTimeVal current;
758 g_get_current_time ( &current );
36179a2e 759
dd81e762 760 time_iso8601 = g_time_val_to_iso8601 ( &current );
36179a2e 761 }
dd81e762
GB
762 if ( time_iso8601 != NULL )
763 fprintf ( f, " <time>%s</time>\n", time_iso8601 );
764 g_free(time_iso8601);
765 time_iso8601 = NULL;
36179a2e 766
ad54cd32
T
767 if (!isnan(tp->course)) {
768 gchar *s_course = a_coords_dtostr(tp->course);
769 fprintf ( f, " <course>%s</course>\n", s_course );
770 g_free(s_course);
a2817d3c 771 }
ad54cd32
T
772 if (!isnan(tp->speed)) {
773 gchar *s_speed = a_coords_dtostr(tp->speed);
774 fprintf ( f, " <speed>%s</speed>\n", s_speed );
775 g_free(s_speed);
776 }
777 if (tp->fix_mode == VIK_GPS_MODE_2D)
778 fprintf ( f, " <fix>2d</fix>\n");
779 if (tp->fix_mode == VIK_GPS_MODE_3D)
780 fprintf ( f, " <fix>3d</fix>\n");
781 if (tp->nsats > 0)
782 fprintf ( f, " <sat>%d</sat>\n", tp->nsats );
a2817d3c 783
4a42b254
T
784 s_dop = NULL;
785 if ( tp->hdop != VIK_DEFAULT_DOP )
786 {
787 s_dop = a_coords_dtostr ( tp->hdop );
788 }
789 if (s_dop != NULL)
790 fprintf ( f, " <hdop>%s</hdop>\n", s_dop );
791 g_free ( s_dop ); s_dop = NULL;
792
793 if ( tp->vdop != VIK_DEFAULT_DOP )
794 {
795 s_dop = a_coords_dtostr ( tp->vdop );
796 }
797 if (s_dop != NULL)
798 fprintf ( f, " <vdop>%s</vdop>\n", s_dop );
799 g_free ( s_dop ); s_dop = NULL;
800
801 if ( tp->pdop != VIK_DEFAULT_DOP )
802 {
803 s_dop = a_coords_dtostr ( tp->pdop );
804 }
805 if (s_dop != NULL)
806 fprintf ( f, " <pdop>%s</pdop>\n", s_dop );
807 g_free ( s_dop ); s_dop = NULL;
808
0d2b891f 809 fprintf ( f, " </%spt>\n", (context->options && context->options->is_route) ? "rte" : "trk" );
561e6ad0
EB
810}
811
812
ce4bd1cf 813static void gpx_write_track ( VikTrack *t, GpxWritingContext *context )
561e6ad0 814{
208d2084
RN
815 // Don't write invisible tracks when specified
816 if (context->options && !context->options->hidden && !t->visible)
817 return;
818
0b72c435 819 FILE *f = context->file;
561e6ad0 820 gchar *tmp;
494eb388 821 gboolean first_tp_is_newsegment = FALSE; /* must temporarily make it not so, but we want to restore state. not that it matters. */
561e6ad0 822
ce4bd1cf
RN
823 // Sanity clause
824 if ( t->name )
825 tmp = entitize ( t->name );
826 else
827 tmp = g_strdup ("track");
828
208d2084
RN
829 // NB 'hidden' is not part of any GPX standard - this appears to be a made up Viking 'extension'
830 // luckily most other GPX processing software ignores things they don't understand
0d2b891f
RN
831 fprintf ( f, "<%s%s>\n <name>%s</name>\n",
832 t->is_route ? "rte" : "trk",
833 t->visible ? "" : " hidden=\"hidden\"",
834 tmp );
561e6ad0
EB
835 g_free ( tmp );
836
837 if ( t->comment )
838 {
839 tmp = entitize ( t->comment );
6b2f262e
RN
840 fprintf ( f, " <cmt>%s</cmt>\n", tmp );
841 g_free ( tmp );
842 }
843
844 if ( t->description )
845 {
846 tmp = entitize ( t->description );
561e6ad0
EB
847 fprintf ( f, " <desc>%s</desc>\n", tmp );
848 g_free ( tmp );
849 }
0d2b891f
RN
850
851 /* No such thing as a rteseg! */
852 if ( !t->is_route )
853 fprintf ( f, " <trkseg>\n" );
561e6ad0
EB
854
855 if ( t->trackpoints && t->trackpoints->data ) {
856 first_tp_is_newsegment = VIK_TRACKPOINT(t->trackpoints->data)->newsegment;
857 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = FALSE; /* so we won't write </trkseg><trkseg> already */
0b72c435 858 g_list_foreach ( t->trackpoints, (GFunc) gpx_write_trackpoint, context );
561e6ad0 859 VIK_TRACKPOINT(t->trackpoints->data)->newsegment = first_tp_is_newsegment; /* restore state */
494eb388 860 }
561e6ad0 861
0d2b891f
RN
862 /* NB apparently no such thing as a rteseg! */
863 if (!t->is_route)
864 fprintf ( f, " </trkseg>\n");
865
866 fprintf ( f, "</%s>\n", t->is_route ? "rte" : "trk" );
561e6ad0
EB
867}
868
5092de80 869static void gpx_write_header( FILE *f )
561e6ad0
EB
870{
871 fprintf(f, "<?xml version=\"1.0\"?>\n"
872 "<gpx version=\"1.0\" creator=\"Viking -- http://viking.sf.net/\"\n"
873 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
874 "xmlns=\"http://www.topografix.com/GPX/1/0\"\n"
875 "xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">\n");
5092de80
GB
876}
877
878static void gpx_write_footer( FILE *f )
879{
561e6ad0 880 fprintf(f, "</gpx>\n");
5092de80 881}
561e6ad0 882
c9570f86 883static int gpx_waypoint_compare(const void *x, const void *y)
eab858a1 884{
c9570f86
RN
885 VikWaypoint *a = (VikWaypoint *)x;
886 VikWaypoint *b = (VikWaypoint *)y;
eab858a1
EB
887 return strcmp(a->name,b->name);
888}
889
0d2b891f
RN
890static int gpx_track_compare_name(const void *x, const void *y)
891{
892 VikTrack *a = (VikTrack *)x;
893 VikTrack *b = (VikTrack *)y;
894 return strcmp(a->name,b->name);
895}
896
e3449376 897/* Function to compare two tracks by their first timestamp */
0dabdc4f 898static int gpx_track_compare_timestamp (const void *x, const void *y)
e3449376 899{
0dabdc4f
RN
900 VikTrack *a = (VikTrack *)x;
901 VikTrack *b = (VikTrack *)y;
902
903 VikTrackpoint *tpa = NULL;
904 VikTrackpoint *tpb = NULL;
905
906 if ( a->trackpoints )
907 tpa = VIK_TRACKPOINT(g_list_first(a->trackpoints)->data);
908
909 if ( b->trackpoints )
910 tpb = VIK_TRACKPOINT(g_list_first(b->trackpoints)->data);
911
912 if ( tpa && tpb ) {
913 if ( tpa->timestamp < tpb->timestamp )
914 return -1;
915 if ( tpa->timestamp > tpb->timestamp )
916 return 1;
e3449376 917 }
0dabdc4f
RN
918
919 if ( tpa && !tpb )
e3449376 920 return 1;
0dabdc4f
RN
921
922 if ( !tpa && tpb )
923 return -1;
924
e3449376
HR
925 return 0;
926}
927
208d2084 928void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
0b72c435
OK
929{
930 GpxWritingContext context = { options, f };
eab858a1 931
5092de80 932 gpx_write_header ( f );
eab858a1 933
c9570f86
RN
934 // gather waypoints in a list, then sort
935 // g_hash_table_get_values: glib 2.14+
936 GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
937 gl = g_list_sort ( gl, gpx_waypoint_compare );
0b72c435 938
c9570f86
RN
939 GList *iter;
940 for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
941 gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
942 }
eab858a1 943
c9570f86 944 g_list_free ( gl );
d13f1a57 945
ce4bd1cf 946 gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
d13f1a57
RN
947 // Sort method determined by preference
948 if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
949 gl = g_list_sort ( gl, gpx_track_compare_timestamp );
950 else
951 gl = g_list_sort ( gl, gpx_track_compare_name );
952
0d2b891f
RN
953 // Routes sorted by name
954 GList *glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
955 glrte = g_list_sort ( glrte, gpx_track_compare_name );
ce4bd1cf 956
0d2b891f
RN
957 // g_list_concat doesn't copy memory properly
958 // so process each list separately
959
960 GpxWritingContext context_tmp = context;
961 GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE };
962 // Force trackpoints on tracks
963 if ( !context.options )
964 context_tmp.options = &opt_tmp;
965 context_tmp.options->is_route = FALSE;
966
967 // Loop around each list and write each one
ce4bd1cf 968 for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
0d2b891f
RN
969 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
970 }
971
972 // Routes (to get routepoints)
973 context_tmp.options->is_route = TRUE;
974 for (iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
975 gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
e3449376 976 }
ce4bd1cf
RN
977
978 g_list_free ( gl );
0d2b891f 979 g_list_free ( glrte );
ce4bd1cf 980
5092de80
GB
981 gpx_write_footer ( f );
982}
561e6ad0 983
208d2084 984void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options )
0b72c435
OK
985{
986 GpxWritingContext context = {options, f};
5092de80 987 gpx_write_header ( f );
ce4bd1cf 988 gpx_write_track ( trk, &context );
5092de80 989 gpx_write_footer ( f );
561e6ad0 990}