From 7ffb757e8bca5d6508919b2a864a93b87c921f27 Mon Sep 17 00:00:00 2001 From: Colin Finck Date: Tue, 15 Mar 2011 23:45:38 +0000 Subject: [PATCH] [CRT] - Revert 38185 which synced fputwc to the Wine version for the sake of fixing Winetests. This removed the implicit ANSI conversion, which was honestly checking the wrong flags. The new version correctly checks for WX_TEXT and only does the conversion in this case. This way we also don't fail any additional Winetests. - Simplify streamout_char in the new printf implementation and use _fputtc instead of _flsbuf. This way, we correctly do an implicit ANSI conversion when required. - Fix fgetwc declaration. Thanks to Timo for many hints and assistance. Most of this code is actually his idea :-) Thanks to Olaf for providing a build server to speed up my code tests. See issue #6007 for more details. svn path=/trunk/; revision=51058 --- reactos/lib/sdk/crt/printf/streamout.c | 23 +++++-------------- reactos/lib/sdk/crt/stdio/file.c | 31 +++++++++++++++++++++----- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/reactos/lib/sdk/crt/printf/streamout.c b/reactos/lib/sdk/crt/printf/streamout.c index 8e9ce4f667a..66aae271dd5 100644 --- a/reactos/lib/sdk/crt/printf/streamout.c +++ b/reactos/lib/sdk/crt/printf/streamout.c @@ -16,13 +16,6 @@ #ifdef _UNICODE # define streamout wstreamout # define format_float format_floatw -# define _flsbuf _flswbuf -int __cdecl _flswbuf(int ch, FILE *stream); -#endif - -#ifdef _LIBCNT_ -# undef _flsbuf -# define _flsbuf(chr, stream) _TEOF #endif #define MB_CUR_MAX 10 @@ -234,25 +227,19 @@ static int streamout_char(FILE *stream, int chr) { +#if defined(_USER32_WSPRINTF) || defined(_LIBCNT_) /* Check if the buffer is full */ if (stream->_cnt < sizeof(TCHAR)) - { -#ifdef _USER32_WSPRINTF - return _TEOF; -#else - /* Strings are done now */ - if (stream->_flag & _IOSTRG) return _TEOF; - - /* Flush buffer for files */ - return _flsbuf(chr, stream) != _TEOF; -#endif - } + return 0; *(TCHAR*)stream->_ptr = chr; stream->_ptr += sizeof(TCHAR); stream->_cnt -= sizeof(TCHAR); return 1; +#else + return _fputtc((TCHAR)chr, stream) != _TEOF; +#endif } static diff --git a/reactos/lib/sdk/crt/stdio/file.c b/reactos/lib/sdk/crt/stdio/file.c index a66da9de9d4..cb5f5ae3dd8 100644 --- a/reactos/lib/sdk/crt/stdio/file.c +++ b/reactos/lib/sdk/crt/stdio/file.c @@ -2175,12 +2175,33 @@ size_t CDECL fwrite(const void *ptr, size_t size, size_t nmemb, FILE* file) /********************************************************************* * fputwc (MSVCRT.@) */ -wint_t CDECL fputwc(wint_t wc, FILE* file) +wint_t CDECL fputwc(wchar_t c, FILE* stream) { - wchar_t mwc=wc; - if (fwrite( &mwc, sizeof(mwc), 1, file) != 1) - return WEOF; - return wc; + /* If this is a real file stream (and not some temporary one for + sprintf-like functions), check whether it is opened in text mode. + In this case, we have to perform an implicit conversion to ANSI. */ + if (!(stream->_flag & _IOSTRG) && fdesc[stream->_file].wxflag & WX_TEXT) + { + /* Convert to multibyte in text mode */ + char mbc[MB_LEN_MAX]; + int mb_return; + + mb_return = wctomb(mbc, c); + + if(mb_return == -1) + return WEOF; + + /* Output all characters */ + if (fwrite(mbc, mb_return, 1, stream) != 1) + return WEOF; + } + else + { + if (fwrite(&c, sizeof(c), 1, stream) != 1) + return WEOF; + } + + return c; } /********************************************************************* -- 2.17.1