time: Fix test failure on FreeBSD.
[gnulib.git] / lib / mbfile.h
blobfe83eb648b1debe9af9df9137001394bee8779ed
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 #ifdef __cplusplus
68 extern "C" {
69 #endif
72 struct mbfile_multi {
73 FILE *fp;
74 bool eof_seen;
75 bool have_pushback;
76 mbstate_t state;
77 unsigned int bufcount;
78 char buf[MBCHAR_BUF_SIZE];
79 struct mbchar pushback;
82 MBFILE_INLINE void
83 mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
85 unsigned int new_bufcount;
86 size_t bytes;
88 /* If EOF has already been seen, don't use getc. This matters if
89 mbf->fp is connected to an interactive tty. */
90 if (mbf->eof_seen)
91 goto eof;
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;
98 return;
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));
107 #else
108 if (mbsinit (&mbf->state))
109 #endif
111 /* Before using mbrtoc32, we need at least one byte. */
112 if (new_bufcount == 0)
114 int c = getc (mbf->fp);
115 if (c == EOF)
117 mbf->eof_seen = true;
118 goto eof;
120 mbf->buf[0] = (unsigned char) c;
121 new_bufcount++;
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];
136 mbc->bytes = 1;
137 mbf->bufcount = 0;
138 return;
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. */
145 for (;;)
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. */
155 bytes = 1;
156 mbc->wc_valid = false;
157 /* Allow the next invocation to continue from a sane state. */
158 mbszero (&mbf->state);
159 break;
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. */
169 bytes = 1;
170 mbc->wc_valid = false;
171 break;
173 else
175 /* Read one more byte and retry mbrtoc32. */
176 int c = getc (mbf->fp);
177 if (c == EOF)
179 /* An incomplete multibyte character at the end. */
180 mbf->eof_seen = true;
181 bytes = new_bufcount;
182 mbc->wc_valid = false;
183 break;
185 mbf->buf[new_bufcount] = (unsigned char) c;
186 new_bufcount++;
189 else
191 #if !GNULIB_MBRTOC32_REGULAR
192 if (bytes == (size_t) -3)
194 /* The previous multibyte sequence produced an additional 32-bit
195 wide character. */
196 mbf->bufcount = new_bufcount;
197 bytes = 0;
199 else
200 #endif
202 bytes = mbf->bufcount + bytes;
203 mbf->bufcount = new_bufcount;
204 if (bytes == 0)
206 /* A null 32-bit wide character was encountered. */
207 bytes = 1;
208 assert (mbf->buf[0] == '\0');
209 assert (mbc->wc == 0);
212 mbc->wc_valid = true;
213 break;
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);
220 mbc->bytes = 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];
231 *p = *(p + bytes);
232 p++;
234 while (--count > 0);
236 return;
238 eof:
239 /* An mbchar_t with bytes == 0 is used to indicate EOF. */
240 mbc->ptr = NULL;
241 mbc->bytes = 0;
242 mbc->wc_valid = false;
243 return;
246 MBFILE_INLINE void
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), \
262 (mbf).bufcount = 0)
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)
271 #ifdef __cplusplus
273 #endif
275 _GL_INLINE_HEADER_END
277 #endif /* _MBFILE_H */