Fix init of max_sockets
[yazproxy-moved-to-github.git] / src / charset-converter.cpp
1 /* This file is part of YAZ proxy
2    Copyright (C) 1998-2011 Index Data
3
4 YAZ proxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include <yaz/log.h>
20 #include <yaz/proto.h>
21 #include "proxyp.h"
22
23 Yaz_CharsetConverter::Yaz_CharsetConverter()
24 {
25     m_wrbuf = wrbuf_alloc();
26     m_target_query_charset = 0;
27     m_client_query_charset = 0;
28     m_client_charset_selected = 0;
29 }
30
31 Yaz_CharsetConverter::~Yaz_CharsetConverter()
32 {
33     wrbuf_destroy(m_wrbuf);
34     xfree(m_target_query_charset);
35     xfree(m_client_query_charset);
36 }
37
38 const char *Yaz_CharsetConverter::get_target_query_charset()
39 {
40     return m_target_query_charset;
41 }
42
43 void Yaz_CharsetConverter::set_target_query_charset(const char *s)
44 {
45     xfree(m_target_query_charset);
46     m_target_query_charset = 0;
47     if (s)
48         m_target_query_charset = xstrdup(s);
49 }
50
51 void Yaz_CharsetConverter::set_client_query_charset(const char *s)
52 {
53     xfree(m_client_query_charset);
54     m_client_query_charset = 0;
55     if (s)
56         m_client_query_charset = xstrdup(s);
57 }
58
59 const char *Yaz_CharsetConverter::get_client_query_charset()
60 {
61     return m_client_query_charset;
62 }
63
64 void Yaz_CharsetConverter::set_client_charset_selected(int sel)
65 {
66     m_client_charset_selected = sel;
67 }
68
69 int Yaz_CharsetConverter::get_client_charset_selected()
70 {
71     return m_client_charset_selected;
72 }
73
74 void Yaz_CharsetConverter::convert_type_1(char *buf_in, int len_in,
75                                           char **buf_out, int *len_out,
76                                           ODR o)
77 {
78     wrbuf_rewind(m_wrbuf);
79     wrbuf_iconv_write(m_wrbuf, m_ct, buf_in, len_in);
80     wrbuf_iconv_reset(m_wrbuf, m_ct);
81
82     *len_out = wrbuf_len(m_wrbuf);
83     if (*len_out == 0)
84     {   // we assume conversion failed
85         *buf_out = buf_in;
86         *len_out = len_in;
87     }
88     else
89     {
90         *buf_out = (char*) odr_malloc(o, *len_out);
91         memcpy(*buf_out, wrbuf_buf(m_wrbuf), *len_out);
92     }
93 }
94
95 void Yaz_CharsetConverter::convert_type_1(Z_Term *q, ODR o)
96 {
97     switch(q->which)
98     {
99     case Z_Term_general:
100         convert_type_1((char *) q->u.general->buf, q->u.general->len,
101                        (char **) &q->u.general->buf, &q->u.general->len, o);
102         break;
103     }
104 }
105
106 void Yaz_CharsetConverter::convert_type_1(Z_Operand *q, ODR o)
107 {
108     switch(q->which)
109     {
110     case Z_Operand_APT:
111         convert_type_1(q->u.attributesPlusTerm->term, o);
112         break;
113     case Z_Operand_resultSetId:
114         break;
115     case Z_Operand_resultAttr:
116         break;
117     }
118 }
119
120 void Yaz_CharsetConverter::convert_type_1(Z_RPNStructure *q, ODR o)
121 {
122     switch(q->which)
123     {
124     case Z_RPNStructure_simple:
125         convert_type_1(q->u.simple, o);
126         break;
127     case Z_RPNStructure_complex:
128         convert_type_1(q->u.complex->s1, o);
129         convert_type_1(q->u.complex->s2, o);
130         break;
131     }
132 }
133
134 void Yaz_CharsetConverter::convert_type_1(Z_RPNQuery *q, ODR o)
135 {
136     if (m_target_query_charset && m_client_query_charset)
137     {
138         m_ct = yaz_iconv_open(m_target_query_charset,
139                               m_client_query_charset);
140         if (m_ct)
141         {
142             convert_type_1(q->RPNStructure, o);
143             yaz_iconv_close(m_ct);
144         }
145     }
146 }
147 /*
148  * Local variables:
149  * c-basic-offset: 4
150  * c-file-style: "Stroustrup"
151  * indent-tabs-mode: nil
152  * End:
153  * vim: shiftwidth=4 tabstop=8 expandtab
154  */
155