cf0a5f6e4b62c7c8137ce92fc8031eedf10d08a0
[yaz-moved-to-github.git] / src / iconv_encode_wchar.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 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 <yaz/nmem.h>
24 #include <yaz/snprintf.h>
25 #include "iconv-p.h"
26
27 struct encoder_data
28 {
29     unsigned long compose_char;
30 };
31
32 #if HAVE_WCHAR_H
33 static size_t write_wchar_t(yaz_iconv_t cd, yaz_iconv_encoder_t en,
34                             unsigned long x,
35                             char **outbuf, size_t *outbytesleft)
36 {
37     unsigned char *outp = (unsigned char *) *outbuf;
38
39     if (*outbytesleft >= sizeof(wchar_t))
40     {
41         wchar_t wch = x;
42         memcpy(outp, &wch, sizeof(wch));
43         outp += sizeof(wch);
44         (*outbytesleft) -= sizeof(wch);
45     }
46     else
47     {
48         yaz_iconv_set_errno(cd, YAZ_ICONV_E2BIG);
49         return (size_t)(-1);
50     }
51     *outbuf = (char *) outp;
52     return 0;
53 }
54 #endif
55
56 yaz_iconv_encoder_t yaz_wchar_encoder(const char *tocode,
57                                       yaz_iconv_encoder_t e)
58     
59 {
60 #if HAVE_WCHAR_H
61     if (!yaz_matchstr(tocode, "wchar_t"))
62     {
63         e->write_handle = write_wchar_t;
64         return e;
65     }
66 #endif
67     return 0;
68 }
69
70
71 /*
72  * Local variables:
73  * c-basic-offset: 4
74  * indent-tabs-mode: nil
75  * End:
76  * vim: shiftwidth=4 tabstop=8 expandtab
77  */