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