764988f5c681eca8008df661cf228467096ebf11
[yaz-moved-to-github.git] / src / iconv_encode_wchar.c
1 /*
2  * Copyright (C) 1995-2008, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  */
6 /**
7  * \file
8  * \brief WCHAR_T iconv encoding / decoding
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <assert.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <ctype.h>
19 #if HAVE_WCHAR_H
20 #include <wchar.h>
21 #endif
22
23 #include <yaz/xmalloc.h>
24 #include <yaz/nmem.h>
25 #include <yaz/snprintf.h>
26 #include "iconv-p.h"
27
28 struct encoder_data
29 {
30     unsigned long compose_char;
31 };
32
33 #if HAVE_WCHAR_H
34 static size_t write_wchar_t(yaz_iconv_t cd, yaz_iconv_encoder_t en,
35                             unsigned long x,
36                             char **outbuf, size_t *outbytesleft)
37 {
38     unsigned char *outp = (unsigned char *) *outbuf;
39
40     if (*outbytesleft >= sizeof(wchar_t))
41     {
42         wchar_t wch = x;
43         memcpy(outp, &wch, sizeof(wch));
44         outp += sizeof(wch);
45         (*outbytesleft) -= sizeof(wch);
46     }
47     else
48     {
49         yaz_iconv_set_errno(cd, YAZ_ICONV_E2BIG);
50         return (size_t)(-1);
51     }
52     *outbuf = (char *) outp;
53     return 0;
54 }
55 #endif
56
57 yaz_iconv_encoder_t yaz_wchar_encoder(const char *tocode,
58                                       yaz_iconv_encoder_t e)
59     
60 {
61 #if HAVE_WCHAR_H
62     if (!yaz_matchstr(tocode, "wchar_t"))
63     {
64         e->write_handle = write_wchar_t;
65         return e;
66     }
67 #endif
68     return 0;
69 }
70
71
72 /*
73  * Local variables:
74  * c-basic-offset: 4
75  * indent-tabs-mode: nil
76  * End:
77  * vim: shiftwidth=4 tabstop=8 expandtab
78  */