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