Root/
| 1 | /* |
| 2 | * db.c - Parts database |
| 3 | * |
| 4 | * Copyright 2012 by Werner Almesberger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | #include <stdio.h> |
| 15 | #include <glib.h> |
| 16 | |
| 17 | #include "util.h" |
| 18 | #include "lang.h" |
| 19 | #include "chr.h" |
| 20 | #include "db.h" |
| 21 | |
| 22 | |
| 23 | static GTree *tree = NULL; |
| 24 | |
| 25 | |
| 26 | static gint comp(gconstpointer a, gconstpointer b) |
| 27 | { |
| 28 | const struct part *pa = a; |
| 29 | const struct part *pb = b; |
| 30 | |
| 31 | /* |
| 32 | * Just subtracting the pointers may produce values outside the |
| 33 | * range of gint values. |
| 34 | */ |
| 35 | return pa->domain == pb->domain ? |
| 36 | pa->name < pb->name ? -1 : pa->name > pb->name ? 1 : 0 : |
| 37 | pa->domain < pb->domain ? -1 : 1; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | struct part *part_lookup(const char *domain, const char *name) |
| 42 | { |
| 43 | struct part part = { |
| 44 | .domain = domain, |
| 45 | .name = name, |
| 46 | }; |
| 47 | |
| 48 | if (!tree) |
| 49 | return NULL; |
| 50 | return g_tree_lookup(tree, &part); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | struct part *part_add(const char *domain, const char *name) |
| 55 | { |
| 56 | struct part part = { |
| 57 | .domain = domain, |
| 58 | .name = name, |
| 59 | .param = NULL, |
| 60 | .stock = NULL, |
| 61 | }; |
| 62 | struct part *p; |
| 63 | |
| 64 | if (!tree) |
| 65 | tree = g_tree_new(comp); |
| 66 | p = g_tree_lookup(tree, &part); |
| 67 | if (!p) { |
| 68 | p = alloc_type(struct part); |
| 69 | *p = part; |
| 70 | p->next = p->prev = p; |
| 71 | g_tree_insert(tree, p, p); |
| 72 | } |
| 73 | return p; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void part_alias(struct part *a, struct part *b) |
| 78 | { |
| 79 | struct part *tmp, *tmp2; |
| 80 | |
| 81 | tmp = a->next; |
| 82 | a->next = b; |
| 83 | b->prev->next = tmp; |
| 84 | |
| 85 | tmp2 = b->prev; |
| 86 | b->prev = a; |
| 87 | tmp->prev = tmp2; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static void convert_params(struct param **params, |
| 92 | const struct field *f, struct param ***res) |
| 93 | { |
| 94 | struct param **p, *prm; |
| 95 | const struct selector *sel; |
| 96 | const struct condition *cond; |
| 97 | |
| 98 | while (f) { |
| 99 | for (p = params; *p; p = &(*p)->next) |
| 100 | if ((*p)->u.name == f->name) |
| 101 | break; |
| 102 | if (!*p) { |
| 103 | f = f->next; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | /* remove from list */ |
| 108 | prm = *p; |
| 109 | *p = prm->next; |
| 110 | |
| 111 | /* convert parameter */ |
| 112 | prm->u.field = f; |
| 113 | if (!evaluate(f->fmt, prm->value.u.s, &prm->value)) |
| 114 | yyerrorf("invalid value for %s", f->name); |
| 115 | |
| 116 | /* add to result list */ |
| 117 | **res = prm; |
| 118 | *res = &prm->next; |
| 119 | prm->next = NULL; |
| 120 | |
| 121 | /* process selections */ |
| 122 | for (sel = f->sel; sel; sel = sel->next) { |
| 123 | for (cond = sel->cond; cond; cond = cond->next) |
| 124 | if (compare(f->fmt, &prm->value, cond->relop, |
| 125 | &cond->value)) |
| 126 | break; |
| 127 | if (cond) { |
| 128 | convert_params(params, sel->act.fields, res); |
| 129 | convert_params(params, sel->act.rules, res); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | if (!sel) { |
| 134 | convert_params(params, f->any.fields, res); |
| 135 | convert_params(params, f->any.rules, res); |
| 136 | } |
| 137 | |
| 138 | f = f->next; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | void part_finalize(struct part *part, const struct action *act) |
| 144 | { |
| 145 | struct param *param = part->param; |
| 146 | struct param **res = &part->param; |
| 147 | struct param *next; |
| 148 | |
| 149 | part->param = NULL; |
| 150 | convert_params(¶m, act->fields, &res); |
| 151 | convert_params(¶m, act->rules, &res); |
| 152 | while (param) { |
| 153 | yywarnf("extra parameter: %s", param->u.name); |
| 154 | next = param->next; |
| 155 | free(param); |
| 156 | param = next; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | void part_add_stock(struct part *part, struct stock *s) |
| 162 | { |
| 163 | if (!part->stock) { |
| 164 | part->stock = s; |
| 165 | return; |
| 166 | } |
| 167 | yyerrorf("part %s %s already has stock", part->domain, part->name); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /* ----- Dumping ----------------------------------------------------------- */ |
| 172 | |
| 173 | |
| 174 | static void dump_stock(FILE *file, const struct stock *s) |
| 175 | { |
| 176 | const struct price *p; |
| 177 | |
| 178 | fprintf(file, " %s %s %d %d %s %g", |
| 179 | s->provider->name, s->cat, s->avail, s->package, s->curr->name, |
| 180 | s->add); |
| 181 | for (p = s->price; p; p = p->next) |
| 182 | fprintf(file, " %d %g", p->qty, p->value); |
| 183 | fprintf(file, "\n"); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void part_dump(FILE *file, const struct part *part) |
| 188 | { |
| 189 | const struct param *p; |
| 190 | |
| 191 | fprintf(file, "%s %s\n", part->domain, part->name); |
| 192 | if (part->param) { |
| 193 | fprintf(file, " "); |
| 194 | for (p = part->param; p; p = p->next) { |
| 195 | fprintf(file, " %s=", p->u.field->name); |
| 196 | dump_param(file, p->u.field->fmt, &p->value); |
| 197 | } |
| 198 | fprintf(file, "\n"); |
| 199 | } |
| 200 | if (part->stock) |
| 201 | dump_stock(file, part->stock); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | static gboolean dump_prm_traverse(gpointer key, gpointer value, gpointer data) |
| 206 | { |
| 207 | struct part *p = key; |
| 208 | FILE *file = data; |
| 209 | |
| 210 | (void) value; |
| 211 | part_dump(file, p); |
| 212 | return FALSE; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | void parts_dump(FILE *file) |
| 217 | { |
| 218 | g_tree_foreach(tree, dump_prm_traverse, (void *) file); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /* ----- Currencies -------------------------------------------------------- */ |
| 223 | |
| 224 | |
| 225 | static struct currency *currencies = NULL; |
| 226 | |
| 227 | |
| 228 | const struct currency *currency_lookup(const char *name) |
| 229 | { |
| 230 | const struct currency *c; |
| 231 | |
| 232 | for (c = currencies; c; c = c->next) |
| 233 | if (c->name == name) |
| 234 | break; |
| 235 | return c; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | double currency_convert(const struct currency *from, const struct currency *to, |
| 240 | double value) |
| 241 | { |
| 242 | const struct exchange *ex; |
| 243 | |
| 244 | for (ex = from->exchange; ex; ex = ex->next) |
| 245 | if (ex->dst == to) |
| 246 | return ex->rate*value; |
| 247 | fprintf(stderr, "cannot convert %s to %s\n", from->name, to->name); |
| 248 | exit(1); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | struct currency *currency_add(const char *name) |
| 253 | { |
| 254 | struct currency **c; |
| 255 | |
| 256 | for (c = ¤cies; *c; c = &(*c)->next) |
| 257 | if ((*c)->name == name) |
| 258 | return *c; |
| 259 | *c = alloc_type(struct currency); |
| 260 | (*c)->name = name; |
| 261 | (*c)->exchange = NULL; |
| 262 | (*c)->next = NULL; |
| 263 | return *c; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | void currency_exchange(struct currency *from, const struct currency *to, |
| 268 | double rate) |
| 269 | { |
| 270 | struct exchange **ex; |
| 271 | |
| 272 | for (ex = &from->exchange; *ex; ex = &(*ex)->next) |
| 273 | if ((*ex)->dst == to) |
| 274 | yyerrorf("exchange %s -> %s is already defined", |
| 275 | from->name, to->name); |
| 276 | *ex = alloc_type(struct exchange); |
| 277 | (*ex)->dst = to; |
| 278 | (*ex)->rate = rate; |
| 279 | (*ex)->next = NULL; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /* ----- Provider database ------------------------------------------------- */ |
| 284 | |
| 285 | |
| 286 | static struct provider *providers = NULL; |
| 287 | |
| 288 | |
| 289 | struct provider *provider_lookup(const char *name) |
| 290 | { |
| 291 | struct provider *p; |
| 292 | |
| 293 | for (p = providers; p; p = p->next) |
| 294 | if (p->name == name) |
| 295 | break; |
| 296 | return p; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | struct provider *provider_add(const char *name) |
| 301 | { |
| 302 | struct provider *p; |
| 303 | |
| 304 | p = provider_lookup(name); |
| 305 | if (p) |
| 306 | return p; |
| 307 | |
| 308 | p = alloc_type(struct provider); |
| 309 | p->name = name; |
| 310 | p->curr = NULL; |
| 311 | p->shipping = 0; |
| 312 | p->minimum = 0; |
| 313 | p->next = providers; |
| 314 | providers = p; |
| 315 | return p; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /* ----- Part search (by characteristics) ---------------------------------- */ |
| 320 | |
| 321 | |
| 322 | struct sel_prm_data { |
| 323 | struct param *param; |
| 324 | const struct part **res; |
| 325 | int n_res; |
| 326 | }; |
| 327 | |
| 328 | |
| 329 | static struct param *convert_vars(struct param *vars, const struct action *act) |
| 330 | { |
| 331 | struct param *res = NULL, **last = &res; |
| 332 | |
| 333 | convert_params(&vars, act->fields, &last); |
| 334 | convert_params(&vars, act->rules, &last); |
| 335 | free_vars(vars); |
| 336 | return res; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /* |
| 341 | * @@@ By matching fields (and not field names), we avoid having to check for |
| 342 | * field compatibility. However, this also means that only fields that appear |
| 343 | * in the same place in the hierarchy can be matched. |
| 344 | * |
| 345 | * For example, a tolerance TOL in T=R could thus never match the tolerance TOL |
| 346 | * in T=C. This seems reasonable, but may need some more pondering. |
| 347 | */ |
| 348 | |
| 349 | /* |
| 350 | * @@@ We require all fields specified in the query to exist (and to match). |
| 351 | * Could there be cases where this doesn't make sense ? |
| 352 | */ |
| 353 | |
| 354 | static int params_match(const struct param *query, const struct param *part) |
| 355 | { |
| 356 | const struct param *q, *p; |
| 357 | |
| 358 | for (q = query; q; q = q->next) { |
| 359 | for (p = part; p; p = p->next) { |
| 360 | if (q->u.field != p->u.field) |
| 361 | continue; |
| 362 | if (compare(q->u.field->fmt, &p->value, q->op, |
| 363 | &q->value)) |
| 364 | goto next; |
| 365 | return 0; |
| 366 | } |
| 367 | return 0; |
| 368 | next: ; |
| 369 | } |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | static gboolean sel_prm_traverse(gpointer key, gpointer value, gpointer data) |
| 375 | { |
| 376 | struct part *p = key; |
| 377 | struct sel_prm_data *d = data; |
| 378 | |
| 379 | (void) value; |
| 380 | if (!params_match(d->param, p->param)) |
| 381 | return FALSE; |
| 382 | d->res = realloc(d->res, sizeof(const struct part *)*(d->n_res+1)); |
| 383 | if (!d->res) |
| 384 | abort(); |
| 385 | d->res[d->n_res++] = key; |
| 386 | return FALSE; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | const struct part **select_parametric(struct param *vars, |
| 391 | const struct action *act) |
| 392 | { |
| 393 | struct sel_prm_data data = { |
| 394 | .param = convert_vars(vars, act), |
| 395 | .res = NULL, |
| 396 | .n_res = 0, |
| 397 | }; |
| 398 | |
| 399 | g_tree_foreach(tree, sel_prm_traverse, (void *) &data); |
| 400 | if (data.n_res) |
| 401 | data.res[data.n_res] = NULL; |
| 402 | return data.res; |
| 403 | } |
| 404 |
Branches:
master
