Parent Directory | Revision Log
Initial import
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 | gtk_window_set_title(GTK_WINDOW(dialog->dialog), _("Loading...")); | ||
156 | gtk_window_set_default_size(GTK_WINDOW(dialog->dialog), 300, 10); | ||
157 | |||
158 | gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); | ||
159 | gtk_window_set_transient_for(GTK_WINDOW(dialog->dialog), GTK_WINDOW(parent)); | ||
160 | |||
161 | dialog->label = gtk_label_new("---"); | ||
162 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog->dialog)->vbox), | ||
163 | dialog->label); | ||
164 | |||
165 | dialog->pbar = gtk_progress_bar_new(); | ||
166 | gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(dialog->pbar), | ||
167 | 0.0025 * DLG_DIV); | ||
168 | |||
169 | gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog->dialog)->vbox), | ||
170 | dialog->pbar); | ||
171 | |||
172 | gtk_widget_show_all(dialog->dialog); | ||
173 | |||
174 | return dialog; | ||
175 | #else | ||
176 | return NULL; | ||
177 | #endif | ||
178 | } | ||
179 | |||
180 | void gpx_busy_dialog_destroy(gpx_dialog_t *dialog) { | ||
181 | if(!dialog) return; | ||
182 | |||
183 | gtk_widget_destroy(dialog->dialog); | ||
184 | free(dialog); | ||
185 | } | ||
186 | |||
187 | static void gpx_busy_dialog_progress(gpx_dialog_t *dialog) { | ||
188 | static int sub_dlg = 0; | ||
189 | |||
190 | if(!dialog) return; | ||
191 | |||
192 | if(sub_dlg++ >= DLG_DIV) { | ||
193 | sub_dlg = 0; | ||
194 | |||
195 | gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dialog->pbar)); | ||
196 | |||
197 | /* wait for main gui to appear */ | ||
198 | while(gtk_events_pending()) | ||
199 | gtk_main_iteration(); | ||
200 | } | ||
201 | } | ||
202 | |||
203 | static void gpx_busy_dialog_set(gpx_dialog_t *dialog, char *name) { | ||
204 | if(!dialog) return; | ||
205 | |||
206 | if(strrchr(name, '/')) | ||
207 | name = strrchr(name, '/')+1; | ||
208 | |||
209 | gtk_label_set_text(GTK_LABEL(dialog->label), name); | ||
210 | |||
211 | /* wait for main gui to appear */ | ||
212 | while(gtk_events_pending()) | ||
213 | gtk_main_iteration(); | ||
214 | } | ||
215 | |||
216 | static int str_search(const char *pstr[], char *str, char *type) { | ||
217 | int i=0; | ||
218 | |||
219 | while(pstr[i+1][0]) { | ||
220 | char *p = (char*)pstr[i+1]; | ||
221 | |||
222 | /* multiple substrings in pattern? */ | ||
223 | while(strchr(p, '|')) { | ||
224 | if(!strncasecmp(p, str, strchr(p, '|')-p)) | ||
225 | return i; | ||
226 | |||
227 | p = strchr(p, '|')+1; | ||
228 | } | ||
229 | |||
230 | if(!strcasecmp(p, str)) | ||
231 | return i; | ||
232 | |||
233 | i++; | ||
234 | } | ||
235 | |||
236 | fprintf(stderr, "ERROR parsing \"%s\": Unknown \"%s\"\n", type, str); | ||
237 | return -1; | ||
238 | } | ||
239 | |||
240 | static int log_is_older(log_t *a, log_t *b) { | ||
241 | if(a->year < b->year) return TRUE; | ||
242 | else if(a->year == b->year) { | ||
243 | if(a->month < b->month) return TRUE; | ||
244 | else if(a->month == b->month) { | ||
245 | if(a->day < b->day) return TRUE; | ||
246 | } | ||
247 | } | ||
248 | |||
249 | return FALSE; | ||
250 | } | ||
251 | |||
252 | int is_white(char c) { | ||
253 | return((c==' ')||(c=='\r')||(c=='\n')); | ||
254 | } | ||
255 | |||
256 | static int all_is_white(char *str) { | ||
257 | while(*str) { | ||
258 | if(!is_white(*str)) | ||
259 | return FALSE; | ||
260 | |||
261 | str++; | ||
262 | } | ||
263 | return TRUE; | ||
264 | } | ||
265 | |||
266 | void gpx_display_log(log_t *log) { | ||
267 | printf(" Log:\n"); | ||
268 | printf(" date: %d.%d.%d\n", log->day, log->month, log->year); | ||
269 | printf(" type: %s\n", log_type_str[log->type+1]); | ||
270 | printf(" finder: %s\n", log->finder); | ||
271 | // printf(" text: %s\n", log->text); | ||
272 | } | ||
273 | |||
274 | void gpx_display_cache(cache_t *cache) { | ||
275 | log_t *log = cache->log; | ||
276 | |||
277 | printf("\nCache:\n"); | ||
278 | printf(" id: %s\n", cache->id); | ||
279 | printf(" name: %s\n", cache->name); | ||
280 | printf(" latitude: %f\n", cache->pos.lat); | ||
281 | printf(" longitude: %f\n", cache->pos.lon); | ||
282 | printf(" owner: %s\n", cache->owner); | ||
283 | printf(" type: %s\n", cache_type_str[cache->type+1]); | ||
284 | printf(" container: %s\n", cache_container_str[cache->container+1]); | ||
285 | printf(" difficulty: %.1f\n", cache->difficulty); | ||
286 | printf(" terrain: %.1f\n", cache->terrain); | ||
287 | // printf(" short: %s\n", cache->short_description); | ||
288 | // printf(" long: %s\n", cache->long_description); | ||
289 | // printf(" hint: %s\n", cache->hint); | ||
290 | |||
291 | while(log) { | ||
292 | gpx_display_log(log); | ||
293 | log = log->next; | ||
294 | } | ||
295 | } | ||
296 | |||
297 | void gpx_display_all(gpx_t *gpx) { | ||
298 | while(gpx) { | ||
299 | cache_t *cache = gpx->cache; | ||
300 | |||
301 | printf("GPX name: %s\n", gpx->name); | ||
302 | printf("GPX desc: %s\n", gpx->desc); | ||
303 | printf("GPX date: %d.%d.%d\n", gpx->day, gpx->month, gpx->year); | ||
304 | while(cache) { | ||
305 | gpx_display_cache(cache); | ||
306 | cache = cache->next; | ||
307 | } | ||
308 | gpx = gpx->next; | ||
309 | } | ||
310 | } | ||
311 | |||
312 | static gint my_strcmp(const xmlChar *a, const xmlChar *b) { | ||
313 | if(!a && !b) return 0; | ||
314 | if(!a) return -1; | ||
315 | if(!b) return +1; | ||
316 | return strcmp((char*)a,(char*)b); | ||
317 | } | ||
318 | |||
319 | static float xml_get_prop_float(xmlTextReaderPtr reader, char *name) { | ||
320 | float ret = NAN; | ||
321 | char *prop; | ||
322 | if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST name))) { | ||
323 | ret = g_ascii_strtod(prop, NULL); | ||
324 | xmlFree(prop); | ||
325 | } | ||
326 | return ret; | ||
327 | } | ||
328 | |||
329 | static int xml_prop_is(xmlTextReaderPtr reader, char *name, char *value, | ||
330 | int def_value) { | ||
331 | int match = def_value; | ||
332 | char *prop; | ||
333 | if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST name))) { | ||
334 | match = (strcasecmp(prop, value) == 0); | ||
335 | xmlFree(prop); | ||
336 | } | ||
337 | return match; | ||
338 | } | ||
339 | |||
340 | /* skip current element incl. everything below (mainly for testing) */ | ||
341 | /* returns FALSE if something failed */ | ||
342 | static gboolean skip_element(xmlTextReaderPtr reader) { | ||
343 | g_assert(xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT); | ||
344 | const xmlChar *name = xmlTextReaderConstName(reader); | ||
345 | g_assert(name); | ||
346 | int depth = xmlTextReaderDepth(reader); | ||
347 | |||
348 | if(xmlTextReaderIsEmptyElement(reader)) | ||
349 | return TRUE; | ||
350 | |||
351 | int ret = xmlTextReaderRead(reader); | ||
352 | while((ret == 1) && | ||
353 | ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) || | ||
354 | (xmlTextReaderDepth(reader) > depth) || | ||
355 | (my_strcmp(xmlTextReaderConstName(reader), name) != 0))) { | ||
356 | ret = xmlTextReaderRead(reader); | ||
357 | } | ||
358 | return(ret == 1); | ||
359 | } | ||
360 | |||
361 | static char *process_text(xmlTextReaderPtr reader) { | ||
362 | char *text = NULL; | ||
363 | 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 | (strncasecmp(cache->id, "GC", 2) != 0) && | ||
768 | (strncasecmp(cache->id, "OC", 2) != 0) && | ||
769 | (strncasecmp(cache->id, "GA", 2) != 0)) { | ||
770 | cache_t *parent = gpx->cache; | ||
771 | |||
772 | /* check if the gpx file contains a cache with matching name */ | ||
773 | while(parent && strcasecmp(parent->id+2, cache->id+2)) | ||
774 | parent = parent->next; | ||
775 | |||
776 | if(parent && parent != cache) { | ||
777 | wpt_t **wpt = &parent->wpt; | ||
778 | |||
779 | /* search end of list */ | ||
780 | while(*wpt && (strcmp((*wpt)->id, cache->id)<0)) | ||
781 | wpt = &(*wpt)->next; | ||
782 | |||
783 | *wpt = malloc(sizeof(wpt_t)); | ||
784 | memset(*wpt, 0, sizeof(wpt_t)); | ||
785 | |||
786 | /* transfer name to waypoint entry */ | ||
787 | (*wpt)->id = cache->id; | ||
788 | cache->id = NULL; | ||
789 | |||
790 | (*wpt)->pos.lat = cache->pos.lat; | ||
791 | (*wpt)->pos.lon = cache->pos.lon; | ||
792 | |||
793 | (*wpt)->cmt = cmt; cmt = NULL; | ||
794 | (*wpt)->desc = desc; desc = NULL; | ||
795 | (*wpt)->sym = str_search(wpt_sym_str, sym, "wpt sym"); | ||
796 | |||
797 | /* and just free the current cache entry as we now have used */ | ||
798 | /* the data for a caches waypoint */ | ||
799 | gpx_free_cache(cache); | ||
800 | cache = NULL; | ||
801 | } else { | ||
802 | /* if it doesn't have a name etc, it's probably not a real */ | ||
803 | /* cache, so drop it */ | ||
804 | if(!cache->name || !cache->id) { | ||
805 | printf("Orphaned waypoint: %s\n", cache->id); | ||
806 | gpx_free_cache(cache); | ||
807 | cache = NULL; | ||
808 | } | ||
809 | } | ||
810 | } else { | ||
811 | if(!cache->id) | ||
812 | cache->id = g_strdup_printf("NO ID"); | ||
813 | |||
814 | /* this is known to be a geocache due to its waypoint name */ | ||
815 | /* (gc*, oc*, ga*) and is thus forces to be an entry */ | ||
816 | if(!cache->name) | ||
817 | cache->name = g_strdup_printf("Unnamed(%s)", cache->id); | ||
818 | } | ||
819 | |||
820 | if(desc) free(desc); | ||
821 | if(cmt) free(cmt); | ||
822 | if(sym) free(sym); | ||
823 | |||
824 | return cache; | ||
825 | break; | ||
826 | |||
827 | default: | ||
828 | break; | ||
829 | } | ||
830 | ret = xmlTextReaderRead(reader); | ||
831 | } | ||
832 | |||
833 | g_assert(0); | ||
834 | return cache; | ||
835 | } | ||
836 | |||
837 | static void process_gpx(xmlTextReaderPtr reader, gpx_dialog_t *dialog, | ||
838 | gpx_t *gpx) { | ||
839 | |||
840 | /* no attributes of interest */ | ||
841 | |||
842 | /* the following might be optimized for speed reasons! */ | ||
843 | |||
844 | /* find end of cache chain */ | ||
845 | cache_t **cache = &gpx->cache; | ||
846 | while(*cache) cache = &(*cache)->next; | ||
847 | |||
848 | const xmlChar *name = xmlTextReaderConstName(reader); | ||
849 | g_assert(name); | ||
850 | |||
851 | /* read next node */ | ||
852 | int ret = xmlTextReaderRead(reader); | ||
853 | while(ret == 1) { | ||
854 | |||
855 | switch(xmlTextReaderNodeType(reader)) { | ||
856 | case XML_READER_TYPE_ELEMENT: | ||
857 | |||
858 | g_assert(xmlTextReaderDepth(reader) == 1); | ||
859 | char *name = (char*)xmlTextReaderConstName(reader); | ||
860 | if(name && !gpx->name && strcasecmp(name, "name") == 0) { | ||
861 | gpx->name = process_text(reader); | ||
862 | } else if(name && !gpx->desc && strcasecmp(name, "desc") == 0) { | ||
863 | gpx->desc = process_text(reader); | ||
864 | } else if(name && ((strcasecmp(name, "time") == 0) || | ||
865 | (strcasecmp(name, "date") == 0))) { | ||
866 | xml_get_date(reader, &gpx->year, &gpx->month, &gpx->day); | ||
867 | } else if(name && strcasecmp(name, "wpt") == 0) { | ||
868 | *cache = process_gpx_wpt(reader, dialog, gpx); | ||
869 | if(*cache) cache = &(*cache)->next; | ||
870 | } else { | ||
871 | // printf("something unknown (%s) found\n", name); | ||
872 | skip_element(reader); | ||
873 | } | ||
874 | break; | ||
875 | |||
876 | case XML_READER_TYPE_END_ELEMENT: | ||
877 | /* end element must be for the current element */ | ||
878 | g_assert(xmlTextReaderDepth(reader) == 0); | ||
879 | return; | ||
880 | break; | ||
881 | |||
882 | default: | ||
883 | break; | ||
884 | } | ||
885 | ret = xmlTextReaderRead(reader); | ||
886 | } | ||
887 | |||
888 | g_assert(0); | ||
889 | } | ||
890 | |||
891 | /* parse loc waypoint entry */ | ||
892 | static cache_t *process_loc_waypoint(xmlTextReaderPtr reader, | ||
893 | gpx_dialog_t *dialog) { | ||
894 | |||
895 | gpx_busy_dialog_progress(dialog); | ||
896 | |||
897 | if(xmlTextReaderIsEmptyElement(reader)) | ||
898 | return NULL; | ||
899 | |||
900 | cache_t *cache = malloc(sizeof(cache_t)); | ||
901 | memset(cache, 0, sizeof(cache_t)); | ||
902 | |||
903 | /* set some defaults */ | ||
904 | cache->type = CACHE_TYPE_TRADITIONAL; | ||
905 | cache->container = CACHE_CONT_UNKNOWN; | ||
906 | cache->available = TRUE; | ||
907 | |||
908 | /* process all sub-nodes */ | ||
909 | int depth = xmlTextReaderDepth(reader); | ||
910 | int ret = xmlTextReaderRead(reader); | ||
911 | while(ret == 1) { | ||
912 | |||
913 | switch(xmlTextReaderNodeType(reader)) { | ||
914 | case XML_READER_TYPE_ELEMENT: | ||
915 | g_assert(xmlTextReaderDepth(reader) == depth+1); | ||
916 | char *name = (char*)xmlTextReaderConstName(reader); | ||
917 | if(strrchr(name, ':')) name = strrchr(name, ':')+1; | ||
918 | |||
919 | if(name) { | ||
920 | if(strcasecmp(name, "name") == 0) { | ||
921 | cache->id = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id"); | ||
922 | cache->name = process_text(reader); | ||
923 | } else if(strcasecmp(name, "link") == 0) { | ||
924 | cache->url = process_text(reader); | ||
925 | } else if(strcasecmp(name, "coord") == 0) { | ||
926 | cache->pos.lat = xml_get_prop_float(reader, "lat"); | ||
927 | cache->pos.lon = xml_get_prop_float(reader, "lon"); | ||
928 | skip_element(reader); | ||
929 | } else | ||
930 | skip_element(reader); | ||
931 | } else | ||
932 | skip_element(reader); | ||
933 | break; | ||
934 | |||
935 | case XML_READER_TYPE_END_ELEMENT: | ||
936 | /* end element must be for the current element */ | ||
937 | g_assert(xmlTextReaderDepth(reader) == depth); | ||
938 | return cache; | ||
939 | break; | ||
940 | |||
941 | default: | ||
942 | break; | ||
943 | } | ||
944 | ret = xmlTextReaderRead(reader); | ||
945 | } | ||
946 | |||
947 | g_assert(0); | ||
948 | return NULL; | ||
949 | } | ||
950 | |||
951 | static void process_loc(xmlTextReaderPtr reader, gpx_dialog_t *dialog, | ||
952 | gpx_t *gpx) { | ||
953 | |||
954 | /* find end of cache chain */ | ||
955 | cache_t **cache = &gpx->cache; | ||
956 | while(*cache) cache = &(*cache)->next; | ||
957 | |||
958 | const xmlChar *name = xmlTextReaderConstName(reader); | ||
959 | g_assert(name); | ||
960 | |||
961 | /* read next node */ | ||
962 | int ret = xmlTextReaderRead(reader); | ||
963 | while(ret == 1) { | ||
964 | |||
965 | switch(xmlTextReaderNodeType(reader)) { | ||
966 | case XML_READER_TYPE_ELEMENT: | ||
967 | |||
968 | g_assert(xmlTextReaderDepth(reader) == 1); | ||
969 | char *name = (char*)xmlTextReaderConstName(reader); | ||
970 | |||
971 | if(name && strcasecmp(name, "waypoint") == 0) { | ||
972 | *cache = process_loc_waypoint(reader, dialog); | ||
973 | if(*cache) cache = &(*cache)->next; | ||
974 | } else | ||
975 | skip_element(reader); | ||
976 | break; | ||
977 | |||
978 | case XML_READER_TYPE_END_ELEMENT: | ||
979 | /* end element must be for the current element */ | ||
980 | g_assert(xmlTextReaderDepth(reader) == 0); | ||
981 | return; | ||
982 | break; | ||
983 | |||
984 | default: | ||
985 | break; | ||
986 | } | ||
987 | ret = xmlTextReaderRead(reader); | ||
988 | } | ||
989 | |||
990 | g_assert(0); | ||
991 | return; | ||
992 | } | ||
993 | |||
994 | static gpx_t *process_root(xmlTextReaderPtr reader, gpx_dialog_t *dialog, | ||
995 | char *fname, gpx_t *in) { | ||
996 | |||
997 | /* no gpx entry given, create a new one */ | ||
998 | gpx_t *gpx = NULL; | ||
999 | if(!in) { | ||
1000 | /* allocate memory to hold gpx file description */ | ||
1001 | gpx = malloc(sizeof(gpx_t)); | ||
1002 | memset(gpx, 0, sizeof(gpx_t)); | ||
1003 | gpx->filename = strdup(fname); | ||
1004 | } else | ||
1005 | gpx = in; | ||
1006 | |||
1007 | int ret = xmlTextReaderRead(reader); | ||
1008 | while(ret == 1) { | ||
1009 | switch(xmlTextReaderNodeType(reader)) { | ||
1010 | case XML_READER_TYPE_ELEMENT: | ||
1011 | g_assert(xmlTextReaderDepth(reader) == 0); | ||
1012 | char *name = (char*)xmlTextReaderConstName(reader); | ||
1013 | if(name && strcasecmp(name, "gpx") == 0) { | ||
1014 | process_gpx(reader, dialog, gpx); | ||
1015 | } else if(name && strcasecmp(name, "loc") == 0) { | ||
1016 | process_loc(reader, dialog, gpx); | ||
1017 | } else { | ||
1018 | printf("something unknown found\n"); | ||
1019 | skip_element(reader); | ||
1020 | } | ||
1021 | break; | ||
1022 | |||
1023 | case XML_READER_TYPE_END_ELEMENT: | ||
1024 | /* end element must be for the current element */ | ||
1025 | g_assert(xmlTextReaderDepth(reader) == 0); | ||
1026 | ret = -1; | ||
1027 | break; | ||
1028 | |||
1029 | default: | ||
1030 | break; | ||
1031 | } | ||
1032 | |||
1033 | if(ret == 1) | ||
1034 | ret = xmlTextReaderRead(reader); | ||
1035 | } | ||
1036 | |||
1037 | /* check if a name has been set and use filename if not */ | ||
1038 | if(!in && !gpx->name) { | ||
1039 | if(!gpx->desc) { | ||
1040 | char *str = strrchr(fname, '/'); | ||
1041 | if(str) gpx->name = strdup(str+1); | ||
1042 | else gpx->name = strdup(fname); | ||
1043 | } else | ||
1044 | gpx->name = strdup(gpx->desc); | ||
1045 | } | ||
1046 | |||
1047 | return gpx; | ||
1048 | } | ||
1049 | |||
1050 | static gpx_t *gpx_parse_file(gpx_dialog_t *dialog, char *filename) { | ||
1051 | gpx_t *gpx = NULL; | ||
1052 | |||
1053 | LIBXML_TEST_VERSION; | ||
1054 | |||
1055 | gpx_busy_dialog_set(dialog, filename); | ||
1056 | |||
1057 | xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0); | ||
1058 | if (reader != NULL) { | ||
1059 | gpx = process_root(reader, dialog, filename, NULL); | ||
1060 | xmlFreeTextReader(reader); | ||
1061 | } else { | ||
1062 | fprintf(stderr, "Unable to open %s\n", filename); | ||
1063 | } | ||
1064 | |||
1065 | /* check of there's a waypoints file (*-wpts.gpx) for this */ | ||
1066 | if(strrchr(filename, '.')) { | ||
1067 | char *dot = strrchr(filename, '.'); | ||
1068 | char wpts_name[128]; | ||
1069 | *dot = 0; | ||
1070 | snprintf(wpts_name, sizeof(wpts_name), "%s-wpts.gpx", filename); | ||
1071 | *dot = '.'; | ||
1072 | |||
1073 | if(g_file_test(wpts_name, G_FILE_TEST_EXISTS)) { | ||
1074 | |||
1075 | xmlTextReaderPtr reader = xmlReaderForFile(wpts_name, NULL, 0); | ||
1076 | if (reader != NULL) { | ||
1077 | gpx = process_root(reader, dialog, wpts_name, gpx); | ||
1078 | xmlFreeTextReader(reader); | ||
1079 | } else { | ||
1080 | fprintf(stderr, "Unable to open %s\n", filename); | ||
1081 | } | ||
1082 | } | ||
1083 | } | ||
1084 | |||
1085 | return gpx; | ||
1086 | } | ||
1087 | |||
1088 | static gpx_t *decompress_file(unzFile file, gpx_dialog_t *dialog, | ||
1089 | char *name, char *filename, | ||
1090 | gpx_t *gpx_in) { | ||
1091 | unz_file_info info; | ||
1092 | gpx_t *gpx = NULL; | ||
1093 | |||
1094 | gpx_busy_dialog_set(dialog, name); | ||
1095 | |||
1096 | if((unzLocateFile(file, name, FALSE) != Z_OK) || | ||
1097 | (unzGetCurrentFileInfo(file, &info, NULL,0, NULL,0, NULL,0) != Z_OK) || | ||
1098 | (unzOpenCurrentFile(file) != UNZ_OK)) { | ||
1099 | |||
1100 | /* do not complain if we are processing a waypoints file */ | ||
1101 | if(!gpx_in) | ||
1102 | errorf("Unable to locate/get info/open\n%s\ninside\n%s", | ||
1103 | name, filename); | ||
1104 | else | ||
1105 | printf("Unable to locate/get info/open %s inside %s\n", | ||
1106 | name, filename); | ||
1107 | |||
1108 | return gpx_in; | ||
1109 | } | ||
1110 | |||
1111 | printf("file size is %ld\n", info.uncompressed_size); | ||
1112 | |||
1113 | char *buffer = malloc(info.uncompressed_size); | ||
1114 | if(!buffer) { | ||
1115 | errorf("Out of memory while uncompressing file"); | ||
1116 | unzCloseCurrentFile(file); | ||
1117 | return gpx_in; | ||
1118 | } | ||
1119 | |||
1120 | if(unzReadCurrentFile(file, buffer, info.uncompressed_size) < 0) { | ||
1121 | errorf("Read error on compressed file"); | ||
1122 | free(buffer); | ||
1123 | unzCloseCurrentFile(file); | ||
1124 | return gpx_in; | ||
1125 | } | ||
1126 | |||
1127 | /* fire up libxml */ | ||
1128 | LIBXML_TEST_VERSION; | ||
1129 | |||
1130 | xmlTextReaderPtr reader = | ||
1131 | xmlReaderForMemory(buffer, info.uncompressed_size, | ||
1132 | NULL, NULL, 0); | ||
1133 | if (reader != NULL) { | ||
1134 | gpx = process_root(reader, dialog, filename, gpx_in); | ||
1135 | xmlFreeTextReader(reader); | ||
1136 | } else { | ||
1137 | fprintf(stderr, "Unable to open %s\n", filename); | ||
1138 | } | ||
1139 | |||
1140 | free(buffer); | ||
1141 | unzCloseCurrentFile(file); | ||
1142 | return gpx; | ||
1143 | } | ||
1144 | |||
1145 | static gpx_t *decompress_zip(gpx_dialog_t *dialog, char *filename) { | ||
1146 | char *gpx_name, *fbase; | ||
1147 | gpx_t *gpx = NULL; | ||
1148 | |||
1149 | /* extract base name and allocate space for file names */ | ||
1150 | fbase = strrchr(filename, '/'); | ||
1151 | if(!fbase) fbase = filename; | ||
1152 | else fbase++; /* skip '/' */ | ||
1153 | gpx_name = malloc(strlen(fbase)+strlen("-wpts")+1); | ||
1154 | |||
1155 | unzFile file = unzOpen(filename); | ||
1156 | if(!file) { | ||
1157 | errorf("Error opening file %s for unzip", filename); | ||
1158 | free(gpx_name); | ||
1159 | return NULL; | ||
1160 | } | ||
1161 | |||
1162 | printf("ZIP file successfully opened\n"); | ||
1163 | |||
1164 | /* try to open gpx file inside */ | ||
1165 | strcpy(gpx_name, fbase); | ||
1166 | strcpy(gpx_name+strlen(gpx_name)-4, ".gpx"); | ||
1167 | printf("gpx file name is %s\n", gpx_name); | ||
1168 | |||
1169 | gpx = decompress_file(file, dialog, gpx_name, filename, NULL); | ||
1170 | |||
1171 | /* try to open -wpts.gpx file inside */ | ||
1172 | strcpy(gpx_name, fbase); | ||
1173 | strcpy(gpx_name+strlen(gpx_name)-4, "-wpts.gpx"); | ||
1174 | printf("gpx wpts file name is %s\n", gpx_name); | ||
1175 | |||
1176 | gpx = decompress_file(file, dialog, gpx_name, filename, gpx); | ||
1177 | |||
1178 | unzClose(file); | ||
1179 | free(gpx_name); | ||
1180 | return gpx; | ||
1181 | } | ||
1182 | |||
1183 | gpx_t *gpx_parse(gpx_dialog_t *dialog, char *filename) { | ||
1184 | gpx_t *gpx = NULL; | ||
1185 | |||
1186 | /* show busy dialog */ | ||
1187 | printf("load file %s\n", filename); | ||
1188 | |||
1189 | if((strlen(filename) > 4) && | ||
1190 | !strcasecmp(filename+strlen(filename)-4, ".zip")) { | ||
1191 | printf("trying to load a zip file!\n"); | ||
1192 | |||
1193 | gpx = decompress_zip(dialog, filename); | ||
1194 | } else | ||
1195 | gpx = gpx_parse_file(dialog, filename); | ||
1196 | |||
1197 | return gpx; | ||
1198 | } | ||
1199 | |||
1200 | /* scan entire directory */ | ||
1201 | gpx_t *gpx_parse_dir(gpx_dialog_t *dialog, char *dirname) { | ||
1202 | GnomeVFSResult result; | ||
1203 | GnomeVFSDirectoryHandle *handle; | ||
1204 | GnomeVFSFileInfo *finfo = gnome_vfs_file_info_new();; | ||
1205 | |||
1206 | gpx_t *gpx = NULL; | ||
1207 | |||
1208 | /* show busy dialog */ | ||
1209 | printf("load dir %s\n", dirname); | ||
1210 | gpx_busy_dialog_set(dialog, dirname); | ||
1211 | |||
1212 | LIBXML_TEST_VERSION; | ||
1213 | |||
1214 | result = gnome_vfs_directory_open(&handle, dirname, | ||
1215 | GNOME_VFS_FILE_INFO_DEFAULT); | ||
1216 | |||
1217 | if(result != GNOME_VFS_OK) { | ||
1218 | errorf("Unable to open directory \"%s\":\n%s", | ||
1219 | dirname, gnome_vfs_result_to_string(result)); | ||
1220 | return NULL; | ||
1221 | } | ||
1222 | |||
1223 | while(GNOME_VFS_OK == gnome_vfs_directory_read_next(handle, finfo)) { | ||
1224 | if(finfo->type == GNOME_VFS_FILE_TYPE_REGULAR) { | ||
1225 | char *ext = finfo->name+strlen(finfo->name)-4; | ||
1226 | |||
1227 | /* check if file ends with .gpx or .loc */ | ||
1228 | if((strcasecmp(ext, ".gpx") == 0) || (strcasecmp(ext, ".loc") == 0)) { | ||
1229 | char *filename = malloc(strlen(dirname)+strlen(finfo->name)+2); | ||
1230 | |||
1231 | strcpy(filename, dirname); | ||
1232 | if(strlastchr(filename) != '/') | ||
1233 | strcat(filename, "/"); | ||
1234 | strcat(filename, finfo->name); | ||
1235 | |||
1236 | xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0); | ||
1237 | if (reader != NULL) { | ||
1238 | gpx = process_root(reader, dialog, filename, gpx); | ||
1239 | xmlFreeTextReader(reader); | ||
1240 | } else { | ||
1241 | fprintf(stderr, "Unable to open %s\n", filename); | ||
1242 | } | ||
1243 | |||
1244 | free(filename); | ||
1245 | } | ||
1246 | } | ||
1247 | } | ||
1248 | |||
1249 | if(gpx) { | ||
1250 | /* replace file name with directory name */ | ||
1251 | free(gpx->filename); | ||
1252 | gpx->filename = strdup(dirname); | ||
1253 | |||
1254 | /* replace gpx name with directory name */ | ||
1255 | free(gpx->name); | ||
1256 | |||
1257 | /* retrieve pure dirname if possible */ | ||
1258 | char *n = strrchr(dirname, '/'); | ||
1259 | if(!n) n = dirname; | ||
1260 | else n++; | ||
1261 | |||
1262 | // gpx->name = malloc(strlen("<DIR> ")+strlen(n)+1); | ||
1263 | // strcpy(gpx->name, "<DIR> "); | ||
1264 | // strcat(gpx->name, n); | ||
1265 | gpx->name = strdup(n); | ||
1266 | } | ||
1267 | |||
1268 | gnome_vfs_file_info_unref(finfo); | ||
1269 | gnome_vfs_directory_close(handle); | ||
1270 | |||
1271 | return gpx; | ||
1272 | } | ||
1273 | |||
1274 | /* return number of caches in given gpx file */ | ||
1275 | int gpx_total_caches(gpx_t *gpx) { | ||
1276 | cache_t *cache = gpx->cache; | ||
1277 | int num = 0; | ||
1278 | |||
1279 | while(cache) { | ||
1280 | num++; | ||
1281 | cache = cache->next; | ||
1282 | } | ||
1283 | |||
1284 | return num; | ||
1285 | } | ||
1286 | |||
1287 | int gpx_number_of_waypoints(wpt_t *wpt) { | ||
1288 | int num = 0; | ||
1289 | |||
1290 | while(wpt) { | ||
1291 | num++; | ||
1292 | wpt = wpt->next; | ||
1293 | } | ||
1294 | |||
1295 | return num; | ||
1296 | } | ||
1297 | |||
1298 | int gpx_number_of_logs(log_t *log) { | ||
1299 | int num = 0; | ||
1300 | |||
1301 | while(log) { | ||
1302 | num++; | ||
1303 | log = log->next; | ||
1304 | } | ||
1305 | |||
1306 | return num; | ||
1307 | } | ||
1308 | |||
1309 | int gpx_number_of_tbs(tb_t *tb) { | ||
1310 | int num = 0; | ||
1311 | |||
1312 | while(tb) { | ||
1313 | num++; | ||
1314 | tb = tb->next; | ||
1315 | } | ||
1316 | |||
1317 | return num; | ||
1318 | } | ||
1319 | |||
1320 | /* http://mathforum.org/library/drmath/view/55417.html */ | ||
1321 | float gpx_pos_get_bearing(pos_t p1, pos_t p2) { | ||
1322 | /* convert to radians */ | ||
1323 | p1.lat *= (M_PI/180.0); p1.lon *= (M_PI/180.0); | ||
1324 | p2.lat *= (M_PI/180.0); p2.lon *= (M_PI/180.0); | ||
1325 | |||
1326 | return fmodf(360.0 + (180.0/M_PI) * | ||
1327 | (atan2( sin(p2.lon - p1.lon) * cos(p2.lat), | ||
1328 | cos(p1.lat) * sin(p2.lat) - | ||
1329 | sin(p1.lat) * cos(p2.lat) * cos(p2.lon - p1.lon))), | ||
1330 | 360.0); | ||
1331 | } | ||
1332 | |||
1333 | /* http://mathforum.org/library/drmath/view/51722.html */ | ||
1334 | float gpx_pos_get_distance(pos_t p1, pos_t p2, int miles) { | ||
1335 | /* convert to radians */ | ||
1336 | p1.lat *= (M_PI/180.0); p1.lon *= (M_PI/180.0); | ||
1337 | p2.lat *= (M_PI/180.0); p2.lon *= (M_PI/180.0); | ||
1338 | |||
1339 | float aob = acos(cos(p1.lat) * cos(p2.lat) * cos(p2.lon - p1.lon) + | ||
1340 | sin(p1.lat) * sin(p2.lat)); | ||
1341 | |||
1342 | if(miles) | ||
1343 | return(aob * 3959.0); /* great circle radius in miles */ | ||
1344 | |||
1345 | return(aob * 6371.0); /* great circle radius in kilometers */ | ||
1346 | } | ||
1347 | |||
1348 | void gpx_pos_get_distance_str(char *str, int len, | ||
1349 | pos_t p1, pos_t p2, int mil) { | ||
1350 | if(!p1.lat && !p1.lon) { | ||
1351 | snprintf(str, len, "---"); | ||
1352 | return; | ||
1353 | } | ||
1354 | |||
1355 | float dist = gpx_pos_get_distance(p1, p2, mil); | ||
1356 | distance_str(str, len, dist, mil); | ||
1357 | } | ||
1358 | |||
1359 | /* http://library.gnome.org/devel/gtk/unstable/GtkRadioButton.html */ | ||
1360 | void gpx_sort(gpx_t *gpx, int by, pos_t *refpos) { | ||
1361 | cache_t **new; | ||
1362 | cache_t *cur = gpx->cache; | ||
1363 | int total = gpx_total_caches(gpx); | ||
1364 | float *dist_cache = malloc(total * sizeof(float)); | ||
1365 | |||
1366 | gpx->cache = NULL; /* detach old chain */ | ||
1367 | while(cur) { | ||
1368 | float cur_dist = gpx_pos_get_distance(*refpos, gpx_cache_pos(cur), 0); | ||
1369 | int cur_cnt = 0; | ||
1370 | |||
1371 | new = &(gpx->cache); | ||
1372 | |||
1373 | /* search for currect insertion point */ | ||
1374 | while(*new && (dist_cache[cur_cnt] < cur_dist)) { | ||
1375 | new = &((*new)->next); | ||
1376 | cur_cnt++; | ||
1377 | } | ||
1378 | |||
1379 | /* save distance for further comparisons */ | ||
1380 | memmove(dist_cache+cur_cnt+1, dist_cache+cur_cnt, | ||
1381 | sizeof(float)*(total-cur_cnt-1)); | ||
1382 | dist_cache[cur_cnt++] = cur_dist; | ||
1383 | |||
1384 | cache_t *next = cur->next; | ||
1385 | |||
1386 | /* insert into "new" chain */ | ||
1387 | cur->next = *new; | ||
1388 | *new = cur; | ||
1389 | |||
1390 | cur = next; | ||
1391 | } | ||
1392 | |||
1393 | free(dist_cache); | ||
1394 | } | ||
1395 | |||
1396 | gpx_t *gpx_cache2gpx(gpx_t *gpx, cache_t *search_cache) { | ||
1397 | while(gpx) { | ||
1398 | cache_t *cache = gpx->cache; | ||
1399 | while(cache) { | ||
1400 | if(cache == search_cache) | ||
1401 | return gpx; | ||
1402 | |||
1403 | cache = cache->next; | ||
1404 | } | ||
1405 | gpx = gpx->next; | ||
1406 | } | ||
1407 | |||
1408 | return NULL; | ||
1409 | } | ||
1410 | |||
1411 | /* since the actual cache position may be overridden, we */ | ||
1412 | /* always access the position through this function */ | ||
1413 | pos_t gpx_cache_pos(cache_t *cache) { | ||
1414 | if(cache->notes && cache->notes->override) | ||
1415 | return cache->notes->pos; | ||
1416 | |||
1417 | return cache->pos; | ||
1418 | } |