1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-22 21:32:29 +02:00

b2/: add currency database

This commit is contained in:
Werner Almesberger 2012-04-30 22:49:33 -03:00
parent 24a32525e2
commit c3a68453d0
8 changed files with 125 additions and 4 deletions

2
b2/CURR Normal file
View File

@ -0,0 +1,2 @@
EUR USD 1.3214 GBP 1.62542
GBP USD 0.615219

View File

@ -83,4 +83,4 @@ spotless: clean
# ----- Experiments ----------------------------------------------------------- # ----- Experiments -----------------------------------------------------------
try: try:
$(VALGRIND) ./boom HIERARCHY -c CHAR -i INV $(VALGRIND) ./boom HIERARCHY -c CHAR -x CURR -i INV

View File

@ -42,6 +42,7 @@ static void usage(const char *name)
" file types:\n" " file types:\n"
" -c characteristics\n" " -c characteristics\n"
" -i inventory\n" " -i inventory\n"
" -x currency exchange\n"
, name); , name);
exit(1); exit(1);
} }
@ -58,6 +59,8 @@ int main(int argc, char **argv)
parse = parse_characteristics; parse = parse_characteristics;
else if (!strcmp(argv[i], "-i")) else if (!strcmp(argv[i], "-i"))
parse = parse_inventory; parse = parse_inventory;
else if (!strcmp(argv[i], "-x"))
parse = parse_currencies;
else else
usage(*argv); usage(*argv);
} else { } else {

63
b2/db.c
View File

@ -173,7 +173,7 @@ static void dump_stock(FILE *file, const struct stock *s)
const struct price *p; const struct price *p;
fprintf(file, " %s %d %d %s %g", fprintf(file, " %s %d %d %s %g",
s->cat, s->avail, s->package, "???", s->add); s->cat, s->avail, s->package, s->curr->name, s->add);
for (p = s->price; p; p = p->next) for (p = s->price; p; p = p->next)
fprintf(file, " %d %g", p->qty, p->value); fprintf(file, " %d %g", p->qty, p->value);
fprintf(file, "\n"); fprintf(file, "\n");
@ -196,3 +196,64 @@ void part_dump(FILE *file, const struct part *part)
if (part->stock) if (part->stock)
dump_stock(file, part->stock); dump_stock(file, part->stock);
} }
/* ----- Currencies -------------------------------------------------------- */
static struct currency *currencies = NULL;
const struct currency *currency_lookup(const char *name)
{
const struct currency *c;
for (c = currencies; c; c = c->next)
if (c->name == name)
break;
return c;
}
double currency_convert(const struct currency *from, const struct currency *to,
double value)
{
const struct exchange *ex;
for (ex = from->exchange; ex; ex = ex->next)
if (ex->dst == to)
return ex->rate*value;
fprintf(stderr, "cannot convert %s to %s\n", from->name, to->name);
exit(1);
}
struct currency *currency_add(const char *name)
{
struct currency **c;
for (c = &currencies; *c; c = &(*c)->next)
if ((*c)->name == name)
return *c;
*c = alloc_type(struct currency);
(*c)->name = name;
(*c)->exchange = NULL;
(*c)->next = NULL;
return *c;
}
void currency_exchange(struct currency *from, const struct currency *to,
double rate)
{
struct exchange **ex;
for (ex = &from->exchange; *ex; ex = &(*ex)->next)
if ((*ex)->dst == to)
yyerrorf("exchange %s -> %s is already defined",
from->name, to->name);
*ex = alloc_type(struct exchange);
(*ex)->dst = to;
(*ex)->rate = rate;
(*ex)->next = NULL;
}

View File

@ -18,7 +18,7 @@
struct exchange { struct exchange {
const struct currency *dst; const struct currency *dst;
double factor; double rate; /* dst = rate*src */
struct exchange *next; struct exchange *next;
}; };
@ -98,4 +98,11 @@ void part_finalize(struct part *part, const struct action *act);
void part_add_stock(struct part *part, struct stock *stock); void part_add_stock(struct part *part, struct stock *stock);
void part_dump(FILE *file, const struct part *part); void part_dump(FILE *file, const struct part *part);
const struct currency *currency_lookup(const char *name);
double currency_convert(const struct currency *from, const struct currency *to,
double value);
struct currency *currency_add(const char *name);
void currency_exchange(struct currency *from, const struct currency *to,
double rate);
#endif /* !DB_H */ #endif /* !DB_H */

View File

@ -16,6 +16,7 @@
void parse_hierarchy(void); void parse_hierarchy(void);
void parse_characteristics(void); void parse_characteristics(void);
void parse_inventory(void); void parse_inventory(void);
void parse_currencies(void);
void yywarnf(const char *fmt, ...); void yywarnf(const char *fmt, ...);
void yyerrorf(const char *fmt, ...); void yyerrorf(const char *fmt, ...);

View File

@ -53,6 +53,14 @@ void parse_inventory(void)
yyparse(); yyparse();
} }
void parse_currencies(void)
{
start_token = START_EXCHANGE;
expose_nl = 1;
lineno = 1;
yyparse();
}
%} %}

View File

@ -24,6 +24,8 @@
static struct action hierarchy; static struct action hierarchy;
static struct currency *curr;
static struct field_stack { static struct field_stack {
const struct field *field; const struct field *field;
@ -82,6 +84,7 @@ static const struct field *top_field(void)
%token START_HIERARCHY START_CHAR START_INVENTORY %token START_HIERARCHY START_CHAR START_INVENTORY
%token START_EXCHANGE
%token TOK_LE TOK_GE TOK_NL %token TOK_LE TOK_GE TOK_NL
%token <s> WORD %token <s> WORD
@ -106,6 +109,7 @@ all:
START_HIERARCHY hierarchy START_HIERARCHY hierarchy
| START_CHAR characteristics | START_CHAR characteristics
| START_INVENTORY inventory | START_INVENTORY inventory
| START_EXCHANGE exchange
; ;
@ -432,7 +436,9 @@ stock:
$$->cat = $1; $$->cat = $1;
$$->avail = $2; $$->avail = $2;
$$->package = $3; $$->package = $3;
$$->curr = NULL; /* @@@ later -- currency($4); */ $$->curr = currency_lookup($4);
if (!$$->curr)
yyerrorf("unknown currency %s", $4);
$$->add = $5; $$->add = $5;
$$->price = $6; $$->price = $6;
} }
@ -481,3 +487,36 @@ float:
yyerrorf("\"%s\" is not a number", $1); yyerrorf("\"%s\" is not a number", $1);
} }
; ;
/* ----- Currency exchange ------------------------------------------------- */
exchange:
| TOK_NL
| currency exchange
;
currency:
WORD
{
curr = currency_add($1);
}
rates TOK_NL
;
rates:
| rate rates
;
rate:
WORD float
{
const struct currency *to;
/* add and not lookup, since we just introduce the
currency here */
to = currency_add($1);
currency_exchange(curr, to, $2);
}
;