Parent Directory | Revision Log
Fixed text parser bug
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of GPXView. |
5 | * |
6 | * GPXView 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 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * GPXView 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 GPXView. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include <stdio.h> |
21 | #include <stdlib.h> |
22 | #include <string.h> |
23 | #include <math.h> |
24 | |
25 | #include <libxml/parser.h> |
26 | #include <libxml/tree.h> |
27 | |
28 | #include <libxml/xmlreader.h> |
29 | |
30 | #include <glib.h> |
31 | #include <glib/gstdio.h> |
32 | |
33 | #include <zlib.h> |
34 | |
35 | #include "gpxview.h" |
36 | #include "unzip.h" |
37 | |
38 | void gpx_free_wpt(wpt_t *wpt) { |
39 | if(wpt->id) xmlFree(wpt->id); |
40 | if(wpt->cmt) xmlFree(wpt->cmt); |
41 | if(wpt->desc) xmlFree(wpt->desc); |
42 | free(wpt); |
43 | } |
44 | |
45 | void gpx_free_log(log_t *log) { |
46 | if(log->finder) xmlFree(log->finder); |
47 | if(log->text) xmlFree(log->text); |
48 | free(log); |
49 | } |
50 | |
51 | void gpx_free_tb(tb_t *tb) { |
52 | if(tb->name) xmlFree(tb->name); |
53 | if(tb->ref) xmlFree(tb->ref); |
54 | free(tb); |
55 | } |
56 | |
57 | void gpx_free_cache(cache_t *cache) { |
58 | log_t *log = cache->log; |
59 | wpt_t *wpt = cache->wpt; |
60 | tb_t *tb = cache->tb; |
61 | |
62 | if(cache->id) xmlFree(cache->id); |
63 | if(cache->name) xmlFree(cache->name); |
64 | if(cache->owner) xmlFree(cache->owner); |
65 | if(cache->short_description) xmlFree(cache->short_description); |
66 | if(cache->long_description) xmlFree(cache->long_description); |
67 | if(cache->hint) xmlFree(cache->hint); |
68 | if(cache->url) xmlFree(cache->url); |
69 | |
70 | /* free all logs */ |
71 | while(log) { log_t *next = log->next; gpx_free_log(log); log = next; } |
72 | |
73 | /* free all waypoints */ |
74 | while(wpt) { wpt_t *next = wpt->next; gpx_free_wpt(wpt); wpt = next; } |
75 | |
76 | /* free all tbs */ |
77 | while(tb) { tb_t *next = tb->next; gpx_free_tb(tb); tb = next; } |
78 | |
79 | if(cache->notes) notes_free(cache->notes); |
80 | |
81 | free(cache); |
82 | } |
83 | |
84 | void gpx_free_caches(gpx_t *gpx) { |
85 | cache_t *cache = gpx->cache; |
86 | |
87 | /* free all caches */ |
88 | while(cache) { |
89 | cache_t *next = cache->next; |
90 | gpx_free_cache(cache); |
91 | cache = next; |
92 | } |
93 | |
94 | gpx->cache = NULL; |
95 | } |
96 | |
97 | void gpx_free(gpx_t *gpx) { |
98 | |
99 | if(gpx->name) xmlFree(gpx->name); |
100 | if(gpx->desc) xmlFree(gpx->desc); |
101 | if(gpx->filename) free(gpx->filename); |
102 | |
103 | gpx_free_caches(gpx); |
104 | |
105 | free(gpx); |
106 | } |
107 | |
108 | void gpx_free_all(gpx_t *gpx) { |
109 | while(gpx) { |
110 | gpx_t *next = gpx->next; |
111 | gpx_free(gpx); |
112 | gpx = next; |
113 | } |
114 | } |
115 | |
116 | static const char *cache_type_str[] = { "<Unknown>", |
117 | "Traditional Cache|Traditional|Geocache", "Multi-cache|Multi", |
118 | "Unknown Cache|Other", |
119 | "Virtual Cache|Virtual", "Webcam Cache|Webcam", "Event Cache|Event|Geocoins:", |
120 | "Letterbox Hybrid|Letterbox", "Earthcache", "Wherigo Cache", |
121 | "Mega-Event Cache", "Cache In Trash Out Event", |
122 | ""}; |
123 | |
124 | static const char *cache_container_str[] = { "<Unknown>", |
125 | "Regular", "Small", "Micro", "Not chosen|Unknown", |
126 | "Other", "Large", "Virtual" |
127 | ""}; |
128 | |
129 | static const char *log_type_str[] = { "<Unknown>", |
130 | "Found it|Found", "Didn't find it|Not Found", "Owner Maintenance", |
131 | "Write Note|Note|Other", |
132 | "Post Reviewer Note", "Enable Listing", "Publish Listing", "Will Attend", |
133 | "Attended", "Webcam Photo taken", |
134 | "Temporarily Disable Listing|Cache Disabled!", |
135 | "Needs Maintenance", "Update Coordinates", "Unarchive|Archive (show)", |
136 | "Needs Archived", "Archive", |
137 | ""}; |
138 | |
139 | static const char *wpt_sym_str[] = { "<Unknown>", |
140 | "Stages of a Multicache", "Parking Area", "Final Location", |
141 | "Question to Answer", "Trailhead", "Reference Point", |
142 | ""}; |
143 | |
144 | #define DLG_DIV 10 |
145 | |
146 | /* create the dialog box shown while loading in progress */ |
147 | gpx_dialog_t *gpx_busy_dialog_new(GtkWidget *parent) { |
148 | #ifdef USE_MAEMO |
149 | gpx_dialog_t *dialog = malloc(sizeof(gpx_dialog_t)); |
150 | memset(dialog, 0, sizeof(gpx_dialog_t)); |
151 | |
152 | dialog->dialog = gtk_dialog_new(); |
153 | |
154 | gtk_dialog_set_has_separator(GTK_DIALOG(dialog->dialog), FALSE); |
155 | gtk_window_set_title(GTK_WINDOW(dialog->dialog), _("Loading")); |
156 | gtk_window_set_default_size(GTK_WINDOW(dialog->dialog), 300, 10); |
157 | |
158 | gtk_window_set_modal(GTK_WINDOW(dialog->dialog), TRUE); |
159 | gtk_window_set_transient_for(GTK_WINDOW(dialog->dialog), GTK_WINDOW(parent)); |
160 | |
161 | dialog->label = gtk_label_new("---"); |
162 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog->dialog)->vbox), |
163 | dialog->label); |
164 | |
165 | dialog->pbar = gtk_progress_bar_new(); |
166 | gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(dialog->pbar), |
167 | 0.0025 * DLG_DIV); |
168 | |
169 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog->dialog)->vbox), |
170 | dialog->pbar); |
171 | |
172 | gtk_widget_show_all(dialog->dialog); |
173 | |
174 | return dialog; |
175 | #else |
176 | return NULL; |
177 | #endif |
178 | } |
179 | |
180 | void gpx_busy_dialog_destroy(gpx_dialog_t *dialog) { |
181 | if(!dialog) return; |
182 | |
183 | gtk_widget_destroy(dialog->dialog); |
184 | free(dialog); |
185 | } |
186 | |
187 | static void gpx_busy_dialog_progress(gpx_dialog_t *dialog) { |
188 | static int sub_dlg = 0; |
189 | |
190 | if(!dialog) return; |
191 | |
192 | if(sub_dlg++ >= DLG_DIV) { |
193 | sub_dlg = 0; |
194 | |
195 | gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dialog->pbar)); |
196 | |
197 | /* wait for main gui to appear */ |
198 | while(gtk_events_pending()) |
199 | gtk_main_iteration(); |
200 | } |
201 | } |
202 | |
203 | static void gpx_busy_dialog_set(gpx_dialog_t *dialog, char *name) { |
204 | if(!dialog) return; |
205 | |
206 | if(strrchr(name, '/')) |
207 | name = strrchr(name, '/')+1; |
208 | |
209 | gtk_label_set_text(GTK_LABEL(dialog->label), name); |
210 | |
211 | /* wait for main gui to appear */ |
212 | while(gtk_events_pending()) |
213 | gtk_main_iteration(); |
214 | } |
215 | |
216 | static int str_search(const char *pstr[], char *str, char *type) { |
217 | int i=0; |
218 | |
219 | while(pstr[i+1][0]) { |
220 | char *p = (char*)pstr[i+1]; |
221 | |
222 | /* multiple substrings in pattern? */ |
223 | while(strchr(p, '|')) { |
224 | if(!strncasecmp(p, str, strchr(p, '|')-p)) |
225 | return i; |
226 | |
227 | p = strchr(p, '|')+1; |
228 | } |
229 | |
230 | if(!strcasecmp(p, str)) |
231 | return i; |
232 | |
233 | i++; |
234 | } |
235 | |
236 | fprintf(stderr, "ERROR parsing \"%s\": Unknown \"%s\"\n", type, str); |
237 | return -1; |
238 | } |
239 | |
240 | static int log_is_older(log_t *a, log_t *b) { |
241 | if(a->year < b->year) return TRUE; |
242 | else if(a->year == b->year) { |
243 | if(a->month < b->month) return TRUE; |
244 | else if(a->month == b->month) { |
245 | if(a->day < b->day) return TRUE; |
246 | } |
247 | } |
248 | |
249 | return FALSE; |
250 | } |
251 | |
252 | int is_white(char c) { |
253 | return((c==' ')||(c=='\r')||(c=='\n')); |
254 | } |
255 | |
256 | static int all_is_white(char *str) { |
257 | while(*str) { |
258 | if(!is_white(*str)) |
259 | return FALSE; |
260 | |
261 | str++; |
262 | } |
263 | return TRUE; |
264 | } |
265 | |
266 | void gpx_display_log(log_t *log) { |
267 | printf(" Log:\n"); |
268 | printf(" date: %d.%d.%d\n", log->day, log->month, log->year); |
269 | printf(" type: %s\n", log_type_str[log->type+1]); |
270 | printf(" finder: %s\n", log->finder); |
271 | // printf(" text: %s\n", log->text); |
272 | } |
273 | |
274 | void gpx_display_cache(cache_t *cache) { |
275 | log_t *log = cache->log; |
276 | |
277 | printf("\nCache:\n"); |
278 | printf(" id: %s\n", cache->id); |
279 | printf(" name: %s\n", cache->name); |
280 | printf(" latitude: %f\n", cache->pos.lat); |
281 | printf(" longitude: %f\n", cache->pos.lon); |
282 | printf(" owner: %s\n", cache->owner); |
283 | printf(" type: %s\n", cache_type_str[cache->type+1]); |
284 | printf(" container: %s\n", cache_container_str[cache->container+1]); |
285 | printf(" difficulty: %.1f\n", cache->difficulty); |
286 | printf(" terrain: %.1f\n", cache->terrain); |
287 | // printf(" short: %s\n", cache->short_description); |
288 | // printf(" long: %s\n", cache->long_description); |
289 | // printf(" hint: %s\n", cache->hint); |
290 | |
291 | while(log) { |
292 | gpx_display_log(log); |
293 | log = log->next; |
294 | } |
295 | } |
296 | |
297 | void gpx_display_all(gpx_t *gpx) { |
298 | while(gpx) { |
299 | cache_t *cache = gpx->cache; |
300 | |
301 | printf("GPX name: %s\n", gpx->name); |
302 | printf("GPX desc: %s\n", gpx->desc); |
303 | printf("GPX date: %d.%d.%d\n", gpx->day, gpx->month, gpx->year); |
304 | while(cache) { |
305 | gpx_display_cache(cache); |
306 | cache = cache->next; |
307 | } |
308 | gpx = gpx->next; |
309 | } |
310 | } |
311 | |
312 | static gint my_strcmp(const xmlChar *a, const xmlChar *b) { |
313 | if(!a && !b) return 0; |
314 | if(!a) return -1; |
315 | if(!b) return +1; |
316 | return strcmp((char*)a,(char*)b); |
317 | } |
318 | |
319 | static float xml_get_prop_float(xmlTextReaderPtr reader, char *name) { |
320 | float ret = NAN; |
321 | char *prop; |
322 | if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST name))) { |
323 | ret = g_ascii_strtod(prop, NULL); |
324 | xmlFree(prop); |
325 | } |
326 | return ret; |
327 | } |
328 | |
329 | static int xml_prop_is(xmlTextReaderPtr reader, char *name, char *value, |
330 | int def_value) { |
331 | int match = def_value; |
332 | char *prop; |
333 | if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST name))) { |
334 | match = (strcasecmp(prop, value) == 0); |
335 | xmlFree(prop); |
336 | } |
337 | return match; |
338 | } |
339 | |
340 | /* skip current element incl. everything below (mainly for testing) */ |
341 | /* returns FALSE if something failed */ |
342 | static gboolean skip_element(xmlTextReaderPtr reader) { |
343 | g_assert(xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT); |
344 | const xmlChar *name = xmlTextReaderConstName(reader); |
345 | g_assert(name); |
346 | int depth = xmlTextReaderDepth(reader); |
347 | |
348 | if(xmlTextReaderIsEmptyElement(reader)) |
349 | return TRUE; |
350 | |
351 | int ret = xmlTextReaderRead(reader); |
352 | while((ret == 1) && |
353 | ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) || |
354 | (xmlTextReaderDepth(reader) > depth) || |
355 | (my_strcmp(xmlTextReaderConstName(reader), name) != 0))) { |
356 | ret = xmlTextReaderRead(reader); |
357 | } |
358 | return(ret == 1); |
359 | } |
360 | |
361 | static char *process_text(xmlTextReaderPtr reader) { |
362 | char *text = NULL; |
363 | |
364 | if(!xmlTextReaderIsEmptyElement(reader)) { |
365 | |
366 | int depth = xmlTextReaderDepth(reader); |
367 | int ret = xmlTextReaderRead(reader); |
368 | while((ret == 1) && |
369 | ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) || |
370 | (xmlTextReaderDepth(reader) != depth))) { |
371 | |
372 | /* found a text fragment */ |
373 | if((xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) || |
374 | (xmlTextReaderNodeType(reader) == XML_READER_TYPE_CDATA)) { |
375 | char *frag = (char*)xmlTextReaderConstValue(reader); |
376 | |
377 | if(!text) text = strdup(frag); |
378 | else { |
379 | char *old = text; |
380 | text = malloc(strlen(old) + strlen(frag) + 1); |
381 | strcpy(text, old); |
382 | strcat(text, frag); |
383 | free(old); |
384 | } |
385 | } |
386 | ret = xmlTextReaderRead(reader); |
387 | } |
388 | } |
389 | |
390 | return text; |
391 | } |
392 | |
393 | static int xml_str_search(xmlTextReaderPtr reader, |
394 | const char *pstr[], char *type, int def) { |
395 | char *text = process_text(reader); |
396 | int result = def; |
397 | if(text) { |
398 | result = str_search(pstr, text, type); |
399 | free(text); |
400 | } |
401 | return result; |
402 | } |
403 | |
404 | static float xml_float(xmlTextReaderPtr reader, float def) { |
405 | char *text = process_text(reader); |
406 | float result = def; |
407 | if(text) { |
408 | result = g_ascii_strtod(text, NULL); |
409 | free(text); |
410 | } |
411 | return result; |
412 | } |
413 | |
414 | static void xml_get_date(xmlTextReaderPtr reader, int *year, int *month, int *day) { |
415 | char *str = process_text(reader); |
416 | if(str) { |
417 | sscanf(str, "%d-%d-%d", year, month, day); |
418 | free(str); |
419 | } |
420 | } |
421 | |
422 | static log_t *process_gpx_wpt_gc_logs_log(xmlTextReaderPtr reader) { |
423 | |
424 | if(xmlTextReaderIsEmptyElement(reader)) |
425 | return NULL; |
426 | |
427 | /* create a new log entry */ |
428 | log_t *log = malloc(sizeof(log_t)); |
429 | memset(log, 0, sizeof(log_t)); |
430 | |
431 | /* process all sub-nodes */ |
432 | int depth = xmlTextReaderDepth(reader); |
433 | int ret = xmlTextReaderRead(reader); |
434 | while(ret == 1) { |
435 | |
436 | switch(xmlTextReaderNodeType(reader)) { |
437 | case XML_READER_TYPE_ELEMENT: |
438 | g_assert(xmlTextReaderDepth(reader) == depth+1); |
439 | char *name = (char*)xmlTextReaderConstName(reader); |
440 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; |
441 | if(name) { |
442 | if((strcasecmp(name, "date") == 0) || |
443 | (strcasecmp(name, "time") == 0)) { |
444 | xml_get_date(reader, &log->year, &log->month, &log->day); |
445 | } else if(strcasecmp(name, "type") == 0) { |
446 | log->type = xml_str_search(reader, log_type_str, "log", 0); |
447 | } else if((strcasecmp(name, "finder") == 0) || |
448 | (strcasecmp(name, "geocacher") == 0)) { |
449 | if(!log->finder) log->finder = process_text(reader); |
450 | } else if(strcasecmp(name, "text") == 0) { |
451 | if(!log->text) log->text = process_text(reader); |
452 | } else |
453 | skip_element(reader); |
454 | } else |
455 | skip_element(reader); |
456 | break; |
457 | |
458 | case XML_READER_TYPE_END_ELEMENT: |
459 | /* end element must be for the current element */ |
460 | g_assert(xmlTextReaderDepth(reader) == depth); |
461 | return log; |
462 | break; |
463 | |
464 | default: |
465 | break; |
466 | } |
467 | ret = xmlTextReaderRead(reader); |
468 | } |
469 | |
470 | gpx_free_log(log); |
471 | return NULL; |
472 | } |
473 | |
474 | static log_t *process_gpx_wpt_gc_logs(xmlTextReaderPtr reader) { |
475 | log_t *log_chain = NULL; |
476 | |
477 | if(xmlTextReaderIsEmptyElement(reader)) |
478 | return NULL; |
479 | |
480 | /* process all sub-nodes */ |
481 | int depth = xmlTextReaderDepth(reader); |
482 | int ret = xmlTextReaderRead(reader); |
483 | while(ret == 1) { |
484 | |
485 | switch(xmlTextReaderNodeType(reader)) { |
486 | case XML_READER_TYPE_ELEMENT: |
487 | g_assert(xmlTextReaderDepth(reader) == depth+1); |
488 | char *name = (char*)xmlTextReaderConstName(reader); |
489 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; |
490 | if(name) { |
491 | if(strcasecmp(name, "log") == 0) { |
492 | log_t *log = process_gpx_wpt_gc_logs_log(reader); |
493 | if(log) { |
494 | /* add log to chain */ |
495 | log_t **cur = &log_chain; |
496 | while(*cur && log_is_older(log, *cur)) |
497 | cur = &((*cur)->next); |
498 | |
499 | log->next = *cur; |
500 | *cur = log; |
501 | } |
502 | } else |
503 | skip_element(reader); |
504 | } else |
505 | skip_element(reader); |
506 | break; |
507 | |
508 | case XML_READER_TYPE_END_ELEMENT: |
509 | /* end element must be for the current element */ |
510 | g_assert(xmlTextReaderDepth(reader) == depth); |
511 | return log_chain; |
512 | break; |
513 | |
514 | default: |
515 | break; |
516 | } |
517 | ret = xmlTextReaderRead(reader); |
518 | } |
519 | |
520 | /* free the entire log chain */ |
521 | while(log_chain) { |
522 | log_t *next = log_chain->next; |
523 | gpx_free_log(log_chain); |
524 | log_chain = next; |
525 | } |
526 | |
527 | return NULL; |
528 | } |
529 | |
530 | static tb_t *process_gpx_wpt_gc_tbs_travelbug(xmlTextReaderPtr reader) { |
531 | |
532 | if(xmlTextReaderIsEmptyElement(reader)) |
533 | return NULL; |
534 | |
535 | /* create a new tb entry */ |
536 | tb_t *tb = malloc(sizeof(tb_t)); |
537 | memset(tb, 0, sizeof(tb_t)); |
538 | |
539 | char *prop; |
540 | if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "ref"))) |
541 | tb->ref = strdup(prop); |
542 | else |
543 | tb->ref = strdup("<NONE>"); |
544 | |
545 | /* process all sub-nodes */ |
546 | int depth = xmlTextReaderDepth(reader); |
547 | int ret = xmlTextReaderRead(reader); |
548 | while(ret == 1) { |
549 | |
550 | switch(xmlTextReaderNodeType(reader)) { |
551 | case XML_READER_TYPE_ELEMENT: |
552 | g_assert(xmlTextReaderDepth(reader) == depth+1); |
553 | char *name = (char*)xmlTextReaderConstName(reader); |
554 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; |
555 | if(name) { |
556 | if(strcasecmp(name, "name") == 0) { |
557 | if(!tb->name) tb->name = process_text(reader); |
558 | } else |
559 | skip_element(reader); |
560 | } else |
561 | skip_element(reader); |
562 | break; |
563 | |
564 | case XML_READER_TYPE_END_ELEMENT: |
565 | /* end element must be for the current element */ |
566 | g_assert(xmlTextReaderDepth(reader) == depth); |
567 | return tb; |
568 | break; |
569 | |
570 | default: |
571 | break; |
572 | } |
573 | ret = xmlTextReaderRead(reader); |
574 | } |
575 | |
576 | gpx_free_tb(tb); |
577 | return NULL; |
578 | } |
579 | |
580 | static tb_t *process_gpx_wpt_gc_tbs(xmlTextReaderPtr reader) { |
581 | tb_t *tb = NULL, **tbP = &tb; |
582 | |
583 | if(xmlTextReaderIsEmptyElement(reader)) |
584 | return tb; |
585 | |
586 | /* process all sub-nodes */ |
587 | int depth = xmlTextReaderDepth(reader); |
588 | int ret = xmlTextReaderRead(reader); |
589 | while(ret == 1) { |
590 | |
591 | switch(xmlTextReaderNodeType(reader)) { |
592 | case XML_READER_TYPE_ELEMENT: |
593 | g_assert(xmlTextReaderDepth(reader) == depth+1); |
594 | char *name = (char*)xmlTextReaderConstName(reader); |
595 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; |
596 | if(name) { |
597 | if(strcasecmp(name, "travelbug") == 0) { |
598 | *tbP = process_gpx_wpt_gc_tbs_travelbug(reader); |
599 | if(*tbP) tbP = &(*tbP)->next; |
600 | } else |
601 | skip_element(reader); |
602 | } else |
603 | skip_element(reader); |
604 | break; |
605 | |
606 | case XML_READER_TYPE_END_ELEMENT: |
607 | /* end element must be for the current element */ |
608 | g_assert(xmlTextReaderDepth(reader) == depth); |
609 | return tb; |
610 | break; |
611 | |
612 | default: |
613 | break; |
614 | } |
615 | ret = xmlTextReaderRead(reader); |
616 | } |
617 | |
618 | while(tb) { |
619 | tb_t *next = tb; |
620 | gpx_free_tb(tb); |
621 | tb = next; |
622 | } |
623 | |
624 | return NULL; |
625 | } |
626 | |
627 | static void process_gpx_wpt_gc(xmlTextReaderPtr reader, cache_t *cache) { |
628 | cache->available = xml_prop_is(reader, "available", "true", TRUE); |
629 | cache->archived = xml_prop_is(reader, "archived", "true", FALSE); |
630 | |
631 | if(xmlTextReaderIsEmptyElement(reader)) |
632 | return; |
633 | |
634 | /* process all sub-nodes */ |
635 | int depth = xmlTextReaderDepth(reader); |
636 | int ret = xmlTextReaderRead(reader); |
637 | while(ret == 1) { |
638 | |
639 | switch(xmlTextReaderNodeType(reader)) { |
640 | case XML_READER_TYPE_ELEMENT: |
641 | g_assert(xmlTextReaderDepth(reader) == depth+1); |
642 | char *name = (char*)xmlTextReaderConstName(reader); |
643 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; |
644 | if(name) { |
645 | if(strcasecmp(name, "name") == 0) { |
646 | if(!cache->name) cache->name = process_text(reader); |
647 | } else if(strcasecmp(name, "owner") == 0) { |
648 | if(!cache->owner) cache->owner = process_text(reader); |
649 | } else if(strcasecmp(name, "type") == 0) { |
650 | cache->type = xml_str_search(reader, cache_type_str, |
651 | "cache type", CACHE_TYPE_UNKNOWN); |
652 | } else if(strcasecmp(name, "container") == 0) { |
653 | cache->container = xml_str_search(reader, cache_container_str, |
654 | "container", CACHE_CONT_UNKNOWN); |
655 | } else if((strcasecmp(name, "short_description") == 0) || |
656 | (strcasecmp(name, "summary") == 0)) { |
657 | if(!cache->short_description) { |
658 | cache->short_description = process_text(reader); |
659 | cache->short_is_html = xml_prop_is(reader, "html", "true", FALSE); |
660 | } |
661 | } else if((strcasecmp(name, "long_description") == 0) || |
662 | (strcasecmp(name, "description") == 0)) { |
663 | if(!cache->long_description) { |
664 | cache->long_description = process_text(reader); |
665 | cache->long_is_html = xml_prop_is(reader, "html", "true", FALSE); |
666 | } |
667 | } else if((strcasecmp(name, "encoded_hints") == 0) || |
668 | (strcasecmp(name, "hints") == 0)) { |
669 | if(!cache->hint) { |
670 | cache->hint = process_text(reader); |
671 | |
672 | /* often hints aren't more than just a bunch of blanks ... */ |
673 | if(cache->hint && all_is_white(cache->hint)) { |
674 | free(cache->hint); |
675 | cache->hint = NULL; |
676 | } else |
677 | cache->hint_is_html = xml_prop_is(reader, "html", "true", FALSE); |
678 | } |
679 | } else if(strcasecmp(name, "difficulty") == 0) { |
680 | cache->difficulty = xml_float(reader, 0.0); |
681 | } else if(strcasecmp(name, "terrain") == 0) { |
682 | cache->terrain = xml_float(reader, 0.0); |
683 | } else if(strcasecmp(name, "logs") == 0) { |
684 | if(!cache->log) cache->log = process_gpx_wpt_gc_logs(reader); |
685 | } else if(strcasecmp(name, "travelbugs") == 0) { |
686 | if(!cache->tb) cache->tb = process_gpx_wpt_gc_tbs(reader); |
687 | } else { |
688 | // printf("unhandled item found: gpx/wpt/cache/%s\n", name); |
689 | skip_element(reader); |
690 | } |
691 | } else |
692 | skip_element(reader); |
693 | break; |
694 | |
695 | case XML_READER_TYPE_END_ELEMENT: |
696 | /* end element must be for the current element */ |
697 | g_assert(xmlTextReaderDepth(reader) == depth); |
698 | return; |
699 | break; |
700 | |
701 | default: |
702 | break; |
703 | } |
704 | ret = xmlTextReaderRead(reader); |
705 | } |
706 | } |
707 | |
708 | /* parse waypoint entry */ |
709 | static cache_t *process_gpx_wpt(xmlTextReaderPtr reader, gpx_dialog_t *dialog, |
710 | gpx_t *gpx) { |
711 | char *cmt = NULL, *desc = NULL; |
712 | char *sym = NULL; |
713 | |
714 | gpx_busy_dialog_progress(dialog); |
715 | |
716 | if(xmlTextReaderIsEmptyElement(reader)) |
717 | return NULL; |
718 | |
719 | cache_t *cache = malloc(sizeof(cache_t)); |
720 | memset(cache, 0, sizeof(cache_t)); |
721 | |
722 | /* set some defaults */ |
723 | cache->type = CACHE_TYPE_UNKNOWN; |
724 | cache->container = CACHE_CONT_UNKNOWN; |
725 | cache->available = TRUE; |
726 | |
727 | /* parse attributes */ |
728 | cache->pos.lat = xml_get_prop_float(reader, "lat"); |
729 | cache->pos.lon = xml_get_prop_float(reader, "lon"); |
730 | |
731 | /* process all sub-nodes */ |
732 | int depth = xmlTextReaderDepth(reader); |
733 | int ret = xmlTextReaderRead(reader); |
734 | while(ret == 1) { |
735 | |
736 | switch(xmlTextReaderNodeType(reader)) { |
737 | case XML_READER_TYPE_ELEMENT: |
738 | g_assert(xmlTextReaderDepth(reader) == depth+1); |
739 | char *name = (char*)xmlTextReaderConstName(reader); |
740 | |
741 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; |
742 | |
743 | if(name) { |
744 | if(strcasecmp(name, "name") == 0) { |
745 | if(!cache->id) cache->id = process_text(reader); |
746 | } else if(strcasecmp(name, "url") == 0) { |
747 | if(!cache->url) cache->url = process_text(reader); |
748 | } else if((strcasecmp(name, "cache") == 0) || |
749 | (strcasecmp(name, "geocache") == 0)) { |
750 | process_gpx_wpt_gc(reader, cache); |
751 | |
752 | /* the following are used if the current entry is a waypoint */ |
753 | } else if(strcasecmp(name, "cmt") == 0) { |
754 | if(!cmt) cmt = process_text(reader); |
755 | } else if(strcasecmp(name, "desc") == 0) { |
756 | if(!desc) desc = process_text(reader); |
757 | } else if(strcasecmp(name, "sym") == 0) { |
758 | if(!sym) sym = process_text(reader); |
759 | } else { |
760 | skip_element(reader); |
761 | } |
762 | } else |
763 | skip_element(reader); |
764 | break; |
765 | |
766 | case XML_READER_TYPE_END_ELEMENT: |
767 | /* end element must be for the current element */ |
768 | g_assert(xmlTextReaderDepth(reader) == depth); |
769 | |
770 | /* ------------ cleanup -------------- */ |
771 | |
772 | /* special handling for opencaching.de caches */ |
773 | if(cache->id && strncasecmp(cache->id, "OC", 2) == 0) { |
774 | /* the html attributes are either missing or wrong on OC ... */ |
775 | cache->long_is_html = TRUE; |
776 | cache->hint_is_html = TRUE; |
777 | cache->logs_are_html = TRUE; |
778 | } |
779 | |
780 | /* neither geocaching.com GC* nor opencaching.com OC* nor */ |
781 | /* geocaching australia GA* waypoint */ |
782 | if(cache->id && |
783 | (strncasecmp(cache->id, "__", 2) != 0) && |
784 | (strncasecmp(cache->id, "GC", 2) != 0) && |
785 | (strncasecmp(cache->id, "OC", 2) != 0) && |
786 | (strncasecmp(cache->id, "GA", 2) != 0)) { |
787 | cache_t *parent = gpx->cache; |
788 | |
789 | /* check if the gpx file contains a cache with matching name */ |
790 | while(parent && strcasecmp(parent->id+2, cache->id+2)) |
791 | parent = parent->next; |
792 | |
793 | if(parent && parent != cache) { |
794 | wpt_t **wpt = &parent->wpt; |
795 | |
796 | /* search end of list */ |
797 | while(*wpt && (strcmp((*wpt)->id, cache->id)<0)) |
798 | wpt = &(*wpt)->next; |
799 | |
800 | *wpt = malloc(sizeof(wpt_t)); |
801 | memset(*wpt, 0, sizeof(wpt_t)); |
802 | |
803 | /* transfer name to waypoint entry */ |
804 | (*wpt)->id = cache->id; |
805 | cache->id = NULL; |
806 | |
807 | (*wpt)->pos.lat = cache->pos.lat; |
808 | (*wpt)->pos.lon = cache->pos.lon; |
809 | |
810 | (*wpt)->cmt = cmt; cmt = NULL; |
811 | (*wpt)->desc = desc; desc = NULL; |
812 | (*wpt)->sym = str_search(wpt_sym_str, sym, "wpt sym"); |
813 | |
814 | /* and just free the current cache entry as we now have used */ |
815 | /* the data for a caches waypoint */ |
816 | gpx_free_cache(cache); |
817 | cache = NULL; |
818 | } else { |
819 | /* if it doesn't have a name etc, it's probably not a real */ |
820 | /* cache, so drop it */ |
821 | if(!cache->name || !cache->id) { |
822 | printf("Orphaned waypoint: %s\n", cache->id); |
823 | gpx_free_cache(cache); |
824 | cache = NULL; |
825 | } |
826 | } |
827 | } else { |
828 | if(!cache->id) |
829 | cache->id = g_strdup_printf("NO ID"); |
830 | |
831 | /* this is known to be a geocache due to its waypoint name */ |
832 | /* (gc*, oc*, ga*) and is thus forces to be an entry */ |
833 | if(!cache->name) |
834 | cache->name = g_strdup_printf("Unnamed(%s)", cache->id); |
835 | } |
836 | |
837 | if(desc) free(desc); |
838 | if(cmt) free(cmt); |
839 | if(sym) free(sym); |
840 | |
841 | return cache; |
842 | break; |
843 | |
844 | default: |
845 | break; |
846 | } |
847 | ret = xmlTextReaderRead(reader); |
848 | } |
849 | |
850 | gpx_free_cache(cache); |
851 | return NULL; |
852 | } |
853 | |
854 | static gboolean process_gpx(xmlTextReaderPtr reader, gpx_dialog_t *dialog, |
855 | gpx_t *gpx) { |
856 | |
857 | /* no attributes of interest */ |
858 | |
859 | /* the following might be optimized for speed reasons! */ |
860 | |
861 | /* find end of cache chain */ |
862 | cache_t **cache = &gpx->cache; |
863 | while(*cache) cache = &(*cache)->next; |
864 | |
865 | const xmlChar *name = xmlTextReaderConstName(reader); |
866 | if(!name) return FALSE; |
867 | |
868 | /* read next node */ |
869 | int ret = xmlTextReaderRead(reader); |
870 | while(ret == 1) { |
871 | |
872 | switch(xmlTextReaderNodeType(reader)) { |
873 | case XML_READER_TYPE_ELEMENT: |
874 | |
875 | g_assert(xmlTextReaderDepth(reader) == 1); |
876 | char *name = (char*)xmlTextReaderConstName(reader); |
877 | if(name && !gpx->name && strcasecmp(name, "name") == 0) { |
878 | gpx->name = process_text(reader); |
879 | } else if(name && !gpx->desc && strcasecmp(name, "desc") == 0) { |
880 | gpx->desc = process_text(reader); |
881 | } else if(name && ((strcasecmp(name, "time") == 0) || |
882 | (strcasecmp(name, "date") == 0))) { |
883 | xml_get_date(reader, &gpx->year, &gpx->month, &gpx->day); |
884 | } else if(name && strcasecmp(name, "wpt") == 0) { |
885 | *cache = process_gpx_wpt(reader, dialog, gpx); |
886 | if(*cache) cache = &(*cache)->next; |
887 | } else { |
888 | // printf("something unknown (%s) found\n", name); |
889 | skip_element(reader); |
890 | } |
891 | break; |
892 | |
893 | case XML_READER_TYPE_END_ELEMENT: |
894 | /* end element must be for the current element */ |
895 | g_assert(xmlTextReaderDepth(reader) == 0); |
896 | return TRUE; |
897 | break; |
898 | |
899 | default: |
900 | break; |
901 | } |
902 | ret = xmlTextReaderRead(reader); |
903 | } |
904 | |
905 | return FALSE; |
906 | } |
907 | |
908 | /* parse loc waypoint entry */ |
909 | static cache_t *process_loc_waypoint(xmlTextReaderPtr reader, |
910 | gpx_dialog_t *dialog) { |
911 | |
912 | gpx_busy_dialog_progress(dialog); |
913 | |
914 | if(xmlTextReaderIsEmptyElement(reader)) |
915 | return NULL; |
916 | |
917 | cache_t *cache = malloc(sizeof(cache_t)); |
918 | memset(cache, 0, sizeof(cache_t)); |
919 | |
920 | /* set some defaults */ |
921 | cache->type = CACHE_TYPE_TRADITIONAL; |
922 | cache->container = CACHE_CONT_UNKNOWN; |
923 | cache->available = TRUE; |
924 | |
925 | /* process all sub-nodes */ |
926 | int depth = xmlTextReaderDepth(reader); |
927 | int ret = xmlTextReaderRead(reader); |
928 | while(ret == 1) { |
929 | |
930 | switch(xmlTextReaderNodeType(reader)) { |
931 | case XML_READER_TYPE_ELEMENT: |
932 | g_assert(xmlTextReaderDepth(reader) == depth+1); |
933 | char *name = (char*)xmlTextReaderConstName(reader); |
934 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; |
935 | |
936 | if(name) { |
937 | if(strcasecmp(name, "name") == 0) { |
938 | cache->id = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id"); |
939 | cache->name = process_text(reader); |
940 | } else if(strcasecmp(name, "link") == 0) { |
941 | cache->url = process_text(reader); |
942 | } else if(strcasecmp(name, "coord") == 0) { |
943 | cache->pos.lat = xml_get_prop_float(reader, "lat"); |
944 | cache->pos.lon = xml_get_prop_float(reader, "lon"); |
945 | skip_element(reader); |
946 | } else |
947 | skip_element(reader); |
948 | } else |
949 | skip_element(reader); |
950 | break; |
951 | |
952 | case XML_READER_TYPE_END_ELEMENT: |
953 | /* end element must be for the current element */ |
954 | g_assert(xmlTextReaderDepth(reader) == depth); |
955 | return cache; |
956 | break; |
957 | |
958 | default: |
959 | break; |
960 | } |
961 | ret = xmlTextReaderRead(reader); |
962 | } |
963 | |
964 | g_assert(0); |
965 | return NULL; |
966 | } |
967 | |
968 | static void process_loc(xmlTextReaderPtr reader, gpx_dialog_t *dialog, |
969 | gpx_t *gpx) { |
970 | |
971 | /* find end of cache chain */ |
972 | cache_t **cache = &gpx->cache; |
973 | while(*cache) cache = &(*cache)->next; |
974 | |
975 | const xmlChar *name = xmlTextReaderConstName(reader); |
976 | g_assert(name); |
977 | |
978 | /* read next node */ |
979 | int ret = xmlTextReaderRead(reader); |
980 | while(ret == 1) { |
981 | |
982 | switch(xmlTextReaderNodeType(reader)) { |
983 | case XML_READER_TYPE_ELEMENT: |
984 | |
985 | g_assert(xmlTextReaderDepth(reader) == 1); |
986 | char *name = (char*)xmlTextReaderConstName(reader); |
987 | |
988 | if(name && strcasecmp(name, "waypoint") == 0) { |
989 | *cache = process_loc_waypoint(reader, dialog); |
990 | if(*cache) cache = &(*cache)->next; |
991 | } else |
992 | skip_element(reader); |
993 | break; |
994 | |
995 | case XML_READER_TYPE_END_ELEMENT: |
996 | /* end element must be for the current element */ |
997 | g_assert(xmlTextReaderDepth(reader) == 0); |
998 | return; |
999 | break; |
1000 | |
1001 | default: |
1002 | break; |
1003 | } |
1004 | ret = xmlTextReaderRead(reader); |
1005 | } |
1006 | |
1007 | g_assert(0); |
1008 | return; |
1009 | } |
1010 | |
1011 | static gpx_t *process_root(xmlTextReaderPtr reader, gpx_dialog_t *dialog, |
1012 | char *fname, gpx_t *in) { |
1013 | |
1014 | /* no gpx entry given, create a new one */ |
1015 | gpx_t *gpx = NULL; |
1016 | if(!in) { |
1017 | /* allocate memory to hold gpx file description */ |
1018 | gpx = malloc(sizeof(gpx_t)); |
1019 | memset(gpx, 0, sizeof(gpx_t)); |
1020 | gpx->filename = strdup(fname); |
1021 | } else |
1022 | gpx = in; |
1023 | |
1024 | int ret = xmlTextReaderRead(reader); |
1025 | while(ret == 1) { |
1026 | switch(xmlTextReaderNodeType(reader)) { |
1027 | case XML_READER_TYPE_ELEMENT: |
1028 | g_assert(xmlTextReaderDepth(reader) == 0); |
1029 | char *name = (char*)xmlTextReaderConstName(reader); |
1030 | if(name && strcasecmp(name, "gpx") == 0) { |
1031 | process_gpx(reader, dialog, gpx); |
1032 | } else if(name && strcasecmp(name, "loc") == 0) { |
1033 | process_loc(reader, dialog, gpx); |
1034 | } else { |
1035 | printf("something unknown found\n"); |
1036 | skip_element(reader); |
1037 | } |
1038 | break; |
1039 | |
1040 | case XML_READER_TYPE_END_ELEMENT: |
1041 | /* end element must be for the current element */ |
1042 | g_assert(xmlTextReaderDepth(reader) == 0); |
1043 | ret = -1; |
1044 | break; |
1045 | |
1046 | default: |
1047 | break; |
1048 | } |
1049 | |
1050 | if(ret == 1) |
1051 | ret = xmlTextReaderRead(reader); |
1052 | } |
1053 | |
1054 | /* check if a name has been set and use filename if not */ |
1055 | if(!in && !gpx->name) { |
1056 | if(!gpx->desc) { |
1057 | char *str = strrchr(fname, '/'); |
1058 | if(str) gpx->name = strdup(str+1); |
1059 | else gpx->name = strdup(fname); |
1060 | } else |
1061 | gpx->name = strdup(gpx->desc); |
1062 | } |
1063 | |
1064 | return gpx; |
1065 | } |
1066 | |
1067 | static gpx_t *gpx_parse_file(gpx_dialog_t *dialog, char *filename) { |
1068 | gpx_t *gpx = NULL; |
1069 | |
1070 | LIBXML_TEST_VERSION; |
1071 | |
1072 | gpx_busy_dialog_set(dialog, filename); |
1073 | |
1074 | xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0); |
1075 | if (reader != NULL) { |
1076 | gpx = process_root(reader, dialog, filename, NULL); |
1077 | xmlFreeTextReader(reader); |
1078 | } else { |
1079 | fprintf(stderr, "Unable to open %s\n", filename); |
1080 | } |
1081 | |
1082 | /* check of there's a waypoints file (*-wpts.gpx) for this */ |
1083 | if(strrchr(filename, '.')) { |
1084 | char *dot = strrchr(filename, '.'); |
1085 | char wpts_name[128]; |
1086 | *dot = 0; |
1087 | snprintf(wpts_name, sizeof(wpts_name), "%s-wpts.gpx", filename); |
1088 | *dot = '.'; |
1089 | if(g_file_test(wpts_name, G_FILE_TEST_EXISTS)) { |
1090 | xmlTextReaderPtr reader = xmlReaderForFile(wpts_name, NULL, 0); |
1091 | if (reader != NULL) { |
1092 | gpx = process_root(reader, dialog, wpts_name, gpx); |
1093 | xmlFreeTextReader(reader); |
1094 | } else { |
1095 | fprintf(stderr, "Unable to open %s\n", filename); |
1096 | } |
1097 | } |
1098 | } |
1099 | |
1100 | return gpx; |
1101 | } |
1102 | |
1103 | static gpx_t *decompress_file(unzFile file, gpx_dialog_t *dialog, |
1104 | char *name, char *filename, |
1105 | gpx_t *gpx_in) { |
1106 | unz_file_info info; |
1107 | gpx_t *gpx = NULL; |
1108 | |
1109 | gpx_busy_dialog_set(dialog, name); |
1110 | |
1111 | if((unzLocateFile(file, name, FALSE) != Z_OK) || |
1112 | (unzGetCurrentFileInfo(file, &info, NULL,0, NULL,0, NULL,0) != Z_OK) || |
1113 | (unzOpenCurrentFile(file) != UNZ_OK)) { |
1114 | |
1115 | /* do not complain if we are processing a waypoints file */ |
1116 | if(!gpx_in) |
1117 | errorf("Unable to locate/get info/open\n%s\ninside\n%s", |
1118 | name, filename); |
1119 | else |
1120 | printf("Unable to locate/get info/open %s inside %s\n", |
1121 | name, filename); |
1122 | |
1123 | return gpx_in; |
1124 | } |
1125 | |
1126 | printf("file size is %ld\n", info.uncompressed_size); |
1127 | |
1128 | char *buffer = malloc(info.uncompressed_size); |
1129 | if(!buffer) { |
1130 | errorf("Out of memory while uncompressing file"); |
1131 | unzCloseCurrentFile(file); |
1132 | return gpx_in; |
1133 | } |
1134 | |
1135 | if(unzReadCurrentFile(file, buffer, info.uncompressed_size) < 0) { |
1136 | errorf("Read error on compressed file"); |
1137 | free(buffer); |
1138 | unzCloseCurrentFile(file); |
1139 | return gpx_in; |
1140 | } |
1141 | |
1142 | /* fire up libxml */ |
1143 | LIBXML_TEST_VERSION; |
1144 | |
1145 | xmlTextReaderPtr reader = |
1146 | xmlReaderForMemory(buffer, info.uncompressed_size, |
1147 | NULL, NULL, 0); |
1148 | if (reader != NULL) { |
1149 | gpx = process_root(reader, dialog, filename, gpx_in); |
1150 | xmlFreeTextReader(reader); |
1151 | } else { |
1152 | fprintf(stderr, "Unable to open %s\n", filename); |
1153 | } |
1154 | |
1155 | free(buffer); |
1156 | unzCloseCurrentFile(file); |
1157 | return gpx; |
1158 | } |
1159 | |
1160 | static gpx_t *decompress_zip(gpx_dialog_t *dialog, char *filename) { |
1161 | char *gpx_name, *fbase; |
1162 | gpx_t *gpx = NULL; |
1163 | |
1164 | /* extract base name and allocate space for file names */ |
1165 | fbase = strrchr(filename, '/'); |
1166 | if(!fbase) fbase = filename; |
1167 | else fbase++; /* skip '/' */ |
1168 | gpx_name = malloc(strlen(fbase)+strlen("-wpts")+1); |
1169 | |
1170 | unzFile file = unzOpen(filename); |
1171 | if(!file) { |
1172 | errorf("Error opening file %s for unzip", filename); |
1173 | free(gpx_name); |
1174 | return NULL; |
1175 | } |
1176 | |
1177 | printf("ZIP file successfully opened\n"); |
1178 | |
1179 | /* try to open gpx file inside */ |
1180 | strcpy(gpx_name, fbase); |
1181 | strcpy(gpx_name+strlen(gpx_name)-4, ".gpx"); |
1182 | printf("gpx file name is %s\n", gpx_name); |
1183 | |
1184 | gpx = decompress_file(file, dialog, gpx_name, filename, NULL); |
1185 | |
1186 | /* try to open -wpts.gpx file inside */ |
1187 | strcpy(gpx_name, fbase); |
1188 | strcpy(gpx_name+strlen(gpx_name)-4, "-wpts.gpx"); |
1189 | printf("gpx wpts file name is %s\n", gpx_name); |
1190 | |
1191 | gpx = decompress_file(file, dialog, gpx_name, filename, gpx); |
1192 | |
1193 | unzClose(file); |
1194 | free(gpx_name); |
1195 | return gpx; |
1196 | } |
1197 | |
1198 | gpx_t *gpx_parse(gpx_dialog_t *dialog, char *filename) { |
1199 | gpx_t *gpx = NULL; |
1200 | |
1201 | /* show busy dialog */ |
1202 | printf("load file %s\n", filename); |
1203 | |
1204 | if((strlen(filename) > 4) && |
1205 | !strcasecmp(filename+strlen(filename)-4, ".zip")) { |
1206 | printf("trying to load a zip file!\n"); |
1207 | |
1208 | gpx = decompress_zip(dialog, filename); |
1209 | } else |
1210 | gpx = gpx_parse_file(dialog, filename); |
1211 | |
1212 | return gpx; |
1213 | } |
1214 | |
1215 | /* scan entire directory */ |
1216 | gpx_t *gpx_parse_dir(gpx_dialog_t *dialog, char *dirname) { |
1217 | GnomeVFSResult result; |
1218 | GnomeVFSDirectoryHandle *handle; |
1219 | GnomeVFSFileInfo *finfo = gnome_vfs_file_info_new();; |
1220 | |
1221 | gpx_t *gpx = NULL; |
1222 | |
1223 | /* show busy dialog */ |
1224 | printf("load dir %s\n", dirname); |
1225 | gpx_busy_dialog_set(dialog, dirname); |
1226 | |
1227 | LIBXML_TEST_VERSION; |
1228 | |
1229 | result = gnome_vfs_directory_open(&handle, dirname, |
1230 | GNOME_VFS_FILE_INFO_DEFAULT); |
1231 | |
1232 | if(result != GNOME_VFS_OK) { |
1233 | errorf("Unable to open directory \"%s\":\n%s", |
1234 | dirname, gnome_vfs_result_to_string(result)); |
1235 | return NULL; |
1236 | } |
1237 | |
1238 | while(GNOME_VFS_OK == gnome_vfs_directory_read_next(handle, finfo)) { |
1239 | if(finfo->type == GNOME_VFS_FILE_TYPE_REGULAR) { |
1240 | char *ext = finfo->name+strlen(finfo->name)-4; |
1241 | |
1242 | /* check if file ends with .gpx or .loc */ |
1243 | if((strcasecmp(ext, ".gpx") == 0) || (strcasecmp(ext, ".loc") == 0)) { |
1244 | char *filename = malloc(strlen(dirname)+strlen(finfo->name)+2); |
1245 | |
1246 | strcpy(filename, dirname); |
1247 | if(strlastchr(filename) != '/') |
1248 | strcat(filename, "/"); |
1249 | strcat(filename, finfo->name); |
1250 | |
1251 | xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0); |
1252 | if (reader != NULL) { |
1253 | gpx = process_root(reader, dialog, filename, gpx); |
1254 | xmlFreeTextReader(reader); |
1255 | } else { |
1256 | fprintf(stderr, "Unable to open %s\n", filename); |
1257 | } |
1258 | |
1259 | free(filename); |
1260 | } |
1261 | } |
1262 | } |
1263 | |
1264 | if(gpx) { |
1265 | /* replace file name with directory name */ |
1266 | free(gpx->filename); |
1267 | gpx->filename = strdup(dirname); |
1268 | |
1269 | /* replace gpx name with directory name */ |
1270 | free(gpx->name); |
1271 | |
1272 | /* retrieve pure dirname if possible */ |
1273 | char *n = strrchr(dirname, '/'); |
1274 | if(!n) n = dirname; |
1275 | else n++; |
1276 | |
1277 | // gpx->name = malloc(strlen("<DIR> ")+strlen(n)+1); |
1278 | // strcpy(gpx->name, "<DIR> "); |
1279 | // strcat(gpx->name, n); |
1280 | gpx->name = strdup(n); |
1281 | } |
1282 | |
1283 | gnome_vfs_file_info_unref(finfo); |
1284 | gnome_vfs_directory_close(handle); |
1285 | |
1286 | return gpx; |
1287 | } |
1288 | |
1289 | /* return number of caches in given gpx file */ |
1290 | int gpx_total_caches(gpx_t *gpx) { |
1291 | cache_t *cache = gpx->cache; |
1292 | int num = 0; |
1293 | |
1294 | while(cache) { |
1295 | num++; |
1296 | cache = cache->next; |
1297 | } |
1298 | |
1299 | return num; |
1300 | } |
1301 | |
1302 | int gpx_number_of_waypoints(wpt_t *wpt) { |
1303 | int num = 0; |
1304 | |
1305 | while(wpt) { |
1306 | num++; |
1307 | wpt = wpt->next; |
1308 | } |
1309 | |
1310 | return num; |
1311 | } |
1312 | |
1313 | int gpx_number_of_logs(log_t *log) { |
1314 | int num = 0; |
1315 | |
1316 | while(log) { |
1317 | num++; |
1318 | log = log->next; |
1319 | } |
1320 | |
1321 | return num; |
1322 | } |
1323 | |
1324 | int gpx_number_of_tbs(tb_t *tb) { |
1325 | int num = 0; |
1326 | |
1327 | while(tb) { |
1328 | num++; |
1329 | tb = tb->next; |
1330 | } |
1331 | |
1332 | return num; |
1333 | } |
1334 | |
1335 | /* http://mathforum.org/library/drmath/view/55417.html */ |
1336 | float gpx_pos_get_bearing(pos_t p1, pos_t p2) { |
1337 | /* convert to radians */ |
1338 | p1.lat *= (M_PI/180.0); p1.lon *= (M_PI/180.0); |
1339 | p2.lat *= (M_PI/180.0); p2.lon *= (M_PI/180.0); |
1340 | |
1341 | return fmodf(360.0 + (180.0/M_PI) * |
1342 | (atan2( sin(p2.lon - p1.lon) * cos(p2.lat), |
1343 | cos(p1.lat) * sin(p2.lat) - |
1344 | sin(p1.lat) * cos(p2.lat) * cos(p2.lon - p1.lon))), |
1345 | 360.0); |
1346 | } |
1347 | |
1348 | /* http://mathforum.org/library/drmath/view/51722.html */ |
1349 | float gpx_pos_get_distance(pos_t p1, pos_t p2, int miles) { |
1350 | /* convert to radians */ |
1351 | p1.lat *= (M_PI/180.0); p1.lon *= (M_PI/180.0); |
1352 | p2.lat *= (M_PI/180.0); p2.lon *= (M_PI/180.0); |
1353 | |
1354 | float aob = acos(cos(p1.lat) * cos(p2.lat) * cos(p2.lon - p1.lon) + |
1355 | sin(p1.lat) * sin(p2.lat)); |
1356 | |
1357 | if(miles) |
1358 | return(aob * 3959.0); /* great circle radius in miles */ |
1359 | |
1360 | return(aob * 6371.0); /* great circle radius in kilometers */ |
1361 | } |
1362 | |
1363 | void gpx_pos_get_distance_str(char *str, int len, |
1364 | pos_t p1, pos_t p2, int mil) { |
1365 | if(isnan(p1.lat) || isnan(p1.lon)) { |
1366 | snprintf(str, len, "---"); |
1367 | return; |
1368 | } |
1369 | |
1370 | float dist = gpx_pos_get_distance(p1, p2, mil); |
1371 | distance_str(str, len, dist, mil); |
1372 | } |
1373 | |
1374 | void gpx_sort(gpx_t *gpx, int by, pos_t *refpos) { |
1375 | cache_t **new; |
1376 | cache_t *cur = gpx->cache; |
1377 | int total = gpx_total_caches(gpx); |
1378 | float *dist_cache = malloc(total * sizeof(float)); |
1379 | |
1380 | gpx->cache = NULL; /* detach old chain */ |
1381 | while(cur) { |
1382 | float cur_dist = -1; |
1383 | int cur_cnt = 0; |
1384 | |
1385 | if(!isnan(cur->pos.lat) && !isnan(cur->pos.lon)) |
1386 | cur_dist = gpx_pos_get_distance(*refpos, gpx_cache_pos(cur), 0); |
1387 | |
1388 | new = &(gpx->cache); |
1389 | |
1390 | /* search for currect insertion point */ |
1391 | while(*new && (dist_cache[cur_cnt] < cur_dist)) { |
1392 | new = &((*new)->next); |
1393 | cur_cnt++; |
1394 | } |
1395 | |
1396 | /* save distance for further comparisons */ |
1397 | memmove(dist_cache+cur_cnt+1, dist_cache+cur_cnt, |
1398 | sizeof(float)*(total-cur_cnt-1)); |
1399 | dist_cache[cur_cnt++] = cur_dist; |
1400 | |
1401 | cache_t *next = cur->next; |
1402 | |
1403 | /* insert into "new" chain */ |
1404 | cur->next = *new; |
1405 | *new = cur; |
1406 | |
1407 | cur = next; |
1408 | } |
1409 | |
1410 | free(dist_cache); |
1411 | } |
1412 | |
1413 | gpx_t *gpx_cache2gpx(gpx_t *gpx, cache_t *search_cache) { |
1414 | while(gpx) { |
1415 | cache_t *cache = gpx->cache; |
1416 | while(cache) { |
1417 | if(cache == search_cache) |
1418 | return gpx; |
1419 | |
1420 | cache = cache->next; |
1421 | } |
1422 | gpx = gpx->next; |
1423 | } |
1424 | |
1425 | return NULL; |
1426 | } |
1427 | |
1428 | /* since the actual cache position may be overridden, we */ |
1429 | /* always access the position through this function */ |
1430 | pos_t gpx_cache_pos(cache_t *cache) { |
1431 | if(cache->notes && cache->notes->override) |
1432 | return cache->notes->pos; |
1433 | |
1434 | return cache->pos; |
1435 | } |