SRU SOAP decoding: handle HTTP basic auth as well
[yaz-moved-to-github.git] / src / otherinfo.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file otherinfo.c
7  * \brief Implements Z39.50 OtherInfo utilities
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <yaz/otherinfo.h>
17
18 void yaz_oi_APDU(Z_APDU *apdu, Z_OtherInformation ***oip)
19 {
20     switch (apdu->which)
21     {
22     case Z_APDU_initRequest:
23         *oip = &apdu->u.initRequest->otherInfo;
24         break;
25     case Z_APDU_searchRequest:
26         *oip = &apdu->u.searchRequest->otherInfo;
27         break;
28     case Z_APDU_presentRequest:
29         *oip = &apdu->u.presentRequest->otherInfo;
30         break;
31     case Z_APDU_sortRequest:
32         *oip = &apdu->u.sortRequest->otherInfo;
33         break;
34     case Z_APDU_scanRequest:
35         *oip = &apdu->u.scanRequest->otherInfo;
36         break;
37     case Z_APDU_extendedServicesRequest:
38         *oip = &apdu->u.extendedServicesRequest->otherInfo;
39         break;
40     case Z_APDU_deleteResultSetRequest:
41         *oip = &apdu->u.deleteResultSetRequest->otherInfo;
42         break;
43     case Z_APDU_initResponse:
44         *oip = &apdu->u.initResponse->otherInfo;
45         break;
46     case Z_APDU_searchResponse:
47         *oip = &apdu->u.searchResponse->otherInfo;
48         break;
49     case Z_APDU_presentResponse:
50         *oip = &apdu->u.presentResponse->otherInfo;
51         break;
52     case Z_APDU_sortResponse:
53         *oip = &apdu->u.sortResponse->otherInfo;
54         break;
55     case Z_APDU_scanResponse:
56         *oip = &apdu->u.scanResponse->otherInfo;
57         break;
58     case Z_APDU_extendedServicesResponse:
59         *oip = &apdu->u.extendedServicesResponse->otherInfo;
60         break;
61     case Z_APDU_deleteResultSetResponse:
62         *oip = &apdu->u.deleteResultSetResponse->otherInfo;
63         break;
64     case Z_APDU_duplicateDetectionRequest:
65         *oip = &apdu->u.duplicateDetectionRequest->otherInfo;
66         break;
67     case Z_APDU_duplicateDetectionResponse:
68         *oip = &apdu->u.duplicateDetectionResponse->otherInfo;
69         break;
70     default:
71         *oip = 0;
72         break;
73     }
74 }
75
76 Z_OtherInformationUnit *yaz_oi_update(
77     Z_OtherInformation **otherInformationP, ODR odr,
78     const Odr_oid *oid, int categoryValue, int delete_flag)
79 {
80     int i;
81     Z_OtherInformation *otherInformation;
82
83     if (!otherInformationP)
84         return 0;
85     otherInformation = *otherInformationP;
86     if (!otherInformation)
87     {
88         if (!odr)
89             return 0;
90         otherInformation = *otherInformationP = (Z_OtherInformation *)
91             odr_malloc (odr, sizeof(*otherInformation));
92         otherInformation->num_elements = 0;
93         otherInformation->list = 0;
94     }
95     for (i = 0; i<otherInformation->num_elements; i++)
96     {
97         if (!oid)
98         {
99             /*  DS: Want does this do? Returns the first element without a category */
100             if (!otherInformation->list[i]->category)
101                 return otherInformation->list[i];
102         }
103         else
104         {
105             if (otherInformation->list[i]->category &&
106                 categoryValue ==
107                 *otherInformation->list[i]->category->categoryValue &&
108                 !oid_oidcmp (oid, otherInformation->list[i]->category->
109                              categoryTypeId))
110             {
111                 Z_OtherInformationUnit *this_list = otherInformation->list[i];
112
113                 if (delete_flag)
114                 {
115                     (otherInformation->num_elements)--;
116                     while (i < otherInformation->num_elements)
117                     {
118                         otherInformation->list[i] =
119                             otherInformation->list[i+1];
120                         i++;
121                     }
122                 }
123                 return this_list;
124             }
125         }
126     }
127     if (!odr)
128         return 0;
129     else
130     {
131         Z_OtherInformationUnit **newlist = (Z_OtherInformationUnit**)
132             odr_malloc(odr, (otherInformation->num_elements+1) *
133                        sizeof(*newlist));
134         for (i = 0; i<otherInformation->num_elements; i++)
135             newlist[i] = otherInformation->list[i];
136         otherInformation->list = newlist;
137         
138         otherInformation->list[i] = (Z_OtherInformationUnit*)
139             odr_malloc (odr, sizeof(Z_OtherInformationUnit));
140         if (oid)
141         {
142             otherInformation->list[i]->category = (Z_InfoCategory*)
143                 odr_malloc (odr, sizeof(Z_InfoCategory));
144             otherInformation->list[i]->category->categoryTypeId = (Odr_oid*)
145                 odr_oiddup (odr, oid);
146             otherInformation->list[i]->category->categoryValue = 
147                 odr_intdup(odr, categoryValue);
148         }
149         else
150             otherInformation->list[i]->category = 0;
151         otherInformation->list[i]->which = Z_OtherInfo_characterInfo;
152         otherInformation->list[i]->information.characterInfo = 0;
153         
154         otherInformation->num_elements = i+1;
155         return otherInformation->list[i];
156     }
157 }
158
159 void yaz_oi_set_string_oid (
160     Z_OtherInformation **otherInformation, ODR odr,
161     const Odr_oid *oid, int categoryValue,
162     const char *str)
163 {
164     Z_OtherInformationUnit *oi =
165         yaz_oi_update(otherInformation, odr, oid, categoryValue, 0);
166     if (!oi)
167         return;
168     oi->which = Z_OtherInfo_characterInfo;
169     oi->information.characterInfo = odr_strdup (odr, str);
170 }
171
172 char *yaz_oi_get_string_oid (
173     Z_OtherInformation **otherInformation,
174     const Odr_oid *oid, int categoryValue, int delete_flag)
175 {
176     Z_OtherInformationUnit *oi;
177     
178     if ((oi = yaz_oi_update(otherInformation, 0, oid, categoryValue,
179                             delete_flag)) &&
180         oi->which == Z_OtherInfo_characterInfo)
181         return oi->information.characterInfo;
182     return 0;
183 }
184
185
186 /*
187  * Local variables:
188  * c-basic-offset: 4
189  * c-file-style: "Stroustrup"
190  * indent-tabs-mode: nil
191  * End:
192  * vim: shiftwidth=4 tabstop=8 expandtab
193  */
194