exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / mbfile.h
blobbea5513f996f947bf6a4ffde1f5aa9227a0cec83
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
21 stream.
23 mb_file_t
24 is the type for multibyte character input stream, usable for variable
25 declarations.
27 mbf_char_t
28 is the type for multibyte character or EOF, usable for variable
29 declarations.
31 mbf_init (mbf, stream)
32 initializes the MB_FILE for reading from stream.
34 mbf_getc (mbc, mbf)
35 reads the next multibyte character from mbf and stores it in mbc.
37 mb_iseof (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);
47 #ifndef _MBFILE_H
48 #define _MBFILE_H 1
50 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE. */
51 #if !_GL_CONFIG_H_INCLUDED
52 #error "Please include config.h first."
53 #endif
55 #include <assert.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <wchar.h>
60 #include "mbchar.h"
62 _GL_INLINE_HEADER_BEGIN
63 #ifndef MBFILE_INLINE
64 # define MBFILE_INLINE _GL_INLINE
65 #endif
67 struct mbfile_multi {
68 FILE *fp;
69 bool eof_seen;
70 bool have_pushback;
71 mbstate_t state;
72 unsigned int bufcount;
73 char buf[MBCHAR_BUF_SIZE];
74 struct mbchar pushback;
77 MBFILE_INLINE void
78 mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
80 unsigned int new_bufcount;
81 size_t bytes;
83 /* If EOF has already been seen, don't use getc. This matters if
84 mbf->fp is connected to an interactive tty. */
85 if (mbf->eof_seen)
86 goto eof;
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;
93 return;
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));
102 #else
103 if (mbsinit (&mbf->state))
104 #endif
106 /* Before using mbrtoc32, we need at least one byte. */
107 if (new_bufcount == 0)
109 int c = getc (mbf->fp);
110 if (c == EOF)
112 mbf->eof_seen = true;
113 goto eof;
115 mbf->buf[0] = (unsigned char) c;
116 new_bufcount++;
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];
131 mbc->bytes = 1;
132 mbf->bufcount = 0;
133 return;
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. */
140 for (;;)
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. */
150 bytes = 1;
151 mbc->wc_valid = false;
152 /* Allow the next invocation to continue from a sane state. */
153 mbszero (&mbf->state);
154 break;
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. */
164 bytes = 1;
165 mbc->wc_valid = false;
166 break;
168 else
170 /* Read one more byte and retry mbrtoc32. */
171 int c = getc (mbf->fp);
172 if (c == EOF)
174 /* An incomplete multibyte character at the end. */
175 mbf->eof_seen = true;
176 bytes = new_bufcount;
177 mbc->wc_valid = false;
178 break;
180 mbf->buf[new_bufcount] = (unsigned char) c;
181 new_bufcount++;
184 else
186 #if !GNULIB_MBRTOC32_REGULAR
187 if (bytes == (size_t) -3)
189 /* The previous multibyte sequence produced an additional 32-bit
190 wide character. */
191 mbf->bufcount = new_bufcount;
192 bytes = 0;
194 else
195 #endif
197 bytes = mbf->bufcount + bytes;
198 mbf->bufcount = new_bufcount;
199 if (bytes == 0)
201 /* A null 32-bit wide character was encountered. */
202 bytes = 1;
203 assert (mbf->buf[0] == '\0');
204 assert (mbc->wc == 0);
207 mbc->wc_valid = true;
208 break;
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);
215 mbc->bytes = 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];
226 *p = *(p + bytes);
227 p++;
229 while (--count > 0);
231 return;
233 eof:
234 /* An mbchar_t with bytes == 0 is used to indicate EOF. */
235 mbc->ptr = NULL;
236 mbc->bytes = 0;
237 mbc->wc_valid = false;
238 return;
241 MBFILE_INLINE void
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), \
257 (mbf).bufcount = 0)
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 */