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