1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-07-02 23:42:55 +03:00
eda-tools/b2/bom.c

102 lines
1.9 KiB
C
Raw Normal View History

/*
* bom.c - BOM data
*
* Copyright 2012 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "util.h"
#include "lang.h"
#include "bom.h"
struct bom *bom = NULL;
int n_bom = 0;
static struct bom *bom_find(const char *ref)
{
struct bom *b;
for (b = bom; b != bom+n_bom; b++)
if (b->ref == ref)
return b;
return NULL;
}
struct bom *bom_parse_line(const char *s)
{
struct bom *b;
const char *ref, *t;
char *f;
if (*s++ != '|')
abort();
while (*s == ' ' || *s == '\t')
s++;
for (t = s+1; *t && !isspace(*t); t++);
if (!*t)
yyerror("invalid BOM record");
ref = unique_n(s, t-s);
if (bom_find(ref))
yyerrorf("duplicate component reference %s", ref);
bom = realloc(bom, sizeof(struct bom)*(n_bom+1));
if (!bom)
abort();
b = bom+n_bom++;
b->ref = ref;
b->sym = NULL;
b->fields = NULL;
b->n_fields = 0;
for (s = t; *t && *t != '\n'; s = t+1) {
while (*s == ' ' || *s == '\t')
s++;
if (*s == ';' || *s == '\n') {
f = NULL;
t = s;
} else {
for (t = s+1; *t && *t != ';' && !isspace(*t); t++);
f = stralloc_n(s, t-s);
while (*t == ' ' || *t == '\t')
t++;
}
/* VAL, FP, F1 ... */
b->fields =
realloc(b->fields, sizeof(const char *)*(b->n_fields+1));
if (!b->fields)
abort();
b->fields[b->n_fields] = f ? unique(f) : unique("");
b->n_fields++;
free(f);
}
return b;
}
void bom_set_sym(const char *ref, const char *sym)
{
struct bom *b;
b = bom_find(ref);
if (!b)
yyerrorf("cannot find component reference \"%s\"", ref);
if (b->sym)
yyerrorf("symbol is already set in \"%s\"", ref);
b->sym = sym;
}