Index: trunk/src_plugins/import_mentor_sch/mentor_parts.map =================================================================== --- trunk/src_plugins/import_mentor_sch/mentor_parts.map (nonexistent) +++ trunk/src_plugins/import_mentor_sch/mentor_parts.map (revision 7423) @@ -0,0 +1,13 @@ +# Format: +# - one rulr per line +# - field separator is | +# - fields: +# 1: priority: if multiple rules match for a givem part attrib, pick the highest prio only +# * means "ignore priority, always execute" +# 2: part attribute name regex - consider the rule only if the part has a matching attribute name +# 3: part attribute value regex - consider the rule only if value matches; back referenced from add-value +# 4: add attribute name: add or overwrite attribute on rule match +# 5: add attribute value: use value for the new attribute; built with back references to part attribute regex + +# plain headers +10|Part Name|Dummy_HDR ([0-9]+)x([0-9]+) P:([0-9.]+mm)|footprint|connector(\1, \2, \3) Index: trunk/src_plugins/import_mentor_sch/mentor_sch.c =================================================================== --- trunk/src_plugins/import_mentor_sch/mentor_sch.c (revision 7422) +++ trunk/src_plugins/import_mentor_sch/mentor_sch.c (revision 7423) @@ -127,6 +127,7 @@ int res = 0; nethlp_new(&nhctx); + nethlp_load_part_map(&nhctx, "mentor_parts.map"); for(contents = view->children; contents != NULL; contents = contents->next) { if (strcmp(contents->str, "contents") == 0) { Index: trunk/src_plugins/import_mentor_sch/netlist_helper.c =================================================================== --- trunk/src_plugins/import_mentor_sch/netlist_helper.c (revision 7422) +++ trunk/src_plugins/import_mentor_sch/netlist_helper.c (revision 7423) @@ -26,6 +26,7 @@ #include #include +#include #include #include "netlist_helper.h" @@ -43,6 +44,8 @@ } else prealloc->alloced = 0; + + prealloc->part_rules = NULL; htsp_init(&prealloc->id2refdes, strhash, strkeyeq); return prealloc; } @@ -50,6 +53,17 @@ void nethlp_destroy(nethlp_ctx_t *nhctx) { htsp_entry_t *e; + nethlp_rule_t *r, *next; + + for(r = nhctx->part_rules; r != NULL; r = next) { + next = r->next; + re_se_free(r->key); + re_se_free(r->val); + free(r->new_key); + free(r->new_val); + free(r); + } + for (e = htsp_first(&nhctx->id2refdes); e; e = htsp_next(&nhctx->id2refdes, e)) { free(e->key); free(e->value); @@ -59,7 +73,100 @@ free(nhctx); } +#define ltrim(s) while(isspace(*s)) s++; +static int part_map_split(char *s, char *argv[], int maxargs) +{ + int argc; + int n; + + ltrim(s); + if ((*s == '#') || (*s == '\0')) + return 0; + + for(argc = n = 0; n < maxargs; n++) { + argv[argc] = s; + if (s != NULL) { + argc++; + s = strchr(s, '|'); + if (s != NULL) { + *s = '\0'; + s++; + } + } + } + return argc; +} + +static int part_map_parse(nethlp_ctx_t *nhctx, int argc, char *argv[], const char *fn, int lineno) +{ + char *end; + nethlp_rule_t *r; + re_se_t *kr, *vr; + int prio; + + if (argc != 5) { + pcb_message(PCB_MSG_ERROR, "Loading part map: wrong number of fields %d in %s:%d - expected 5 - ignoring this rule\n", argc, fn, lineno); + return -1; + } + if (*argv[0] != '*') { + prio = strtol(argv[0], &end, 10); + if (*end != '\0') { + pcb_message(PCB_MSG_ERROR, "Loading part map: invaid priority '%s' in %s:%d - ignoring this rule\n", argv[0], fn, lineno); + return -1; + } + } + else + prio = nethlp_prio_always; + kr = re_se_comp(argv[1]); + if (kr == NULL) { + pcb_message(PCB_MSG_ERROR, "Loading part map: can't compile attribute name regex in %s:%d - ignoring this rule\n", fn, lineno); + return -1; + } + vr = re_se_comp(argv[2]); + if (vr == NULL) { + re_se_free(kr); + pcb_message(PCB_MSG_ERROR, "Loading part map: can't compile attribute value regex in %s:%d - ignoring this rule\n", fn, lineno); + return -1; + } + + r = malloc(sizeof(nethlp_rule_t)); + r->prio = prio; + r->key = kr; + r->val = vr; + r->new_key = pcb_strdup(argv[3]); + r->new_val = pcb_strdup(argv[4]); + r->next = nhctx->part_rules; + nhctx->part_rules = r; + + return 0; +} + +int nethlp_load_part_map(nethlp_ctx_t *nhctx, const char *fn) +{ + FILE *f; + int cnt, argc, lineno; + char line[1024], *argv[8]; + + f = fopen(fn, "r"); + if (f == NULL) + return -1; + + lineno = 0; + while(fgets(line, sizeof(line), f) != NULL) { + lineno++; + argc = part_map_split(line, argv, 6); + if ((argc > 0) && (part_map_parse(nhctx, argc, argv, fn, lineno) == 0)) { +/* printf("MAP %d '%s' '%s' '%s' '%s' '%s'\n", argc, argv[0], argv[1], argv[2], argv[3], argv[4]);*/ + cnt++; + } + } + + fclose(f); + return cnt; +} + + nethlp_elem_ctx_t *nethlp_elem_new(nethlp_ctx_t *nhctx, nethlp_elem_ctx_t *prealloc, const char *id) { if (prealloc == NULL) { Index: trunk/src_plugins/import_mentor_sch/netlist_helper.h =================================================================== --- trunk/src_plugins/import_mentor_sch/netlist_helper.h (revision 7422) +++ trunk/src_plugins/import_mentor_sch/netlist_helper.h (revision 7423) @@ -1,7 +1,22 @@ #include +#include +typedef struct nethlp_rule_s nethlp_rule_t; + +#define nethlp_prio_always -1 + +struct nethlp_rule_s { + int prio; + re_se_t *key; + re_se_t *val; + char *new_key; + char *new_val; + nethlp_rule_t *next; +}; + typedef struct { htsp_t id2refdes; /* element ID -> refdes */ + nethlp_rule_t *part_rules; int alloced; } nethlp_ctx_t; @@ -22,6 +37,8 @@ nethlp_ctx_t *nethlp_new(nethlp_ctx_t *prealloc); void nethlp_destroy(nethlp_ctx_t *nhctx); +int nethlp_load_part_map(nethlp_ctx_t *nhctx, const char *fn); + nethlp_elem_ctx_t *nethlp_elem_new(nethlp_ctx_t *nhctx, nethlp_elem_ctx_t *prealloc, const char *id); void nethlp_elem_refdes(nethlp_elem_ctx_t *ectx, const char *refdes); void nethlp_elem_attr(nethlp_elem_ctx_t *ectx, const char *key, const char *val);