1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-26 07:13:08 +02:00
eda-tools/b2/db.c
2012-03-18 13:24:12 -03:00

83 lines
1.5 KiB
C

/*
* 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.
*/
#include <glib.h>
#include "util.h"
#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,
};
struct part *p;
if (!tree)
tree = g_tree_new(comp);
p = g_tree_lookup(tree, &part);
if (!p) {
p = alloc_type(struct part);
p->stock = NULL;
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;
}