diff options
author | tjr <tjr@FreeBSD.org> | 2004-05-22 15:19:41 +0000 |
---|---|---|
committer | tjr <tjr@FreeBSD.org> | 2004-05-22 15:19:41 +0000 |
commit | f5a461b2700c74383086e755fdaed26dc05d848c (patch) | |
tree | f6b3a5846b5b7d1a722dc68d0c48e70c32df2e55 /lib/libc/stdio/fgetwc.c | |
parent | 90c5701a2cddfda7b43823be60c581ef7dbd9bf0 (diff) | |
download | FreeBSD-src-f5a461b2700c74383086e755fdaed26dc05d848c.zip FreeBSD-src-f5a461b2700c74383086e755fdaed26dc05d848c.tar.gz |
Associate a multibyte conversion state object with each stream. Reset it
to the initial state when a stream is opened or seeked upon. Use the
stream's conversion state object instead of a freshly-zeroed one in
fgetwc(), fputwc() and ungetwc().
This is only a performance improvement for now, but it would also be
required in order to support state-dependent encodings.
Diffstat (limited to 'lib/libc/stdio/fgetwc.c')
-rw-r--r-- | lib/libc/stdio/fgetwc.c | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/lib/libc/stdio/fgetwc.c b/lib/libc/stdio/fgetwc.c index 0b83441..4463042 100644 --- a/lib/libc/stdio/fgetwc.c +++ b/lib/libc/stdio/fgetwc.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2002 Tim J. Robbins. + * Copyright (c) 2002-2004 Tim J. Robbins. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -77,34 +77,31 @@ fgetwc(FILE *fp) static __inline wint_t __fgetwc_nbf(FILE *fp) { - static const mbstate_t initial; - mbstate_t mbs; - char buf[MB_LEN_MAX]; size_t n, nconv; int c; + char cc; wchar_t wc; n = 0; - while (n < MB_CUR_MAX) { + for (;;) { if ((c = __sgetc(fp)) == EOF) { if (n == 0) return (WEOF); break; } - buf[n++] = (char)c; - mbs = initial; - nconv = mbrtowc(&wc, buf, n, &mbs); - if (nconv == n) - return (wc); - else if (nconv == 0) - return (L'\0'); + n++; + cc = (char)c; + nconv = mbrtowc(&wc, &cc, 1, &fp->_extra->mbstate); + if (nconv == (size_t)-2) + continue; else if (nconv == (size_t)-1) break; + else if (nconv == 0) + return (L'\0'); + else + return (wc); } - - while (n-- != 0) - __ungetc((unsigned char)buf[n], fp); - errno = EILSEQ; fp->_flags |= __SERR; + errno = EILSEQ; return (WEOF); } |