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