Updates for yaz/ylog.h
[yazpp-moved-to-github.git] / zlint / test-init-07.cpp
1 /*
2  * Copyright (c) 2004, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: test-init-07.cpp,v 1.2 2004-11-30 21:10:31 adam Exp $
6  */
7
8 #include <yaz/ylog.h>
9 #include <yaz/charneg.h>
10 #include <yaz/otherinfo.h>
11
12 #include <zlint.h>
13
14 Zlint_test_init_07::Zlint_test_init_07()
15 {
16 }
17
18 Zlint_test_init_07::~Zlint_test_init_07()
19 {
20 }
21
22 Zlint_code Zlint_test_init_07::init(Zlint *z)
23 {
24     int len;
25     Z_APDU *apdu = z->create_Z_PDU(Z_APDU_initRequest);
26     Z_InitRequest *init = apdu->u.initRequest;
27     Z_OtherInformation **oi;
28
29     z->msg_check_for("for character set negotiation");
30     
31     /* set all options.. see what target really supports .. */
32     ODR_MASK_SET(init->protocolVersion, Z_ProtocolVersion_3);
33     yaz_oi_APDU(apdu, &oi);
34     if (!oi)
35     {
36         z->msg_check_fail("encoding failure");
37         return TEST_FINISHED;
38     }
39     else
40     {
41         Z_OtherInformationUnit *p0;
42         const char *negotiationCharset[] = {
43             "UTF-8",
44             "UTF-16",
45             "UCS-2",
46             "UCS-4",
47             "ISO-8859-1"
48         };
49         char *yazLang = 0;
50         
51         if ((p0=yaz_oi_update(oi, z->odr_encode(), NULL, 0, 0))) {
52             ODR_MASK_SET(init->options, Z_Options_negotiationModel);
53             
54             p0->which = Z_OtherInfo_externallyDefinedInfo;
55             p0->information.externallyDefinedInfo =
56                 
57                 yaz_set_proposal_charneg(
58                     z->odr_encode(),
59                     negotiationCharset, 5,
60                     (const char**)&yazLang, yazLang ? 1 : 0, 1);
61         }
62     }
63     int r = z->send_Z_PDU(apdu, &len);
64     if (r < 0)
65     {
66         z->msg_check_fail("unable to send init request");
67         return TEST_FINISHED;
68     }
69     return TEST_CONTINUE;
70 }
71
72 Zlint_code Zlint_test_init_07::recv_gdu(Zlint *z, Z_GDU *gdu)
73 {
74     if (gdu->which == Z_GDU_Z3950 &&
75         gdu->u.z3950 && gdu->u.z3950->which == Z_APDU_initResponse)
76     {
77         Z_InitResponse *init = gdu->u.z3950->u.initResponse;
78         int ver = z->initResponseGetVersion(init);
79         int result = init->result ? *init->result : 0;
80
81         if (ODR_MASK_GET(init->options, Z_Options_negotiationModel))
82         {
83             Z_CharSetandLanguageNegotiation *p =
84                 yaz_get_charneg_record(init->otherInfo);
85             
86             if (p) {
87                 
88                 char *charset=NULL, *lang=NULL;
89                 int selected;
90                 NMEM m = nmem_create();
91                 
92                 yaz_get_response_charneg(m, p, &charset, &lang,
93                                          &selected);
94                 z->msg_check_ok();
95                 z->msg_check_info("Accepted character set : %s", charset);
96                 z->msg_check_info("Accepted code language : %s", lang ? lang : "none");
97                 z->msg_check_info("Accepted records in ...: %d", selected );
98                 nmem_destroy(m);
99                 return TEST_FINISHED;
100             }
101         }
102         z->msg_check_notapp();
103     }
104     else
105         z->msg_check_fail("did not receive init response as expected");
106     return TEST_FINISHED;
107 }
108
109