Index: Plug.tmpasm =================================================================== --- Plug.tmpasm (revision 7951) +++ Plug.tmpasm (revision 7952) @@ -3,6 +3,7 @@ $(PLUGDIR)/fp_wget/fp_wget.o $(PLUGDIR)/fp_wget/wget_common.o $(PLUGDIR)/fp_wget/gedasymbols.o + $(PLUGDIR)/fp_wget/edakrill.o @] put /local/pcb/mod/CONF {$(PLUGDIR)/fp_wget/fp_wget_conf.h} Index: edakrill.c =================================================================== --- edakrill.c (nonexistent) +++ edakrill.c (revision 7952) @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "wget_common.h" +#include "edakrill.h" +#include "plugins.h" +#include "plug_footprint.h" +#include "compat_misc.h" + +#define REQUIRE_PATH_PREFIX "wget@edakrill" + +#define ROOT_URL "http://www.repo.hu/projects/edakrill/" +#define FP_URL ROOT_URL "user/" + +static const char *url_idx_md5 = ROOT_URL "tags.idx.md5"; +static const char *url_idx_list = ROOT_URL "tags.idx"; +static const char *gedasym_cache = "fp_wget_cache"; +static const char *last_sum_fn = "fp_wget_cache/edakrill.last"; + +int fp_edakrill_load_dir(pcb_plug_fp_t *ctx, const char *path, int force) +{ + FILE *f; + int fctx; + char *md5_last, *md5_new; + char line[1024]; + fp_get_mode mode; + gds_t vpath; + int vpath_base_len; + fp_get_mode wmode = FP_WGET_OFFLINE; + pcb_fplibrary_t *l; + + if (strncmp(path, REQUIRE_PATH_PREFIX, strlen(REQUIRE_PATH_PREFIX)) != 0) + return -1; + + gds_init(&vpath); + gds_append_str(&vpath, REQUIRE_PATH_PREFIX); + + l = pcb_fp_mkdir_p(vpath.array); + if (l != NULL) + l->data.dir.backend = ctx; + + if (force || (conf_fp_wget.plugins.fp_wget.auto_update_edakrill)) + wmode &= ~FP_WGET_OFFLINE; + + if (fp_wget_open(url_idx_md5, gedasym_cache, &f, &fctx, wmode) != 0) { + if (wmode & FP_WGET_OFFLINE) /* accept that we don't have the index in offline mode */ + goto quit; + goto err; + } + + md5_new = load_md5_sum(f); + fp_wget_close(&f, &fctx); + + if (md5_new == NULL) + goto err; + + f = fopen(last_sum_fn, "r"); + md5_last = load_md5_sum(f); + if (f != NULL) + fclose(f); + + printf("old='%s' new='%s'\n", md5_last, md5_new); + + if (!md5_cmp_free(last_sum_fn, md5_last, md5_new)) { + printf("no chg.\n"); + mode = FP_WGET_OFFLINE; /* use the cache */ + } + else + mode = 0; + + if (fp_wget_open(url_idx_list, gedasym_cache, &f, &fctx, mode) != 0) { + printf("failed to download the new list\n"); + remove(last_sum_fn); /* make sure it is downloaded next time */ + goto err; + } + + gds_append(&vpath, '/'); + vpath_base_len = vpath.used; + + while(fgets(line, sizeof(line), f) != NULL) { + char *end, *fn; + + if (*line == '#') + continue; + end = strchr(line, '|'); + if (end == NULL) + continue; + *end = '\0'; + + /* split path and fn; path stays in vpath.array, fn is a ptr to the file name */ + gds_truncate(&vpath, vpath_base_len); + gds_append_str(&vpath, line); + end = vpath.array + vpath.used - 1; + while((end > vpath.array) && (*end != '/')) { end--; vpath.used--; } + *end = '\0'; + vpath.used--; + end++; + fn = end; + + /* add to the database */ + l = pcb_fp_mkdir_p(vpath.array); + l = pcb_fp_append_entry(l, fn, PCB_FP_FILE, NULL); + fn[-1] = '/'; + l->data.fp.loc_info = pcb_strdup(vpath.array); + } + fp_wget_close(&f, &fctx); + + printf("update!\n"); + + quit:; + gds_uninit(&vpath); + return 0; + + err:; + gds_uninit(&vpath); + return -1; +} + +#define FIELD_WGET_CTX 0 + +FILE *fp_edakrill_fopen(pcb_plug_fp_t *ctx, const char *path, const char *name, pcb_fp_fopen_ctx_t *fctx) +{ + gds_t s; + FILE *f; + + if (strncmp(name, REQUIRE_PATH_PREFIX, strlen(REQUIRE_PATH_PREFIX)) == 0) + name+=strlen(REQUIRE_PATH_PREFIX); + else + return NULL; + + if (*name == '/') + name++; + + gds_init(&s); + gds_append_str(&s, FP_URL); + gds_append_str(&s, name); + + fp_wget_open(s.array, gedasym_cache, &f, &(fctx->field[FIELD_WGET_CTX].i), FP_WGET_UPDATE); + + gds_uninit(&s); + return f; +} + +void fp_edakrill_fclose(pcb_plug_fp_t *ctx, FILE * f, pcb_fp_fopen_ctx_t *fctx) +{ + fp_wget_close(&f, &(fctx->field[FIELD_WGET_CTX].i)); +} + + +static pcb_plug_fp_t fp_edakrill; + +void fp_edakrill_uninit(void) +{ + PCB_HOOK_UNREGISTER(pcb_plug_fp_t, pcb_plug_fp_chain, &fp_edakrill); +} + +void fp_edakrill_init(void) +{ + fp_edakrill.plugin_data = NULL; + fp_edakrill.load_dir = fp_edakrill_load_dir; + fp_edakrill.fopen = fp_edakrill_fopen; + fp_edakrill.fclose = fp_edakrill_fclose; + + PCB_HOOK_REGISTER(pcb_plug_fp_t, pcb_plug_fp_chain, &fp_edakrill); +} Index: edakrill.h =================================================================== --- edakrill.h (nonexistent) +++ edakrill.h (revision 7952) @@ -0,0 +1,8 @@ +#include "plug_footprint.h" +#include "fp_wget_conf.h" +int fp_edakrill_load_dir(pcb_plug_fp_t *ctx, const char *path, int force); +FILE *fp_edakrill_fopen(pcb_plug_fp_t *ctx, const char *path, const char *name, pcb_fp_fopen_ctx_t *fctx); +void fp_edakrill_fclose(pcb_plug_fp_t *ctx, FILE * f, pcb_fp_fopen_ctx_t *fctx); +void fp_edakrill_init(void); +void fp_edakrill_uninit(void); + Index: fp_wget.c =================================================================== --- fp_wget.c (revision 7951) +++ fp_wget.c (revision 7952) @@ -1,5 +1,6 @@ #include "config.h" #include "gedasymbols.h" +#include "edakrill.h" #include "plugins.h" #include "fp_wget_conf.h" @@ -17,5 +18,6 @@ #include "fp_wget_conf_fields.h" fp_gedasymbols_init(); + fp_edakrill_init(); return hid_fp_wget_uninit; } Index: fp_wget_conf.h =================================================================== --- fp_wget_conf.h (revision 7951) +++ fp_wget_conf.h (revision 7952) @@ -7,6 +7,7 @@ const struct plugins { const struct fp_wget { CFT_BOOLEAN auto_update_gedasymbols; /* update the index of gedasymbols on startup automatically */ + CFT_BOOLEAN auto_update_edakrill; /* update the index of edakrill on startup automatically */ } fp_wget; } plugins; } conf_fp_wget_t; Index: gedasymbols.c =================================================================== --- gedasymbols.c (revision 7951) +++ gedasymbols.c (revision 7952) @@ -21,53 +21,6 @@ static const char *gedasym_cache = "fp_wget_cache"; static const char *last_sum_fn = "fp_wget_cache/gedasymbols.last"; -static char *load_md5_sum(FILE *f) -{ - char *s, sum[64]; - - if (f == NULL) - return NULL; - - *sum = '\0'; - fgets(sum, sizeof(sum), f); - sum[sizeof(sum)-1] = '\0'; - - for(s = sum;; s++) { - if ((*s == '\0') || (isspace(*s))) { - if ((s - sum) == 32) { - *s = '\0'; - return pcb_strdup(sum); - } - else - return NULL; - } - if (isdigit(*s)) - continue; - if ((*s >= 'a') && (*s <= 'f')) - continue; - if ((*s >= 'A') && (*s <= 'F')) - continue; - return NULL; - } -} - -static int md5_cmp_free(const char *last_fn, char *md5_last, char *md5_new) -{ - int changed = 0; - - if ((md5_last == NULL) || (strcmp(md5_last, md5_new) != 0)) { - FILE *f; - f = fopen(last_fn, "w"); - fputs(md5_new, f); - fclose(f); - changed = 1; - } - if (md5_last != NULL) - free(md5_last); - free(md5_new); - return changed; -} - int fp_gedasymbols_load_dir(pcb_plug_fp_t *ctx, const char *path, int force) { FILE *f; Index: wget_common.c =================================================================== --- wget_common.c (revision 7951) +++ wget_common.c (revision 7952) @@ -106,3 +106,51 @@ return -1; } + + +char *load_md5_sum(FILE *f) +{ + char *s, sum[64]; + + if (f == NULL) + return NULL; + + *sum = '\0'; + fgets(sum, sizeof(sum), f); + sum[sizeof(sum)-1] = '\0'; + + for(s = sum;; s++) { + if ((*s == '\0') || (isspace(*s))) { + if ((s - sum) == 32) { + *s = '\0'; + return pcb_strdup(sum); + } + else + return NULL; + } + if (isdigit(*s)) + continue; + if ((*s >= 'a') && (*s <= 'f')) + continue; + if ((*s >= 'A') && (*s <= 'F')) + continue; + return NULL; + } +} + +int md5_cmp_free(const char *last_fn, char *md5_last, char *md5_new) +{ + int changed = 0; + + if ((md5_last == NULL) || (strcmp(md5_last, md5_new) != 0)) { + FILE *f; + f = fopen(last_fn, "w"); + fputs(md5_new, f); + fclose(f); + changed = 1; + } + if (md5_last != NULL) + free(md5_last); + free(md5_new); + return changed; +} Index: wget_common.h =================================================================== --- wget_common.h (revision 7951) +++ wget_common.h (revision 7952) @@ -7,3 +7,6 @@ int fp_wget_open(const char *url, const char *cache_path, FILE **f, int *fctx, fp_get_mode mode); int fp_wget_close(FILE **f, int *fctx); + +char *load_md5_sum(FILE *f); +int md5_cmp_free(const char *last_fn, char *md5_last, char *md5_new);