Fix copy and paste code
[yaz-moved-to-github.git] / src / iconv_encode_wchar.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file
7  * \brief WCHAR_T iconv encoding / decoding
8  */
9
10 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #include <assert.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <ctype.h>
18 #if HAVE_WCHAR_H
19 #include <wchar.h>
20 #endif
21
22 #include <yaz/xmalloc.h>
23 #include "iconv-p.h"
24
25 struct encoder_data
26 {
27     unsigned long compose_char;
28 };
29
30 #if HAVE_WCHAR_H
31 static size_t write_wchar_t(yaz_iconv_t cd, yaz_iconv_encoder_t en,
32                             unsigned long x,
33                             char **outbuf, size_t *outbytesleft)
34 {
35     unsigned char *outp = (unsigned char *) *outbuf;
36
37     if (*outbytesleft >= sizeof(wchar_t))
38     {
39         wchar_t wch = x;
40         memcpy(outp, &wch, sizeof(wch));
41         outp += sizeof(wch);
42         (*outbytesleft) -= sizeof(wch);
43     }
44     else
45     {
46         yaz_iconv_set_errno(cd, YAZ_ICONV_E2BIG);
47         return (size_t)(-1);
48     }
49     *outbuf = (char *) outp;
50     return 0;
51 }
52 #endif
53
54 yaz_iconv_encoder_t yaz_wchar_encoder(const char *tocode,
55                                       yaz_iconv_encoder_t e)
56     
57 {
58 #if HAVE_WCHAR_H
59     if (!yaz_matchstr(tocode, "wchar_t"))
60     {
61         e->write_handle = write_wchar_t;
62         return e;
63     }
64 #endif
65     return 0;
66 }
67
68 #if HAVE_WCHAR_H
69 static unsigned long read_wchar_t(yaz_iconv_t cd, yaz_iconv_decoder_t d,
70                                   unsigned char *inp,
71                                   size_t inbytesleft, size_t *no_read)
72 {
73     unsigned long x = 0;
74     
75     if (inbytesleft < sizeof(wchar_t))
76     {
77         yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
78         *no_read = 0;
79     }
80     else
81     {
82         wchar_t wch;
83         memcpy(&wch, inp, sizeof(wch));
84         x = wch;
85         *no_read = sizeof(wch);
86     }
87     return x;
88 }
89 #endif
90
91 yaz_iconv_decoder_t yaz_wchar_decoder(const char *fromcode,
92                                       yaz_iconv_decoder_t d)
93     
94 {
95 #if HAVE_WCHAR_H
96     if (!yaz_matchstr(fromcode, "wchar_t"))
97     {
98         d->read_handle = read_wchar_t;
99         return d;
100     }
101 #endif
102     return 0;
103 }
104
105
106 /*
107  * Local variables:
108  * c-basic-offset: 4
109  * c-file-style: "Stroustrup"
110  * indent-tabs-mode: nil
111  * End:
112  * vim: shiftwidth=4 tabstop=8 expandtab
113  */
114