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