]>
Commit | Line | Data |
---|---|---|
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 | wp->visible = line_visible; | |
198 | wp->altitude = line_altitude; | |
199 | ||
200 | vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon ); | |
201 | ||
202 | vik_trw_layer_filein_add_waypoint ( trw, line_name, wp ); | |
203 | g_free ( line_name ); | |
204 | line_name = NULL; | |
205 | ||
206 | if ( line_comment ) | |
207 | { | |
208 | vik_waypoint_set_comment ( wp, line_comment ); | |
209 | line_comment = NULL; | |
210 | } | |
211 | ||
212 | if ( line_image ) | |
213 | { | |
214 | vik_waypoint_set_image ( wp, line_image ); | |
215 | line_image = NULL; | |
216 | } | |
217 | ||
218 | if ( line_symbol ) | |
219 | { | |
220 | vik_waypoint_set_symbol ( wp, line_symbol ); | |
221 | line_symbol = NULL; | |
222 | } | |
223 | } | |
224 | else if (line_type == GPSPOINT_TYPE_TRACK && line_name) | |
225 | { | |
226 | VikTrack *pl = vik_track_new(); | |
227 | ||
228 | /* Thanks to Peter Jones for this Fix */ | |
229 | if (!line_name) line_name = g_strdup("UNK"); | |
230 | ||
231 | pl->visible = line_visible; | |
232 | ||
233 | if ( line_comment ) | |
234 | { | |
235 | vik_track_set_comment ( pl, line_comment ); | |
236 | line_comment = NULL; | |
237 | } | |
238 | ||
239 | pl->trackpoints = NULL; | |
240 | vik_trw_layer_filein_add_track ( trw, line_name, pl ); | |
241 | g_free ( line_name ); | |
242 | line_name = NULL; | |
243 | ||
244 | current_track = pl; | |
245 | } | |
246 | else if (line_type == GPSPOINT_TYPE_TRACKPOINT && current_track) | |
247 | { | |
248 | VikTrackpoint *tp = vik_trackpoint_new(); | |
249 | vik_coord_load_from_latlon ( &(tp->coord), coord_mode, &line_latlon ); | |
250 | tp->newsegment = line_newsegment; | |
251 | tp->has_timestamp = line_has_timestamp; | |
252 | tp->timestamp = line_timestamp; | |
253 | tp->altitude = line_altitude; | |
254 | if (line_extended) { | |
255 | tp->extended = TRUE; | |
256 | tp->speed = line_speed; | |
257 | tp->course = line_course; | |
258 | tp->nsats = line_sat; | |
259 | tp->fix_mode = line_fix; | |
260 | } | |
261 | else { | |
262 | tp->extended = FALSE; | |
263 | } | |
264 | current_track->trackpoints = g_list_append ( current_track->trackpoints, tp ); | |
265 | } | |
266 | ||
267 | if (line_name) | |
268 | g_free ( line_name ); | |
269 | line_name = NULL; | |
270 | if (line_comment) | |
271 | g_free ( line_comment ); | |
272 | if (line_image) | |
273 | g_free ( line_image ); | |
274 | if (line_symbol) | |
275 | g_free ( line_symbol ); | |
276 | line_comment = NULL; | |
277 | line_image = NULL; | |
278 | line_symbol = NULL; | |
279 | line_type = GPSPOINT_TYPE_NONE; | |
280 | line_newsegment = FALSE; | |
281 | line_has_timestamp = FALSE; | |
282 | line_timestamp = 0; | |
283 | line_altitude = VIK_DEFAULT_ALTITUDE; | |
284 | line_visible = TRUE; | |
285 | line_symbol = NULL; | |
286 | ||
287 | line_extended = FALSE; | |
288 | line_speed = NAN; | |
289 | line_course = NAN; | |
290 | line_sat = 0; | |
291 | line_fix = 0; | |
292 | } | |
293 | } | |
294 | ||
295 | /* Tag will be of a few defined forms: | |
296 | ^[:alpha:]*=".*"$ | |
297 | ^[:alpha:]*=.*$ | |
298 | ||
299 | <invalid tag> | |
300 | ||
301 | So we must determine end of tag name, start of value, end of value. | |
302 | */ | |
303 | static void gpspoint_process_tag ( const gchar *tag, gint len ) | |
304 | { | |
305 | const gchar *key_end, *value_start, *value_end; | |
306 | ||
307 | /* Searching for key end */ | |
308 | key_end = tag; | |
309 | ||
310 | while (++key_end - tag < len) | |
311 | if (*key_end == '=') | |
312 | break; | |
313 | ||
314 | if (key_end - tag == len) | |
315 | return; /* no good */ | |
316 | ||
317 | if (key_end - tag == len + 1) | |
318 | value_start = value_end = 0; /* size = 0 */ | |
319 | else | |
320 | { | |
321 | value_start = key_end + 1; /* equal_sign plus one */ | |
322 | ||
323 | if (*value_start == '"') | |
324 | { | |
325 | value_start++; | |
326 | if (*value_start == '"') | |
327 | value_start = value_end = 0; /* size = 0 */ | |
328 | else | |
329 | { | |
330 | if (*(tag+len-1) == '"') | |
331 | value_end = tag + len - 1; | |
332 | else | |
333 | return; /* bogus */ | |
334 | } | |
335 | } | |
336 | else | |
337 | value_end = tag + len; /* value start really IS value start. */ | |
338 | ||
339 | gpspoint_process_key_and_value(tag, key_end - tag, value_start, value_end - value_start); | |
340 | } | |
341 | } | |
342 | ||
343 | /* | |
344 | value = NULL for none | |
345 | */ | |
346 | static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, const gchar *value, gint value_len ) | |
347 | { | |
348 | if (key_len == 4 && strncasecmp( key, "type", key_len ) == 0 ) | |
349 | { | |
350 | if (value == NULL) | |
351 | line_type = GPSPOINT_TYPE_NONE; | |
352 | else if (value_len == 5 && strncasecmp( value, "track", value_len ) == 0 ) | |
353 | line_type = GPSPOINT_TYPE_TRACK; | |
354 | else if (value_len == 10 && strncasecmp( value, "trackpoint", value_len ) == 0 ) | |
355 | line_type = GPSPOINT_TYPE_TRACKPOINT; | |
356 | else if (value_len == 8 && strncasecmp( value, "waypoint", value_len ) == 0 ) | |
357 | line_type = GPSPOINT_TYPE_WAYPOINT; | |
358 | else | |
359 | /* all others are ignored */ | |
360 | line_type = GPSPOINT_TYPE_NONE; | |
361 | } | |
362 | else if (key_len == 4 && strncasecmp( key, "name", key_len ) == 0 && value != NULL) | |
363 | { | |
364 | if (line_name == NULL) | |
365 | { | |
366 | line_name = g_strndup ( value, value_len ); | |
367 | } | |
368 | } | |
369 | else if (key_len == 7 && strncasecmp( key, "comment", key_len ) == 0 && value != NULL) | |
370 | { | |
371 | if (line_comment == NULL) | |
372 | line_comment = deslashndup ( value, value_len ); | |
373 | } | |
374 | else if (key_len == 5 && strncasecmp( key, "image", key_len ) == 0 && value != NULL) | |
375 | { | |
376 | if (line_image == NULL) | |
377 | line_image = deslashndup ( value, value_len ); | |
378 | } | |
379 | else if (key_len == 8 && strncasecmp( key, "latitude", key_len ) == 0 && value != NULL) | |
380 | { | |
381 | line_latlon.lat = g_strtod(value, NULL); | |
382 | } | |
383 | else if (key_len == 9 && strncasecmp( key, "longitude", key_len ) == 0 && value != NULL) | |
384 | { | |
385 | line_latlon.lon = g_strtod(value, NULL); | |
386 | } | |
387 | else if (key_len == 8 && strncasecmp( key, "altitude", key_len ) == 0 && value != NULL) | |
388 | { | |
389 | line_altitude = g_strtod(value, NULL); | |
390 | } | |
391 | else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T') | |
392 | { | |
393 | line_visible = FALSE; | |
394 | } | |
395 | else if (key_len == 6 && strncasecmp( key, "symbol", key_len ) == 0 && value != NULL) | |
396 | { | |
397 | line_symbol = g_strndup ( value, value_len ); | |
398 | } | |
399 | else if (key_len == 8 && strncasecmp( key, "unixtime", key_len ) == 0 && value != NULL) | |
400 | { | |
401 | line_timestamp = g_strtod(value, NULL); | |
402 | if ( line_timestamp != 0x80000000 ) | |
403 | line_has_timestamp = TRUE; | |
404 | } | |
405 | else if (key_len == 10 && strncasecmp( key, "newsegment", key_len ) == 0 && value != NULL) | |
406 | { | |
407 | line_newsegment = TRUE; | |
408 | } | |
409 | else if (key_len == 8 && strncasecmp( key, "extended", key_len ) == 0 && value != NULL) | |
410 | { | |
411 | line_extended = TRUE; | |
412 | } | |
413 | else if (key_len == 5 && strncasecmp( key, "speed", key_len ) == 0 && value != NULL) | |
414 | { | |
415 | line_speed = g_strtod(value, NULL); | |
416 | } | |
417 | else if (key_len == 6 && strncasecmp( key, "course", key_len ) == 0 && value != NULL) | |
418 | { | |
419 | line_course = g_strtod(value, NULL); | |
420 | } | |
421 | else if (key_len == 3 && strncasecmp( key, "sat", key_len ) == 0 && value != NULL) | |
422 | { | |
423 | line_sat = atoi(value); | |
424 | } | |
425 | else if (key_len == 3 && strncasecmp( key, "fix", key_len ) == 0 && value != NULL) | |
426 | { | |
427 | line_fix = atoi(value); | |
428 | } | |
429 | } | |
430 | ||
431 | static void a_gpspoint_write_waypoint ( const gchar *name, VikWaypoint *wp, FILE *f ) | |
432 | { | |
433 | static struct LatLon ll; | |
434 | gchar *s_lat, *s_lon; | |
435 | vik_coord_to_latlon ( &(wp->coord), &ll ); | |
436 | s_lat = a_coords_dtostr(ll.lat); | |
437 | s_lon = a_coords_dtostr(ll.lon); | |
438 | fprintf ( f, "type=\"waypoint\" latitude=\"%s\" longitude=\"%s\" name=\"%s\"", s_lat, s_lon, name ); | |
439 | g_free ( s_lat ); | |
440 | g_free ( s_lon ); | |
441 | ||
442 | if ( wp->altitude != VIK_DEFAULT_ALTITUDE ) { | |
443 | gchar *s_alt = a_coords_dtostr(wp->altitude); | |
444 | fprintf ( f, " altitude=\"%s\"", s_alt ); | |
445 | g_free(s_alt); | |
446 | } | |
447 | if ( wp->comment ) | |
448 | { | |
449 | gchar *tmp_comment = slashdup(wp->comment); | |
450 | fprintf ( f, " comment=\"%s\"", tmp_comment ); | |
451 | g_free ( tmp_comment ); | |
452 | } | |
453 | if ( wp->image ) | |
454 | { | |
455 | gchar *tmp_image = slashdup(wp->image); | |
456 | fprintf ( f, " image=\"%s\"", tmp_image ); | |
457 | g_free ( tmp_image ); | |
458 | } | |
459 | if ( wp->symbol ) | |
460 | { | |
461 | fprintf ( f, " symbol=\"%s\"", wp->symbol ); | |
462 | } | |
463 | if ( ! wp->visible ) | |
464 | fprintf ( f, " visible=\"n\"" ); | |
465 | fprintf ( f, "\n" ); | |
466 | } | |
467 | ||
468 | static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, FILE *f ) | |
469 | { | |
470 | static struct LatLon ll; | |
471 | gchar *s_lat, *s_lon; | |
472 | vik_coord_to_latlon ( &(tp->coord), &ll ); | |
473 | ||
474 | /* TODO: modify a_coords_dtostr() to accept (optional) buffer | |
475 | * instead of doing malloc/free everytime */ | |
476 | s_lat = a_coords_dtostr(ll.lat); | |
477 | s_lon = a_coords_dtostr(ll.lon); | |
478 | fprintf ( f, "type=\"trackpoint\" latitude=\"%s\" longitude=\"%s\"", s_lat, s_lon ); | |
479 | g_free ( s_lat ); | |
480 | g_free ( s_lon ); | |
481 | ||
482 | if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) { | |
483 | gchar *s_alt = a_coords_dtostr(tp->altitude); | |
484 | fprintf ( f, " altitude=\"%s\"", s_alt ); | |
485 | g_free(s_alt); | |
486 | } | |
487 | if ( tp->has_timestamp ) | |
488 | fprintf ( f, " unixtime=\"%ld\"", tp->timestamp ); | |
489 | if ( tp->newsegment ) | |
490 | fprintf ( f, " newsegment=\"yes\"" ); | |
491 | ||
492 | if (tp->extended) { | |
493 | fprintf ( f, " extended=\"yes\"" ); | |
494 | if (!isnan(tp->speed)) { | |
495 | gchar *s_speed = a_coords_dtostr(tp->speed); | |
496 | fprintf ( f, " speed=\"%s\"", s_speed ); | |
497 | g_free(s_speed); | |
498 | } | |
499 | if (!isnan(tp->course)) { | |
500 | gchar *s_course = a_coords_dtostr(tp->course); | |
501 | fprintf ( f, " course=\"%s\"", s_course ); | |
502 | g_free(s_course); | |
503 | } | |
504 | if (tp->nsats > 0) | |
505 | fprintf ( f, " sat=\"%d\"", tp->nsats ); | |
506 | if (tp->fix_mode > 0) | |
507 | fprintf ( f, " fix=\"%d\"", tp->fix_mode ); | |
508 | } | |
509 | fprintf ( f, "\n" ); | |
510 | } | |
511 | ||
512 | ||
513 | static void a_gpspoint_write_track ( const gchar *name, VikTrack *t, FILE *f ) | |
514 | { | |
515 | if ( t->comment ) | |
516 | { | |
517 | gchar *tmp_comment = slashdup(t->comment); | |
518 | fprintf ( f, "type=\"track\" name=\"%s\" comment=\"%s\"%s\n", name, tmp_comment, t->visible ? "" : " visible=\"n\"" ); | |
519 | g_free ( tmp_comment ); | |
520 | } | |
521 | else | |
522 | fprintf ( f, "type=\"track\" name=\"%s\"%s\n", name, t->visible ? "" : " visible=\"n\"" ); | |
523 | g_list_foreach ( t->trackpoints, (GFunc) a_gpspoint_write_trackpoint, f ); | |
524 | fprintf ( f, "type=\"trackend\"\n" ); | |
525 | } | |
526 | ||
527 | void a_gpspoint_write_file ( VikTrwLayer *trw, FILE *f ) | |
528 | { | |
529 | GHashTable *tracks = vik_trw_layer_get_tracks ( trw ); | |
530 | GHashTable *waypoints = vik_trw_layer_get_waypoints ( trw ); | |
531 | ||
532 | fprintf ( f, "type=\"waypointlist\"\n" ); | |
533 | g_hash_table_foreach ( waypoints, (GHFunc) a_gpspoint_write_waypoint, f ); | |
534 | fprintf ( f, "type=\"waypointlistend\"\n" ); | |
535 | g_hash_table_foreach ( tracks, (GHFunc) a_gpspoint_write_track, f ); | |
536 | } |