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