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