Add configuration options for OpenAerial and BlueMarble
[viking/guyou.git] / src / gpspoint.c
blob64defa5f7ae06efc973d6dcf4961fb16789e7337
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #ifdef HAVE_MATH_H
26 #include <math.h>
27 #endif
29 #include "viking.h"
31 #include <ctype.h>
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
36 #include <stdlib.h>
37 /* strtod */
40 static void a_gpspoint_write_track ( const gchar *name, VikTrack *t, FILE *f );
41 static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, FILE *f );
42 static void a_gpspoint_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f );
45 /* outline for file gpspoint.c
47 reading file:
49 take a line.
50 get first tag, if not type, skip it.
51 if type, record type. if waypoint list, etc move on. if track, make a new track, make it current track, add it, etc.
52 if waypoint, read on and store to the waypoint.
53 if trackpoint, make trackpoint, store to current track (error / skip if none)
57 /* Thanks to etrex-cache's gpsbabel's gpspoint.c for starting me off! */
59 static char line_buffer[2048];
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
67 /* #define GPSPOINT_TYPE_ROUTE 5 */
69 static VikTrack *current_track; /* pointer to pointer to first GList */
71 static gint line_type = GPSPOINT_TYPE_NONE;
72 static struct LatLon line_latlon;
73 static gchar *line_name;
74 static gchar *line_comment;
75 static gchar *line_image;
76 static gchar *line_symbol;
77 static gboolean line_newsegment = FALSE;
78 static gboolean line_has_timestamp = FALSE;
79 static time_t line_timestamp = 0;
80 static gdouble line_altitude = VIK_DEFAULT_ALTITUDE;
81 static gboolean line_visible = TRUE;
83 static gboolean line_extended = FALSE;
84 static gdouble line_speed = NAN;
85 static gdouble line_course = NAN;
86 static gint line_sat = 0;
87 static gint line_fix = 0;
88 /* other possible properties go here */
91 static void gpspoint_process_tag ( const gchar *tag, gint len );
92 static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len );
94 static gchar *slashdup(const gchar *str)
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++ )
105 if ( str[i] == '\\' || str[i] == '"' )
106 rv[j++] = '\\';
107 rv[j] = str[i];
109 rv[j] = '\0';
110 return rv;
113 static gchar *deslashndup ( const gchar *str, guint16 len )
115 guint16 i,j, bs_count, new_len;
116 gboolean backslash = FALSE;
117 gchar *rv;
119 if ( len < 1 )
120 return NULL;
122 for ( i = 0, bs_count = 0; i < len; i++ )
123 if ( str[i] == '\\' )
125 bs_count++;
126 i++;
129 if ( str[i-1] == '\\' && (len == 1 || str[i-2] != '\\') )
130 bs_count--;
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
139 rv[j++] = str[i];
140 backslash = FALSE;
143 rv[new_len] = '\0';
144 return rv;
147 void 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;
150 g_assert ( f != NULL && trw != NULL );
151 line_type = 0;
152 line_timestamp = 0;
153 line_newsegment = FALSE;
154 line_image = NULL;
155 line_symbol = NULL;
157 current_track = NULL;
158 while (fgets(line_buffer, 2048, f))
160 gboolean inside_quote = 0;
161 gboolean backslash = 0;
163 line_buffer[strlen(line_buffer)-1] = '\0'; /* chop off newline */
165 /* for gpspoint files wrapped inside */
166 if ( strlen(line_buffer) >= 13 && strncmp ( line_buffer, "~EndLayerData", 13 ) == 0 )
167 break;
169 /* each line: nullify stuff, make thing if nes, free name if ness */
170 tag_start = line_buffer;
171 for (;;)
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;
179 if (*tag_start == '#')
180 break;
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;
195 gpspoint_process_tag ( tag_start, tag_end - tag_start );
197 if (*tag_end == '\0' )
198 break;
199 else
200 tag_start = tag_end+1;
202 if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name)
204 VikWaypoint *wp = vik_waypoint_new();
205 wp->visible = line_visible;
206 wp->altitude = line_altitude;
208 vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon );
210 vik_trw_layer_filein_add_waypoint ( trw, line_name, wp );
211 g_free ( line_name );
212 line_name = NULL;
214 if ( line_comment )
216 vik_waypoint_set_comment ( wp, line_comment );
217 line_comment = NULL;
220 if ( line_image )
222 vik_waypoint_set_image ( wp, line_image );
223 line_image = NULL;
226 if ( line_symbol )
228 vik_waypoint_set_symbol ( wp, line_symbol );
229 line_symbol = NULL;
232 else if (line_type == GPSPOINT_TYPE_TRACK && line_name)
234 VikTrack *pl = vik_track_new();
236 /* Thanks to Peter Jones for this Fix */
237 if (!line_name) line_name = g_strdup("UNK");
239 pl->visible = line_visible;
241 if ( line_comment )
243 vik_track_set_comment ( pl, line_comment );
244 line_comment = NULL;
247 pl->trackpoints = NULL;
248 vik_trw_layer_filein_add_track ( trw, line_name, pl );
249 g_free ( line_name );
250 line_name = NULL;
252 current_track = pl;
254 else if (line_type == GPSPOINT_TYPE_TRACKPOINT && current_track)
256 VikTrackpoint *tp = vik_trackpoint_new();
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;
262 if (line_extended) {
263 tp->extended = TRUE;
264 tp->speed = line_speed;
265 tp->course = line_course;
266 tp->nsats = line_sat;
267 tp->fix_mode = line_fix;
269 else {
270 tp->extended = FALSE;
272 current_track->trackpoints = g_list_append ( current_track->trackpoints, tp );
275 if (line_name)
276 g_free ( line_name );
277 line_name = NULL;
278 if (line_comment)
279 g_free ( line_comment );
280 if (line_image)
281 g_free ( line_image );
282 if (line_symbol)
283 g_free ( line_symbol );
284 line_comment = NULL;
285 line_image = NULL;
286 line_symbol = NULL;
287 line_type = GPSPOINT_TYPE_NONE;
288 line_newsegment = FALSE;
289 line_has_timestamp = FALSE;
290 line_timestamp = 0;
291 line_altitude = VIK_DEFAULT_ALTITUDE;
292 line_visible = TRUE;
293 line_symbol = NULL;
295 line_extended = FALSE;
296 line_speed = NAN;
297 line_course = NAN;
298 line_sat = 0;
299 line_fix = 0;
303 /* Tag will be of a few defined forms:
304 ^[:alpha:]*=".*"$
305 ^[:alpha:]*=.*$
307 <invalid tag>
309 So we must determine end of tag name, start of value, end of value.
311 static void gpspoint_process_tag ( const gchar *tag, gint len )
313 const gchar *key_end, *value_start, *value_end;
315 /* Searching for key end */
316 key_end = tag;
318 while (++key_end - tag < len)
319 if (*key_end == '=')
320 break;
322 if (key_end - tag == len)
323 return; /* no good */
325 if (key_end - tag == len + 1)
326 value_start = value_end = 0; /* size = 0 */
327 else
329 value_start = key_end + 1; /* equal_sign plus one */
331 if (*value_start == '"')
333 value_start++;
334 if (*value_start == '"')
335 value_start = value_end = 0; /* size = 0 */
336 else
338 if (*(tag+len-1) == '"')
339 value_end = tag + len - 1;
340 else
341 return; /* bogus */
344 else
345 value_end = tag + len; /* value start really IS value start. */
347 gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start);
352 value = NULL for none
354 static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len )
356 if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 )
358 if (value == NULL)
359 line_type = GPSPOINT_TYPE_NONE;
360 else if (value_len == 5 && strncasecmp( value, "track", value_len ) == 0 )
361 line_type = GPSPOINT_TYPE_TRACK;
362 else if (value_len == 10 && strncasecmp( value, "trackpoint", value_len ) == 0 )
363 line_type = GPSPOINT_TYPE_TRACKPOINT;
364 else if (value_len == 8 && strncasecmp( value, "waypoint", value_len ) == 0 )
365 line_type = GPSPOINT_TYPE_WAYPOINT;
366 else
367 /* all others are ignored */
368 line_type = GPSPOINT_TYPE_NONE;
370 else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL)
372 if (line_name == NULL)
374 line_name = g_strndup ( value, value_len );
377 else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL)
379 if (line_comment == NULL)
380 line_comment = deslashndup ( value, value_len );
382 else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL)
384 if (line_image == NULL)
385 line_image = deslashndup ( value, value_len );
387 else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL)
389 line_latlon.lat = g_strtod(value, NULL);
391 else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL)
393 line_latlon.lon = g_strtod(value, NULL);
395 else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL)
397 line_altitude = g_strtod(value, NULL);
399 else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
401 line_visible = FALSE;
403 else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL)
405 line_symbol = g_strndup ( value, value_len );
407 else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL)
409 line_timestamp = g_strtod(value, NULL);
410 if ( line_timestamp != 0x80000000 )
411 line_has_timestamp = TRUE;
413 else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL)
415 line_newsegment = TRUE;
417 else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL)
419 line_extended = TRUE;
421 else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL)
423 line_speed = g_strtod(value, NULL);
425 else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL)
427 line_course = g_strtod(value, NULL);
429 else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL)
431 line_sat = atoi(value);
433 else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL)
435 line_fix = atoi(value);
439 static void a_gpspoint_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f )
441 static struct LatLon ll;
442 gchar *s_lat, *s_lon;
443 vik_coord_to_latlon ( &(wp->coord), &ll );
444 s_lat = a_coords_dtostr(ll.lat);
445 s_lon = a_coords_dtostr(ll.lon);
446 fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, name );
447 g_free ( s_lat );
448 g_free ( s_lon );
450 if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) {
451 gchar *s_alt = a_coords_dtostr(wp->altitude);
452 fprintf ( f, " altitude=\"%s\"", s_alt );
453 g_free(s_alt);
455 if ( wp->comment )
457 gchar *tmp_comment = slashdup(wp->comment);
458 fprintf ( f, " comment=\"%s\"", tmp_comment );
459 g_free ( tmp_comment );
461 if ( wp->image )
463 gchar *tmp_image = slashdup(wp->image);
464 fprintf ( f, " image=\"%s\"", tmp_image );
465 g_free ( tmp_image );
467 if ( wp->symbol )
469 fprintf ( f, " symbol=\"%s\"", wp->symbol );
471 if ( ! wp->visible )
472 fprintf ( f, " visible=\"n\"" );
473 fprintf ( f, "\n" );
476 static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, FILE *f )
478 static struct LatLon ll;
479 gchar *s_lat, *s_lon;
480 vik_coord_to_latlon ( &(tp->coord), &ll );
482 /* TODO: modify a_coords_dtostr() to accept (optional) buffer
483 * instead of doing malloc/free everytime */
484 s_lat = a_coords_dtostr(ll.lat);
485 s_lon = a_coords_dtostr(ll.lon);
486 fprintf ( f, "type=\"trackpoint\" latitude=\"%s\" longitude=\"%s\"", s_lat, s_lon );
487 g_free ( s_lat );
488 g_free ( s_lon );
490 if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
491 gchar *s_alt = a_coords_dtostr(tp->altitude);
492 fprintf ( f, " altitude=\"%s\"", s_alt );
493 g_free(s_alt);
495 if ( tp->has_timestamp )
496 fprintf ( f, " unixtime=\"%ld\"", tp->timestamp );
497 if ( tp->newsegment )
498 fprintf ( f, " newsegment=\"yes\"" );
500 if (tp->extended) {
501 fprintf ( f, " extended=\"yes\"" );
502 if (!isnan(tp->speed)) {
503 gchar *s_speed = a_coords_dtostr(tp->speed);
504 fprintf ( f, " speed=\"%s\"", s_speed );
505 g_free(s_speed);
507 if (!isnan(tp->course)) {
508 gchar *s_course = a_coords_dtostr(tp->course);
509 fprintf ( f, " course=\"%s\"", s_course );
510 g_free(s_course);
512 if (tp->nsats > 0)
513 fprintf ( f, " sat=\"%d\"", tp->nsats );
514 if (tp->fix_mode > 0)
515 fprintf ( f, " fix=\"%d\"", tp->fix_mode );
517 fprintf ( f, "\n" );
521 static void a_gpspoint_write_track ( const gchar *name, VikTrack *t, FILE *f )
523 if ( t->comment )
525 gchar *tmp_comment = slashdup(t->comment);
526 fprintf ( f, "type=\"track\" name=\"%s\" comment=\"%s\"%s\n", name, tmp_comment, t->visible ? "" : " visible=\"n\"" );
527 g_free ( tmp_comment );
529 else
530 fprintf ( f, "type=\"track\" name=\"%s\"%s\n", name, t->visible ? "" : " visible=\"n\"" );
531 g_list_foreach ( t->trackpoints, (GFunc) a_gpspoint_write_trackpoint, f );
532 fprintf ( f, "type=\"trackend\"\n" );
535 void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f )
537 GHashTable *tracks = vik_trw_layer_get_tracks ( trw );
538 GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw );
540 fprintf ( f, "type=\"waypointlist\"\n" );
541 g_hash_table_foreach ( waypoints, (GHFunc) a_gpspoint_write_waypoint, f );
542 fprintf ( f, "type=\"waypointlistend\"\n" );
543 g_hash_table_foreach ( tracks, (GHFunc) a_gpspoint_write_track, f );