Add --with-xml2 to configure
[yaz-moved-to-github.git] / src / otherinfo.c
1 /*
2  * Copyright (c) 1999-2003, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: otherinfo.c,v 1.1 2003-10-27 12:21:34 adam Exp $
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11
12 #include <yaz/otherinfo.h>
13
14 void yaz_oi_APDU(Z_APDU *apdu, Z_OtherInformation ***oip)
15 {
16     switch (apdu->which)
17     {
18     case Z_APDU_initRequest:
19         *oip = &apdu->u.initRequest->otherInfo;
20         break;
21     case Z_APDU_searchRequest:
22         *oip = &apdu->u.searchRequest->otherInfo;
23         break;
24     case Z_APDU_presentRequest:
25         *oip = &apdu->u.presentRequest->otherInfo;
26         break;
27     case Z_APDU_sortRequest:
28         *oip = &apdu->u.sortRequest->otherInfo;
29         break;
30     case Z_APDU_scanRequest:
31         *oip = &apdu->u.scanRequest->otherInfo;
32         break;
33     case Z_APDU_extendedServicesRequest:
34         *oip = &apdu->u.extendedServicesRequest->otherInfo;
35         break;
36     case Z_APDU_deleteResultSetRequest:
37         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
38         break;
39     case Z_APDU_initResponse:
40         *oip = &apdu->u.initResponse->otherInfo;
41         break;
42     case Z_APDU_searchResponse:
43         *oip = &apdu->u.searchResponse->otherInfo;
44         break;
45     case Z_APDU_presentResponse:
46         *oip = &apdu->u.presentResponse->otherInfo;
47         break;
48     case Z_APDU_sortResponse:
49         *oip = &apdu->u.sortResponse->otherInfo;
50         break;
51     case Z_APDU_scanResponse:
52         *oip = &apdu->u.scanResponse->otherInfo;
53         break;
54     case Z_APDU_extendedServicesResponse:
55         *oip = &apdu->u.extendedServicesResponse->otherInfo;
56         break;
57     case Z_APDU_deleteResultSetResponse:
58         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
59         break;
60     case Z_APDU_duplicateDetectionRequest:
61         *oip = &apdu->u.duplicateDetectionRequest->otherInfo;
62         break;
63     case Z_APDU_duplicateDetectionResponse:
64         *oip = &apdu->u.duplicateDetectionResponse->otherInfo;
65         break;
66     default:
67         *oip = 0;
68         break;
69     }
70 }
71
72 Z_OtherInformationUnit *yaz_oi_update (
73     Z_OtherInformation **otherInformationP, ODR odr,
74     int *oid, int categoryValue, int delete_flag)
75 {
76     int i;
77     Z_OtherInformation *otherInformation;
78
79     if (!otherInformationP)
80         return 0;
81     otherInformation = *otherInformationP;
82     if (!otherInformation)
83     {
84         if (!odr)
85             return 0;
86         otherInformation = *otherInformationP = (Z_OtherInformation *)
87             odr_malloc (odr, sizeof(*otherInformation));
88         otherInformation->num_elements = 0;
89         otherInformation->list = 0;
90     }
91     for (i = 0; i<otherInformation->num_elements; i++)
92     {
93         if (!oid)
94         {
95             if (!otherInformation->list[i]->category)
96                 return otherInformation->list[i];
97         }
98         else
99         {
100             if (otherInformation->list[i]->category &&
101                 categoryValue ==
102                 *otherInformation->list[i]->category->categoryValue &&
103                 !oid_oidcmp (oid, otherInformation->list[i]->category->
104                              categoryTypeId))
105             {
106                 Z_OtherInformationUnit *this_list = otherInformation->list[i];
107
108                 if (delete_flag)
109                 {
110                     (otherInformation->num_elements)--;
111                     while (i < otherInformation->num_elements)
112                     {
113                         otherInformation->list[i] =
114                             otherInformation->list[i+1];
115                         i++;
116                     }
117                 }
118                 return this_list;
119             }
120         }
121     }
122     if (!odr)
123         return 0;
124     else
125     {
126         Z_OtherInformationUnit **newlist = (Z_OtherInformationUnit**)
127             odr_malloc(odr, (otherInformation->num_elements+1) *
128                        sizeof(*newlist));
129         for (i = 0; i<otherInformation->num_elements; i++)
130             newlist[i] = otherInformation->list[i];
131         otherInformation->list = newlist;
132         
133         otherInformation->list[i] = (Z_OtherInformationUnit*)
134             odr_malloc (odr, sizeof(Z_OtherInformationUnit));
135         if (oid)
136         {
137             otherInformation->list[i]->category = (Z_InfoCategory*)
138                 odr_malloc (odr, sizeof(Z_InfoCategory));
139             otherInformation->list[i]->category->categoryTypeId = (int*)
140                 odr_oiddup (odr, oid);
141             otherInformation->list[i]->category->categoryValue = (int*)
142                 odr_malloc (odr, sizeof(int));
143             *otherInformation->list[i]->category->categoryValue =
144                 categoryValue;
145         }
146         else
147             otherInformation->list[i]->category = 0;
148         otherInformation->list[i]->which = Z_OtherInfo_characterInfo;
149         otherInformation->list[i]->information.characterInfo = 0;
150         
151         otherInformation->num_elements = i+1;
152         return otherInformation->list[i];
153     }
154 }
155
156 void yaz_oi_set_string_oid (
157     Z_OtherInformation **otherInformation, ODR odr,
158     int *oid, int categoryValue,
159     const char *str)
160 {
161     Z_OtherInformationUnit *oi =
162         yaz_oi_update(otherInformation, odr, oid, categoryValue, 0);
163     if (!oi)
164         return;
165     oi->which = Z_OtherInfo_characterInfo;
166     oi->information.characterInfo = odr_strdup (odr, str);
167 }
168
169 void yaz_oi_set_string_oidval (
170     Z_OtherInformation **otherInformation, ODR odr,
171     int oidval, int categoryValue,
172     const char *str)
173 {
174     int oid[OID_SIZE];
175     struct oident ent;
176     ent.proto = PROTO_Z3950;
177     ent.oclass = CLASS_USERINFO;
178     ent.value = (oid_value) oidval;
179     if (!oid_ent_to_oid (&ent, oid))
180         return ;
181     yaz_oi_set_string_oid(otherInformation,
182                           odr, oid, categoryValue, str);
183 }
184
185 char *yaz_oi_get_string_oid (
186     Z_OtherInformation **otherInformation,
187     int *oid, int categoryValue, int delete_flag)
188 {
189     Z_OtherInformationUnit *oi;
190     
191     if ((oi = yaz_oi_update(otherInformation, 0, oid, 1, delete_flag)) &&
192         oi->which == Z_OtherInfo_characterInfo)
193         return oi->information.characterInfo;
194     return 0;
195 }
196
197 char *yaz_oi_get_string_oidval(Z_OtherInformation **otherInformation,
198                                int oidval, int categoryValue, int delete_flag)
199 {
200     int oid[OID_SIZE];
201     struct oident ent;
202     ent.proto = PROTO_Z3950;
203     ent.oclass = CLASS_USERINFO;
204     ent.value = (oid_value) oidval;
205
206     if (!oid_ent_to_oid (&ent, oid))
207         return 0;
208     return yaz_oi_get_string_oid (otherInformation, oid, categoryValue,
209                                   delete_flag);
210 }
211