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