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