1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements simple ICONV
9 * This implements an interface similar to that of iconv and
10 * is used by YAZ to interface with iconv (if present).
11 * For systems where iconv is not present, this layer
12 * provides a few important conversions: UTF-8, MARC-8, Latin-1.
28 #include <yaz/xmalloc.h>
29 #include <yaz/errno.h>
32 struct yaz_iconv_struct {
36 unsigned long unget_x;
40 struct yaz_iconv_encoder_s encoder;
41 struct yaz_iconv_decoder_s decoder;
45 int yaz_iconv_isbuiltin(yaz_iconv_t cd)
47 return cd->decoder.read_handle && cd->encoder.write_handle;
51 static int prepare_encoders(yaz_iconv_t cd, const char *tocode)
53 if (yaz_marc8_encoder(tocode, &cd->encoder))
55 if (yaz_utf8_encoder(tocode, &cd->encoder))
57 if (yaz_ucs4_encoder(tocode, &cd->encoder))
59 if (yaz_iso_8859_1_encoder(tocode, &cd->encoder))
61 if (yaz_iso_5428_encoder(tocode, &cd->encoder))
63 if (yaz_advancegreek_encoder(tocode, &cd->encoder))
65 if (yaz_wchar_encoder(tocode, &cd->encoder))
70 static int prepare_decoders(yaz_iconv_t cd, const char *tocode)
72 if (yaz_marc8_decoder(tocode, &cd->decoder))
74 if (yaz_iso5426_decoder(tocode, &cd->decoder))
76 if (yaz_utf8_decoder(tocode, &cd->decoder))
78 if (yaz_ucs4_decoder(tocode, &cd->decoder))
80 if (yaz_iso_8859_1_decoder(tocode, &cd->decoder))
82 if (yaz_iso_5428_decoder(tocode, &cd->decoder))
84 if (yaz_advancegreek_decoder(tocode, &cd->decoder))
86 if (yaz_wchar_decoder(tocode, &cd->decoder))
88 if (yaz_danmarc_decoder(tocode, &cd->decoder))
93 yaz_iconv_t yaz_iconv_open(const char *tocode, const char *fromcode)
95 yaz_iconv_t cd = (yaz_iconv_t) xmalloc(sizeof(*cd));
98 cd->encoder.write_handle = 0;
99 cd->encoder.flush_handle = 0;
100 cd->encoder.init_handle = 0;
101 cd->encoder.destroy_handle = 0;
103 cd->decoder.data = 0;
104 cd->decoder.read_handle = 0;
105 cd->decoder.init_handle = 0;
106 cd->decoder.destroy_handle = 0;
108 cd->my_errno = YAZ_ICONV_UNKNOWN;
110 /* a useful hack: if fromcode has leading @,
111 the library not use YAZ's own conversions .. */
112 if (fromcode[0] == '@')
116 prepare_encoders(cd, tocode);
117 prepare_decoders(cd, fromcode);
119 if (cd->decoder.read_handle && cd->encoder.write_handle)
122 cd->iconv_cd = (iconv_t) (-1);
129 cd->iconv_cd = iconv_open(tocode, fromcode);
130 if (cd->iconv_cd == (iconv_t) (-1))
144 size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
145 char **outbuf, size_t *outbytesleft)
151 if (cd->iconv_cd != (iconv_t) (-1))
154 iconv(cd->iconv_cd, inbuf, inbytesleft, outbuf, outbytesleft);
155 if (r == (size_t)(-1))
160 cd->my_errno = YAZ_ICONV_E2BIG;
163 cd->my_errno = YAZ_ICONV_EINVAL;
166 cd->my_errno = YAZ_ICONV_EILSEQ;
169 cd->my_errno = YAZ_ICONV_UNKNOWN;
181 cd->my_errno = YAZ_ICONV_UNKNOWN;
183 if (cd->encoder.init_handle)
184 (*cd->encoder.init_handle)(&cd->encoder);
189 if (cd->decoder.init_handle)
192 size_t r = (cd->decoder.init_handle)(
194 inbuf ? (unsigned char *) *inbuf : 0,
195 inbytesleft ? *inbytesleft : 0,
199 if (cd->my_errno == YAZ_ICONV_EINVAL)
205 *inbytesleft -= no_read;
212 if (!inbuf || !*inbuf)
214 if (outbuf && *outbuf)
217 r = (*cd->encoder.write_handle)(cd, &cd->encoder,
218 cd->unget_x, outbuf, outbytesleft);
219 if (cd->encoder.flush_handle)
220 r = (*cd->encoder.flush_handle)(cd, &cd->encoder,
221 outbuf, outbytesleft);
236 no_read = cd->no_read_x;
240 if (*inbytesleft == 0)
245 x = (*cd->decoder.read_handle)(
247 (unsigned char *) *inbuf, *inbytesleft, &no_read);
256 r = (*cd->encoder.write_handle)(cd, &cd->encoder,
257 x, outbuf, outbytesleft);
260 /* unable to write it. save it because read_handle cannot
262 if (cd->my_errno == YAZ_ICONV_E2BIG)
265 cd->no_read_x = no_read;
271 *inbytesleft -= no_read;
277 int yaz_iconv_error(yaz_iconv_t cd)
282 int yaz_iconv_close(yaz_iconv_t cd)
285 if (cd->iconv_cd != (iconv_t) (-1))
286 iconv_close(cd->iconv_cd);
288 if (cd->encoder.destroy_handle)
289 (*cd->encoder.destroy_handle)(&cd->encoder);
290 if (cd->decoder.destroy_handle)
291 (*cd->decoder.destroy_handle)(&cd->decoder);
296 void yaz_iconv_set_errno(yaz_iconv_t cd, int no)
304 * c-file-style: "Stroustrup"
305 * indent-tabs-mode: nil
307 * vim: shiftwidth=4 tabstop=8 expandtab