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