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