ad615f657fd9bb7b76e58d0ce918690dd05c63fa
[yazproxy-moved-to-github.git] / src / charset-converter.cpp
1 /* $Id: charset-converter.cpp,v 1.2 2005-05-06 06:55:54 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
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 }
32
33 Yaz_CharsetConverter::~Yaz_CharsetConverter()
34 {
35     wrbuf_free(m_wrbuf, 1);
36     xfree(m_target_query_charset);
37     xfree(m_client_query_charset);
38 }
39
40 void Yaz_CharsetConverter::set_target_query_charset(const char *s)
41 {
42     xfree(m_target_query_charset);
43     m_target_query_charset = 0;
44     if (s)
45         m_target_query_charset = xstrdup(s);
46 }
47
48 void Yaz_CharsetConverter::set_client_query_charset(const char *s)
49 {
50     xfree(m_client_query_charset);
51     m_client_query_charset = 0;
52     if (s)
53         m_client_query_charset = xstrdup(s);
54 }
55
56 void Yaz_CharsetConverter::convert_type_1(char *buf_in, int len_in,
57                                           char **buf_out, int *len_out,
58                                           ODR o)
59 {
60     wrbuf_rewind(m_wrbuf);
61     wrbuf_iconv_write(m_wrbuf, m_ct, buf_in, len_in);
62
63     *len_out = wrbuf_len(m_wrbuf);
64     if (*len_out == 0)
65     {   // we assume conversion failed
66         *buf_out = buf_in;
67         *len_out = len_in;
68     }
69     else
70     {
71         *buf_out = (char*) odr_malloc(o, *len_out);
72         memcpy(*buf_out, wrbuf_buf(m_wrbuf), *len_out);
73     }
74 }
75
76 void Yaz_CharsetConverter::convert_type_1(Z_Term *q, ODR o)
77 {
78     switch(q->which)
79     {
80     case Z_Term_general:
81         convert_type_1((char *) q->u.general->buf, q->u.general->len,
82                        (char **) &q->u.general->buf, &q->u.general->len, o);
83         break;
84     }
85 }
86
87 void Yaz_CharsetConverter::convert_type_1(Z_Operand *q, ODR o)
88 {
89     switch(q->which)
90     {
91     case Z_Operand_APT:
92         convert_type_1(q->u.attributesPlusTerm->term, o);
93         break;
94     case Z_Operand_resultSetId:
95         break;
96     case Z_Operand_resultAttr:
97         break;
98     }
99 }
100
101 void Yaz_CharsetConverter::convert_type_1(Z_RPNStructure *q, ODR o)
102 {
103     switch(q->which)
104     {
105     case Z_RPNStructure_simple:
106         convert_type_1(q->u.simple, o);
107         break;
108     case Z_RPNStructure_complex:
109         convert_type_1(q->u.complex->s1, o);
110         convert_type_1(q->u.complex->s2, o);
111         break;
112     }
113 }
114 void Yaz_CharsetConverter::convert_type_1(Z_RPNQuery *q, ODR o)
115 {
116     if (m_target_query_charset && m_client_query_charset)
117     {
118         m_ct = yaz_iconv_open(m_target_query_charset,
119                               m_client_query_charset);
120         if (m_ct)
121         {
122             convert_type_1(q->RPNStructure, o);
123             yaz_iconv_close(m_ct);
124         }
125     }
126 }