Index: trunk/src_plugins/io_mentor_cell/read.c =================================================================== --- trunk/src_plugins/io_mentor_cell/read.c (revision 8892) +++ trunk/src_plugins/io_mentor_cell/read.c (revision 8893) @@ -27,28 +27,111 @@ #include #include +#include #include "board.h" #include "conf_core.h" #include "plug_io.h" +#include "error.h" +typedef struct node_s node_t; + +struct node_s { + char **argv; + int argc; + int level; + node_t *parent, *next; + node_t *first_child, *last_child; +}; + typedef struct { - FILE *f; pcb_board_t *pcb; + char *unit; + node_t *root; } hkp_ctx_t; +static void dump(node_t *nd) +{ + int n; + for(n = 0; n < nd->level; n++) + printf(" "); + if (nd->argc > 0) { + printf("%s", nd->argv[0]); + for(n = 1; n < nd->argc; n++) + printf("|%s", nd->argv[n]); + } + else + printf(""); + printf("\n"); + for(nd = nd->first_child; nd != NULL; nd = nd->next) + dump(nd); +} + +static void destroy(node_t *nd) +{ + node_t *n, *next; + + qparse_free(nd->argc, &nd->argv); + + for(n = nd->first_child; n != NULL; n = next) { + next = n->next; + destroy(n); + } + + free(nd); +} + + int io_mentor_cell_read_pcb(pcb_plug_io_t *pctx, pcb_board_t *pcb, const char *fn, conf_role_t settings_dest) { hkp_ctx_t ctx; + char *s, **argv; + int argc; + FILE *f; + char line[1024]; + node_t *curr = NULL, *nd, *last; - ctx.pcb = pcb; - ctx.f = fopen(fn, "r"); - if (ctx.f == NULL) + f = fopen(fn, "r"); + if (f == NULL) goto err; + ctx.pcb = pcb; + ctx.unit = "mm"; + curr = ctx.root = calloc(sizeof(node_t), 1); + while(fgets(line, sizeof(line), f) != NULL) { + int level = 0; + s = line; + while(isspace(*s)) s++; + while(*s == '.') { s++; level++; }; + if (level == 0) + continue; + nd = calloc(sizeof(node_t), 1); + nd->argc = qparse(s, &nd->argv); + nd->level = level; + + if (level == curr->level) { /* sibling */ + sibling:; + nd->parent = curr->parent; + curr->next = nd; + nd->parent->last_child = nd; + } + else if (level == curr->level+1) { /* first child */ + curr->first_child = curr->last_child = nd; + nd->parent = curr; + } + else if (level < curr->level) { /* step back to a previous level */ + while(level < curr->level) curr = curr->parent; + goto sibling; + } + curr = nd; + } + + err:; - fclose(ctx.f); + dump(ctx.root); + destroy(ctx.root); + fclose(f); return -1; }