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