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