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