b0224604767b83614ea67f479187b50d5bdc4664
[yaz-moved-to-github.git] / src / otherinfo.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  * \file otherinfo.c
7  * \brief Implements Z39.50 OtherInfo utilities
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     const Odr_oid *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             /*  DS: Want does this do? Returns the first element without a category */
96             if (!otherInformation->list[i]->category)
97                 return otherInformation->list[i];
98         }
99         else
100         {
101             if (otherInformation->list[i]->category &&
102                 categoryValue ==
103                 *otherInformation->list[i]->category->categoryValue &&
104                 !oid_oidcmp (oid, otherInformation->list[i]->category->
105                              categoryTypeId))
106             {
107                 Z_OtherInformationUnit *this_list = otherInformation->list[i];
108
109                 if (delete_flag)
110                 {
111                     (otherInformation->num_elements)--;
112                     while (i < otherInformation->num_elements)
113                     {
114                         otherInformation->list[i] =
115                             otherInformation->list[i+1];
116                         i++;
117                     }
118                 }
119                 return this_list;
120             }
121         }
122     }
123     if (!odr)
124         return 0;
125     else
126     {
127         Z_OtherInformationUnit **newlist = (Z_OtherInformationUnit**)
128             odr_malloc(odr, (otherInformation->num_elements+1) *
129                        sizeof(*newlist));
130         for (i = 0; i<otherInformation->num_elements; i++)
131             newlist[i] = otherInformation->list[i];
132         otherInformation->list = newlist;
133         
134         otherInformation->list[i] = (Z_OtherInformationUnit*)
135             odr_malloc (odr, sizeof(Z_OtherInformationUnit));
136         if (oid)
137         {
138             otherInformation->list[i]->category = (Z_InfoCategory*)
139                 odr_malloc (odr, sizeof(Z_InfoCategory));
140             otherInformation->list[i]->category->categoryTypeId = (Odr_oid*)
141                 odr_oiddup (odr, oid);
142             otherInformation->list[i]->category->categoryValue = 
143                 odr_intdup(odr, categoryValue);
144         }
145         else
146             otherInformation->list[i]->category = 0;
147         otherInformation->list[i]->which = Z_OtherInfo_characterInfo;
148         otherInformation->list[i]->information.characterInfo = 0;
149         
150         otherInformation->num_elements = i+1;
151         return otherInformation->list[i];
152     }
153 }
154
155 void yaz_oi_set_string_oid (
156     Z_OtherInformation **otherInformation, ODR odr,
157     const Odr_oid *oid, int categoryValue,
158     const char *str)
159 {
160     Z_OtherInformationUnit *oi =
161         yaz_oi_update(otherInformation, odr, oid, categoryValue, 0);
162     if (!oi)
163         return;
164     oi->which = Z_OtherInfo_characterInfo;
165     oi->information.characterInfo = odr_strdup (odr, str);
166 }
167
168 char *yaz_oi_get_string_oid (
169     Z_OtherInformation **otherInformation,
170     const Odr_oid *oid, int categoryValue, int delete_flag)
171 {
172     Z_OtherInformationUnit *oi;
173     
174     if ((oi = yaz_oi_update(otherInformation, 0, oid, categoryValue,
175                             delete_flag)) &&
176         oi->which == Z_OtherInfo_characterInfo)
177         return oi->information.characterInfo;
178     return 0;
179 }
180
181 void yaz_oi_set_facetlist_oid (
182     Z_OtherInformation **otherInformation, ODR odr,
183     const Odr_oid *oid, int categoryValue,
184     Z_FacetList *facet_list)
185 {
186     Z_External *z_external = 0;
187     Z_OtherInformationUnit *oi =
188         yaz_oi_update(otherInformation, odr, oid, categoryValue, 0);
189     if (!oi)
190         return;
191     oi->which = Z_OtherInfo_externallyDefinedInfo;
192     z_external = odr_malloc(odr, sizeof(*z_external));
193     z_external->which = Z_External_userFacets;
194     z_external->direct_reference = odr_oiddup(odr, oid);
195     z_external->indirect_reference = 0;
196     z_external->descriptor = 0;
197     z_external->u.facetList = facet_list;
198     oi->information.externallyDefinedInfo = z_external;
199 }
200
201 Z_FacetList *yaz_oi_get_facetlist_oid (
202     Z_OtherInformation **otherInformation, ODR odr,
203     const Odr_oid *oid, int categoryValue, int delete_flag)
204 {
205     Z_External *z_external = 0;
206     Z_OtherInformationUnit *oi =
207         yaz_oi_update(otherInformation, 0, oid, categoryValue, delete_flag);
208     if (!oi)
209         return 0;
210     z_external = oi->information.externallyDefinedInfo;
211
212     if (z_external && z_external->which == Z_External_userFacets) {
213         return z_external->u.facetList;
214     }
215     return 0;
216 }
217
218
219 /*
220  * Local variables:
221  * c-basic-offset: 4
222  * c-file-style: "Stroustrup"
223  * indent-tabs-mode: nil
224  * End:
225  * vim: shiftwidth=4 tabstop=8 expandtab
226  */
227