]> git.street.me.uk Git - andy/viking.git/blob - src/gpspoint.c
Start to make compression code more reuseable.
[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_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;
88
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 */
95
96
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 );
99
100 static gchar *slashdup(const gchar *str)
101 {
102   guint16 len = strlen(str);
103   guint16 need_bs_count, i, j;
104   gchar *rv;
105   for ( i = 0, need_bs_count = 0; i < len; i++ )
106     if ( str[i] == '\\' || str[i] == '"' )
107       need_bs_count++;
108   rv = g_malloc ( (len+need_bs_count+1) * sizeof(gchar) );
109   for ( i = 0, j = 0; i < len; i++, j++ )
110   {
111     if ( str[i] == '\\' || str[i] == '"' )
112       rv[j++] = '\\';
113     rv[j] = str[i];
114   }
115   rv[j] = '\0';
116   return rv;
117 }
118
119 static gchar *deslashndup ( const gchar *str, guint16 len )
120 {
121   guint16 i,j, bs_count, new_len;
122   gboolean backslash = FALSE;
123   gchar *rv;
124
125   if ( len < 1 )
126     return NULL;
127
128   for ( i = 0, bs_count = 0; i < len; i++ )
129    if ( str[i] == '\\' )
130    {
131      bs_count++;
132      i++;
133    }
134
135   if ( str[i-1] == '\\' && (len == 1 || str[i-2] != '\\') )
136     bs_count--;
137
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 )
142       backslash = TRUE;
143     else
144     {
145       rv[j++] = str[i];
146       backslash = FALSE;
147     }
148
149   rv[new_len] = '\0';
150   return rv;
151 }
152
153 /*
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
157  */
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 );
162   line_type = 0;
163   line_timestamp = 0;
164   line_newsegment = FALSE;
165   line_image = NULL;
166   line_symbol = NULL;
167   current_track = NULL;
168   gboolean have_read_something = FALSE;
169
170   while (fgets(line_buffer, 2048, f))
171   {
172     gboolean inside_quote = 0;
173     gboolean backslash = 0;
174
175     line_buffer[strlen(line_buffer)-1] = '\0'; /* chop off newline */
176
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;
181       break;
182     }
183
184     /* each line: nullify stuff, make thing if nes, free name if ness */
185     tag_start = line_buffer;
186     for (;;)
187     {
188       /* my addition: find first non-whitespace character. if the null, skip line. */
189       while (*tag_start != '\0' && isspace(*tag_start))
190         tag_start++;
191       if (tag_start == '\0')
192         break;
193
194       if (*tag_start == '#')
195         break;
196
197       tag_end = tag_start;
198         if (*tag_end == '"')
199           inside_quote = !inside_quote;
200       while (*tag_end != '\0' && (!isspace(*tag_end) || inside_quote)) {
201         tag_end++;
202         if (*tag_end == '\\' && !backslash)
203           backslash = TRUE;
204         else if (backslash)
205           backslash = FALSE;
206         else if (*tag_end == '"')
207           inside_quote = !inside_quote;
208       }
209
210       gpspoint_process_tag ( tag_start, tag_end - tag_start );
211
212       if (*tag_end == '\0' )
213         break;
214       else
215         tag_start = tag_end+1;
216     }
217     if (line_type == GPSPOINT_TYPE_WAYPOINT && line_name)
218     {
219       have_read_something = TRUE;
220       VikWaypoint *wp = vik_waypoint_new();
221       wp->visible = line_visible;
222       wp->altitude = line_altitude;
223
224       vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon );
225
226       vik_trw_layer_filein_add_waypoint ( trw, line_name, wp );
227       g_free ( line_name );
228       line_name = NULL;
229
230       if ( line_comment )
231         vik_waypoint_set_comment ( wp, line_comment );
232
233       if ( line_description )
234         vik_waypoint_set_description ( wp, line_description );
235
236       if ( line_image )
237         vik_waypoint_set_image ( wp, line_image );
238
239       if ( line_symbol )
240         vik_waypoint_set_symbol ( wp, line_symbol );
241     }
242     else if ((line_type == GPSPOINT_TYPE_TRACK || line_type == GPSPOINT_TYPE_ROUTE) && line_name)
243     {
244       have_read_something = TRUE;
245       VikTrack *pl = vik_track_new();
246
247       /* Thanks to Peter Jones for this Fix */
248       if (!line_name) line_name = g_strdup("UNK");
249
250       pl->visible = line_visible;
251       pl->is_route = (line_type == GPSPOINT_TYPE_ROUTE);
252
253       if ( line_comment )
254         vik_track_set_comment ( pl, line_comment );
255
256       if ( line_description )
257         vik_track_set_description ( pl, line_description );
258
259       if ( line_color )
260       {
261         if ( gdk_color_parse ( line_color, &(pl->color) ) )
262         pl->has_color = TRUE;
263       }
264
265       pl->trackpoints = NULL;
266       vik_trw_layer_filein_add_track ( trw, line_name, pl );
267       g_free ( line_name );
268       line_name = NULL;
269
270       current_track = pl;
271     }
272     else if ((line_type == GPSPOINT_TYPE_TRACKPOINT || line_type == GPSPOINT_TYPE_ROUTEPOINT) && current_track)
273     {
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;
281       if (line_extended) {
282         tp->speed = line_speed;
283         tp->course = line_course;
284         tp->nsats = line_sat;
285         tp->fix_mode = line_fix;
286       }
287       current_track->trackpoints = g_list_append ( current_track->trackpoints, tp );
288     }
289
290     if (line_name) 
291       g_free ( line_name );
292     line_name = NULL;
293     if (line_comment)
294       g_free ( line_comment );
295     if (line_description)
296       g_free ( line_description );
297     if (line_color)
298       g_free ( line_color );
299     if (line_image)
300       g_free ( line_image );
301     if (line_symbol)
302       g_free ( line_symbol );
303     line_comment = NULL;
304     line_description = NULL;
305     line_color = NULL;
306     line_image = NULL;
307     line_symbol = NULL;
308     line_type = GPSPOINT_TYPE_NONE;
309     line_newsegment = FALSE;
310     line_has_timestamp = FALSE;
311     line_timestamp = 0;
312     line_altitude = VIK_DEFAULT_ALTITUDE;
313     line_visible = TRUE;
314     line_symbol = NULL;
315
316     line_extended = FALSE;
317     line_speed = NAN;
318     line_course = NAN;
319     line_sat = 0;
320     line_fix = 0;
321   }
322
323   return have_read_something;
324 }
325
326 /* Tag will be of a few defined forms:
327    ^[:alpha:]*=".*"$
328    ^[:alpha:]*=.*$
329
330    <invalid tag>
331
332 So we must determine end of tag name, start of value, end of value.
333 */
334 static void gpspoint_process_tag ( const gchar *tag, gint len )
335 {
336   const gchar *key_end, *value_start, *value_end;
337
338   /* Searching for key end */
339   key_end = tag;
340
341   while (++key_end - tag < len)
342     if (*key_end == '=')
343       break;
344
345   if (key_end - tag == len)
346     return; /* no good */
347
348   if (key_end - tag == len + 1)
349     value_start = value_end = 0; /* size = 0 */
350   else
351   {
352     value_start = key_end + 1; /* equal_sign plus one */
353
354     if (*value_start == '"')
355     {
356       value_start++;
357       if (*value_start == '"')
358         value_start = value_end = 0; /* size = 0 */
359       else
360       {
361         if (*(tag+len-1) == '"')
362         value_end = tag + len - 1;
363         else
364           return; /* bogus */
365       }
366     }
367     else
368       value_end = tag + len; /* value start really IS value start. */
369
370     gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start);
371   }
372 }
373
374 /*
375 value = NULL for none
376 */
377 static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len )
378 {
379   if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 )
380   {
381     if (value == NULL)
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;
393     else
394       /* all others are ignored */
395       line_type = GPSPOINT_TYPE_NONE;
396   }
397   else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL)
398   {
399     if (line_name == NULL)
400     {
401       line_name = deslashndup ( value, value_len );
402     }
403   }
404   else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL)
405   {
406     if (line_comment == NULL)
407       line_comment = deslashndup ( value, value_len );
408   }
409   else if (key_len == 11 && strncasecmp( key, "description", key_len ) == 0 && value != NULL)
410   {
411     if (line_description == NULL)
412       line_description = deslashndup ( value, value_len );
413   }
414   else if (key_len == 5 && strncasecmp( key, "color", key_len ) == 0 && value != NULL)
415   {
416     if (line_color == NULL)
417       line_color = deslashndup ( value, value_len );
418   }
419   else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL)
420   {
421     if (line_image == NULL)
422       line_image = deslashndup ( value, value_len );
423   }
424   else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL)
425   {
426     line_latlon.lat = g_ascii_strtod(value, NULL);
427   }
428   else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL)
429   {
430     line_latlon.lon = g_ascii_strtod(value, NULL);
431   }
432   else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL)
433   {
434     line_altitude = g_ascii_strtod(value, NULL);
435   }
436   else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
437   {
438     line_visible = FALSE;
439   }
440   else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL)
441   {
442     line_symbol = g_strndup ( value, value_len );
443   }
444   else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL)
445   {
446     line_timestamp = g_ascii_strtod(value, NULL);
447     if ( line_timestamp != 0x80000000 )
448       line_has_timestamp = TRUE;
449   }
450   else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL)
451   {
452     line_newsegment = TRUE;
453   }
454   else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL)
455   {
456     line_extended = TRUE;
457   }
458   else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL)
459   {
460     line_speed = g_ascii_strtod(value, NULL);
461   }
462   else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL)
463   {
464     line_course = g_ascii_strtod(value, NULL);
465   }
466   else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL)
467   {
468     line_sat = atoi(value);
469   }
470   else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL)
471   {
472     line_fix = atoi(value);
473   }
474 }
475
476 static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f )
477 {
478   static struct LatLon ll;
479   gchar *s_lat, *s_lon;
480   // Sanity clause
481   if ( wp && !(wp->name) ) {
482     return;
483   }
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 );
489   g_free ( tmp_name );
490   g_free ( s_lat ); 
491   g_free ( s_lon );
492
493   if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) {
494     gchar *s_alt = a_coords_dtostr(wp->altitude);
495     fprintf ( f, " altitude=\"%s\"", s_alt );
496     g_free(s_alt);
497   }
498   if ( wp->comment )
499   {
500     gchar *tmp_comment = slashdup(wp->comment);
501     fprintf ( f, " comment=\"%s\"", tmp_comment );
502     g_free ( tmp_comment );
503   }
504   if ( wp->description )
505   {
506     gchar *tmp_description = slashdup(wp->description);
507     fprintf ( f, " description=\"%s\"", tmp_description );
508     g_free ( tmp_description );
509   }
510   if ( wp->image )
511   {
512     gchar *tmp_image = NULL;
513     gchar *cwd = NULL;
514     if ( a_vik_get_file_ref_format() == VIK_FILE_REF_FORMAT_RELATIVE ) {
515       cwd = g_get_current_dir();
516       if ( cwd )
517         tmp_image = g_strdup ( file_GetRelativeFilename ( cwd, wp->image ) );
518     }
519
520     // if cwd not available - use image filename as is
521     // this should be an absolute path as set in thumbnails
522     if ( !cwd )
523       tmp_image = slashdup(wp->image);
524
525     if ( tmp_image )
526       fprintf ( f, " image=\"%s\"", tmp_image );
527
528     g_free ( cwd );
529     g_free ( tmp_image );
530   }
531   if ( wp->symbol )
532   {
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 );
539   }
540   if ( ! wp->visible )
541     fprintf ( f, " visible=\"n\"" );
542   fprintf ( f, "\n" );
543 }
544
545 static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info )
546 {
547   static struct LatLon ll;
548   gchar *s_lat, *s_lon;
549   vik_coord_to_latlon ( &(tp->coord), &ll );
550
551   FILE *f = write_info->f;
552
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 );
558   g_free ( s_lat ); 
559   g_free ( s_lon );
560
561   if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
562     gchar *s_alt = a_coords_dtostr(tp->altitude);
563     fprintf ( f, " altitude=\"%s\"", s_alt );
564     g_free(s_alt);
565   }
566   if ( tp->has_timestamp )
567     fprintf ( f, " unixtime=\"%ld\"", tp->timestamp );
568   if ( tp->newsegment )
569     fprintf ( f, " newsegment=\"yes\"" );
570
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 );
576       g_free(s_speed);
577     }
578     if (!isnan(tp->course)) {
579       gchar *s_course = a_coords_dtostr(tp->course);
580       fprintf ( f, " course=\"%s\"", s_course );
581       g_free(s_course);
582     }
583     if (tp->nsats > 0)
584       fprintf ( f, " sat=\"%d\"", tp->nsats );
585     if (tp->fix_mode > 0)
586       fprintf ( f, " fix=\"%d\"", tp->fix_mode );
587   }
588   fprintf ( f, "\n" );
589 }
590
591
592 static void a_gpspoint_write_track ( const gpointer id, const VikTrack *trk, FILE *f )
593 {
594   // Sanity clauses
595   if ( !trk )
596     return;
597   if ( !(trk->name) )
598     return;
599
600   gchar *tmp_name = slashdup(trk->name);
601   fprintf ( f, "type=\"%s\" name=\"%s\"", trk->is_route ? "route" : "track", tmp_name );
602   g_free ( tmp_name );
603
604   if ( trk->comment ) {
605     gchar *tmp = slashdup(trk->comment);
606     fprintf ( f, " comment=\"%s\"", tmp );
607     g_free ( tmp );
608   }
609
610   if ( trk->description ) {
611     gchar *tmp = slashdup(trk->description);
612     fprintf ( f, " description=\"%s\"", tmp );
613     g_free ( tmp );
614   }
615
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));
618   }
619
620   if ( ! trk->visible ) {
621     fprintf ( f, " visible=\"n\"" );
622   }
623   fprintf ( f, "\n" );
624
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" );
628 }
629
630 void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f )
631 {
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 );
635
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 );
641 }