1 /* Multibyte character I/O: macros for multi-byte encodings.
2 Copyright (C) 2001, 2005, 2009-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Mitsuru Chinen <mchinen@yamato.ibm.com>
18 and Bruno Haible <bruno@clisp.org>. */
20 /* The macros in this file implement multi-byte character input from a
24 is the type for multibyte character input stream, usable for variable
28 is the type for multibyte character or EOF, usable for variable
31 mbf_init (mbf, stream)
32 initializes the MB_FILE for reading from stream.
35 reads the next multibyte character from mbf and stores it in mbc.
38 returns true if mbc represents the EOF value.
40 Here are the function prototypes of the macros.
42 extern void mbf_init (mb_file_t mbf, FILE *stream);
43 extern void mbf_getc (mbf_char_t mbc, mb_file_t mbf);
44 extern bool mb_iseof (const mbf_char_t mbc);
50 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE. */
51 #if !_GL_CONFIG_H_INCLUDED
52 #error "Please include config.h first."
62 _GL_INLINE_HEADER_BEGIN
64 # define MBFILE_INLINE _GL_INLINE
77 unsigned int bufcount
;
78 char buf
[MBCHAR_BUF_SIZE
];
79 struct mbchar pushback
;
83 mbfile_multi_getc (struct mbchar
*mbc
, struct mbfile_multi
*mbf
)
85 unsigned int new_bufcount
;
88 /* If EOF has already been seen, don't use getc. This matters if
89 mbf->fp is connected to an interactive tty. */
93 /* Return character pushed back, if there is one. */
94 if (mbf
->have_pushback
)
96 mb_copy (mbc
, &mbf
->pushback
);
97 mbf
->have_pushback
= false;
101 new_bufcount
= mbf
->bufcount
;
103 /* If mbf->state is not in an initial state, some more 32-bit wide character
104 may be hiding in the state. We need to call mbrtoc32 again. */
105 #if GNULIB_MBRTOC32_REGULAR
106 assert (mbsinit (&mbf
->state
));
108 if (mbsinit (&mbf
->state
))
111 /* Before using mbrtoc32, we need at least one byte. */
112 if (new_bufcount
== 0)
114 int c
= getc (mbf
->fp
);
117 mbf
->eof_seen
= true;
120 mbf
->buf
[0] = (unsigned char) c
;
124 /* Handle most ASCII characters quickly, without calling mbrtoc32(). */
125 if (new_bufcount
== 1 && is_basic (mbf
->buf
[0]))
127 /* These characters are part of the POSIX portable character set.
128 For most of them, namely those in the ISO C basic character set,
129 ISO C 99 guarantees that their wide character code is identical to
130 their char code. For the few other ones, this is the case as well,
131 in all locale encodings that are in use. The 32-bit wide character
132 code is the same as well. */
133 mbc
->wc
= mbc
->buf
[0] = mbf
->buf
[0];
134 mbc
->wc_valid
= true;
135 mbc
->ptr
= &mbc
->buf
[0];
142 /* Use mbrtoc32 on an increasing number of bytes. Read only as many bytes
143 from mbf->fp as needed. This is needed to give reasonable interactive
144 behaviour when mbf->fp is connected to an interactive tty. */
147 /* Feed the bytes one by one into mbrtoc32. */
148 bytes
= mbrtoc32 (&mbc
->wc
, &mbf
->buf
[mbf
->bufcount
], new_bufcount
- mbf
->bufcount
, &mbf
->state
);
150 if (bytes
== (size_t) -1)
152 /* An invalid multibyte sequence was encountered. */
153 mbf
->bufcount
= new_bufcount
;
154 /* Return a single byte. */
156 mbc
->wc_valid
= false;
157 /* Allow the next invocation to continue from a sane state. */
158 mbszero (&mbf
->state
);
161 else if (bytes
== (size_t) -2)
163 /* An incomplete multibyte character. */
164 mbf
->bufcount
= new_bufcount
;
165 if (mbf
->bufcount
== MBCHAR_BUF_SIZE
)
167 /* An overlong incomplete multibyte sequence was encountered. */
168 /* Return a single byte. */
170 mbc
->wc_valid
= false;
175 /* Read one more byte and retry mbrtoc32. */
176 int c
= getc (mbf
->fp
);
179 /* An incomplete multibyte character at the end. */
180 mbf
->eof_seen
= true;
181 bytes
= new_bufcount
;
182 mbc
->wc_valid
= false;
185 mbf
->buf
[new_bufcount
] = (unsigned char) c
;
191 #if !GNULIB_MBRTOC32_REGULAR
192 if (bytes
== (size_t) -3)
194 /* The previous multibyte sequence produced an additional 32-bit
196 mbf
->bufcount
= new_bufcount
;
202 bytes
= mbf
->bufcount
+ bytes
;
203 mbf
->bufcount
= new_bufcount
;
206 /* A null 32-bit wide character was encountered. */
208 assert (mbf
->buf
[0] == '\0');
209 assert (mbc
->wc
== 0);
212 mbc
->wc_valid
= true;
217 /* Return the multibyte sequence mbf->buf[0..bytes-1]. */
218 mbc
->ptr
= &mbc
->buf
[0];
219 memcpy (&mbc
->buf
[0], &mbf
->buf
[0], bytes
);
222 mbf
->bufcount
-= bytes
;
223 if (mbf
->bufcount
> 0)
225 /* It's not worth calling memmove() for so few bytes. */
226 unsigned int count
= mbf
->bufcount
;
227 char *p
= &mbf
->buf
[0];
239 /* An mbchar_t with bytes == 0 is used to indicate EOF. */
242 mbc
->wc_valid
= false;
247 mbfile_multi_ungetc (const struct mbchar
*mbc
, struct mbfile_multi
*mbf
)
249 mb_copy (&mbf
->pushback
, mbc
);
250 mbf
->have_pushback
= true;
253 typedef struct mbfile_multi mb_file_t
;
255 typedef mbchar_t mbf_char_t
;
257 #define mbf_init(mbf, stream) \
258 ((mbf).fp = (stream), \
259 (mbf).eof_seen = false, \
260 (mbf).have_pushback = false, \
261 mbszero (&(mbf).state), \
264 #define mbf_getc(mbc, mbf) mbfile_multi_getc (&(mbc), &(mbf))
266 #define mbf_ungetc(mbc, mbf) mbfile_multi_ungetc (&(mbc), &(mbf))
268 #define mb_iseof(mbc) ((mbc).bytes == 0)
275 _GL_INLINE_HEADER_END
277 #endif /* _MBFILE_H */