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