2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2012, Rob Norris <rw_norris@hotmail.com>
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.
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.
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
45 static void a_gpspoint_write_track ( const gpointer id, const VikTrack *t, FILE *f );
46 static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info );
47 static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f );
50 /* outline for file gpspoint.c
55 get first tag, if not type, skip it.
56 if type, record type. if waypoint list, etc move on. if track, make a new track, make it current track, add it, etc.
57 if waypoint, read on and store to the waypoint.
58 if trackpoint, make trackpoint, store to current track (error / skip if none)
62 /* Thanks to etrex-cache's gpsbabel's gpspoint.c for starting me off! */
64 static char line_buffer[2048];
66 #define GPSPOINT_TYPE_NONE 0
67 #define GPSPOINT_TYPE_WAYPOINT 1
68 #define GPSPOINT_TYPE_TRACKPOINT 2
69 #define GPSPOINT_TYPE_ROUTEPOINT 3
70 #define GPSPOINT_TYPE_TRACK 4
71 #define GPSPOINT_TYPE_ROUTE 5
73 static VikTrack *current_track; /* pointer to pointer to first GList */
75 static gint line_type = GPSPOINT_TYPE_NONE;
76 static struct LatLon line_latlon;
77 static gchar *line_name;
78 static gchar *line_comment;
79 static gchar *line_description;
80 static gchar *line_color;
81 static gchar *line_image;
82 static gchar *line_symbol;
83 static gboolean line_newsegment = FALSE;
84 static gboolean line_has_timestamp = FALSE;
85 static time_t line_timestamp = 0;
86 static gdouble line_altitude = VIK_DEFAULT_ALTITUDE;
87 static gboolean line_visible = TRUE;
89 static gboolean line_extended = FALSE;
90 static gdouble line_speed = NAN;
91 static gdouble line_course = NAN;
92 static gint line_sat = 0;
93 static gint line_fix = 0;
94 /* other possible properties go here */
97 static void gpspoint_process_tag ( const gchar *tag, gint len );
98 static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len );
100 static gchar *slashdup(const gchar *str)
102 guint16 len = strlen(str);
103 guint16 need_bs_count, i, j;
105 for ( i = 0, need_bs_count = 0; i < len; i++ )
106 if ( str[i] == '\\' || str[i] == '"' )
108 rv = g_malloc ( (len+need_bs_count+1) * sizeof(gchar) );
109 for ( i = 0, j = 0; i < len; i++, j++ )
111 if ( str[i] == '\\' || str[i] == '"' )
119 static gchar *deslashndup ( const gchar *str, guint16 len )
121 guint16 i,j, bs_count, new_len;
122 gboolean backslash = FALSE;
128 for ( i = 0, bs_count = 0; i < len; i++ )
129 if ( str[i] == '\\' )
135 if ( str[i-1] == '\\' && (len == 1 || str[i-2] != '\\') )
138 new_len = len - bs_count;
139 rv = g_malloc ( (new_len+1) * sizeof(gchar) );
140 for ( i = 0, j = 0; i < len && j < new_len; i++ )
141 if ( str[i] == '\\' && !backslash )
154 * Returns whether file read was a success
155 * No obvious way to test for a 'gpspoint' file,
156 * thus set a flag if any actual tag found during processing of the file
158 gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
159 VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw );
160 gchar *tag_start, *tag_end;
161 g_assert ( f != NULL && trw != NULL );
164 line_newsegment = FALSE;
167 current_track = NULL;
168 gboolean have_read_something = FALSE;
170 while (fgets(line_buffer, 2048, f))
172 gboolean inside_quote = 0;
173 gboolean backslash = 0;
175 line_buffer[strlen(line_buffer)-1] = '\0'; /* chop off newline */
177 /* for gpspoint files wrapped inside */
178 if ( strlen(line_buffer) >= 13 && strncmp ( line_buffer, "~EndLayerData", 13 ) == 0 ) {
179 // Even just a blank TRW is ok when in a .vik file
180 have_read_something = TRUE;
184 /* each line: nullify stuff, make thing if nes, free name if ness */
185 tag_start = line_buffer;
188 /* my addition: find first non-whitespace character. if the null, skip line. */
189 while (*tag_start != '\0' && isspace(*tag_start))
191 if (tag_start == '\0')
194 if (*tag_start == '#')
199 inside_quote = !inside_quote;
200 while (*tag_end != '\0' && (!isspace(*tag_end) || inside_quote)) {
202 if (*tag_end == '\\' && !backslash)
206 else if (*tag_end == '"')
207 inside_quote = !inside_quote;
210 gpspoint_process_tag ( tag_start, tag_end - tag_start );
212 if (*tag_end == '\0' )
215 tag_start = tag_end+1;
217 if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name)
219 have_read_something = TRUE;
220 VikWaypoint *wp = vik_waypoint_new();
221 wp->visible = line_visible;
222 wp->altitude = line_altitude;
224 vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon );
226 vik_trw_layer_filein_add_waypoint ( trw, line_name, wp );
227 g_free ( line_name );
231 vik_waypoint_set_comment ( wp, line_comment );
233 if ( line_description )
234 vik_waypoint_set_description ( wp, line_description );
237 vik_waypoint_set_image ( wp, line_image );
240 vik_waypoint_set_symbol ( wp, line_symbol );
242 else if ((line_type == GPSPOINT_TYPE_TRACK || line_type == GPSPOINT_TYPE_ROUTE) && line_name)
244 have_read_something = TRUE;
245 VikTrack *pl = vik_track_new();
247 /* Thanks to Peter Jones for this Fix */
248 if (!line_name) line_name = g_strdup("UNK");
250 pl->visible = line_visible;
251 pl->is_route = (line_type == GPSPOINT_TYPE_ROUTE);
254 vik_track_set_comment ( pl, line_comment );
256 if ( line_description )
257 vik_track_set_description ( pl, line_description );
261 if ( gdk_color_parse ( line_color, &(pl->color) ) )
262 pl->has_color = TRUE;
265 pl->trackpoints = NULL;
266 vik_trw_layer_filein_add_track ( trw, line_name, pl );
267 g_free ( line_name );
272 else if ((line_type == GPSPOINT_TYPE_TRACKPOINT || line_type == GPSPOINT_TYPE_ROUTEPOINT) && current_track)
274 have_read_something = TRUE;
275 VikTrackpoint *tp = vik_trackpoint_new();
276 vik_coord_load_from_latlon ( &(tp->coord), coord_mode, &line_latlon );
277 tp->newsegment = line_newsegment;
278 tp->has_timestamp = line_has_timestamp;
279 tp->timestamp = line_timestamp;
280 tp->altitude = line_altitude;
282 tp->speed = line_speed;
283 tp->course = line_course;
284 tp->nsats = line_sat;
285 tp->fix_mode = line_fix;
287 current_track->trackpoints = g_list_append ( current_track->trackpoints, tp );
291 g_free ( line_name );
294 g_free ( line_comment );
295 if (line_description)
296 g_free ( line_description );
298 g_free ( line_color );
300 g_free ( line_image );
302 g_free ( line_symbol );
304 line_description = NULL;
308 line_type = GPSPOINT_TYPE_NONE;
309 line_newsegment = FALSE;
310 line_has_timestamp = FALSE;
312 line_altitude = VIK_DEFAULT_ALTITUDE;
316 line_extended = FALSE;
323 return have_read_something;
326 /* Tag will be of a few defined forms:
332 So we must determine end of tag name, start of value, end of value.
334 static void gpspoint_process_tag ( const gchar *tag, gint len )
336 const gchar *key_end, *value_start, *value_end;
338 /* Searching for key end */
341 while (++key_end - tag < len)
345 if (key_end - tag == len)
346 return; /* no good */
348 if (key_end - tag == len + 1)
349 value_start = value_end = 0; /* size = 0 */
352 value_start = key_end + 1; /* equal_sign plus one */
354 if (*value_start == '"')
357 if (*value_start == '"')
358 value_start = value_end = 0; /* size = 0 */
361 if (*(tag+len-1) == '"')
362 value_end = tag + len - 1;
368 value_end = tag + len; /* value start really IS value start. */
370 gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start);
375 value = NULL for none
377 static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len )
379 if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 )
382 line_type = GPSPOINT_TYPE_NONE;
383 else if (value_len == 5 && strncasecmp( value, "track", value_len ) == 0 )
384 line_type = GPSPOINT_TYPE_TRACK;
385 else if (value_len == 10 && strncasecmp( value, "trackpoint", value_len ) == 0 )
386 line_type = GPSPOINT_TYPE_TRACKPOINT;
387 else if (value_len == 8 && strncasecmp( value, "waypoint", value_len ) == 0 )
388 line_type = GPSPOINT_TYPE_WAYPOINT;
389 else if (value_len == 5 && strncasecmp( value, "route", value_len ) == 0 )
390 line_type = GPSPOINT_TYPE_ROUTE;
391 else if (value_len == 10 && strncasecmp( value, "routepoint", value_len ) == 0 )
392 line_type = GPSPOINT_TYPE_ROUTEPOINT;
394 /* all others are ignored */
395 line_type = GPSPOINT_TYPE_NONE;
397 else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL)
399 if (line_name == NULL)
401 line_name = deslashndup ( value, value_len );
404 else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL)
406 if (line_comment == NULL)
407 line_comment = deslashndup ( value, value_len );
409 else if (key_len == 11 && strncasecmp( key, "description", key_len ) == 0 && value != NULL)
411 if (line_description == NULL)
412 line_description = deslashndup ( value, value_len );
414 else if (key_len == 5 && strncasecmp( key, "color", key_len ) == 0 && value != NULL)
416 if (line_color == NULL)
417 line_color = deslashndup ( value, value_len );
419 else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL)
421 if (line_image == NULL)
422 line_image = deslashndup ( value, value_len );
424 else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL)
426 line_latlon.lat = g_ascii_strtod(value, NULL);
428 else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL)
430 line_latlon.lon = g_ascii_strtod(value, NULL);
432 else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL)
434 line_altitude = g_ascii_strtod(value, NULL);
436 else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
438 line_visible = FALSE;
440 else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL)
442 line_symbol = g_strndup ( value, value_len );
444 else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL)
446 line_timestamp = g_ascii_strtod(value, NULL);
447 if ( line_timestamp != 0x80000000 )
448 line_has_timestamp = TRUE;
450 else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL)
452 line_newsegment = TRUE;
454 else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL)
456 line_extended = TRUE;
458 else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL)
460 line_speed = g_ascii_strtod(value, NULL);
462 else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL)
464 line_course = g_ascii_strtod(value, NULL);
466 else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL)
468 line_sat = atoi(value);
470 else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL)
472 line_fix = atoi(value);
476 static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f )
478 static struct LatLon ll;
479 gchar *s_lat, *s_lon;
481 if ( wp && !(wp->name) ) {
484 vik_coord_to_latlon ( &(wp->coord), &ll );
485 s_lat = a_coords_dtostr(ll.lat);
486 s_lon = a_coords_dtostr(ll.lon);
487 gchar *tmp_name = slashdup(wp->name);
488 fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, tmp_name );
493 if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) {
494 gchar *s_alt = a_coords_dtostr(wp->altitude);
495 fprintf ( f, " altitude=\"%s\"", s_alt );
500 gchar *tmp_comment = slashdup(wp->comment);
501 fprintf ( f, " comment=\"%s\"", tmp_comment );
502 g_free ( tmp_comment );
504 if ( wp->description )
506 gchar *tmp_description = slashdup(wp->description);
507 fprintf ( f, " description=\"%s\"", tmp_description );
508 g_free ( tmp_description );
512 gchar *tmp_image = NULL;
514 if ( a_vik_get_file_ref_format() == VIK_FILE_REF_FORMAT_RELATIVE ) {
515 cwd = g_get_current_dir();
517 tmp_image = g_strdup ( file_GetRelativeFilename ( cwd, wp->image ) );
520 // if cwd not available - use image filename as is
521 // this should be an absolute path as set in thumbnails
523 tmp_image = slashdup(wp->image);
526 fprintf ( f, " image=\"%s\"", tmp_image );
529 g_free ( tmp_image );
533 // Due to changes in garminsymbols - the symbol name is now in Title Case
534 // However to keep newly generated .vik files better compatible with older Viking versions
535 // The symbol names will always be lowercase
536 gchar *tmp_symbol = g_utf8_strdown(wp->symbol, -1);
537 fprintf ( f, " symbol=\"%s\"", tmp_symbol );
538 g_free ( tmp_symbol );
541 fprintf ( f, " visible=\"n\"" );
545 static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info )
547 static struct LatLon ll;
548 gchar *s_lat, *s_lon;
549 vik_coord_to_latlon ( &(tp->coord), &ll );
551 FILE *f = write_info->f;
553 /* TODO: modify a_coords_dtostr() to accept (optional) buffer
554 * instead of doing malloc/free everytime */
555 s_lat = a_coords_dtostr(ll.lat);
556 s_lon = a_coords_dtostr(ll.lon);
557 fprintf ( f, "type=\"%spoint\" latitude=\"%s\" longitude=\"%s\"", write_info->is_route ? "route" : "track", s_lat, s_lon );
561 if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
562 gchar *s_alt = a_coords_dtostr(tp->altitude);
563 fprintf ( f, " altitude=\"%s\"", s_alt );
566 if ( tp->has_timestamp )
567 fprintf ( f, " unixtime=\"%ld\"", tp->timestamp );
568 if ( tp->newsegment )
569 fprintf ( f, " newsegment=\"yes\"" );
571 if (!isnan(tp->speed) || !isnan(tp->course) || tp->nsats > 0) {
572 fprintf ( f, " extended=\"yes\"" );
573 if (!isnan(tp->speed)) {
574 gchar *s_speed = a_coords_dtostr(tp->speed);
575 fprintf ( f, " speed=\"%s\"", s_speed );
578 if (!isnan(tp->course)) {
579 gchar *s_course = a_coords_dtostr(tp->course);
580 fprintf ( f, " course=\"%s\"", s_course );
584 fprintf ( f, " sat=\"%d\"", tp->nsats );
585 if (tp->fix_mode > 0)
586 fprintf ( f, " fix=\"%d\"", tp->fix_mode );
592 static void a_gpspoint_write_track ( const gpointer id, const VikTrack *trk, FILE *f )
600 gchar *tmp_name = slashdup(trk->name);
601 fprintf ( f, "type=\"%s\" name=\"%s\"", trk->is_route ? "route" : "track", tmp_name );
604 if ( trk->comment ) {
605 gchar *tmp = slashdup(trk->comment);
606 fprintf ( f, " comment=\"%s\"", tmp );
610 if ( trk->description ) {
611 gchar *tmp = slashdup(trk->description);
612 fprintf ( f, " description=\"%s\"", tmp );
616 if ( trk->has_color ) {
617 fprintf ( f, " color=#%.2x%.2x%.2x", (int)(trk->color.red/256),(int)(trk->color.green/256),(int)(trk->color.blue/256));
620 if ( ! trk->visible ) {
621 fprintf ( f, " visible=\"n\"" );
625 TP_write_info_type tp_write_info = { f, trk->is_route };
626 g_list_foreach ( trk->trackpoints, (GFunc) a_gpspoint_write_trackpoint, &tp_write_info );
627 fprintf ( f, "type=\"%send\"\n", trk->is_route ? "route" : "track" );
630 void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f )
632 GHashTable *tracks = vik_trw_layer_get_tracks ( trw );
633 GHashTable *routes = vik_trw_layer_get_routes ( trw );
634 GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw );
636 fprintf ( f, "type=\"waypointlist\"\n" );
637 g_hash_table_foreach ( waypoints, (GHFunc) a_gpspoint_write_waypoint, f );
638 fprintf ( f, "type=\"waypointlistend\"\n" );
639 g_hash_table_foreach ( tracks, (GHFunc) a_gpspoint_write_track, f );
640 g_hash_table_foreach ( routes, (GHFunc) a_gpspoint_write_track, f );