Debian Codegraph @

ext/charconv/charconv.h

raw
1 /*
2 * charconv.h - character code conversion library
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef GAUCHE_CHARCONV_H
35 #define GAUCHE_CHARCONV_H
36
37 #include <gauche.h>
38 #include <gauche/priv/configP.h>
39 #include "iconv-adapter.h"
40
41 SCM_DECL_BEGIN
42
43 struct ScmConvInfoRec;
44
45 typedef ScmSize ScmConvProc(struct ScmConvInfoRec*, const char*, ScmSize,
46 char*, ScmSize, ScmSize*);
47 typedef ScmSize ScmConvReset(struct ScmConvInfoRec*, char*, ScmSize);
48 typedef ScmSize ScmConvHandler(struct ScmConvInfoRec*, const char **,
49 ScmSize*, char**, ScmSize*);
50
51 /* Packaging conversion context info.*/
52 typedef struct ScmConvInfoRec {
53 ScmConvHandler *jconv; /* jconv handler */
54 ScmConvProc *convert; /* 1-character conversion routine */
55 ScmConvReset *reset; /* reset routine */
56 iconv_t handle; /* iconv handle, if the conversion is
57 handled by iconv */
58 const char *fromCode; /* convert from ... */
59 const char *toCode; /* conver to ... */
60 int istate; /* current input state */
61 int ostate; /* current output state */
62 ScmPort *remote; /* source or drain port */
63 int ownerp; /* do I own remote port? */
64 int remoteClosed; /* true if remore port is closed */
65 int replacep; /* true if we replace unrecognized input
66 with replacement sequence */
67 ScmSize replaceSize; /* size of replaceSeq */
68 const char *replaceSeq; /* the replacement sequence, NULL terminated */
69 ScmSize bufsiz; /* size of conversion buffer */
70 char *buf; /* internal conversion buffer */
71 char *ptr; /* current ptr in the internal conv buf */
72 } ScmConvInfo;
73
74 /* Bitmask for 'flags' argument.
75 Scm_ConversionSupportedP only recognizes CVPORT_ICONV. */
76 enum {
77 CVPORT_OWNER = (1L<<0), /* Close the inner port if the conversion port
78 is closed. */
79 CVPORT_REPLACE = (1L<<1), /* Use replacement character for illegal
80 sequences instead of signaling an error */
81 };
82
83 extern ScmObj Scm_MakeInputConversionPort(ScmPort *source,
84 const char *fromCode,
85 const char *toCode,
86 ScmSize bufsiz,
87 u_long flags);
88 extern ScmObj Scm_MakeOutputConversionPort(ScmPort *sink,
89 const char *toCode,
90 const char *fromCode,
91 ScmSize bufsiz,
92 u_long flags);
93
94 typedef const char *(*ScmCodeGuessingProc)(const char *buf,
95 ScmSize bufsiz,
96 void *data);
97
98 extern const char *Scm_GetCESName(ScmObj code, const char *argname);
99 extern int Scm_ConversionSupportedP(const char *from, const char *to,
100 u_long flags);
101
102 extern void Scm_RegisterCodeGuessingProc(const char *code,
103 ScmCodeGuessingProc proc,
104 void *data);
105
106 extern const char *Scm_GuessCES(const char *code,
107 const char *buf,
108 ScmSize buflen);
109
110 /*
111 * jconv interface
112 *
113 * jconv is a lower-level layer
114 */
115
116 /* jconv error code */
117 #define ILLEGAL_SEQUENCE (-1) /* input contains illegal sequence */
118 #define INPUT_NOT_ENOUGH (-2) /* input terminates prematurely */
119 #define OUTPUT_NOT_ENOUGH (-3) /* output buffer is too small */
120 #define NO_OUTPUT_CHAR (-4) /* char can't be represented in output CES */
121
122 extern ScmConvInfo *jconv_open(const char *toCode,
123 const char *fromCode,
124 int useIconv);
125 extern int jconv_close(ScmConvInfo*);
126 extern ScmSize jconv(ScmConvInfo*, const char **inptr, ScmSize *inroom,
127 char **outptr, ScmSize *outroom);
128 extern ScmSize jconv_reset(ScmConvInfo *, char *outptr, ScmSize outroom);
129 extern void jconv_set_replacement(ScmConvInfo *info);
130
131 /* Given UCS char, return # of bytes required for UTF8 encoding.
132 We have these in char_utf8.h, but it is only available when the
133 native encoding is utf-8. Eventually we need to factor these out.
134 */
135 #define UCS2UTF_NBYTES(ucs) \
136 (((ucs) < 0x80) ? 1 : \
137 (((ucs) < 0x800) ? 2 : \
138 (((ucs) < 0x10000) ? 3 : \
139 (((ucs) < 0x200000) ? 4 : \
140 (((ucs) < 0x4000000) ? 5 : 6)))))
141
142 extern void jconv_ucs4_to_utf8(unsigned int ucs, char *cp);
143 extern int jconv_utf8_to_ucs4(const char *cp,
144 ScmSize size,
145 ScmChar *ucs); /* out */
146
147 SCM_DECL_END
148
149 #endif /*GAUCHE_CHARCONV_H*/
150
151 /*
152 * Local variables:
153 * mode: c
154 * end:
155 */