7e5ac4d862ad87bf5e64ed89b2785f0359dddbdc
[yaz-moved-to-github.git] / zutil / charneg.c
1 /* 
2  $ $Id: charneg.c,v 1.1 2002-05-18 09:41:11 oleg Exp $
3  * Helper functions for Character Set and Language Negotiation - 3
4  */
5
6 #include <stdio.h>
7 #include <yaz/proto.h>
8 #include <yaz/z-charneg.h>
9 #include <yaz/charneg.h>
10
11 static Z_External* z_ext_record2(ODR o, int oid_class, int oid_value,
12         const char *buf, int len)
13 {
14         Z_External *p;
15         oident oid;
16                 
17         if (!(p = (Z_External *)odr_malloc(o, sizeof(*p)))) return 0;
18         
19         p->descriptor = 0;
20         p->indirect_reference = 0;
21         
22         oid.proto = PROTO_Z3950;
23         oid.oclass = oid_class;
24         oid.value = oid_value;
25         p->direct_reference = odr_oiddup(o, oid_getoidbyent(&oid));
26         
27         p->which = Z_External_octet;
28         if (!(p->u.octet_aligned = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)))) {
29                 return 0;
30         }
31         if (!(p->u.octet_aligned->buf = (unsigned char *)odr_malloc(o, len))) {
32                 return 0;
33         }
34         p->u.octet_aligned->len = p->u.octet_aligned->size = len;
35         memcpy(p->u.octet_aligned->buf, buf, len);
36         
37         return p;
38 }
39 static Z_OriginProposal_0 *z_get_OriginProposal_0(ODR o, const char *charset)
40 {
41         Z_OriginProposal_0 *p0 =
42                                 (Z_OriginProposal_0*)odr_malloc(o, sizeof(*p0));
43         Z_PrivateCharacterSet *pc =
44                                 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
45
46         memset(p0, 0, sizeof(*p0));
47         memset(pc, 0, sizeof(*pc));
48                         
49         p0->which = Z_OriginProposal_0_private;
50         p0->u.zprivate = pc;
51         
52         pc->which = Z_PrivateCharacterSet_externallySpecified;
53         pc->u.externallySpecified =
54                         z_ext_record2(o, CLASS_RECSYN, VAL_NOP, charset, (strlen(charset)+1));
55         
56         return p0;
57 }
58 static Z_OriginProposal *z_get_OriginProposal(ODR o, const char **charsets,
59         int num_charsets, const char **langs, int num_langs)
60 {       
61         int i;
62         Z_OriginProposal *p = (Z_OriginProposal *) odr_malloc(o, sizeof(*p));
63                 
64         memset(p, 0, sizeof(*p));
65                 
66         p->num_proposedCharSets = num_charsets;
67         p->num_proposedlanguages = num_langs;
68         p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
69         *p->recordsInSelectedCharSets = 1;
70         
71         p->proposedCharSets = 
72                         (Z_OriginProposal_0**) odr_malloc(o,
73                                 num_charsets*sizeof(Z_OriginProposal_0*));
74
75         for (i = 0; i<num_charsets; i++) {
76
77                 p->proposedCharSets[i] =
78                         z_get_OriginProposal_0(o, charsets[i]);
79                         
80         }
81         p->proposedlanguages = 
82                         (char **) odr_malloc(o, num_langs*sizeof(char *));
83
84         for (i = 0; i<num_langs; i++) {
85                 char *plang = (char *)langs[i];
86
87                 p->proposedlanguages[i] = plang;
88                         
89         }
90
91         return p;
92 }
93 static Z_CharSetandLanguageNegotiation *z_get_CharSetandLanguageNegotiation(ODR o)
94 {
95         Z_CharSetandLanguageNegotiation *p =
96                 (Z_CharSetandLanguageNegotiation *) odr_malloc(o, sizeof(*p));
97         
98         memset(p, 0, sizeof(*p));
99         
100         return p;
101 }
102 Z_External *yaz_set_charset_and_lang(ODR o, int oid_class, int oid_value,
103         const char **charsets, int num_charsets,
104         const char **langs, int num_langs)
105 {
106         Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
107         oident oid;
108         
109         p->descriptor = 0;
110         p->indirect_reference = 0;      
111
112         oid.proto = PROTO_Z3950;
113         oid.oclass = oid_class;
114         oid.value = oid_value;
115         p->direct_reference = odr_oiddup(o, oid_getoidbyent(&oid));
116
117         p->which = Z_External_charSetandLanguageNegotiation;
118         p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
119         p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_proposal;
120         p->u.charNeg3->u.proposal =
121                 z_get_OriginProposal(o, charsets, num_charsets,
122                         langs, num_langs);
123
124         return p;
125 }