]> git.street.me.uk Git - andy/viking.git/blame - src/gpspoint.c
Fix sensitivity of the track properties window split marker to be disabled when the...
[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
147void a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
148 VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw );
149 gchar *tag_start, *tag_end;
50a14534
EB
150 g_assert ( f != NULL && trw != NULL );
151 line_type = 0;
152 line_timestamp = 0;
153 line_newsegment = FALSE;
acaf7113
AF
154 line_image = NULL;
155 line_symbol = NULL;
156
50a14534
EB
157 current_track = NULL;
158 while (fgets(line_buffer, 2048, f))
159 {
160 gboolean inside_quote = 0;
161 gboolean backslash = 0;
162
163 line_buffer[strlen(line_buffer)-1] = '\0'; /* chop off newline */
164
165 /* for gpspoint files wrapped inside */
166 if ( strlen(line_buffer) >= 13 && strncmp ( line_buffer, "~EndLayerData", 13 ) == 0 )
167 break;
168
169/* each line: nullify stuff, make thing if nes, free name if ness */
170 tag_start = line_buffer;
171 for (;;)
172 {
173 /* my addition: find first non-whitespace character. if the null, skip line. */
174 while (*tag_start != '\0' && isspace(*tag_start))
175 tag_start++;
176 if (tag_start == '\0')
177 break;
178
179 if (*tag_start == '#')
180 break;
181
182 tag_end = tag_start;
183 if (*tag_end == '"')
184 inside_quote = !inside_quote;
185 while (*tag_end != '\0' && (!isspace(*tag_end) || inside_quote)) {
186 tag_end++;
187 if (*tag_end == '\\' && !backslash)
188 backslash = TRUE;
189 else if (backslash)
190 backslash = FALSE;
191 else if (*tag_end == '"')
192 inside_quote = !inside_quote;
193 }
194
195 gpspoint_process_tag ( tag_start, tag_end - tag_start );
196
197 if (*tag_end == '\0' )
198 break;
199 else
200 tag_start = tag_end+1;
201 }
202 if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name)
203 {
204 VikWaypoint *wp = vik_waypoint_new();
50a14534
EB
205 wp->visible = line_visible;
206 wp->altitude = line_altitude;
50a14534
EB
207
208 vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon );
209
805d282e
EB
210 vik_trw_layer_filein_add_waypoint ( trw, line_name, wp );
211 g_free ( line_name );
212 line_name = NULL;
50a14534
EB
213
214 if ( line_comment )
215 {
216 vik_waypoint_set_comment ( wp, line_comment );
217 line_comment = NULL;
218 }
219
220 if ( line_image )
221 {
222 vik_waypoint_set_image ( wp, line_image );
223 line_image = NULL;
224 }
225
acaf7113
AF
226 if ( line_symbol )
227 {
228 vik_waypoint_set_symbol ( wp, line_symbol );
229 line_symbol = NULL;
230 }
50a14534
EB
231 }
232 else if (line_type == GPSPOINT_TYPE_TRACK && line_name)
233 {
234 VikTrack *pl = vik_track_new();
50a14534
EB
235
236 /* Thanks to Peter Jones for this Fix */
237 if (!line_name) line_name = g_strdup("UNK");
238
239 pl->visible = line_visible;
240
50a14534
EB
241 if ( line_comment )
242 {
243 vik_track_set_comment ( pl, line_comment );
244 line_comment = NULL;
245 }
246
247 pl->trackpoints = NULL;
805d282e
EB
248 vik_trw_layer_filein_add_track ( trw, line_name, pl );
249 g_free ( line_name );
250 line_name = NULL;
50a14534
EB
251
252 current_track = pl;
253 }
254 else if (line_type == GPSPOINT_TYPE_TRACKPOINT && current_track)
255 {
a2817d3c 256 VikTrackpoint *tp = vik_trackpoint_new();
50a14534
EB
257 vik_coord_load_from_latlon ( &(tp->coord), coord_mode, &line_latlon );
258 tp->newsegment = line_newsegment;
259 tp->has_timestamp = line_has_timestamp;
260 tp->timestamp = line_timestamp;
261 tp->altitude = line_altitude;
a2817d3c 262 if (line_extended) {
a2817d3c
QT
263 tp->speed = line_speed;
264 tp->course = line_course;
265 tp->nsats = line_sat;
266 tp->fix_mode = line_fix;
267 }
50a14534
EB
268 current_track->trackpoints = g_list_append ( current_track->trackpoints, tp );
269 }
270
271 if (line_name)
272 g_free ( line_name );
273 line_name = NULL;
274 if (line_comment)
275 g_free ( line_comment );
276 if (line_image)
277 g_free ( line_image );
acaf7113
AF
278 if (line_symbol)
279 g_free ( line_symbol );
50a14534
EB
280 line_comment = NULL;
281 line_image = NULL;
acaf7113 282 line_symbol = NULL;
50a14534
EB
283 line_type = GPSPOINT_TYPE_NONE;
284 line_newsegment = FALSE;
285 line_has_timestamp = FALSE;
286 line_timestamp = 0;
287 line_altitude = VIK_DEFAULT_ALTITUDE;
288 line_visible = TRUE;
acaf7113 289 line_symbol = NULL;
a2817d3c
QT
290
291 line_extended = FALSE;
292 line_speed = NAN;
293 line_course = NAN;
294 line_sat = 0;
295 line_fix = 0;
50a14534
EB
296 }
297}
298
299/* Tag will be of a few defined forms:
300 ^[:alpha:]*=".*"$
301 ^[:alpha:]*=.*$
302
303 <invalid tag>
304
305So we must determine end of tag name, start of value, end of value.
306*/
307static void gpspoint_process_tag ( const gchar *tag, gint len )
308{
309 const gchar *key_end, *value_start, *value_end;
310
311 /* Searching for key end */
312 key_end = tag;
313
314 while (++key_end - tag < len)
315 if (*key_end == '=')
316 break;
317
318 if (key_end - tag == len)
319 return; /* no good */
320
321 if (key_end - tag == len + 1)
322 value_start = value_end = 0; /* size = 0 */
323 else
324 {
325 value_start = key_end + 1; /* equal_sign plus one */
326
327 if (*value_start == '"')
328 {
329 value_start++;
330 if (*value_start == '"')
331 value_start = value_end = 0; /* size = 0 */
332 else
333 {
334 if (*(tag+len-1) == '"')
335 value_end = tag + len - 1;
336 else
337 return; /* bogus */
338 }
339 }
340 else
341 value_end = tag + len; /* value start really IS value start. */
342
343 gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start);
344 }
345}
346
347/*
348value = NULL for none
349*/
350static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len )
351{
352 if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 )
353 {
354 if (value == NULL)
355 line_type = GPSPOINT_TYPE_NONE;
356 else if (value_len == 5 && strncasecmp( value, "track", value_len ) == 0 )
357 line_type = GPSPOINT_TYPE_TRACK;
358 else if (value_len == 10 && strncasecmp( value, "trackpoint", value_len ) == 0 )
359 line_type = GPSPOINT_TYPE_TRACKPOINT;
360 else if (value_len == 8 && strncasecmp( value, "waypoint", value_len ) == 0 )
361 line_type = GPSPOINT_TYPE_WAYPOINT;
362 else
363 /* all others are ignored */
364 line_type = GPSPOINT_TYPE_NONE;
365 }
366 else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL)
367 {
368 if (line_name == NULL)
369 {
370 line_name = g_strndup ( value, value_len );
371 }
372 }
373 else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL)
374 {
375 if (line_comment == NULL)
376 line_comment = deslashndup ( value, value_len );
377 }
378 else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL)
379 {
380 if (line_image == NULL)
381 line_image = deslashndup ( value, value_len );
382 }
383 else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL)
384 {
d1cd12c0 385 line_latlon.lat = g_ascii_strtod(value, NULL);
50a14534
EB
386 }
387 else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL)
388 {
d1cd12c0 389 line_latlon.lon = g_ascii_strtod(value, NULL);
50a14534
EB
390 }
391 else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL)
392 {
d1cd12c0 393 line_altitude = g_ascii_strtod(value, NULL);
50a14534
EB
394 }
395 else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
396 {
397 line_visible = FALSE;
398 }
acaf7113
AF
399 else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL)
400 {
401 line_symbol = g_strndup ( value, value_len );
402 }
50a14534
EB
403 else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL)
404 {
d1cd12c0 405 line_timestamp = g_ascii_strtod(value, NULL);
50a14534
EB
406 if ( line_timestamp != 0x80000000 )
407 line_has_timestamp = TRUE;
408 }
409 else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL)
410 {
411 line_newsegment = TRUE;
412 }
a2817d3c
QT
413 else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL)
414 {
415 line_extended = TRUE;
416 }
417 else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL)
418 {
d1cd12c0 419 line_speed = g_ascii_strtod(value, NULL);
a2817d3c
QT
420 }
421 else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL)
422 {
d1cd12c0 423 line_course = g_ascii_strtod(value, NULL);
a2817d3c
QT
424 }
425 else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL)
426 {
427 line_sat = atoi(value);
428 }
429 else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL)
430 {
431 line_fix = atoi(value);
432 }
50a14534
EB
433}
434
435static void a_gpspoint_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f )
436{
437 static struct LatLon ll;
161aa492 438 gchar *s_lat, *s_lon;
50a14534 439 vik_coord_to_latlon ( &(wp->coord), &ll );
161aa492
EB
440 s_lat = a_coords_dtostr(ll.lat);
441 s_lon = a_coords_dtostr(ll.lon);
442 fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, name );
443 g_free ( s_lat );
444 g_free ( s_lon );
445
446 if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) {
447 gchar *s_alt = a_coords_dtostr(wp->altitude);
448 fprintf ( f, " altitude=\"%s\"", s_alt );
449 g_free(s_alt);
450 }
50a14534
EB
451 if ( wp->comment )
452 {
453 gchar *tmp_comment = slashdup(wp->comment);
454 fprintf ( f, " comment=\"%s\"", tmp_comment );
455 g_free ( tmp_comment );
456 }
457 if ( wp->image )
458 {
459 gchar *tmp_image = slashdup(wp->image);
460 fprintf ( f, " image=\"%s\"", tmp_image );
461 g_free ( tmp_image );
462 }
acaf7113
AF
463 if ( wp->symbol )
464 {
465 fprintf ( f, " symbol=\"%s\"", wp->symbol );
466 }
50a14534
EB
467 if ( ! wp->visible )
468 fprintf ( f, " visible=\"n\"" );
469 fprintf ( f, "\n" );
470}
471
472static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, FILE *f )
473{
474 static struct LatLon ll;
161aa492 475 gchar *s_lat, *s_lon;
50a14534
EB
476 vik_coord_to_latlon ( &(tp->coord), &ll );
477
a2817d3c
QT
478 /* TODO: modify a_coords_dtostr() to accept (optional) buffer
479 * instead of doing malloc/free everytime */
161aa492
EB
480 s_lat = a_coords_dtostr(ll.lat);
481 s_lon = a_coords_dtostr(ll.lon);
482 fprintf ( f, "type=\"trackpoint\" latitude=\"%s\" longitude=\"%s\"", s_lat, s_lon );
483 g_free ( s_lat );
484 g_free ( s_lon );
50a14534 485
161aa492
EB
486 if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
487 gchar *s_alt = a_coords_dtostr(tp->altitude);
488 fprintf ( f, " altitude=\"%s\"", s_alt );
489 g_free(s_alt);
490 }
50a14534
EB
491 if ( tp->has_timestamp )
492 fprintf ( f, " unixtime=\"%ld\"", tp->timestamp );
493 if ( tp->newsegment )
494 fprintf ( f, " newsegment=\"yes\"" );
a2817d3c 495
376c9177 496 if (!isnan(tp->speed) || !isnan(tp->course) || tp->nsats > 0) {
a2817d3c
QT
497 fprintf ( f, " extended=\"yes\"" );
498 if (!isnan(tp->speed)) {
499 gchar *s_speed = a_coords_dtostr(tp->speed);
500 fprintf ( f, " speed=\"%s\"", s_speed );
501 g_free(s_speed);
502 }
503 if (!isnan(tp->course)) {
504 gchar *s_course = a_coords_dtostr(tp->course);
505 fprintf ( f, " course=\"%s\"", s_course );
506 g_free(s_course);
507 }
508 if (tp->nsats > 0)
509 fprintf ( f, " sat=\"%d\"", tp->nsats );
510 if (tp->fix_mode > 0)
511 fprintf ( f, " fix=\"%d\"", tp->fix_mode );
512 }
50a14534
EB
513 fprintf ( f, "\n" );
514}
515
516
517static void a_gpspoint_write_track ( const gchar *name, VikTrack *t, FILE *f )
518{
519 if ( t->comment )
520 {
521 gchar *tmp_comment = slashdup(t->comment);
522 fprintf ( f, "type=\"track\" name=\"%s\" comment=\"%s\"%s\n", name, tmp_comment, t->visible ? "" : " visible=\"n\"" );
523 g_free ( tmp_comment );
524 }
525 else
526 fprintf ( f, "type=\"track\" name=\"%s\"%s\n", name, t->visible ? "" : " visible=\"n\"" );
527 g_list_foreach ( t->trackpoints, (GFunc) a_gpspoint_write_trackpoint, f );
528 fprintf ( f, "type=\"trackend\"\n" );
529}
530
531void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f )
532{
533 GHashTable *tracks = vik_trw_layer_get_tracks ( trw );
534 GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw );
535
536 fprintf ( f, "type=\"waypointlist\"\n" );
537 g_hash_table_foreach ( waypoints, (GHFunc) a_gpspoint_write_waypoint, f );
538 fprintf ( f, "type=\"waypointlistend\"\n" );
539 g_hash_table_foreach ( tracks, (GHFunc) a_gpspoint_write_track, f );
540}