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