TCPD libs only used in libyaz's LIBADD
[yaz-moved-to-github.git] / src / oid_db.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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     0, 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 #define get_entries(db) (db->xmalloced==0 ? yaz_oid_standard_entries : db->entries)
40
41 const Odr_oid *yaz_string_to_oid(yaz_oid_db_t oid_db,
42                                  oid_class oclass, const char *name)
43 {
44     for (; oid_db; oid_db = oid_db->next)
45     {
46         struct yaz_oid_entry *e;
47         if (oclass != CLASS_GENERAL)
48         {
49             for (e = get_entries(oid_db); e->name; e++)
50             {
51                 if (!yaz_matchstr(e->name, name) && oclass == e->oclass)
52                     return e->oid;
53             }
54         }
55         for (e = get_entries(oid_db); e->name; e++)
56         {
57             if (!yaz_matchstr(e->name, name))
58                 return e->oid;
59         }
60     }
61     return 0;
62 }
63
64 Odr_oid *yaz_string_to_oid_nmem(yaz_oid_db_t oid_list,
65                                 oid_class oclass, const char *name, NMEM nmem)
66 {
67     const Odr_oid *oid = yaz_string_to_oid(oid_list, oclass, name);
68     if (oid)
69         return odr_oiddup_nmem(nmem, oid);
70     return odr_getoidbystr_nmem(nmem, name);
71 }
72
73 Odr_oid *yaz_string_to_oid_odr(yaz_oid_db_t oid_list,
74                                oid_class oclass, const char *name, ODR o)
75 {
76     return yaz_string_to_oid_nmem(oid_list, oclass, name, odr_getmem(o));
77 }
78
79 const char *yaz_oid_to_string(yaz_oid_db_t oid_db,
80                               const Odr_oid *oid, oid_class *oclass)
81 {
82     if (!oid)
83         return 0;
84     for (; oid_db; oid_db = oid_db->next)
85     {
86         struct yaz_oid_entry *e = get_entries(oid_db);
87         for (; e->name; e++)
88         {
89             if (!oid_oidcmp(e->oid, oid))
90             {
91                 if (oclass)
92                     *oclass = e->oclass;
93                 return e->name;
94             }
95         }
96     }
97     return 0;
98 }
99
100 const char *yaz_oid_to_string_buf(const Odr_oid *oid, oid_class *oclass, char *buf)
101 {
102     const char *p = yaz_oid_to_string(yaz_oid_std(), oid, oclass);
103     if (p)
104         return p;
105     if (oclass)
106         *oclass = CLASS_GENERAL;
107     return oid_oid_to_dotstring(oid, buf);
108 }
109
110
111 char *oid_name_to_dotstring(oid_class oclass, const char *name, char *oid_buf)
112 {
113     const Odr_oid *oid = yaz_string_to_oid(yaz_oid_std(), oclass, name);
114     if (oid)
115         return oid_oid_to_dotstring(oid, oid_buf);
116     return 0;
117 }
118
119
120 int yaz_oid_is_iso2709(const Odr_oid *oid)
121 {
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)
125         return 1;
126     return 0;
127 }
128
129 int yaz_oid_add(yaz_oid_db_t oid_db, oid_class oclass, const char *name,
130                 const Odr_oid *new_oid)
131 {
132     const Odr_oid *oid = yaz_string_to_oid(oid_db, oclass, name);
133     if (!oid)
134     {
135         struct yaz_oid_entry *ent;
136         Odr_oid *alloc_oid;
137
138         while (oid_db->next)
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;
142
143         oid_db->next = 0;
144         oid_db->xmalloced = 1;
145         oid_db->entries = ent = (struct yaz_oid_entry *) xmalloc(2 * sizeof(*ent));
146
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;
153
154         ent[1].oid = 0;
155         ent[1].name = 0;
156         ent[1].oclass = CLASS_NOP;
157         return 0;
158     }
159     return -1;
160 }
161
162 yaz_oid_db_t yaz_oid_db_new(void)
163 {
164     yaz_oid_db_t p = (yaz_oid_db_t) xmalloc(sizeof(*p));
165     p->entries = 0;
166     p->next = 0;
167     p->xmalloced = 1;
168     return p;
169 }
170
171 void yaz_oid_db_destroy(yaz_oid_db_t oid_db)
172 {
173     while (oid_db)
174     {
175         yaz_oid_db_t p = oid_db;
176
177         oid_db = oid_db->next;
178         if (p->xmalloced)
179         {
180             struct yaz_oid_entry *e = p->entries;
181             for (; e->name; e++)
182                 xfree (e->name);
183             xfree(p->entries);
184             xfree(p);
185         }
186     }
187 }
188
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,
192                                void *client_data),
193                   void *client_data)
194 {
195     for (; oid_db; oid_db = oid_db->next)
196     {
197         struct yaz_oid_entry *e = get_entries(oid_db);
198         
199         for (; e->name; e++)
200             func(e->oid, e->oclass, e->name, client_data);
201     }
202 }
203
204 /*
205  * Local variables:
206  * c-basic-offset: 4
207  * c-file-style: "Stroustrup"
208  * indent-tabs-mode: nil
209  * End:
210  * vim: shiftwidth=4 tabstop=8 expandtab
211  */
212