Diff of /trunk/src/gpx.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 34 by harbaum, Wed Jul 29 19:24:15 2009 UTC revision 298 by harbaum, Thu Aug 26 18:16:17 2010 UTC
# Line 42  void gpx_free_wpt(wpt_t *wpt) { Line 42  void gpx_free_wpt(wpt_t *wpt) {
42    free(wpt);    free(wpt);
43  }  }
44    
45    void gpx_free_user(user_t *user) {
46      if(user->name) xmlFree(user->name);
47      free(user);
48    }
49    
50  void gpx_free_log(log_t *log) {  void gpx_free_log(log_t *log) {
51    if(log->finder)  xmlFree(log->finder);    if(log->finder)  gpx_free_user(log->finder);
52    if(log->text)    xmlFree(log->text);    if(log->text)    xmlFree(log->text);
53    free(log);    free(log);
54  }  }
# Line 54  void gpx_free_tb(tb_t *tb) { Line 59  void gpx_free_tb(tb_t *tb) {
59    free(tb);    free(tb);
60  }  }
61    
62    void gpx_free_attribute(attribute_t *att) {
63      if(att->name) xmlFree(att->name);
64      free(att);
65    }
66    
67  void gpx_free_cache(cache_t *cache) {  void gpx_free_cache(cache_t *cache) {
68    log_t *log = cache->log;    log_t        *log = cache->log;
69    wpt_t *wpt = cache->wpt;    wpt_t        *wpt = cache->wpt;
70    tb_t  *tb  = cache->tb;    tb_t         *tb  = cache->tb;
71      attribute_t  *att = cache->attribute;
72    
73    if(cache->id)                xmlFree(cache->id);    if(cache->id)                xmlFree(cache->id);
74    if(cache->name)              xmlFree(cache->name);    if(cache->name)              xmlFree(cache->name);
75    if(cache->owner)             xmlFree(cache->owner);    if(cache->owner)             gpx_free_user(cache->owner);
76    if(cache->short_description) xmlFree(cache->short_description);    if(cache->short_description) xmlFree(cache->short_description);
77    if(cache->long_description)  xmlFree(cache->long_description);    if(cache->long_description)  xmlFree(cache->long_description);
78    if(cache->hint)              xmlFree(cache->hint);    if(cache->hint)              xmlFree(cache->hint);
# Line 76  void gpx_free_cache(cache_t *cache) { Line 87  void gpx_free_cache(cache_t *cache) {
87    /* free all tbs */    /* free all tbs */
88    while(tb) { tb_t *next = tb->next; gpx_free_tb(tb); tb = next; }    while(tb) { tb_t *next = tb->next; gpx_free_tb(tb); tb = next; }
89    
90      /* free all attributes */
91      while(att) { attribute_t *next = att->next; gpx_free_attribute(att); att = next; }
92    
93    if(cache->notes)             notes_free(cache->notes);    if(cache->notes)             notes_free(cache->notes);
94    
95    free(cache);    free(cache);
# Line 114  void gpx_free_all(gpx_t *gpx) { Line 128  void gpx_free_all(gpx_t *gpx) {
128  }  }
129    
130  static const char *cache_type_str[] = { "<Unknown>",  static const char *cache_type_str[] = { "<Unknown>",
131    "Traditional Cache|Traditional|Geocache", "Multi-cache|Multi",    "Traditional Cache|Traditional|Geocache", "Multi-cache|Multi|Multicache",
132    "Unknown Cache|Other",    "Unknown Cache|Other|Unknown",
133    "Virtual Cache|Virtual", "Webcam Cache|Webcam", "Event Cache|Event|Geocoins:",    "Virtual Cache|Virtual", "Webcam Cache|Webcam", "Event Cache|Event|Geocoins:",
134    "Letterbox Hybrid|Letterbox", "Earthcache", "Wherigo Cache",    "Letterbox Hybrid|Letterbox", "Earthcache", "Wherigo Cache",
135    "Mega-Event Cache", "Cache In Trash Out Event",    "Mega-Event Cache", "Cache In Trash Out Event",
# Line 267  void gpx_display_log(log_t *log) { Line 281  void gpx_display_log(log_t *log) {
281    printf("  Log:\n");    printf("  Log:\n");
282    printf("    date:     %d.%d.%d\n", log->day, log->month, log->year);    printf("    date:     %d.%d.%d\n", log->day, log->month, log->year);
283    printf("    type:     %s\n", log_type_str[log->type+1]);    printf("    type:     %s\n", log_type_str[log->type+1]);
284    printf("    finder:   %s\n", log->finder);    printf("    finder:   %s\n", log->finder->name);
285      printf("    id:       #%u\n", log->id);
286    //  printf("    text:     %s\n", log->text);    //  printf("    text:     %s\n", log->text);
287  }  }
288    
# Line 279  void gpx_display_cache(cache_t *cache) { Line 294  void gpx_display_cache(cache_t *cache) {
294    printf("  name:       %s\n", cache->name);    printf("  name:       %s\n", cache->name);
295    printf("  latitude:   %f\n", cache->pos.lat);    printf("  latitude:   %f\n", cache->pos.lat);
296    printf("  longitude:  %f\n", cache->pos.lon);    printf("  longitude:  %f\n", cache->pos.lon);
297    printf("  owner:      %s\n", cache->owner);    printf("  owner:      %s\n", cache->owner->name);
298    printf("  type:       %s\n", cache_type_str[cache->type+1]);    printf("  type:       %s\n", cache_type_str[cache->type+1]);
299    printf("  container:  %s\n", cache_container_str[cache->container+1]);    printf("  container:  %s\n", cache_container_str[cache->container+1]);
300    printf("  difficulty: %.1f\n", cache->difficulty);    printf("  difficulty: %.1f\n", cache->difficulty);
# Line 326  static float xml_get_prop_float(xmlTextR Line 341  static float xml_get_prop_float(xmlTextR
341    return ret;    return ret;
342  }  }
343    
344    static int xml_get_prop_int(xmlTextReaderPtr reader, char *name) {
345      int ret = -1;
346      char *prop;
347      if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST name))) {
348        ret = atoi(prop);
349        xmlFree(prop);
350      }
351      return ret;
352    }
353    
354    static unsigned int xml_get_prop_id(xmlTextReaderPtr reader) {
355      unsigned int ret = 0;
356      char *prop;
357      if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id"))) {
358        ret = atoi(prop);
359        xmlFree(prop);
360      }
361      return ret;
362    }
363    
364  static int xml_prop_is(xmlTextReaderPtr reader, char *name, char *value,  static int xml_prop_is(xmlTextReaderPtr reader, char *name, char *value,
365                         int def_value) {                         int def_value) {
366    int match = def_value;    int match = def_value;
# Line 360  static gboolean skip_element(xmlTextRead Line 395  static gboolean skip_element(xmlTextRead
395    
396  static char *process_text(xmlTextReaderPtr reader) {  static char *process_text(xmlTextReaderPtr reader) {
397    char *text = NULL;    char *text = NULL;
   int depth = xmlTextReaderDepth(reader);  
   int ret = xmlTextReaderRead(reader);  
   while((ret == 1) &&  
         ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) ||  
          (xmlTextReaderDepth(reader) != depth))) {  
398    
399      /* found a text fragment */    if(!xmlTextReaderIsEmptyElement(reader)) {
400      if((xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) ||  
401         (xmlTextReaderNodeType(reader) == XML_READER_TYPE_CDATA)) {      int depth = xmlTextReaderDepth(reader);
402        char *frag = (char*)xmlTextReaderConstValue(reader);      int ret = xmlTextReaderRead(reader);
403        while((ret == 1) &&
404        if(!text) text = strdup(frag);            ((xmlTextReaderNodeType(reader) != XML_READER_TYPE_END_ELEMENT) ||
405        else {             (xmlTextReaderDepth(reader) != depth))) {
406          char *old = text;  
407          text = malloc(strlen(old) + strlen(frag) + 1);        /* found a text fragment */
408          strcpy(text, old);        if((xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) ||
409          strcat(text, frag);           (xmlTextReaderNodeType(reader) == XML_READER_TYPE_CDATA)) {
410          free(old);          char *frag = (char*)xmlTextReaderConstValue(reader);
411    
412            if(!text) text = strdup(frag);
413            else {
414              char *old = text;
415              text = malloc(strlen(old) + strlen(frag) + 1);
416              strcpy(text, old);
417              strcat(text, frag);
418              free(old);
419            }
420        }        }
421          ret = xmlTextReaderRead(reader);
422      }      }
     ret = xmlTextReaderRead(reader);  
423    }    }
424    
425    return text;    return text;
# Line 415  static void xml_get_date(xmlTextReaderPt Line 454  static void xml_get_date(xmlTextReaderPt
454    }    }
455  }  }
456    
457  static log_t *process_gpx_wpt_gc_logs_log(xmlTextReaderPtr reader) {  static log_t *process_gpx_wpt_gc_logs_log(xmlTextReaderPtr reader,
458                                              char *username, cache_t *cache) {
459    
460      gboolean by_user = FALSE;
461    
462    if(xmlTextReaderIsEmptyElement(reader))    if(xmlTextReaderIsEmptyElement(reader))
463      return NULL;      return NULL;
464    
465    /* create a new log entry */    /* create a new log entry */
466    log_t *log = malloc(sizeof(log_t));    log_t *log = g_new0(log_t, 1);
467    memset(log, 0, sizeof(log_t));  
468      char *prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id");
469      if(prop)
470        log->id = atoi(prop);
471    
472    /* process all sub-nodes */    /* process all sub-nodes */
473    int depth = xmlTextReaderDepth(reader);    int depth = xmlTextReaderDepth(reader);
# Line 442  static log_t *process_gpx_wpt_gc_logs_lo Line 487  static log_t *process_gpx_wpt_gc_logs_lo
487            log->type = xml_str_search(reader, log_type_str, "log", 0);            log->type = xml_str_search(reader, log_type_str, "log", 0);
488          } else if((strcasecmp(name, "finder") == 0) ||          } else if((strcasecmp(name, "finder") == 0) ||
489                    (strcasecmp(name, "geocacher") == 0)) {                    (strcasecmp(name, "geocacher") == 0)) {
490            if(!log->finder) log->finder = process_text(reader);            if(!log->finder) {
491                log->finder = g_new0(user_t, 1);
492                log->finder->name = process_text(reader);
493                log->finder->id = xml_get_prop_id(reader);
494    
495                /* is this a log by us? */
496                if(username && !strcasecmp(username, log->finder->name))
497                  by_user = TRUE;
498              }
499          } else if(strcasecmp(name, "text") == 0) {          } else if(strcasecmp(name, "text") == 0) {
500            if(!log->text) log->text = process_text(reader);            if(!log->text) log->text = process_text(reader);
501          } else          } else
# Line 452  static log_t *process_gpx_wpt_gc_logs_lo Line 505  static log_t *process_gpx_wpt_gc_logs_lo
505        break;        break;
506    
507      case XML_READER_TYPE_END_ELEMENT:      case XML_READER_TYPE_END_ELEMENT:
508          if(by_user && log->type == LOG_TYPE_FOUND)
509            cache->found = TRUE;
510    
511        /* end element must be for the current element */        /* end element must be for the current element */
512        g_assert(xmlTextReaderDepth(reader) == depth);        g_assert(xmlTextReaderDepth(reader) == depth);
513        return log;        return log;
# Line 463  static log_t *process_gpx_wpt_gc_logs_lo Line 519  static log_t *process_gpx_wpt_gc_logs_lo
519      ret = xmlTextReaderRead(reader);      ret = xmlTextReaderRead(reader);
520    }    }
521    
522    g_assert(0);    gpx_free_log(log);
523    return log;    return NULL;
524  }  }
525    
526  static log_t *process_gpx_wpt_gc_logs(xmlTextReaderPtr reader) {  static log_t *process_gpx_wpt_gc_logs(xmlTextReaderPtr reader, char *username,
527                                          cache_t *cache) {
528    log_t *log_chain = NULL;    log_t *log_chain = NULL;
529    
530    if(xmlTextReaderIsEmptyElement(reader))    if(xmlTextReaderIsEmptyElement(reader))
# Line 485  static log_t *process_gpx_wpt_gc_logs(xm Line 542  static log_t *process_gpx_wpt_gc_logs(xm
542        if(strrchr(name, ':')) name = strrchr(name, ':')+1;        if(strrchr(name, ':')) name = strrchr(name, ':')+1;
543        if(name) {        if(name) {
544          if(strcasecmp(name, "log") == 0) {          if(strcasecmp(name, "log") == 0) {
545            log_t *log = process_gpx_wpt_gc_logs_log(reader);            log_t *log = process_gpx_wpt_gc_logs_log(reader, username, cache);
546            if(log) {            if(log) {
547              /* add log to chain */              /* add log to chain */
548              log_t **cur = &log_chain;              log_t **cur = &log_chain;
# Line 513  static log_t *process_gpx_wpt_gc_logs(xm Line 570  static log_t *process_gpx_wpt_gc_logs(xm
570      ret = xmlTextReaderRead(reader);      ret = xmlTextReaderRead(reader);
571    }    }
572    
573    g_assert(0);    /* free the entire log chain */
574    return log_chain;    while(log_chain) {
575        log_t *next = log_chain->next;
576        gpx_free_log(log_chain);
577        log_chain = next;
578      }
579    
580      return NULL;
581  }  }
582    
583  static tb_t *process_gpx_wpt_gc_tbs_travelbug(xmlTextReaderPtr reader) {  static tb_t *process_gpx_wpt_gc_tbs_travelbug(xmlTextReaderPtr reader) {
# Line 532  static tb_t *process_gpx_wpt_gc_tbs_trav Line 595  static tb_t *process_gpx_wpt_gc_tbs_trav
595    else    else
596      tb->ref = strdup("<NONE>");      tb->ref = strdup("<NONE>");
597    
598      if((prop = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "id")))
599        tb->id = atoi(prop);
600    
601    /* process all sub-nodes */    /* process all sub-nodes */
602    int depth = xmlTextReaderDepth(reader);    int depth = xmlTextReaderDepth(reader);
603    int ret = xmlTextReaderRead(reader);    int ret = xmlTextReaderRead(reader);
# Line 563  static tb_t *process_gpx_wpt_gc_tbs_trav Line 629  static tb_t *process_gpx_wpt_gc_tbs_trav
629      ret = xmlTextReaderRead(reader);      ret = xmlTextReaderRead(reader);
630    }    }
631    
632    g_assert(0);    gpx_free_tb(tb);
633    return tb;    return NULL;
634  }  }
635    
636  static tb_t *process_gpx_wpt_gc_tbs(xmlTextReaderPtr reader) {  static tb_t *process_gpx_wpt_gc_tbs(xmlTextReaderPtr reader) {
# Line 605  static tb_t *process_gpx_wpt_gc_tbs(xmlT Line 671  static tb_t *process_gpx_wpt_gc_tbs(xmlT
671      ret = xmlTextReaderRead(reader);      ret = xmlTextReaderRead(reader);
672    }    }
673    
674    g_assert(0);    while(tb) {
675    return tb;      tb_t *next = tb;
676        gpx_free_tb(tb);
677        tb = next;
678      }
679    
680      return NULL;
681    }
682    
683    static void process_gpx_wpt_gc_atts(xmlTextReaderPtr reader,
684                                        cache_t *cache) {
685      if(xmlTextReaderIsEmptyElement(reader))
686        return;
687    
688      attribute_t **att = &cache->attribute;
689      while(*att) att = &(*att)->next;
690    
691      /* process all sub-nodes */
692      int depth = xmlTextReaderDepth(reader);
693      int ret = xmlTextReaderRead(reader);
694      while(ret == 1) {
695    
696        switch(xmlTextReaderNodeType(reader)) {
697        case XML_READER_TYPE_ELEMENT:
698          g_assert(xmlTextReaderDepth(reader) == depth+1);
699          char *name = (char*)xmlTextReaderConstName(reader);
700          if(strrchr(name, ':')) name = strrchr(name, ':')+1;
701          if(name) {
702            if(strcasecmp(name, "attribute") == 0) {
703              *att = g_new0(attribute_t, 1);
704              (*att)->name = process_text(reader);
705              (*att)->id = xml_get_prop_id(reader);
706              if((*att)->id < 0 || (*att)->id > ATT_MAX)
707                (*att)->id = ATT_UNKNOWN;
708              (*att)->inc = xml_get_prop_int(reader, "inc");
709    
710              att = &(*att)->next;
711            } else {
712              // printf("unhandled item found: gpx/wpt/cache/attributes/%s\n", name);
713              skip_element(reader);
714            }
715          } else
716            skip_element(reader);
717          break;
718    
719        case XML_READER_TYPE_END_ELEMENT:
720          /* end element must be for the current element */
721          g_assert(xmlTextReaderDepth(reader) == depth);
722          return;
723          break;
724    
725        default:
726          break;
727        }
728        ret = xmlTextReaderRead(reader);
729      }
730  }  }
731    
732  static void process_gpx_wpt_gc(xmlTextReaderPtr reader, cache_t *cache) {  static void process_gpx_wpt_gc(xmlTextReaderPtr reader,
733                                   cache_t *cache, char *username) {
734    cache->available = xml_prop_is(reader, "available", "true", TRUE);    cache->available = xml_prop_is(reader, "available", "true", TRUE);
735    cache->archived = xml_prop_is(reader, "archived", "true", FALSE);    cache->archived = xml_prop_is(reader, "archived", "true", FALSE);
736    
# Line 630  static void process_gpx_wpt_gc(xmlTextRe Line 751  static void process_gpx_wpt_gc(xmlTextRe
751          if(strcasecmp(name, "name") == 0) {          if(strcasecmp(name, "name") == 0) {
752            if(!cache->name) cache->name = process_text(reader);            if(!cache->name) cache->name = process_text(reader);
753          } else if(strcasecmp(name, "owner") == 0) {          } else if(strcasecmp(name, "owner") == 0) {
754            if(!cache->owner) cache->owner = process_text(reader);            if(!cache->owner) {
755                cache->owner = g_new0(user_t, 1);
756                cache->owner->name = process_text(reader);
757                cache->owner->id = xml_get_prop_id(reader);
758    
759                if(username && !strcasecmp(cache->owner->name, username))
760                  cache->mine = TRUE;
761              }
762          } else if(strcasecmp(name, "type") == 0) {          } else if(strcasecmp(name, "type") == 0) {
763            cache->type = xml_str_search(reader, cache_type_str,            cache->type = xml_str_search(reader, cache_type_str,
764                                         "cache type", CACHE_TYPE_UNKNOWN);                                         "cache type", CACHE_TYPE_UNKNOWN);
765          } else if(strcasecmp(name, "container") == 0) {          } else if(strcasecmp(name, "container") == 0) {
766            cache->container = xml_str_search(reader, cache_container_str,            cache->container = xml_str_search(reader, cache_container_str,
767                                              "container", CACHE_CONT_UNKNOWN);                                              "container", CACHE_CONT_UNKNOWN);
768            } else if(strcasecmp(name, "attributes") == 0) {
769              process_gpx_wpt_gc_atts(reader, cache);
770          } else if((strcasecmp(name, "short_description") == 0) ||          } else if((strcasecmp(name, "short_description") == 0) ||
771                    (strcasecmp(name, "summary") == 0))   {                    (strcasecmp(name, "summary") == 0))   {
772            if(!cache->short_description) {            if(!cache->short_description) {
# Line 653  static void process_gpx_wpt_gc(xmlTextRe Line 783  static void process_gpx_wpt_gc(xmlTextRe
783                    (strcasecmp(name, "hints") == 0))     {                    (strcasecmp(name, "hints") == 0))     {
784            if(!cache->hint) {            if(!cache->hint) {
785              cache->hint = process_text(reader);              cache->hint = process_text(reader);
786    
787              /* often hints aren't more than just a bunch of blanks ... */              /* often hints aren't more than just a bunch of blanks ... */
788              if(cache->hint && all_is_white(cache->hint)) {              if(cache->hint && all_is_white(cache->hint)) {
789                free(cache->hint);                free(cache->hint);
# Line 665  static void process_gpx_wpt_gc(xmlTextRe Line 796  static void process_gpx_wpt_gc(xmlTextRe
796          } else if(strcasecmp(name, "terrain") == 0) {          } else if(strcasecmp(name, "terrain") == 0) {
797            cache->terrain = xml_float(reader, 0.0);            cache->terrain = xml_float(reader, 0.0);
798          } else if(strcasecmp(name, "logs") == 0) {          } else if(strcasecmp(name, "logs") == 0) {
799            if(!cache->log) cache->log = process_gpx_wpt_gc_logs(reader);            if(!cache->log) cache->log =
800                        process_gpx_wpt_gc_logs(reader, username, cache);
801          } else if(strcasecmp(name, "travelbugs") == 0) {          } else if(strcasecmp(name, "travelbugs") == 0) {
802            if(!cache->tb) cache->tb = process_gpx_wpt_gc_tbs(reader);            if(!cache->tb) cache->tb = process_gpx_wpt_gc_tbs(reader);
803          } else {          } else {
# Line 691  static void process_gpx_wpt_gc(xmlTextRe Line 823  static void process_gpx_wpt_gc(xmlTextRe
823    
824  /* parse waypoint entry */  /* parse waypoint entry */
825  static cache_t *process_gpx_wpt(xmlTextReaderPtr reader,  gpx_dialog_t *dialog,  static cache_t *process_gpx_wpt(xmlTextReaderPtr reader,  gpx_dialog_t *dialog,
826                                  gpx_t *gpx) {                                  gpx_t *gpx, char *username) {
827    char *cmt = NULL, *desc = NULL;    char *cmt = NULL, *desc = NULL;
828    char *sym = NULL;    char *sym = NULL;
829    
# Line 731  static cache_t *process_gpx_wpt(xmlTextR Line 863  static cache_t *process_gpx_wpt(xmlTextR
863            if(!cache->url) cache->url = process_text(reader);            if(!cache->url) cache->url = process_text(reader);
864          } else if((strcasecmp(name, "cache") == 0) ||          } else if((strcasecmp(name, "cache") == 0) ||
865                    (strcasecmp(name, "geocache") == 0)) {                    (strcasecmp(name, "geocache") == 0)) {
866            process_gpx_wpt_gc(reader, cache);            process_gpx_wpt_gc(reader, cache, username);
867    
868            /* the following are used if the current entry is a waypoint */            /* the following are used if the current entry is a waypoint */
869          } else if(strcasecmp(name, "cmt") == 0) {          } else if(strcasecmp(name, "cmt") == 0) {
# Line 831  static cache_t *process_gpx_wpt(xmlTextR Line 963  static cache_t *process_gpx_wpt(xmlTextR
963      ret = xmlTextReaderRead(reader);      ret = xmlTextReaderRead(reader);
964    }    }
965    
966    g_assert(0);    gpx_free_cache(cache);
967    return cache;    return NULL;
968  }  }
969    
970  static void process_gpx(xmlTextReaderPtr reader, gpx_dialog_t *dialog,  static gboolean process_gpx(xmlTextReaderPtr reader, gpx_dialog_t *dialog,
971                          gpx_t *gpx) {                              gpx_t *gpx, char *username) {
972    
973    /* no attributes of interest */    /* no attributes of interest */
974    
# Line 847  static void process_gpx(xmlTextReaderPtr Line 979  static void process_gpx(xmlTextReaderPtr
979    while(*cache) cache = &(*cache)->next;    while(*cache) cache = &(*cache)->next;
980    
981    const xmlChar *name = xmlTextReaderConstName(reader);    const xmlChar *name = xmlTextReaderConstName(reader);
982    g_assert(name);    if(!name) return FALSE;
983    
984    /* read next node */    /* read next node */
985    int ret = xmlTextReaderRead(reader);    int ret = xmlTextReaderRead(reader);
# Line 866  static void process_gpx(xmlTextReaderPtr Line 998  static void process_gpx(xmlTextReaderPtr
998                           (strcasecmp(name, "date") == 0))) {                           (strcasecmp(name, "date") == 0))) {
999          xml_get_date(reader, &gpx->year, &gpx->month, &gpx->day);          xml_get_date(reader, &gpx->year, &gpx->month, &gpx->day);
1000        } else if(name && strcasecmp(name, "wpt") == 0) {        } else if(name && strcasecmp(name, "wpt") == 0) {
1001          *cache = process_gpx_wpt(reader, dialog, gpx);          *cache = process_gpx_wpt(reader, dialog, gpx, username);
1002          if(*cache) cache = &(*cache)->next;          if(*cache) cache = &(*cache)->next;
1003        } else {        } else {
1004          //      printf("something unknown (%s) found\n", name);          //      printf("something unknown (%s) found\n", name);
# Line 877  static void process_gpx(xmlTextReaderPtr Line 1009  static void process_gpx(xmlTextReaderPtr
1009      case XML_READER_TYPE_END_ELEMENT:      case XML_READER_TYPE_END_ELEMENT:
1010        /* end element must be for the current element */        /* end element must be for the current element */
1011        g_assert(xmlTextReaderDepth(reader) == 0);        g_assert(xmlTextReaderDepth(reader) == 0);
1012        return;        return TRUE;
1013        break;        break;
1014    
1015      default:      default:
# Line 886  static void process_gpx(xmlTextReaderPtr Line 1018  static void process_gpx(xmlTextReaderPtr
1018      ret = xmlTextReaderRead(reader);      ret = xmlTextReaderRead(reader);
1019    }    }
1020    
1021    g_assert(0);    return FALSE;
1022  }  }
1023    
1024  /* parse loc waypoint entry */  /* parse loc waypoint entry */
# Line 993  static void process_loc(xmlTextReaderPtr Line 1125  static void process_loc(xmlTextReaderPtr
1125  }  }
1126    
1127  static gpx_t *process_root(xmlTextReaderPtr reader, gpx_dialog_t *dialog,  static gpx_t *process_root(xmlTextReaderPtr reader, gpx_dialog_t *dialog,
1128                             char *fname, gpx_t *in) {                             char *fname, gpx_t *in, char *username) {
1129    
1130    /* no gpx entry given, create a new one */    /* no gpx entry given, create a new one */
1131    gpx_t *gpx = NULL;    gpx_t *gpx = NULL;
# Line 1012  static gpx_t *process_root(xmlTextReader Line 1144  static gpx_t *process_root(xmlTextReader
1144        g_assert(xmlTextReaderDepth(reader) == 0);        g_assert(xmlTextReaderDepth(reader) == 0);
1145        char *name = (char*)xmlTextReaderConstName(reader);        char *name = (char*)xmlTextReaderConstName(reader);
1146        if(name && strcasecmp(name, "gpx") == 0) {        if(name && strcasecmp(name, "gpx") == 0) {
1147          process_gpx(reader, dialog, gpx);          process_gpx(reader, dialog, gpx, username);
1148        } else if(name && strcasecmp(name, "loc") == 0) {        } else if(name && strcasecmp(name, "loc") == 0) {
1149          process_loc(reader, dialog, gpx);          process_loc(reader, dialog, gpx);
1150        } else {        } else {
# Line 1048  static gpx_t *process_root(xmlTextReader Line 1180  static gpx_t *process_root(xmlTextReader
1180    return gpx;    return gpx;
1181  }  }
1182    
1183  static gpx_t *gpx_parse_file(gpx_dialog_t *dialog, char *filename) {  static gpx_t *gpx_parse_file(gpx_dialog_t *dialog,
1184                                 char *filename, char *username) {
1185    gpx_t *gpx = NULL;    gpx_t *gpx = NULL;
1186    
1187    LIBXML_TEST_VERSION;    LIBXML_TEST_VERSION;
# Line 1057  static gpx_t *gpx_parse_file(gpx_dialog_ Line 1190  static gpx_t *gpx_parse_file(gpx_dialog_
1190    
1191    xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0);    xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0);
1192    if (reader != NULL) {    if (reader != NULL) {
1193      gpx = process_root(reader, dialog, filename, NULL);      gpx = process_root(reader, dialog, filename, NULL, username);
1194      xmlFreeTextReader(reader);      xmlFreeTextReader(reader);
1195    } else {    } else {
1196      fprintf(stderr, "Unable to open %s\n", filename);      fprintf(stderr, "Unable to open %s\n", filename);
# Line 1073  static gpx_t *gpx_parse_file(gpx_dialog_ Line 1206  static gpx_t *gpx_parse_file(gpx_dialog_
1206      if(g_file_test(wpts_name,  G_FILE_TEST_EXISTS)) {      if(g_file_test(wpts_name,  G_FILE_TEST_EXISTS)) {
1207        xmlTextReaderPtr reader = xmlReaderForFile(wpts_name, NULL, 0);        xmlTextReaderPtr reader = xmlReaderForFile(wpts_name, NULL, 0);
1208        if (reader != NULL) {        if (reader != NULL) {
1209          gpx = process_root(reader, dialog, wpts_name, gpx);          gpx = process_root(reader, dialog, wpts_name, gpx, username);
1210          xmlFreeTextReader(reader);          xmlFreeTextReader(reader);
1211        } else {        } else {
1212          fprintf(stderr, "Unable to open %s\n", filename);          fprintf(stderr, "Unable to open %s\n", filename);
# Line 1086  static gpx_t *gpx_parse_file(gpx_dialog_ Line 1219  static gpx_t *gpx_parse_file(gpx_dialog_
1219    
1220  static gpx_t *decompress_file(unzFile file, gpx_dialog_t *dialog,  static gpx_t *decompress_file(unzFile file, gpx_dialog_t *dialog,
1221                                char *name, char *filename,                                char *name, char *filename,
1222                                gpx_t *gpx_in) {                                gpx_t *gpx_in, char *username) {
1223    unz_file_info info;    unz_file_info info;
1224    gpx_t *gpx = NULL;    gpx_t *gpx = NULL;
1225    
# Line 1130  static gpx_t *decompress_file(unzFile fi Line 1263  static gpx_t *decompress_file(unzFile fi
1263      xmlReaderForMemory(buffer, info.uncompressed_size,      xmlReaderForMemory(buffer, info.uncompressed_size,
1264                         NULL, NULL, 0);                         NULL, NULL, 0);
1265    if (reader != NULL) {    if (reader != NULL) {
1266      gpx = process_root(reader, dialog, filename, gpx_in);      gpx = process_root(reader, dialog, filename, gpx_in, username);
1267      xmlFreeTextReader(reader);      xmlFreeTextReader(reader);
1268    } else {    } else {
1269      fprintf(stderr, "Unable to open %s\n", filename);      fprintf(stderr, "Unable to open %s\n", filename);
# Line 1141  static gpx_t *decompress_file(unzFile fi Line 1274  static gpx_t *decompress_file(unzFile fi
1274    return gpx;    return gpx;
1275  }  }
1276    
1277  static gpx_t *decompress_zip(gpx_dialog_t *dialog, char *filename) {  static gpx_t *decompress_zip(gpx_dialog_t *dialog,
1278                                 char *filename, char *username) {
1279    char *gpx_name, *fbase;    char *gpx_name, *fbase;
1280    gpx_t *gpx = NULL;    gpx_t *gpx = NULL;
1281    
# Line 1165  static gpx_t *decompress_zip(gpx_dialog_ Line 1299  static gpx_t *decompress_zip(gpx_dialog_
1299    strcpy(gpx_name+strlen(gpx_name)-4, ".gpx");    strcpy(gpx_name+strlen(gpx_name)-4, ".gpx");
1300    printf("gpx file name is %s\n", gpx_name);    printf("gpx file name is %s\n", gpx_name);
1301    
1302    gpx = decompress_file(file, dialog, gpx_name, filename, NULL);    gpx = decompress_file(file, dialog, gpx_name, filename, NULL, username);
1303    
1304    /* try to open -wpts.gpx file inside */    /* try to open -wpts.gpx file inside */
1305    strcpy(gpx_name, fbase);    strcpy(gpx_name, fbase);
1306    strcpy(gpx_name+strlen(gpx_name)-4, "-wpts.gpx");    strcpy(gpx_name+strlen(gpx_name)-4, "-wpts.gpx");
1307    printf("gpx wpts file name is %s\n", gpx_name);    printf("gpx wpts file name is %s\n", gpx_name);
1308    
1309    gpx = decompress_file(file, dialog, gpx_name, filename, gpx);    gpx = decompress_file(file, dialog, gpx_name, filename, gpx, username);
1310    
1311    unzClose(file);    unzClose(file);
1312    free(gpx_name);    free(gpx_name);
1313    return gpx;    return gpx;
1314  }  }
1315    
1316  gpx_t *gpx_parse(gpx_dialog_t *dialog, char *filename) {  gpx_t *gpx_parse(gpx_dialog_t *dialog, char *filename, char *username) {
1317    gpx_t *gpx = NULL;    gpx_t *gpx = NULL;
1318    
1319    /* show busy dialog */    /* show busy dialog */
# Line 1189  gpx_t *gpx_parse(gpx_dialog_t *dialog, c Line 1323  gpx_t *gpx_parse(gpx_dialog_t *dialog, c
1323       !strcasecmp(filename+strlen(filename)-4, ".zip")) {       !strcasecmp(filename+strlen(filename)-4, ".zip")) {
1324      printf("trying to load a zip file!\n");      printf("trying to load a zip file!\n");
1325    
1326      gpx = decompress_zip(dialog, filename);      gpx = decompress_zip(dialog, filename, username);
1327    } else    } else
1328      gpx = gpx_parse_file(dialog, filename);      gpx = gpx_parse_file(dialog, filename, username);
1329    
1330    return gpx;    return gpx;
1331  }  }
1332    
1333  /* scan entire directory */  /* scan entire directory */
1334  gpx_t *gpx_parse_dir(gpx_dialog_t *dialog, char *dirname) {  gpx_t *gpx_parse_dir(gpx_dialog_t *dialog, char *dirname, char *username) {
1335    GnomeVFSResult result;    GnomeVFSResult result;
1336    GnomeVFSDirectoryHandle *handle;    GnomeVFSDirectoryHandle *handle;
1337    GnomeVFSFileInfo *finfo = gnome_vfs_file_info_new();;    GnomeVFSFileInfo *finfo = gnome_vfs_file_info_new();;
# Line 1234  gpx_t *gpx_parse_dir(gpx_dialog_t *dialo Line 1368  gpx_t *gpx_parse_dir(gpx_dialog_t *dialo
1368    
1369          xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0);          xmlTextReaderPtr reader = xmlReaderForFile(filename, NULL, 0);
1370          if (reader != NULL) {          if (reader != NULL) {
1371            gpx = process_root(reader, dialog, filename, gpx);            gpx = process_root(reader, dialog, filename, gpx, username);
1372            xmlFreeTextReader(reader);            xmlFreeTextReader(reader);
1373          } else {          } else {
1374            fprintf(stderr, "Unable to open %s\n", filename);            fprintf(stderr, "Unable to open %s\n", filename);
1375          }          }
1376    
1377          free(filename);          free(filename);
1378    
1379          } else if(strcasecmp(ext, ".zip") == 0) {
1380            char *filename = malloc(strlen(dirname)+strlen(finfo->name)+2);
1381    
1382            strcpy(filename, dirname);
1383            if(strlastchr(filename) != '/')
1384              strcat(filename, "/");
1385            strcat(filename, finfo->name);
1386    
1387            gpx_t *new_gpx = decompress_zip(dialog, filename, username);
1388            if(new_gpx) {
1389              if(!gpx) gpx = new_gpx;
1390              else {
1391                /* append all caches from "new_gpx" to gpx */
1392    
1393                cache_t **cache = &(gpx->cache);
1394                while(*cache) cache = &(*cache)->next;
1395    
1396                *cache = new_gpx->cache;
1397                new_gpx->cache = NULL;
1398                gpx_free(new_gpx);
1399              }
1400            }
1401    
1402            free(filename);
1403        }        }
1404      }      }
1405    }    }
# Line 1281  int gpx_total_caches(gpx_t *gpx) { Line 1440  int gpx_total_caches(gpx_t *gpx) {
1440    }    }
1441    
1442    return num;    return num;
1443    }
1444    
1445    /* return number of caches in all gpx files */
1446    int gpx_total_caches_global(gpx_t *gpx) {
1447      int num = 0;
1448      while(gpx) {
1449        num += gpx_total_caches(gpx);
1450        gpx = gpx->next;
1451      }
1452    
1453      return num;
1454  }  }
1455    
1456  int gpx_number_of_waypoints(wpt_t *wpt) {  int gpx_number_of_waypoints(wpt_t *wpt) {

Legend:
Removed from v.34  
changed lines
  Added in v.298