YAZ 5.35.1
iconv_encode_wchar.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
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#if HAVE_WCHAR_H
18#include <wchar.h>
19#endif
20
21#include <yaz/xmalloc.h>
22#include "iconv-p.h"
23
24struct encoder_data
25{
26 unsigned long compose_char;
27};
28
29#if HAVE_WCHAR_H
30static size_t write_wchar_t(yaz_iconv_t cd, yaz_iconv_encoder_t en,
31 unsigned long x,
32 char **outbuf, size_t *outbytesleft)
33{
34 unsigned char *outp = (unsigned char *) *outbuf;
35
36 if (*outbytesleft >= sizeof(wchar_t))
37 {
38 wchar_t wch = x;
39 memcpy(outp, &wch, sizeof(wch));
40 outp += sizeof(wch);
41 (*outbytesleft) -= sizeof(wch);
42 }
43 else
44 {
46 return (size_t)(-1);
47 }
48 *outbuf = (char *) outp;
49 return 0;
50}
51#endif
52
55
56{
57#if HAVE_WCHAR_H
58 if (!yaz_matchstr(tocode, "wchar_t"))
59 {
60 e->write_handle = write_wchar_t;
61 return e;
62 }
63#endif
64 return 0;
65}
66
67#if HAVE_WCHAR_H
68static unsigned long read_wchar_t(yaz_iconv_t cd, yaz_iconv_decoder_t d,
69 unsigned char *inp,
70 size_t inbytesleft, size_t *no_read)
71{
72 unsigned long x = 0;
73
74 if (inbytesleft < sizeof(wchar_t))
75 {
76 yaz_iconv_set_errno(cd, YAZ_ICONV_EINVAL); /* incomplete input */
77 *no_read = 0;
78 }
79 else
80 {
81 wchar_t wch;
82 memcpy(&wch, inp, sizeof(wch));
83 x = wch;
84 *no_read = sizeof(wch);
85 }
86 return x;
87}
88#endif
89
92
93{
94#if HAVE_WCHAR_H
95 if (!yaz_matchstr(fromcode, "wchar_t"))
96 {
97 d->read_handle = read_wchar_t;
98 return d;
99 }
100#endif
101 return 0;
102}
103
104
105/*
106 * Local variables:
107 * c-basic-offset: 4
108 * c-file-style: "Stroustrup"
109 * indent-tabs-mode: nil
110 * End:
111 * vim: shiftwidth=4 tabstop=8 expandtab
112 */
113
Header for errno utilities.
Internal header for iconv.
void yaz_iconv_set_errno(yaz_iconv_t cd, int no)
Definition siconv.c:298
yaz_iconv_encoder_t yaz_wchar_encoder(const char *tocode, yaz_iconv_encoder_t e)
yaz_iconv_decoder_t yaz_wchar_decoder(const char *fromcode, yaz_iconv_decoder_t d)
int yaz_matchstr(const char *s1, const char *s2)
match strings - independent of case and '-'
Definition matchstr.c:42
unsigned long compose_char
unsigned long(* read_handle)(yaz_iconv_t cd, yaz_iconv_decoder_t d, unsigned char *inbuf, size_t inbytesleft, size_t *no_read)
Definition iconv-p.h:86
size_t(* write_handle)(yaz_iconv_t cd, yaz_iconv_encoder_t e, unsigned long x, char **outbuf, size_t *outbytesleft)
Definition iconv-p.h:45
Header for memory handling functions.
#define YAZ_ICONV_E2BIG
error code: Not sufficient room for output buffer
Definition yaz-iconv.h:47
#define YAZ_ICONV_EINVAL
error code: An incomplete multibyte sequence is in input buffer
Definition yaz-iconv.h:51