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