Parent Directory | Revision Log
Fixed fremantle submenues
1 | harbaum | 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 | harbaum | 7 | gtk_window_set_title(GTK_WINDOW(dialog->dialog), _("Loading")); |
156 | harbaum | 1 | gtk_window_set_default_size(GTK_WINDOW(dialog->dialog), 300, 10); |
157 | |||
158 | harbaum | 30 | gtk_window_set_modal(GTK_WINDOW(dialog->dialog), TRUE); |
159 | harbaum | 1 | 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 | int depth = xmlTextReaderDepth(reader); | ||
364 | int ret = xmlTextReaderRead(reader); | ||
365 | while((ret == 1) && | ||
366 | ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) || | ||
367 | (xmlTextReaderDepth(reader) != depth))) { | ||
368 | |||
369 | /* found a text fragment */ | ||
370 | if((xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) || | ||
371 | (xmlTextReaderNodeType(reader) == XML_READER_TYPE_CDATA)) { | ||
372 | char *frag = (char*)xmlTextReaderConstValue(reader); | ||
373 | |||
374 | if(!text) text = strdup(frag); | ||
375 | else { | ||
376 | char *old = text; | ||
377 | text = malloc(strlen(old) + strlen(frag) + 1); | ||
378 | strcpy(text, old); | ||
379 | strcat(text, frag); | ||
380 | free(old); | ||
381 | } | ||
382 | } | ||
383 | ret = xmlTextReaderRead(reader); | ||
384 | } | ||
385 | |||
386 | return text; | ||
387 | } | ||
388 | |||
389 | static int xml_str_search(xmlTextReaderPtr reader, | ||
390 | const char *pstr[], char *type, int def) { | ||
391 | char *text = process_text(reader); | ||
392 | int result = def; | ||
393 | if(text) { | ||
394 | result = str_search(pstr, text, type); | ||
395 | free(text); | ||
396 | } | ||
397 | return result; | ||
398 | } | ||
399 | |||
400 | static float xml_float(xmlTextReaderPtr reader, float def) { | ||
401 | char *text = process_text(reader); | ||
402 | float result = def; | ||
403 | if(text) { | ||
404 | result = g_ascii_strtod(text, NULL); | ||
405 | free(text); | ||
406 | } | ||
407 | return result; | ||
408 | } | ||
409 | |||
410 | static void xml_get_date(xmlTextReaderPtr reader, int *year, int *month, int *day) { | ||
411 | char *str = process_text(reader); | ||
412 | if(str) { | ||
413 | sscanf(str, "%d-%d-%d", year, month, day); | ||
414 | free(str); | ||
415 | } | ||
416 | } | ||
417 | |||
418 | static log_t *process_gpx_wpt_gc_logs_log(xmlTextReaderPtr reader) { | ||
419 | |||
420 | if(xmlTextReaderIsEmptyElement(reader)) | ||
421 | return NULL; | ||
422 | |||
423 | /* create a new log entry */ | ||
424 | log_t *log = malloc(sizeof(log_t)); | ||
425 | memset(log, 0, sizeof(log_t)); | ||
426 | |||
427 | /* process all sub-nodes */ | ||
428 | int depth = xmlTextReaderDepth(reader); | ||
429 | int ret = xmlTextReaderRead(reader); | ||
430 | while(ret == 1) { | ||
431 | |||
432 | switch(xmlTextReaderNodeType(reader)) { | ||
433 | case XML_READER_TYPE_ELEMENT: | ||
434 | g_assert(xmlTextReaderDepth(reader) == depth+1); | ||
435 | char *name = (char*)xmlTextReaderConstName(reader); | ||
436 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; | ||
437 | if(name) { | ||
438 | if((strcasecmp(name, "date") == 0) || | ||
439 | (strcasecmp(name, "time") == 0)) { | ||
440 | xml_get_date(reader, &log->year, &log->month, &log->day); | ||
441 | } else if(strcasecmp(name, "type") == 0) { | ||
442 | log->type = xml_str_search(reader, log_type_str, "log", 0); | ||
443 | } else if((strcasecmp(name, "finder") == 0) || | ||
444 | (strcasecmp(name, "geocacher") == 0)) { | ||
445 | if(!log->finder) log->finder = process_text(reader); | ||
446 | } else if(strcasecmp(name, "text") == 0) { | ||
447 | if(!log->text) log->text = process_text(reader); | ||
448 | } else | ||
449 | skip_element(reader); | ||
450 | } else | ||
451 | skip_element(reader); | ||
452 | break; | ||
453 | |||
454 | case XML_READER_TYPE_END_ELEMENT: | ||
455 | /* end element must be for the current element */ | ||
456 | g_assert(xmlTextReaderDepth(reader) == depth); | ||
457 | return log; | ||
458 | break; | ||
459 | |||
460 | default: | ||
461 | break; | ||
462 | } | ||
463 | ret = xmlTextReaderRead(reader); | ||
464 | } | ||
465 | |||
466 | g_assert(0); | ||
467 | return log; | ||
468 | } | ||
469 | |||
470 | static log_t *process_gpx_wpt_gc_logs(xmlTextReaderPtr reader) { | ||
471 | log_t *log_chain = NULL; | ||
472 | |||
473 | if(xmlTextReaderIsEmptyElement(reader)) | ||
474 | return NULL; | ||
475 | |||
476 | /* process all sub-nodes */ | ||
477 | int depth = xmlTextReaderDepth(reader); | ||
478 | int ret = xmlTextReaderRead(reader); | ||
479 | while(ret == 1) { | ||
480 | |||
481 | switch(xmlTextReaderNodeType(reader)) { | ||
482 | case XML_READER_TYPE_ELEMENT: | ||
483 | g_assert(xmlTextReaderDepth(reader) == depth+1); | ||
484 | char *name = (char*)xmlTextReaderConstName(reader); | ||
485 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; | ||
486 | if(name) { | ||
487 | if(strcasecmp(name, "log") == 0) { | ||
488 | log_t *log = process_gpx_wpt_gc_logs_log(reader); | ||
489 | if(log) { | ||
490 | /* add log to chain */ | ||
491 | log_t **cur = &log_chain; | ||
492 | while(*cur && log_is_older(log, *cur)) | ||
493 | cur = &((*cur)->next); | ||
494 | |||
495 | log->next = *cur; | ||
496 | *cur = log; | ||
497 | } | ||
498 | } else | ||
499 | skip_element(reader); | ||
500 | } else | ||
501 | skip_element(reader); | ||
502 | break; | ||
503 | |||
504 | case XML_READER_TYPE_END_ELEMENT: | ||
505 | /* end element must be for the current element */ | ||
506 | g_assert(xmlTextReaderDepth(reader) == depth); | ||
507 | return log_chain; | ||
508 | break; | ||
509 | |||
510 | default: | ||
511 | break; | ||
512 | } | ||
513 | ret = xmlTextReaderRead(reader); | ||
514 | } | ||
515 | |||
516 | g_assert(0); | ||
517 | return log_chain; | ||
518 | } | ||
519 | |||
520 | static tb_t *process_gpx_wpt_gc_tbs_travelbug(xmlTextReaderPtr reader) { | ||
521 | |||
522 | if(xmlTextReaderIsEmptyElement(reader)) | ||
523 | return NULL; | ||
524 | |||
525 | /* create a new tb entry */ | ||
526 | tb_t *tb = malloc(sizeof(tb_t)); | ||
527 | memset(tb, 0, sizeof(tb_t)); | ||
528 | |||
529 | char *prop; | ||
530 | if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "ref"))) | ||
531 | tb->ref = strdup(prop); | ||
532 | else | ||
533 | tb->ref = strdup("<NONE>"); | ||
534 | |||
535 | /* process all sub-nodes */ | ||
536 | int depth = xmlTextReaderDepth(reader); | ||
537 | int ret = xmlTextReaderRead(reader); | ||
538 | while(ret == 1) { | ||
539 | |||
540 | switch(xmlTextReaderNodeType(reader)) { | ||
541 | case XML_READER_TYPE_ELEMENT: | ||
542 | g_assert(xmlTextReaderDepth(reader) == depth+1); | ||
543 | char *name = (char*)xmlTextReaderConstName(reader); | ||
544 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; | ||
545 | if(name) { | ||
546 | if(strcasecmp(name, "name") == 0) { | ||
547 | if(!tb->name) tb->name = process_text(reader); | ||
548 | } else | ||
549 | skip_element(reader); | ||
550 | } else | ||
551 | skip_element(reader); | ||
552 | break; | ||
553 | |||
554 | case XML_READER_TYPE_END_ELEMENT: | ||
555 | /* end element must be for the current element */ | ||
556 | g_assert(xmlTextReaderDepth(reader) == depth); | ||
557 | return tb; | ||
558 | break; | ||
559 | |||
560 | default: | ||
561 | break; | ||
562 | } | ||
563 | ret = xmlTextReaderRead(reader); | ||
564 | } | ||
565 | |||
566 | g_assert(0); | ||
567 | return tb; | ||
568 | } | ||
569 | |||
570 | static tb_t *process_gpx_wpt_gc_tbs(xmlTextReaderPtr reader) { | ||
571 | tb_t *tb = NULL, **tbP = &tb; | ||
572 | |||
573 | if(xmlTextReaderIsEmptyElement(reader)) | ||
574 | return tb; | ||
575 | |||
576 | /* process all sub-nodes */ | ||
577 | int depth = xmlTextReaderDepth(reader); | ||
578 | int ret = xmlTextReaderRead(reader); | ||
579 | while(ret == 1) { | ||
580 | |||
581 | switch(xmlTextReaderNodeType(reader)) { | ||
582 | case XML_READER_TYPE_ELEMENT: | ||
583 | g_assert(xmlTextReaderDepth(reader) == depth+1); | ||
584 | char *name = (char*)xmlTextReaderConstName(reader); | ||
585 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; | ||
586 | if(name) { | ||
587 | if(strcasecmp(name, "travelbug") == 0) { | ||
588 | *tbP = process_gpx_wpt_gc_tbs_travelbug(reader); | ||
589 | if(*tbP) tbP = &(*tbP)->next; | ||
590 | } else | ||
591 | skip_element(reader); | ||
592 | } else | ||
593 | skip_element(reader); | ||
594 | break; | ||
595 | |||
596 | case XML_READER_TYPE_END_ELEMENT: | ||
597 | /* end element must be for the current element */ | ||
598 | g_assert(xmlTextReaderDepth(reader) == depth); | ||
599 | return tb; | ||
600 | break; | ||
601 | |||
602 | default: | ||
603 | break; | ||
604 | } | ||
605 | ret = xmlTextReaderRead(reader); | ||
606 | } | ||
607 | |||
608 | g_assert(0); | ||
609 | return tb; | ||
610 | } | ||
611 | |||
612 | static void process_gpx_wpt_gc(xmlTextReaderPtr reader, cache_t *cache) { | ||
613 | cache->available = xml_prop_is(reader, "available", "true", TRUE); | ||
614 | cache->archived = xml_prop_is(reader, "archived", "true", FALSE); | ||
615 | |||
616 | if(xmlTextReaderIsEmptyElement(reader)) | ||
617 | return; | ||
618 | |||
619 | /* process all sub-nodes */ | ||
620 | int depth = xmlTextReaderDepth(reader); | ||
621 | int ret = xmlTextReaderRead(reader); | ||
622 | while(ret == 1) { | ||
623 | |||
624 | switch(xmlTextReaderNodeType(reader)) { | ||
625 | case XML_READER_TYPE_ELEMENT: | ||
626 | g_assert(xmlTextReaderDepth(reader) == depth+1); | ||
627 | char *name = (char*)xmlTextReaderConstName(reader); | ||
628 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; | ||
629 | if(name) { | ||
630 | if(strcasecmp(name, "name") == 0) { | ||
631 | if(!cache->name) cache->name = process_text(reader); | ||
632 | } else if(strcasecmp(name, "owner") == 0) { | ||
633 | if(!cache->owner) cache->owner = process_text(reader); | ||
634 | } else if(strcasecmp(name, "type") == 0) { | ||
635 | cache->type = xml_str_search(reader, cache_type_str, | ||
636 | "cache type", CACHE_TYPE_UNKNOWN); | ||
637 | } else if(strcasecmp(name, "container") == 0) { | ||
638 | cache->container = xml_str_search(reader, cache_container_str, | ||
639 | "container", CACHE_CONT_UNKNOWN); | ||
640 | } else if((strcasecmp(name, "short_description") == 0) || | ||
641 | (strcasecmp(name, "summary") == 0)) { | ||
642 | if(!cache->short_description) { | ||
643 | cache->short_description = process_text(reader); | ||
644 | cache->short_is_html = xml_prop_is(reader, "html", "true", FALSE); | ||
645 | } | ||
646 | } else if((strcasecmp(name, "long_description") == 0) || | ||
647 | (strcasecmp(name, "description") == 0)) { | ||
648 | if(!cache->long_description) { | ||
649 | cache->long_description = process_text(reader); | ||
650 | cache->long_is_html = xml_prop_is(reader, "html", "true", FALSE); | ||
651 | } | ||
652 | } else if((strcasecmp(name, "encoded_hints") == 0) || | ||
653 | (strcasecmp(name, "hints") == 0)) { | ||
654 | if(!cache->hint) { | ||
655 | cache->hint = process_text(reader); | ||
656 | /* often hints aren't more than just a bunch of blanks ... */ | ||
657 | if(cache->hint && all_is_white(cache->hint)) { | ||
658 | free(cache->hint); | ||
659 | cache->hint = NULL; | ||
660 | } else | ||
661 | cache->hint_is_html = xml_prop_is(reader, "html", "true", FALSE); | ||
662 | } | ||
663 | } else if(strcasecmp(name, "difficulty") == 0) { | ||
664 | cache->difficulty = xml_float(reader, 0.0); | ||
665 | } else if(strcasecmp(name, "terrain") == 0) { | ||
666 | cache->terrain = xml_float(reader, 0.0); | ||
667 | } else if(strcasecmp(name, "logs") == 0) { | ||
668 | if(!cache->log) cache->log = process_gpx_wpt_gc_logs(reader); | ||
669 | } else if(strcasecmp(name, "travelbugs") == 0) { | ||
670 | if(!cache->tb) cache->tb = process_gpx_wpt_gc_tbs(reader); | ||
671 | } else { | ||
672 | // printf("unhandled item found: gpx/wpt/cache/%s\n", name); | ||
673 | skip_element(reader); | ||
674 | } | ||
675 | } else | ||
676 | skip_element(reader); | ||
677 | break; | ||
678 | |||
679 | case XML_READER_TYPE_END_ELEMENT: | ||
680 | /* end element must be for the current element */ | ||
681 | g_assert(xmlTextReaderDepth(reader) == depth); | ||
682 | return; | ||
683 | break; | ||
684 | |||
685 | default: | ||
686 | break; | ||
687 | } | ||
688 | ret = xmlTextReaderRead(reader); | ||
689 | } | ||
690 | } | ||
691 | |||
692 | /* parse waypoint entry */ | ||
693 | static cache_t *process_gpx_wpt(xmlTextReaderPtr reader, gpx_dialog_t *dialog, | ||
694 | gpx_t *gpx) { | ||
695 | char *cmt = NULL, *desc = NULL; | ||
696 | char *sym = NULL; | ||
697 | |||
698 | gpx_busy_dialog_progress(dialog); | ||
699 | |||
700 | if(xmlTextReaderIsEmptyElement(reader)) | ||
701 | return NULL; | ||
702 | |||
703 | cache_t *cache = malloc(sizeof(cache_t)); | ||
704 | memset(cache, 0, sizeof(cache_t)); | ||
705 | |||
706 | /* set some defaults */ | ||
707 | cache->type = CACHE_TYPE_UNKNOWN; | ||
708 | cache->container = CACHE_CONT_UNKNOWN; | ||
709 | cache->available = TRUE; | ||
710 | |||
711 | /* parse attributes */ | ||
712 | cache->pos.lat = xml_get_prop_float(reader, "lat"); | ||
713 | cache->pos.lon = xml_get_prop_float(reader, "lon"); | ||
714 | |||
715 | /* process all sub-nodes */ | ||
716 | int depth = xmlTextReaderDepth(reader); | ||
717 | int ret = xmlTextReaderRead(reader); | ||
718 | while(ret == 1) { | ||
719 | |||
720 | switch(xmlTextReaderNodeType(reader)) { | ||
721 | case XML_READER_TYPE_ELEMENT: | ||
722 | g_assert(xmlTextReaderDepth(reader) == depth+1); | ||
723 | char *name = (char*)xmlTextReaderConstName(reader); | ||
724 | |||
725 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; | ||
726 | |||
727 | if(name) { | ||
728 | if(strcasecmp(name, "name") == 0) { | ||
729 | if(!cache->id) cache->id = process_text(reader); | ||
730 | } else if(strcasecmp(name, "url") == 0) { | ||
731 | if(!cache->url) cache->url = process_text(reader); | ||
732 | } else if((strcasecmp(name, "cache") == 0) || | ||
733 | (strcasecmp(name, "geocache") == 0)) { | ||
734 | process_gpx_wpt_gc(reader, cache); | ||
735 | |||
736 | /* the following are used if the current entry is a waypoint */ | ||
737 | } else if(strcasecmp(name, "cmt") == 0) { | ||
738 | if(!cmt) cmt = process_text(reader); | ||
739 | } else if(strcasecmp(name, "desc") == 0) { | ||
740 | if(!desc) desc = process_text(reader); | ||
741 | } else if(strcasecmp(name, "sym") == 0) { | ||
742 | if(!sym) sym = process_text(reader); | ||
743 | } else { | ||
744 | skip_element(reader); | ||
745 | } | ||
746 | } else | ||
747 | skip_element(reader); | ||
748 | break; | ||
749 | |||
750 | case XML_READER_TYPE_END_ELEMENT: | ||
751 | /* end element must be for the current element */ | ||
752 | g_assert(xmlTextReaderDepth(reader) == depth); | ||
753 | |||
754 | /* ------------ cleanup -------------- */ | ||
755 | |||
756 | /* special handling for opencaching.de caches */ | ||
757 | if(cache->id && strncasecmp(cache->id, "OC", 2) == 0) { | ||
758 | /* the html attributes are either missing or wrong on OC ... */ | ||
759 | cache->long_is_html = TRUE; | ||
760 | cache->hint_is_html = TRUE; | ||
761 | cache->logs_are_html = TRUE; | ||
762 | } | ||
763 | |||
764 | /* neither geocaching.com GC* nor opencaching.com OC* nor */ | ||
765 | /* geocaching australia GA* waypoint */ | ||
766 | if(cache->id && | ||
767 | harbaum | 14 | (strncasecmp(cache->id, "__", 2) != 0) && |
768 | harbaum | 1 | (strncasecmp(cache->id, "GC", 2) != 0) && |
769 | (strncasecmp(cache->id, "OC", 2) != 0) && | ||
770 | (strncasecmp(cache->id, "GA", 2) != 0)) { | ||
771 | cache_t *parent = gpx->cache; | ||
772 | |||
773 | /* check if the gpx file contains a cache with matching name */ | ||
774 | while(parent && strcasecmp(parent->id+2, cache->id+2)) | ||
775 | parent = parent->next; | ||
776 | |||
777 | if(parent && parent != cache) { | ||
778 | wpt_t **wpt = &parent->wpt; | ||
779 | |||
780 | /* search end of list */ | ||
781 | while(*wpt && (strcmp((*wpt)->id, cache->id)<0)) | ||
782 | wpt = &(*wpt)->next; | ||
783 | |||
784 | *wpt = malloc(sizeof(wpt_t)); | ||
785 | memset(*wpt, 0, sizeof(wpt_t)); | ||
786 | |||
787 | /* transfer name to waypoint entry */ | ||
788 | (*wpt)->id = cache->id; | ||
789 | cache->id = NULL; | ||
790 | |||
791 | (*wpt)->pos.lat = cache->pos.lat; | ||
792 | (*wpt)->pos.lon = cache->pos.lon; | ||
793 | |||
794 | (*wpt)->cmt = cmt; cmt = NULL; | ||
795 | (*wpt)->desc = desc; desc = NULL; | ||
796 | (*wpt)->sym = str_search(wpt_sym_str, sym, "wpt sym"); | ||
797 | |||
798 | /* and just free the current cache entry as we now have used */ | ||
799 | /* the data for a caches waypoint */ | ||
800 | gpx_free_cache(cache); | ||
801 | cache = NULL; | ||
802 | } else { | ||
803 | /* if it doesn't have a name etc, it's probably not a real */ | ||
804 | /* cache, so drop it */ | ||
805 | if(!cache->name || !cache->id) { | ||
806 | printf("Orphaned waypoint: %s\n", cache->id); | ||
807 | gpx_free_cache(cache); | ||
808 | cache = NULL; | ||
809 | } | ||
810 | } | ||
811 | } else { | ||
812 | if(!cache->id) | ||
813 | cache->id = g_strdup_printf("NO ID"); | ||
814 | |||
815 | /* this is known to be a geocache due to its waypoint name */ | ||
816 | /* (gc*, oc*, ga*) and is thus forces to be an entry */ | ||
817 | if(!cache->name) | ||
818 | cache->name = g_strdup_printf("Unnamed(%s)", cache->id); | ||
819 | } | ||
820 | |||
821 | if(desc) free(desc); | ||
822 | if(cmt) free(cmt); | ||
823 | if(sym) free(sym); | ||
824 | |||
825 | return cache; | ||
826 | break; | ||
827 | |||
828 | default: | ||
829 | break; | ||
830 | } | ||
831 | ret = xmlTextReaderRead(reader); | ||
832 | } | ||
833 | |||
834 | g_assert(0); | ||
835 | return cache; | ||
836 | } | ||
837 | |||
838 | static void process_gpx(xmlTextReaderPtr reader, gpx_dialog_t *dialog, | ||
839 | gpx_t *gpx) { | ||
840 | |||
841 | /* no attributes of interest */ | ||
842 | |||
843 | /* the following might be optimized for speed reasons! */ | ||
844 | |||
845 | /* find end of cache chain */ | ||
846 | cache_t **cache = &gpx->cache; | ||
847 | while(*cache) cache = &(*cache)->next; | ||
848 | |||
849 | const xmlChar *name = xmlTextReaderConstName(reader); | ||
850 | g_assert(name); | ||
851 | |||
852 | /* read next node */ | ||
853 | int ret = xmlTextReaderRead(reader); | ||
854 | while(ret == 1) { | ||
855 | |||
856 | switch(xmlTextReaderNodeType(reader)) { | ||
857 | case XML_READER_TYPE_ELEMENT: | ||
858 | |||
859 | g_assert(xmlTextReaderDepth(reader) == 1); | ||
860 | char *name = (char*)xmlTextReaderConstName(reader); | ||
861 | if(name && !gpx->name && strcasecmp(name, "name") == 0) { | ||
862 | gpx->name = process_text(reader); | ||
863 | } else if(name && !gpx->desc && strcasecmp(name, "desc") == 0) { | ||
864 | gpx->desc = process_text(reader); | ||
865 | } else if(name && ((strcasecmp(name, "time") == 0) || | ||
866 | (strcasecmp(name, "date") == 0))) { | ||
867 | xml_get_date(reader, &gpx->year, &gpx->month, &gpx->day); | ||
868 | } else if(name && strcasecmp(name, "wpt") == 0) { | ||
869 | *cache = process_gpx_wpt(reader, dialog, gpx); | ||
870 | if(*cache) cache = &(*cache)->next; | ||
871 | } else { | ||
872 | // printf("something unknown (%s) found\n", name); | ||
873 | skip_element(reader); | ||
874 | } | ||
875 | break; | ||
876 | |||
877 | case XML_READER_TYPE_END_ELEMENT: | ||
878 | /* end element must be for the current element */ | ||
879 | g_assert(xmlTextReaderDepth(reader) == 0); | ||
880 | return; | ||
881 | break; | ||
882 | |||
883 | default: | ||
884 | break; | ||
885 | } | ||
886 | ret = xmlTextReaderRead(reader); | ||
887 | } | ||
888 | |||
889 | g_assert(0); | ||
890 | } | ||
891 | |||
892 | /* parse loc waypoint entry */ | ||
893 | static cache_t *process_loc_waypoint(xmlTextReaderPtr reader, | ||
894 | gpx_dialog_t *dialog) { | ||
895 | |||
896 | gpx_busy_dialog_progress(dialog); | ||
897 | |||
898 | if(xmlTextReaderIsEmptyElement(reader)) | ||
899 | return NULL; | ||
900 | |||
901 | cache_t *cache = malloc(sizeof(cache_t)); | ||
902 | memset(cache, 0, sizeof(cache_t)); | ||
903 | |||
904 | /* set some defaults */ | ||
905 | cache->type = CACHE_TYPE_TRADITIONAL; | ||
906 | cache->container = CACHE_CONT_UNKNOWN; | ||
907 | cache->available = TRUE; | ||
908 | |||
909 | /* process all sub-nodes */ | ||
910 | int depth = xmlTextReaderDepth(reader); | ||
911 | int ret = xmlTextReaderRead(reader); | ||
912 | while(ret == 1) { | ||
913 | |||
914 | switch(xmlTextReaderNodeType(reader)) { | ||
915 | case XML_READER_TYPE_ELEMENT: | ||
916 | g_assert(xmlTextReaderDepth(reader) == depth+1); | ||
917 | char *name = (char*)xmlTextReaderConstName(reader); | ||
918 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; | ||
919 | |||
920 | if(name) { | ||
921 | if(strcasecmp(name, "name") == 0) { | ||
922 | cache->id = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id"); | ||
923 | cache->name = process_text(reader); | ||
924 | } else if(strcasecmp(name, "link") == 0) { | ||
925 | cache->url = process_text(reader); | ||
926 | } else if(strcasecmp(name, "coord") == 0) { | ||
927 | cache->pos.lat = xml_get_prop_float(reader, "lat"); | ||
928 | cache->pos.lon = xml_get_prop_float(reader, "lon"); | ||
929 | skip_element(reader); | ||
930 | } else | ||
931 | skip_element(reader); | ||
932 | } else | ||
933 | skip_element(reader); | ||
934 | break; | ||
935 | |||
936 | case XML_READER_TYPE_END_ELEMENT: | ||
937 | /* end element must be for the current element */ | ||
938 | g_assert(xmlTextReaderDepth(reader) == depth); | ||
939 | return cache; | ||
940 | break; | ||
941 | |||
942 | default: | ||
943 | break; | ||
944 | } | ||
945 | ret = xmlTextReaderRead(reader); | ||
946 | } | ||
947 | |||
948 | g_assert(0); | ||
949 | return NULL; | ||
950 | } | ||
951 | |||
952 | static void process_loc(xmlTextReaderPtr reader, gpx_dialog_t *dialog, | ||
953 | gpx_t *gpx) { | ||
954 | |||
955 | /* find end of cache chain */ | ||
956 | cache_t **cache = &gpx->cache; | ||
957 | while(*cache) cache = &(*cache)->next; | ||
958 | |||
959 | const xmlChar *name = xmlTextReaderConstName(reader); | ||
960 | g_assert(name); | ||
961 | |||
962 | /* read next node */ | ||
963 | int ret = xmlTextReaderRead(reader); | ||
964 | while(ret == 1) { | ||
965 | |||
966 | switch(xmlTextReaderNodeType(reader)) { | ||
967 | case XML_READER_TYPE_ELEMENT: | ||
968 | |||
969 | g_assert(xmlTextReaderDepth(reader) == 1); | ||
970 | char *name = (char*)xmlTextReaderConstName(reader); | ||
971 | |||
972 | if(name && strcasecmp(name, "waypoint") == 0) { | ||
973 | *cache = process_loc_waypoint(reader, dialog); | ||
974 | if(*cache) cache = &(*cache)->next; | ||
975 | } else | ||
976 | skip_element(reader); | ||
977 | break; | ||
978 | |||
979 | case XML_READER_TYPE_END_ELEMENT: | ||
980 | /* end element must be for the current element */ | ||
981 | g_assert(xmlTextReaderDepth(reader) == 0); | ||
982 | return; | ||
983 | break; | ||
984 | |||
985 | default: | ||
986 | break; | ||
987 | } | ||
988 | ret = xmlTextReaderRead(reader); | ||
989 | } | ||
990 | |||
991 | g_assert(0); | ||
992 | return; | ||
993 | } | ||
994 | |||
995 | static gpx_t *process_root(xmlTextReaderPtr reader, gpx_dialog_t *dialog, | ||
996 | char *fname, gpx_t *in) { | ||
997 | |||
998 | /* no gpx entry given, create a new one */ | ||
999 | gpx_t *gpx = NULL; | ||
1000 | if(!in) { | ||
1001 | /* allocate memory to hold gpx file description */ | ||
1002 | gpx = malloc(sizeof(gpx_t)); | ||
1003 | memset(gpx, 0, sizeof(gpx_t)); | ||
1004 | gpx->filename = strdup(fname); | ||
1005 | } else | ||
1006 | gpx = in; | ||
1007 | |||
1008 | int ret = xmlTextReaderRead(reader); | ||
1009 | while(ret == 1) { | ||
1010 | switch(xmlTextReaderNodeType(reader)) { | ||
1011 | case XML_READER_TYPE_ELEMENT: | ||
1012 | g_assert(xmlTextReaderDepth(reader) == 0); | ||
1013 | char *name = (char*)xmlTextReaderConstName(reader); | ||
1014 | if(name && strcasecmp(name, "gpx") == 0) { | ||
1015 | process_gpx(reader, dialog, gpx); | ||
1016 | } else if(name && strcasecmp(name, "loc") == 0) { | ||
1017 | process_loc(reader, dialog, gpx); | ||
1018 | } else { | ||
1019 | printf("something unknown found\n"); | ||
1020 | skip_element(reader); | ||
1021 | } | ||
1022 | break; | ||
1023 | |||
1024 | case XML_READER_TYPE_END_ELEMENT: | ||
1025 | /* end element must be for the current element */ | ||
1026 | g_assert(xmlTextReaderDepth(reader) == 0); | ||
1027 | ret = -1; | ||
1028 | break; | ||
1029 | |||
1030 | default: | ||
1031 | break; | ||
1032 | } | ||
1033 | |||
1034 | if(ret == 1) | ||
1035 | ret = xmlTextReaderRead(reader); | ||
1036 | } | ||
1037 | |||
1038 | /* check if a name has been set and use filename if not */ | ||
1039 | if(!in && !gpx->name) { | ||
1040 | if(!gpx->desc) { | ||
1041 | char *str = strrchr(fname, '/'); | ||
1042 | if(str) gpx->name = strdup(str+1); | ||
1043 | else gpx->name = strdup(fname); | ||
1044 | } else | ||
1045 | gpx->name = strdup(gpx->desc); | ||
1046 | } | ||
1047 | |||
1048 | return gpx; | ||
1049 | } | ||
1050 | |||
1051 | static gpx_t *gpx_parse_file(gpx_dialog_t *dialog, char *filename) { | ||
1052 | gpx_t *gpx = NULL; | ||
1053 | |||
1054 | LIBXML_TEST_VERSION; | ||
1055 | |||
1056 | gpx_busy_dialog_set(dialog, filename); | ||
1057 | |||
1058 | xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0); | ||
1059 | if (reader != NULL) { | ||
1060 | gpx = process_root(reader, dialog, filename, NULL); | ||
1061 | xmlFreeTextReader(reader); | ||
1062 | } else { | ||
1063 | fprintf(stderr, "Unable to open %s\n", filename); | ||
1064 | } | ||
1065 | |||
1066 | /* check of there's a waypoints file (*-wpts.gpx) for this */ | ||
1067 | if(strrchr(filename, '.')) { | ||
1068 | char *dot = strrchr(filename, '.'); | ||
1069 | char wpts_name[128]; | ||
1070 | *dot = 0; | ||
1071 | snprintf(wpts_name, sizeof(wpts_name), "%s-wpts.gpx", filename); | ||
1072 | *dot = '.'; | ||
1073 | if(g_file_test(wpts_name, G_FILE_TEST_EXISTS)) { | ||
1074 | xmlTextReaderPtr reader = xmlReaderForFile(wpts_name, NULL, 0); | ||
1075 | if (reader != NULL) { | ||
1076 | gpx = process_root(reader, dialog, wpts_name, gpx); | ||
1077 | xmlFreeTextReader(reader); | ||
1078 | } else { | ||
1079 | fprintf(stderr, "Unable to open %s\n", filename); | ||
1080 | } | ||
1081 | } | ||
1082 | } | ||
1083 | |||
1084 | return gpx; | ||
1085 | } | ||
1086 | |||
1087 | static gpx_t *decompress_file(unzFile file, gpx_dialog_t *dialog, | ||
1088 | char *name, char *filename, | ||
1089 | gpx_t *gpx_in) { | ||
1090 | unz_file_info info; | ||
1091 | gpx_t *gpx = NULL; | ||
1092 | |||
1093 | gpx_busy_dialog_set(dialog, name); | ||
1094 | |||
1095 | if((unzLocateFile(file, name, FALSE) != Z_OK) || | ||
1096 | (unzGetCurrentFileInfo(file, &info, NULL,0, NULL,0, NULL,0) != Z_OK) || | ||
1097 | (unzOpenCurrentFile(file) != UNZ_OK)) { | ||
1098 | |||
1099 | /* do not complain if we are processing a waypoints file */ | ||
1100 | if(!gpx_in) | ||
1101 | errorf("Unable to locate/get info/open\n%s\ninside\n%s", | ||
1102 | name, filename); | ||
1103 | else | ||
1104 | printf("Unable to locate/get info/open %s inside %s\n", | ||
1105 | name, filename); | ||
1106 | |||
1107 | return gpx_in; | ||
1108 | } | ||
1109 | |||
1110 | printf("file size is %ld\n", info.uncompressed_size); | ||
1111 | |||
1112 | char *buffer = malloc(info.uncompressed_size); | ||
1113 | if(!buffer) { | ||
1114 | errorf("Out of memory while uncompressing file"); | ||
1115 | unzCloseCurrentFile(file); | ||
1116 | return gpx_in; | ||
1117 | } | ||
1118 | |||
1119 | if(unzReadCurrentFile(file, buffer, info.uncompressed_size) < 0) { | ||
1120 | errorf("Read error on compressed file"); | ||
1121 | free(buffer); | ||
1122 | unzCloseCurrentFile(file); | ||
1123 | return gpx_in; | ||
1124 | } | ||
1125 | |||
1126 | /* fire up libxml */ | ||
1127 | LIBXML_TEST_VERSION; | ||
1128 | |||
1129 | xmlTextReaderPtr reader = | ||
1130 | xmlReaderForMemory(buffer, info.uncompressed_size, | ||
1131 | NULL, NULL, 0); | ||
1132 | if (reader != NULL) { | ||
1133 | gpx = process_root(reader, dialog, filename, gpx_in); | ||
1134 | xmlFreeTextReader(reader); | ||
1135 | } else { | ||
1136 | fprintf(stderr, "Unable to open %s\n", filename); | ||
1137 | } | ||
1138 | |||
1139 | free(buffer); | ||
1140 | unzCloseCurrentFile(file); | ||
1141 | return gpx; | ||
1142 | } | ||
1143 | |||
1144 | static gpx_t *decompress_zip(gpx_dialog_t *dialog, char *filename) { | ||
1145 | char *gpx_name, *fbase; | ||
1146 | gpx_t *gpx = NULL; | ||
1147 | |||
1148 | /* extract base name and allocate space for file names */ | ||
1149 | fbase = strrchr(filename, '/'); | ||
1150 | if(!fbase) fbase = filename; | ||
1151 | else fbase++; /* skip '/' */ | ||
1152 | gpx_name = malloc(strlen(fbase)+strlen("-wpts")+1); | ||
1153 | |||
1154 | unzFile file = unzOpen(filename); | ||
1155 | if(!file) { | ||
1156 | errorf("Error opening file %s for unzip", filename); | ||
1157 | free(gpx_name); | ||
1158 | return NULL; | ||
1159 | } | ||
1160 | |||
1161 | printf("ZIP file successfully opened\n"); | ||
1162 | |||
1163 | /* try to open gpx file inside */ | ||
1164 | strcpy(gpx_name, fbase); | ||
1165 | strcpy(gpx_name+strlen(gpx_name)-4, ".gpx"); | ||
1166 | printf("gpx file name is %s\n", gpx_name); | ||
1167 | |||
1168 | gpx = decompress_file(file, dialog, gpx_name, filename, NULL); | ||
1169 | |||
1170 | /* try to open -wpts.gpx file inside */ | ||
1171 | strcpy(gpx_name, fbase); | ||
1172 | strcpy(gpx_name+strlen(gpx_name)-4, "-wpts.gpx"); | ||
1173 | printf("gpx wpts file name is %s\n", gpx_name); | ||
1174 | |||
1175 | gpx = decompress_file(file, dialog, gpx_name, filename, gpx); | ||
1176 | |||
1177 | unzClose(file); | ||
1178 | free(gpx_name); | ||
1179 | return gpx; | ||
1180 | } | ||
1181 | |||
1182 | gpx_t *gpx_parse(gpx_dialog_t *dialog, char *filename) { | ||
1183 | gpx_t *gpx = NULL; | ||
1184 | |||
1185 | /* show busy dialog */ | ||
1186 | printf("load file %s\n", filename); | ||
1187 | |||
1188 | if((strlen(filename) > 4) && | ||
1189 | !strcasecmp(filename+strlen(filename)-4, ".zip")) { | ||
1190 | printf("trying to load a zip file!\n"); | ||
1191 | |||
1192 | gpx = decompress_zip(dialog, filename); | ||
1193 | } else | ||
1194 | gpx = gpx_parse_file(dialog, filename); | ||
1195 | |||
1196 | return gpx; | ||
1197 | } | ||
1198 | |||
1199 | /* scan entire directory */ | ||
1200 | gpx_t *gpx_parse_dir(gpx_dialog_t *dialog, char *dirname) { | ||
1201 | GnomeVFSResult result; | ||
1202 | GnomeVFSDirectoryHandle *handle; | ||
1203 | GnomeVFSFileInfo *finfo = gnome_vfs_file_info_new();; | ||
1204 | |||
1205 | gpx_t *gpx = NULL; | ||
1206 | |||
1207 | /* show busy dialog */ | ||
1208 | printf("load dir %s\n", dirname); | ||
1209 | gpx_busy_dialog_set(dialog, dirname); | ||
1210 | |||
1211 | LIBXML_TEST_VERSION; | ||
1212 | |||
1213 | result = gnome_vfs_directory_open(&handle, dirname, | ||
1214 | GNOME_VFS_FILE_INFO_DEFAULT); | ||
1215 | |||
1216 | if(result != GNOME_VFS_OK) { | ||
1217 | errorf("Unable to open directory \"%s\":\n%s", | ||
1218 | dirname, gnome_vfs_result_to_string(result)); | ||
1219 | return NULL; | ||
1220 | } | ||
1221 | |||
1222 | while(GNOME_VFS_OK == gnome_vfs_directory_read_next(handle, finfo)) { | ||
1223 | if(finfo->type == GNOME_VFS_FILE_TYPE_REGULAR) { | ||
1224 | char *ext = finfo->name+strlen(finfo->name)-4; | ||
1225 | |||
1226 | /* check if file ends with .gpx or .loc */ | ||
1227 | if((strcasecmp(ext, ".gpx") == 0) || (strcasecmp(ext, ".loc") == 0)) { | ||
1228 | char *filename = malloc(strlen(dirname)+strlen(finfo->name)+2); | ||
1229 | |||
1230 | strcpy(filename, dirname); | ||
1231 | if(strlastchr(filename) != '/') | ||
1232 | strcat(filename, "/"); | ||
1233 | strcat(filename, finfo->name); | ||
1234 | |||
1235 | xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0); | ||
1236 | if (reader != NULL) { | ||
1237 | gpx = process_root(reader, dialog, filename, gpx); | ||
1238 | xmlFreeTextReader(reader); | ||
1239 | } else { | ||
1240 | fprintf(stderr, "Unable to open %s\n", filename); | ||
1241 | } | ||
1242 | |||
1243 | free(filename); | ||
1244 | } | ||
1245 | } | ||
1246 | } | ||
1247 | |||
1248 | if(gpx) { | ||
1249 | /* replace file name with directory name */ | ||
1250 | free(gpx->filename); | ||
1251 | gpx->filename = strdup(dirname); | ||
1252 | |||
1253 | /* replace gpx name with directory name */ | ||
1254 | free(gpx->name); | ||
1255 | |||
1256 | /* retrieve pure dirname if possible */ | ||
1257 | char *n = strrchr(dirname, '/'); | ||
1258 | if(!n) n = dirname; | ||
1259 | else n++; | ||
1260 | |||
1261 | // gpx->name = malloc(strlen("<DIR> ")+strlen(n)+1); | ||
1262 | // strcpy(gpx->name, "<DIR> "); | ||
1263 | // strcat(gpx->name, n); | ||
1264 | gpx->name = strdup(n); | ||
1265 | } | ||
1266 | |||
1267 | gnome_vfs_file_info_unref(finfo); | ||
1268 | gnome_vfs_directory_close(handle); | ||
1269 | |||
1270 | return gpx; | ||
1271 | } | ||
1272 | |||
1273 | /* return number of caches in given gpx file */ | ||
1274 | int gpx_total_caches(gpx_t *gpx) { | ||
1275 | cache_t *cache = gpx->cache; | ||
1276 | int num = 0; | ||
1277 | |||
1278 | while(cache) { | ||
1279 | num++; | ||
1280 | cache = cache->next; | ||
1281 | } | ||
1282 | |||
1283 | return num; | ||
1284 | } | ||
1285 | |||
1286 | int gpx_number_of_waypoints(wpt_t *wpt) { | ||
1287 | int num = 0; | ||
1288 | |||
1289 | while(wpt) { | ||
1290 | num++; | ||
1291 | wpt = wpt->next; | ||
1292 | } | ||
1293 | |||
1294 | return num; | ||
1295 | } | ||
1296 | |||
1297 | int gpx_number_of_logs(log_t *log) { | ||
1298 | int num = 0; | ||
1299 | |||
1300 | while(log) { | ||
1301 | num++; | ||
1302 | log = log->next; | ||
1303 | } | ||
1304 | |||
1305 | return num; | ||
1306 | } | ||
1307 | |||
1308 | int gpx_number_of_tbs(tb_t *tb) { | ||
1309 | int num = 0; | ||
1310 | |||
1311 | while(tb) { | ||
1312 | num++; | ||
1313 | tb = tb->next; | ||
1314 | } | ||
1315 | |||
1316 | return num; | ||
1317 | } | ||
1318 | |||
1319 | /* http://mathforum.org/library/drmath/view/55417.html */ | ||
1320 | float gpx_pos_get_bearing(pos_t p1, pos_t p2) { | ||
1321 | /* convert to radians */ | ||
1322 | p1.lat *= (M_PI/180.0); p1.lon *= (M_PI/180.0); | ||
1323 | p2.lat *= (M_PI/180.0); p2.lon *= (M_PI/180.0); | ||
1324 | |||
1325 | return fmodf(360.0 + (180.0/M_PI) * | ||
1326 | (atan2( sin(p2.lon - p1.lon) * cos(p2.lat), | ||
1327 | cos(p1.lat) * sin(p2.lat) - | ||
1328 | sin(p1.lat) * cos(p2.lat) * cos(p2.lon - p1.lon))), | ||
1329 | 360.0); | ||
1330 | } | ||
1331 | |||
1332 | /* http://mathforum.org/library/drmath/view/51722.html */ | ||
1333 | float gpx_pos_get_distance(pos_t p1, pos_t p2, int miles) { | ||
1334 | /* convert to radians */ | ||
1335 | p1.lat *= (M_PI/180.0); p1.lon *= (M_PI/180.0); | ||
1336 | p2.lat *= (M_PI/180.0); p2.lon *= (M_PI/180.0); | ||
1337 | |||
1338 | float aob = acos(cos(p1.lat) * cos(p2.lat) * cos(p2.lon - p1.lon) + | ||
1339 | sin(p1.lat) * sin(p2.lat)); | ||
1340 | |||
1341 | if(miles) | ||
1342 | return(aob * 3959.0); /* great circle radius in miles */ | ||
1343 | |||
1344 | return(aob * 6371.0); /* great circle radius in kilometers */ | ||
1345 | } | ||
1346 | |||
1347 | void gpx_pos_get_distance_str(char *str, int len, | ||
1348 | pos_t p1, pos_t p2, int mil) { | ||
1349 | if(!p1.lat && !p1.lon) { | ||
1350 | snprintf(str, len, "---"); | ||
1351 | return; | ||
1352 | } | ||
1353 | |||
1354 | float dist = gpx_pos_get_distance(p1, p2, mil); | ||
1355 | distance_str(str, len, dist, mil); | ||
1356 | } | ||
1357 | |||
1358 | void gpx_sort(gpx_t *gpx, int by, pos_t *refpos) { | ||
1359 | cache_t **new; | ||
1360 | cache_t *cur = gpx->cache; | ||
1361 | int total = gpx_total_caches(gpx); | ||
1362 | float *dist_cache = malloc(total * sizeof(float)); | ||
1363 | |||
1364 | gpx->cache = NULL; /* detach old chain */ | ||
1365 | while(cur) { | ||
1366 | harbaum | 13 | float cur_dist = -1; |
1367 | harbaum | 1 | int cur_cnt = 0; |
1368 | |||
1369 | harbaum | 13 | if(!isnan(cur->pos.lat) && !isnan(cur->pos.lon)) |
1370 | cur_dist = gpx_pos_get_distance(*refpos, gpx_cache_pos(cur), 0); | ||
1371 | |||
1372 | harbaum | 1 | new = &(gpx->cache); |
1373 | |||
1374 | /* search for currect insertion point */ | ||
1375 | while(*new && (dist_cache[cur_cnt] < cur_dist)) { | ||
1376 | new = &((*new)->next); | ||
1377 | cur_cnt++; | ||
1378 | } | ||
1379 | |||
1380 | /* save distance for further comparisons */ | ||
1381 | memmove(dist_cache+cur_cnt+1, dist_cache+cur_cnt, | ||
1382 | sizeof(float)*(total-cur_cnt-1)); | ||
1383 | dist_cache[cur_cnt++] = cur_dist; | ||
1384 | |||
1385 | cache_t *next = cur->next; | ||
1386 | |||
1387 | /* insert into "new" chain */ | ||
1388 | cur->next = *new; | ||
1389 | *new = cur; | ||
1390 | |||
1391 | cur = next; | ||
1392 | } | ||
1393 | |||
1394 | free(dist_cache); | ||
1395 | } | ||
1396 | |||
1397 | gpx_t *gpx_cache2gpx(gpx_t *gpx, cache_t *search_cache) { | ||
1398 | while(gpx) { | ||
1399 | cache_t *cache = gpx->cache; | ||
1400 | while(cache) { | ||
1401 | if(cache == search_cache) | ||
1402 | return gpx; | ||
1403 | |||
1404 | cache = cache->next; | ||
1405 | } | ||
1406 | gpx = gpx->next; | ||
1407 | } | ||
1408 | |||
1409 | return NULL; | ||
1410 | } | ||
1411 | |||
1412 | /* since the actual cache position may be overridden, we */ | ||
1413 | /* always access the position through this function */ | ||
1414 | pos_t gpx_cache_pos(cache_t *cache) { | ||
1415 | if(cache->notes && cache->notes->override) | ||
1416 | return cache->notes->pos; | ||
1417 | |||
1418 | return cache->pos; | ||
1419 | } |