1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
17 #include <yaz/yaz-util.h>
19 #include <yaz/oid_util.h>
20 #include <yaz/oid_db.h>
23 struct yaz_oid_entry *entries;
24 struct yaz_oid_db *next;
28 struct yaz_oid_db standard_db_l = {
31 yaz_oid_db_t standard_db = &standard_db_l;
33 yaz_oid_db_t yaz_oid_std(void)
38 #define get_entries(db) (db->xmalloced==0 ? yaz_oid_standard_entries : db->entries)
40 const Odr_oid *yaz_string_to_oid(yaz_oid_db_t oid_db,
41 oid_class oclass, const char *name)
43 for (; oid_db; oid_db = oid_db->next)
45 struct yaz_oid_entry *e;
46 if (oclass != CLASS_GENERAL)
48 for (e = get_entries(oid_db); e->name; e++)
50 if (!yaz_matchstr(e->name, name) && oclass == e->oclass)
54 for (e = get_entries(oid_db); e->name; e++)
56 if (!yaz_matchstr(e->name, name))
63 Odr_oid *yaz_string_to_oid_nmem(yaz_oid_db_t oid_list,
64 oid_class oclass, const char *name, NMEM nmem)
66 const Odr_oid *oid = yaz_string_to_oid(oid_list, oclass, name);
68 return odr_oiddup_nmem(nmem, oid);
69 return odr_getoidbystr_nmem(nmem, name);
72 Odr_oid *yaz_string_to_oid_odr(yaz_oid_db_t oid_list,
73 oid_class oclass, const char *name, ODR o)
75 return yaz_string_to_oid_nmem(oid_list, oclass, name, odr_getmem(o));
78 const char *yaz_oid_to_string(yaz_oid_db_t oid_db,
79 const Odr_oid *oid, oid_class *oclass)
83 for (; oid_db; oid_db = oid_db->next)
85 struct yaz_oid_entry *e = get_entries(oid_db);
88 if (!oid_oidcmp(e->oid, oid))
99 const char *yaz_oid_to_string_buf(const Odr_oid *oid, oid_class *oclass, char *buf)
101 const char *p = yaz_oid_to_string(yaz_oid_std(), oid, oclass);
105 *oclass = CLASS_GENERAL;
106 return oid_oid_to_dotstring(oid, buf);
110 char *oid_name_to_dotstring(oid_class oclass, const char *name, char *oid_buf)
112 const Odr_oid *oid = yaz_string_to_oid(yaz_oid_std(), oclass, name);
114 return oid_oid_to_dotstring(oid, oid_buf);
119 int yaz_oid_is_iso2709(const Odr_oid *oid)
121 if (oid_oidlen(oid) == 6 && oid[0] == 1 && oid[1] == 2
122 && oid[2] == 840 && oid[3] == 10003 && oid[4] == 5
123 && oid[5] <= 29 && oid[5] != 16)
128 int yaz_oid_add(yaz_oid_db_t oid_db, oid_class oclass, const char *name,
129 const Odr_oid *new_oid)
131 const Odr_oid *oid = yaz_string_to_oid(oid_db, oclass, name);
134 struct yaz_oid_entry *ent;
138 oid_db = oid_db->next;
139 oid_db->next = (struct yaz_oid_db *) xmalloc(sizeof(*oid_db->next));
140 oid_db = oid_db->next;
143 oid_db->xmalloced = 1;
144 oid_db->entries = ent = (struct yaz_oid_entry *) xmalloc(2 * sizeof(*ent));
146 alloc_oid = (Odr_oid *)
147 xmalloc(sizeof(*alloc_oid) * (oid_oidlen(new_oid)+1));
148 oid_oidcpy(alloc_oid, new_oid);
149 ent[0].oid = alloc_oid;
150 ent[0].name = xstrdup(name);
151 ent[0].oclass = oclass;
155 ent[1].oclass = CLASS_NOP;
161 yaz_oid_db_t yaz_oid_db_new(void)
163 yaz_oid_db_t p = (yaz_oid_db_t) xmalloc(sizeof(*p));
170 void yaz_oid_db_destroy(yaz_oid_db_t oid_db)
174 yaz_oid_db_t p = oid_db;
176 oid_db = oid_db->next;
179 struct yaz_oid_entry *e = p->entries;
188 void yaz_oid_trav(yaz_oid_db_t oid_db,
189 void (*func)(const Odr_oid *oid,
190 oid_class oclass, const char *name,
194 for (; oid_db; oid_db = oid_db->next)
196 struct yaz_oid_entry *e = get_entries(oid_db);
199 func(e->oid, e->oclass, e->name, client_data);
206 * c-file-style: "Stroustrup"
207 * indent-tabs-mode: nil
209 * vim: shiftwidth=4 tabstop=8 expandtab