For OIDs use Odr_oid type everywhere, i.e. do not assume Odr_oid=int.
[yaz-moved-to-github.git] / src / otherinfo.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: otherinfo.c,v 1.8 2007-05-08 08:22:36 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     const Odr_oid *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 = (Odr_oid*)
142                 odr_oiddup (odr, oid);
143             otherInformation->list[i]->category->categoryValue = 
144                 odr_intdup(odr, 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     const Odr_oid *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 char *yaz_oi_get_string_oid (
170     Z_OtherInformation **otherInformation,
171     const Odr_oid *oid, int categoryValue, int delete_flag)
172 {
173     Z_OtherInformationUnit *oi;
174     
175     if ((oi = yaz_oi_update(otherInformation, 0, oid, categoryValue,
176                             delete_flag)) &&
177         oi->which == Z_OtherInfo_characterInfo)
178         return oi->information.characterInfo;
179     return 0;
180 }
181
182 /*
183  * Local variables:
184  * c-basic-offset: 4
185  * indent-tabs-mode: nil
186  * End:
187  * vim: shiftwidth=4 tabstop=8 expandtab
188  */
189