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