104ff930881abaacc778642ea21d6418fe06d992
[yaz-moved-to-github.git] / src / oid_db.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file oid_db.c
8  * \brief OID Database
9  */
10 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 #include <yaz/yaz-util.h>
19 #include <yaz/odr.h>
20 #include <yaz/oid_util.h>
21 #include <yaz/oid_db.h>
22
23 struct yaz_oid_db {
24      struct yaz_oid_entry *entries;
25      struct yaz_oid_db *next;
26      int xmalloced;
27 };
28
29 struct yaz_oid_db standard_db_l = {
30     yaz_oid_standard_entries, 0, 0
31 };
32 yaz_oid_db_t standard_db = &standard_db_l;
33
34 yaz_oid_db_t yaz_oid_std(void)
35 {
36     return standard_db;
37 }
38
39 const Odr_oid *yaz_string_to_oid(yaz_oid_db_t oid_db,
40                                  oid_class oclass, const char *name)
41 {
42     for (; oid_db; oid_db = oid_db->next)
43     {
44         struct yaz_oid_entry *e;
45         if (oclass != CLASS_GENERAL)
46         {
47             for (e = oid_db->entries; e->name; e++)
48             {
49                 if (!yaz_matchstr(e->name, name) && oclass == e->oclass)
50                     return e->oid;
51             }
52         }
53         for (e = oid_db->entries; e->name; e++)
54         {
55             if (!yaz_matchstr(e->name, name))
56                 return e->oid;
57         }
58     }
59     return 0;
60 }
61
62 Odr_oid *yaz_string_to_oid_nmem(yaz_oid_db_t oid_list,
63                                 oid_class oclass, const char *name, NMEM nmem)
64 {
65     const Odr_oid *oid = yaz_string_to_oid(oid_list, oclass, name);
66     if (oid)
67         return odr_oiddup_nmem(nmem, oid);
68     return odr_getoidbystr_nmem(nmem, name);
69 }
70
71 Odr_oid *yaz_string_to_oid_odr(yaz_oid_db_t oid_list,
72                                oid_class oclass, const char *name, ODR o)
73 {
74     return yaz_string_to_oid_nmem(oid_list, oclass, name, odr_getmem(o));
75 }
76
77 const char *yaz_oid_to_string(yaz_oid_db_t oid_db,
78                               const Odr_oid *oid, oid_class *oclass)
79 {
80     if (!oid)
81         return 0;
82     for (; oid_db; oid_db = oid_db->next)
83     {
84         struct yaz_oid_entry *e = oid_db->entries;
85         for (; e->name; e++)
86         {
87             if (!oid_oidcmp(e->oid, oid))
88             {
89                 if (oclass)
90                     *oclass = e->oclass;
91                 return e->name;
92             }
93         }
94     }
95     return 0;
96 }
97
98 const char *yaz_oid_to_string_buf(const Odr_oid *oid, oid_class *oclass, char *buf)
99 {
100     const char *p = yaz_oid_to_string(standard_db, oid, oclass);
101     if (p)
102         return p;
103     if (oclass)
104         *oclass = CLASS_GENERAL;
105     return oid_oid_to_dotstring(oid, buf);
106 }
107
108
109 char *oid_name_to_dotstring(oid_class oclass, const char *name, char *oid_buf)
110 {
111     const Odr_oid *oid = yaz_string_to_oid(yaz_oid_std(), oclass, name);
112     if (oid)
113         return oid_oid_to_dotstring(oid, oid_buf);
114     return 0;
115 }
116
117
118 int yaz_oid_is_iso2709(const Odr_oid *oid)
119 {
120     if (oid_oidlen(oid) == 6 && oid[0] == 1 && oid[1] == 2
121         && oid[2] == 840 && oid[3] == 10003 && oid[4] == 5 
122         && oid[5] <= 29 && oid[5] != 16)
123         return 1;
124     return 0;
125 }
126
127 int yaz_oid_add(yaz_oid_db_t oid_db, oid_class oclass, const char *name,
128                 const Odr_oid *new_oid)
129 {
130     const Odr_oid *oid = yaz_string_to_oid(oid_db, oclass, name);
131     if (!oid)
132     {
133         struct yaz_oid_entry *ent;
134         Odr_oid *alloc_oid;
135
136         while (oid_db->next)
137             oid_db = oid_db->next;
138         oid_db->next = (struct yaz_oid_db *) xmalloc(sizeof(*oid_db->next));
139         oid_db = oid_db->next;
140
141         oid_db->next = 0;
142         oid_db->xmalloced = 1;
143         oid_db->entries = ent = (struct yaz_oid_entry *) xmalloc(2 * sizeof(*ent));
144
145         alloc_oid = (Odr_oid *)
146             xmalloc(sizeof(*alloc_oid) * (oid_oidlen(new_oid)+1));
147         oid_oidcpy(alloc_oid, new_oid);
148         ent[0].oid = alloc_oid;
149         ent[0].name = xstrdup(name);
150         ent[0].oclass = oclass;
151
152         ent[1].oid = 0;
153         ent[1].name = 0;
154         ent[1].oclass = CLASS_NOP;
155         return 0;
156     }
157     return -1;
158 }
159
160 yaz_oid_db_t yaz_oid_db_new(void)
161 {
162     yaz_oid_db_t p = (yaz_oid_db_t) xmalloc(sizeof(*p));
163     p->entries = 0;
164     p->next = 0;
165     p->xmalloced = 1;
166     return p;
167 }
168
169 void yaz_oid_db_destroy(yaz_oid_db_t oid_db)
170 {
171     while (oid_db)
172     {
173         yaz_oid_db_t p = oid_db;
174
175         oid_db = oid_db->next;
176         if (p->xmalloced)
177         {
178             struct yaz_oid_entry *e = p->entries;
179             for (; e->name; e++)
180                 xfree (e->name);
181             xfree(p->entries);
182             xfree(p);
183         }
184     }
185 }
186
187 void yaz_oid_trav(yaz_oid_db_t oid_db,
188                   void (*func)(const Odr_oid *oid,
189                                oid_class oclass, const char *name,
190                                void *client_data),
191                   void *client_data)
192 {
193     for (; oid_db; oid_db = oid_db->next)
194     {
195         struct yaz_oid_entry *e = oid_db->entries;
196         
197         for (; e->name; e++)
198             func(e->oid, e->oclass, e->name, client_data);
199     }
200 }
201
202 /*
203  * Local variables:
204  * c-basic-offset: 4
205  * indent-tabs-mode: nil
206  * End:
207  * vim: shiftwidth=4 tabstop=8 expandtab
208  */
209