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
72 unsigned int bufcount
;
73 char buf
[MBCHAR_BUF_SIZE
];
74 struct mbchar pushback
;
78 mbfile_multi_getc (struct mbchar
*mbc
, struct mbfile_multi
*mbf
)
80 unsigned int new_bufcount
;
83 /* If EOF has already been seen, don't use getc. This matters if
84 mbf->fp is connected to an interactive tty. */
88 /* Return character pushed back, if there is one. */
89 if (mbf
->have_pushback
)
91 mb_copy (mbc
, &mbf
->pushback
);
92 mbf
->have_pushback
= false;
96 new_bufcount
= mbf
->bufcount
;
98 /* If mbf->state is not in an initial state, some more 32-bit wide character
99 may be hiding in the state. We need to call mbrtoc32 again. */
100 #if GNULIB_MBRTOC32_REGULAR
101 assert (mbsinit (&mbf
->state
));
103 if (mbsinit (&mbf
->state
))
106 /* Before using mbrtoc32, we need at least one byte. */
107 if (new_bufcount
== 0)
109 int c
= getc (mbf
->fp
);
112 mbf
->eof_seen
= true;
115 mbf
->buf
[0] = (unsigned char) c
;
119 /* Handle most ASCII characters quickly, without calling mbrtoc32(). */
120 if (new_bufcount
== 1 && is_basic (mbf
->buf
[0]))
122 /* These characters are part of the POSIX portable character set.
123 For most of them, namely those in the ISO C basic character set,
124 ISO C 99 guarantees that their wide character code is identical to
125 their char code. For the few other ones, this is the case as well,
126 in all locale encodings that are in use. The 32-bit wide character
127 code is the same as well. */
128 mbc
->wc
= mbc
->buf
[0] = mbf
->buf
[0];
129 mbc
->wc_valid
= true;
130 mbc
->ptr
= &mbc
->buf
[0];
137 /* Use mbrtoc32 on an increasing number of bytes. Read only as many bytes
138 from mbf->fp as needed. This is needed to give reasonable interactive
139 behaviour when mbf->fp is connected to an interactive tty. */
142 /* Feed the bytes one by one into mbrtoc32. */
143 bytes
= mbrtoc32 (&mbc
->wc
, &mbf
->buf
[mbf
->bufcount
], new_bufcount
- mbf
->bufcount
, &mbf
->state
);
145 if (bytes
== (size_t) -1)
147 /* An invalid multibyte sequence was encountered. */
148 mbf
->bufcount
= new_bufcount
;
149 /* Return a single byte. */
151 mbc
->wc_valid
= false;
152 /* Allow the next invocation to continue from a sane state. */
153 mbszero (&mbf
->state
);
156 else if (bytes
== (size_t) -2)
158 /* An incomplete multibyte character. */
159 mbf
->bufcount
= new_bufcount
;
160 if (mbf
->bufcount
== MBCHAR_BUF_SIZE
)
162 /* An overlong incomplete multibyte sequence was encountered. */
163 /* Return a single byte. */
165 mbc
->wc_valid
= false;
170 /* Read one more byte and retry mbrtoc32. */
171 int c
= getc (mbf
->fp
);
174 /* An incomplete multibyte character at the end. */
175 mbf
->eof_seen
= true;
176 bytes
= new_bufcount
;
177 mbc
->wc_valid
= false;
180 mbf
->buf
[new_bufcount
] = (unsigned char) c
;
186 #if !GNULIB_MBRTOC32_REGULAR
187 if (bytes
== (size_t) -3)
189 /* The previous multibyte sequence produced an additional 32-bit
191 mbf
->bufcount
= new_bufcount
;
197 bytes
= mbf
->bufcount
+ bytes
;
198 mbf
->bufcount
= new_bufcount
;
201 /* A null 32-bit wide character was encountered. */
203 assert (mbf
->buf
[0] == '\0');
204 assert (mbc
->wc
== 0);
207 mbc
->wc_valid
= true;
212 /* Return the multibyte sequence mbf->buf[0..bytes-1]. */
213 mbc
->ptr
= &mbc
->buf
[0];
214 memcpy (&mbc
->buf
[0], &mbf
->buf
[0], bytes
);
217 mbf
->bufcount
-= bytes
;
218 if (mbf
->bufcount
> 0)
220 /* It's not worth calling memmove() for so few bytes. */
221 unsigned int count
= mbf
->bufcount
;
222 char *p
= &mbf
->buf
[0];
234 /* An mbchar_t with bytes == 0 is used to indicate EOF. */
237 mbc
->wc_valid
= false;
242 mbfile_multi_ungetc (const struct mbchar
*mbc
, struct mbfile_multi
*mbf
)
244 mb_copy (&mbf
->pushback
, mbc
);
245 mbf
->have_pushback
= true;
248 typedef struct mbfile_multi mb_file_t
;
250 typedef mbchar_t mbf_char_t
;
252 #define mbf_init(mbf, stream) \
253 ((mbf).fp = (stream), \
254 (mbf).eof_seen = false, \
255 (mbf).have_pushback = false, \
256 mbszero (&(mbf).state), \
259 #define mbf_getc(mbc, mbf) mbfile_multi_getc (&(mbc), &(mbf))
261 #define mbf_ungetc(mbc, mbf) mbfile_multi_ungetc (&(mbc), &(mbf))
263 #define mb_iseof(mbc) ((mbc).bytes == 0)
265 _GL_INLINE_HEADER_END
267 #endif /* _MBFILE_H */