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