2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: oid_db.c,v 1.9 2007-06-24 19:27:12 adam Exp $
20 #include <yaz/yaz-util.h>
22 #include <yaz/oid_util.h>
23 #include <yaz/oid_db.h>
26 struct yaz_oid_entry *entries;
27 struct yaz_oid_db *next;
31 struct yaz_oid_db standard_db_l = {
32 yaz_oid_standard_entries, 0, 0
34 yaz_oid_db_t standard_db = &standard_db_l;
36 yaz_oid_db_t yaz_oid_std(void)
41 const Odr_oid *yaz_string_to_oid(yaz_oid_db_t oid_db,
42 oid_class oclass, const char *name)
44 for (; oid_db; oid_db = oid_db->next)
46 struct yaz_oid_entry *e;
47 if (oclass != CLASS_GENERAL)
49 for (e = oid_db->entries; e->name; e++)
51 if (!yaz_matchstr(e->name, name) && oclass == e->oclass)
55 for (e = oid_db->entries; e->name; e++)
57 if (!yaz_matchstr(e->name, name))
64 Odr_oid *yaz_string_to_oid_nmem(yaz_oid_db_t oid_list,
65 oid_class oclass, const char *name, NMEM nmem)
67 const Odr_oid *oid = yaz_string_to_oid(oid_list, oclass, name);
69 return odr_oiddup_nmem(nmem, oid);
70 return odr_getoidbystr_nmem(nmem, name);
73 Odr_oid *yaz_string_to_oid_odr(yaz_oid_db_t oid_list,
74 oid_class oclass, const char *name, ODR o)
76 return yaz_string_to_oid_nmem(oid_list, oclass, name, odr_getmem(o));
79 const char *yaz_oid_to_string(yaz_oid_db_t oid_db,
80 const Odr_oid *oid, oid_class *oclass)
84 for (; oid_db; oid_db = oid_db->next)
86 struct yaz_oid_entry *e = oid_db->entries;
89 if (!oid_oidcmp(e->oid, oid))
100 const char *yaz_oid_to_string_buf(const Odr_oid *oid, oid_class *oclass, char *buf)
102 const char *p = yaz_oid_to_string(standard_db, oid, oclass);
106 *oclass = CLASS_GENERAL;
107 return oid_oid_to_dotstring(oid, buf);
111 char *oid_name_to_dotstring(oid_class oclass, const char *name, char *oid_buf)
113 const Odr_oid *oid = yaz_string_to_oid(yaz_oid_std(), oclass, name);
115 return oid_oid_to_dotstring(oid, oid_buf);
120 int yaz_oid_is_iso2709(const Odr_oid *oid)
122 if (oid_oidlen(oid) == 6 && oid[0] == 1 && oid[1] == 2
123 && oid[2] == 840 && oid[3] == 10003 && oid[4] == 5
124 && oid[5] <= 29 && oid[5] != 16)
129 int yaz_oid_add(yaz_oid_db_t oid_db, int oclass, const char *name,
130 const Odr_oid *new_oid)
132 const Odr_oid *oid = yaz_string_to_oid(oid_db, oclass, name);
135 struct yaz_oid_entry *ent;
139 oid_db = oid_db->next;
140 oid_db->next = (struct yaz_oid_db *) xmalloc(sizeof(*oid_db->next));
141 oid_db = oid_db->next;
144 oid_db->xmalloced = 1;
145 oid_db->entries = ent = (struct yaz_oid_entry *) xmalloc(2 * sizeof(*ent));
147 alloc_oid = (Odr_oid *)
148 xmalloc(sizeof(*alloc_oid) * (oid_oidlen(new_oid)+1));
149 oid_oidcpy(alloc_oid, new_oid);
150 ent[0].oid = alloc_oid;
151 ent[0].name = xstrdup(name);
152 ent[0].oclass = oclass;
156 ent[1].oclass = CLASS_NOP;
162 yaz_oid_db_t yaz_oid_db_new(void)
164 yaz_oid_db_t p = (yaz_oid_db_t) xmalloc(sizeof(*p));
171 void yaz_oid_db_destroy(yaz_oid_db_t oid_db)
175 yaz_oid_db_t p = oid_db;
177 oid_db = oid_db->next;
180 struct yaz_oid_entry *e = p->entries;
189 void yaz_oid_trav(yaz_oid_db_t oid_db,
190 void (*func)(const Odr_oid *oid,
191 oid_class oclass, const char *name,
195 for (; oid_db; oid_db = oid_db->next)
197 struct yaz_oid_entry *e = oid_db->entries;
200 func(e->oid, e->oclass, e->name, client_data);
207 * indent-tabs-mode: nil
209 * vim: shiftwidth=4 tabstop=8 expandtab