1 /* This file is part of YAZ proxy
2 Copyright (C) 1998-2011 Index Data
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
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
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
20 #include <yaz/proto.h>
23 Yaz_CharsetConverter::Yaz_CharsetConverter()
25 m_wrbuf = wrbuf_alloc();
26 m_target_query_charset = 0;
27 m_client_query_charset = 0;
28 m_client_charset_selected = 0;
31 Yaz_CharsetConverter::~Yaz_CharsetConverter()
33 wrbuf_destroy(m_wrbuf);
34 xfree(m_target_query_charset);
35 xfree(m_client_query_charset);
38 const char *Yaz_CharsetConverter::get_target_query_charset()
40 return m_target_query_charset;
43 void Yaz_CharsetConverter::set_target_query_charset(const char *s)
45 xfree(m_target_query_charset);
46 m_target_query_charset = 0;
48 m_target_query_charset = xstrdup(s);
51 void Yaz_CharsetConverter::set_client_query_charset(const char *s)
53 xfree(m_client_query_charset);
54 m_client_query_charset = 0;
56 m_client_query_charset = xstrdup(s);
59 const char *Yaz_CharsetConverter::get_client_query_charset()
61 return m_client_query_charset;
64 void Yaz_CharsetConverter::set_client_charset_selected(int sel)
66 m_client_charset_selected = sel;
69 int Yaz_CharsetConverter::get_client_charset_selected()
71 return m_client_charset_selected;
74 void Yaz_CharsetConverter::convert_type_1(char *buf_in, int len_in,
75 char **buf_out, int *len_out,
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);
82 *len_out = wrbuf_len(m_wrbuf);
84 { // we assume conversion failed
90 *buf_out = (char*) odr_malloc(o, *len_out);
91 memcpy(*buf_out, wrbuf_buf(m_wrbuf), *len_out);
95 void Yaz_CharsetConverter::convert_type_1(Z_Term *q, ODR o)
100 convert_type_1((char *) q->u.general->buf, q->u.general->len,
101 (char **) &q->u.general->buf, &q->u.general->len, o);
106 void Yaz_CharsetConverter::convert_type_1(Z_Operand *q, ODR o)
111 convert_type_1(q->u.attributesPlusTerm->term, o);
113 case Z_Operand_resultSetId:
115 case Z_Operand_resultAttr:
120 void Yaz_CharsetConverter::convert_type_1(Z_RPNStructure *q, ODR o)
124 case Z_RPNStructure_simple:
125 convert_type_1(q->u.simple, o);
127 case Z_RPNStructure_complex:
128 convert_type_1(q->u.complex->s1, o);
129 convert_type_1(q->u.complex->s2, o);
134 void Yaz_CharsetConverter::convert_type_1(Z_RPNQuery *q, ODR o)
136 if (m_target_query_charset && m_client_query_charset)
138 m_ct = yaz_iconv_open(m_target_query_charset,
139 m_client_query_charset);
142 convert_type_1(q->RPNStructure, o);
143 yaz_iconv_close(m_ct);
148 void Yaz_CharsetConverter::convert_term(Z_Term *q, ODR o)
150 if (m_target_query_charset && m_client_query_charset)
152 m_ct = yaz_iconv_open(m_target_query_charset,
153 m_client_query_charset);
156 convert_type_1(q, o);
157 yaz_iconv_close(m_ct);
164 * c-file-style: "Stroustrup"
165 * indent-tabs-mode: nil
167 * vim: shiftwidth=4 tabstop=8 expandtab