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