New file needed for character set conversions
[yazproxy-moved-to-github.git] / src / charset-converter.cpp
1 /* $Id: charset-converter.cpp,v 1.1 2005-05-04 08:35:03 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/proto.h>
23 #include "proxyp.h"
24
25 Yaz_CharsetConverter::Yaz_CharsetConverter()
26 {
27     m_wrbuf = wrbuf_alloc();
28     m_target_query_charset = 0;
29     m_client_query_charset = 0;
30 }
31
32 Yaz_CharsetConverter::~Yaz_CharsetConverter()
33 {
34     wrbuf_free(m_wrbuf, 1);
35     xfree(m_target_query_charset);
36     xfree(m_client_query_charset);
37 }
38
39 void Yaz_CharsetConverter::set_target_query_charset(const char *s)
40 {
41     xfree(m_target_query_charset);
42     m_target_query_charset = 0;
43     if (s)
44         m_target_query_charset = xstrdup(s);
45 }
46
47 void Yaz_CharsetConverter::set_client_query_charset(const char *s)
48 {
49     xfree(m_client_query_charset);
50     m_client_query_charset = 0;
51     if (s)
52         m_client_query_charset = xstrdup(s);
53 }
54
55 void Yaz_CharsetConverter::convert_type_1(char *buf_in, int len_in,
56                                           char **buf_out, int *len_out,
57                                           ODR o)
58 {
59     wrbuf_rewind(m_wrbuf);
60     wrbuf_iconv_write(m_wrbuf, m_ct, buf_in, len_in);
61     
62     *len_out = wrbuf_len(m_wrbuf);
63     *buf_out = (char*) odr_malloc(o, *len_out);
64     memcpy(*buf_out, wrbuf_buf(m_wrbuf), *len_out);
65 }
66
67 void Yaz_CharsetConverter::convert_type_1(Z_Term *q, ODR o)
68 {
69     switch(q->which)
70     {
71     case Z_Term_general:
72         convert_type_1((char *) q->u.general->buf, q->u.general->len,
73                        (char **) &q->u.general->buf, &q->u.general->len, o);
74         break;
75     }
76 }
77
78 void Yaz_CharsetConverter::convert_type_1(Z_Operand *q, ODR o)
79 {
80     switch(q->which)
81     {
82     case Z_Operand_APT:
83         convert_type_1(q->u.attributesPlusTerm->term, o);
84         break;
85     case Z_Operand_resultSetId:
86         break;
87     case Z_Operand_resultAttr:
88         break;
89     }
90 }
91
92 void Yaz_CharsetConverter::convert_type_1(Z_RPNStructure *q, ODR o)
93 {
94     switch(q->which)
95     {
96     case Z_RPNStructure_simple:
97         convert_type_1(q->u.simple, o);
98         break;
99     case Z_RPNStructure_complex:
100         convert_type_1(q->u.complex->s1, o);
101         convert_type_1(q->u.complex->s2, o);
102         break;
103     }
104 }
105 void Yaz_CharsetConverter::convert_type_1(Z_RPNQuery *q, ODR o)
106 {
107     if (m_target_query_charset && m_client_query_charset)
108     {
109         m_ct = yaz_iconv_open(m_target_query_charset,
110                               m_client_query_charset);
111         if (m_ct)
112         {
113             convert_type_1(q->RPNStructure, o);
114             yaz_iconv_close(m_ct);
115         }
116     }
117 }