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