1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-30 04:57:11 +02:00
eda-tools/b2/db.c

300 lines
5.7 KiB
C
Raw Normal View History

2012-03-18 18:24:12 +02:00
/*
* db.c - Parts database
*
* 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.
*/
2012-04-29 04:50:15 +03:00
#include <stdlib.h>
#include <stdio.h>
2012-03-18 18:24:12 +02:00
#include <glib.h>
#include "util.h"
2012-04-29 04:50:15 +03:00
#include "lang.h"
#include "chr.h"
2012-03-18 18:24:12 +02:00
#include "db.h"
static GTree *tree = NULL;
static gint comp(gconstpointer a, gconstpointer b)
{
const struct part *pa = a;
const struct part *pb = b;
/*
* Just subtracting the pointers may produce values outside the
* range of gint values.
*/
return pa->domain == pb->domain ?
pa->name < pb->name ? -1 : pa->name > pb->name ? 1 : 0 :
pa->domain < pb->domain ? -1 : 1;
}
struct part *part_lookup(const char *domain, const char *name)
{
struct part part = {
.domain = domain,
.name = name,
};
if (!tree)
return NULL;
return g_tree_lookup(tree, &part);
}
struct part *part_add(const char *domain, const char *name)
{
struct part part = {
.domain = domain,
.name = name,
.param = NULL,
.stock = NULL,
2012-03-18 18:24:12 +02:00
};
struct part *p;
if (!tree)
tree = g_tree_new(comp);
p = g_tree_lookup(tree, &part);
if (!p) {
p = alloc_type(struct part);
*p = part;
2012-03-18 18:24:12 +02:00
p->next = p->prev = p;
g_tree_insert(tree, p, p);
}
return p;
}
void part_alias(struct part *a, struct part *b)
{
struct part *tmp, *tmp2;
tmp = a->next;
a->next = b;
b->prev->next = tmp;
tmp2 = b->prev;
b->prev = a;
tmp->prev = tmp2;
}
2012-04-29 04:50:15 +03:00
static void convert_params(struct param **params,
const struct field *f, struct param ***res)
{
struct param **p, *prm;
const struct selector *sel;
const struct condition *cond;
while (f) {
for (p = params; *p; p = &(*p)->next)
if ((*p)->u.name == f->name)
break;
if (!*p) {
f = f->next;
continue;
}
/* remove from list */
prm = *p;
*p = prm->next;
/* convert parameter */
prm->u.field = f;
if (!evaluate(f->fmt, prm->value.u.name, &prm->value))
yyerrorf("invalid value for %s", f->name);
/* add to result list */
**res = prm;
*res = &prm->next;
prm->next = NULL;
/* process selections */
for (sel = f->sel; sel; sel = sel->next) {
for (cond = sel->cond; cond; cond = cond->next)
if (compare(f->fmt, &prm->value, cond->relop,
&cond->value))
break;
if (cond) {
convert_params(params, sel->act.fields, res);
convert_params(params, sel->act.rules, res);
break;
}
}
if (!sel) {
convert_params(params, f->any.fields, res);
convert_params(params, f->any.rules, res);
}
f = f->next;
}
}
void part_finalize(struct part *part, const struct action *act)
2012-04-29 04:50:15 +03:00
{
struct param *param = part->param;
struct param **res = &part->param;
struct param *next;
part->param = NULL;
convert_params(&param, act->fields, &res);
convert_params(&param, act->rules, &res);
2012-04-29 04:50:15 +03:00
while (param) {
yywarnf("extra parameter: %s", param->u.name);
next = param->next;
free(param);
param = next;
}
}
void part_add_stock(struct part *part, struct stock *stock)
{
if (!part->stock) {
part->stock = stock;
return;
}
yyerrorf("part %s %s already has stock", part->domain, part->name);
}
static void dump_stock(FILE *file, const struct stock *s)
{
const struct price *p;
2012-05-01 21:21:32 +03:00
fprintf(file, " %s %s %d %d %s %g",
s->provider->name, s->cat, s->avail, s->package, s->curr->name,
s->add);
for (p = s->price; p; p = p->next)
fprintf(file, " %d %g", p->qty, p->value);
fprintf(file, "\n");
}
void part_dump(FILE *file, const struct part *part)
{
const struct param *p;
fprintf(file, "%s %s\n", part->domain, part->name);
if (part->param) {
fprintf(file, " ");
for (p = part->param; p; p = p->next) {
fprintf(file, " %s=", p->u.field->name);
dump(file, p->u.field->fmt, &p->value);
}
fprintf(file, "\n");
}
if (part->stock)
dump_stock(file, part->stock);
}
2012-05-01 04:49:33 +03:00
/* ----- 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;
}
2012-05-01 20:57:12 +03:00
/* ----- Provider database ------------------------------------------------- */
static struct provider *providers = NULL;
struct provider *provider_lookup(const char *name)
{
struct provider *p;
for (p = providers; p; p = p->next)
if (p->name == name)
break;
return p;
}
struct provider *provider_add(const char *name)
{
struct provider *p;
p = provider_lookup(name);
if (p)
return p;
p = alloc_type(struct provider);
p->name = name;
p->curr = NULL;
p->shipping = 0;
p->minimum = 0;
p->next = providers;
providers = p;
return p;
}