Index: work/alien_formats/altium_parser/pcbdoc_ascii.c =================================================================== --- work/alien_formats/altium_parser/pcbdoc_ascii.c (revision 35275) +++ work/alien_formats/altium_parser/pcbdoc_ascii.c (revision 35276) @@ -96,7 +96,7 @@ break; /* by now "next" points to the first character of the next block */ - blk = malloc(sizeof(altium_block_t) + (next-curr)); + blk = malloc(sizeof(altium_block_t) + (next-curr) + 1); if (blk == NULL) { fprintf(stderr, "pcbdoc_ascii_load_blocks: failed to alloc memory\n"); return -1; @@ -109,6 +109,7 @@ fprintf(stderr, "pcbdoc_ascii_load_blocks: can't read that many: %ld from %ld (%ld; max is %ld)\n", blk->size, curr, curr+blk->size, max); return -1; } + blk->raw[blk->size] = '\0'; gdl_append(&tree->blocks, blk, link); /* printf("curr=%ld next=%ld\n", curr, next);*/ curr = next; @@ -118,11 +119,62 @@ } -static int pcbdoc_ascii_parse_blocks(altium_tree_t *tree) +static int pcbdoc_ascii_parse_blocks(altium_tree_t *tree, const char *fn) { altium_block_t *blk; + long line = 1; for(blk = gdl_first(&tree->blocks); blk != NULL; blk = gdl_next(&tree->blocks, blk)) { + char *s = blk->raw, *end; + +printf("---blk---\n"); + + for(;;) { /* parse each line within the block */ + int nl = 0; + + /* ignore leading seps and newlines, exit if ran out of the string */ + while((*s == '|') || (*s == '\r') || (*s == '\n')) s++; + if (*s == '\0') + break; + + /* parse the record header */ + if (strncmp(s, "RECORD=", 7) != 0) { + fprintf(stderr, "First field must be record in %s:%ld\n", fn, line); + return -1; + } + s+=7; + end = strpbrk(s, "|\r\n"); + if (end == NULL) { + fprintf(stderr, "Unterminated record in %s:%ld\n", fn, line); + return -1; + } + *end = '\0'; +printf("rec='%s'\n", s); + s = end+1; + + /* parse fields */ + for(;;) { + + /* ignore leading seps and newlines, exit if ran out of the string */ + while(*s == '|') s++; + if (*s == '\0') + break; + + /* find sep */ + end = strpbrk(s, "|\r\n"); + if (end == NULL) { + fprintf(stderr, "Unterminated field in %s:%ld\n", fn, line); + return -1; + } + if (*end != '|') + nl = 1; + *end = '\0'; +printf(" %s\n", s); + s = end+1; + if (nl) + break; + } + } } return 0; @@ -149,7 +201,7 @@ return -1; - return pcbdoc_ascii_parse_blocks(tree); + return pcbdoc_ascii_parse_blocks(tree, fn); }