//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
/***
*fgetwc_nolock.inl - get a wide character from a stream
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines fgetwc_nolock() - read a wide character from a stream
*
*Revision History:
*       07-30-04  AC    Moved from fgetwc.c
*
*******************************************************************************/

/***
*_getwc_nolock() -  getwc() core routine (locked version)
*
*Purpose:
*       Core getwc() routine; assumes stream is already locked.
*
*       [See getwc() above for more info.]
*
*Entry: [See getwc()]
*
*Exit:  [See getwc()]
*
*Exceptions:
*
*******************************************************************************/

wint_t __cdecl _fgetwc_nolock (
        FILE *stream
        )
{
#ifndef _NTSUBSET_

        _CHECK_IO_INIT(WEOF);
        if(!(stream->_flag & _IOSTRG) && _textmode_safe(_fileno(stream)) != __IOINFO_TM_ANSI)
        {
            /* This is for files open with "u" - Unicode Mode. We need to read
            2 chars instead of 1. This is required even for UTF8, because read
            converts UTF8 to a regular Wide Char */

            int ch;
            wchar_t wch;
            
            char * p = (char *)&wch;
            
            ch = _getc_nolock(stream);
            if(ch == EOF)
                return WEOF;
            
            *p = (char)ch;
            ++p;
            
            ch = _getc_nolock(stream);
            if(ch == EOF)
                return WEOF;
            
            *p = (char)ch;

            return wch;
        
        }

        if (!(stream->_flag & _IOSTRG) && (_osfile_safe(_fileno(stream)) & 
              FTEXT))
        {
                int size = 1;
                int ch;
                char mbc[4];
                wchar_t wch;
                
                /* text (multi-byte) mode */
                if ((ch = _getc_nolock(stream)) == EOF)
                        return WEOF;

                mbc[0] = (char)ch;

                if (isleadbyte((unsigned char)mbc[0]))
                {
                        if ((ch = _getc_nolock(stream)) == EOF)
                        {
                                ungetc(mbc[0], stream);
                                return WEOF;
                        }
                        mbc[1] = (char)ch;
                        size = 2;
                }
                if (mbtowc(&wch, mbc, size) == -1)
                {
                        /*
                         * Conversion failed! Set errno and return
                         * failure.
                         */
                        errno = EILSEQ;
                        return WEOF;
                }
                return wch;
        }
#endif
        /* binary (Unicode) mode */
        if ((stream->_cnt -= sizeof(wchar_t)) >= 0)
                return *((wchar_t *)(stream->_ptr))++;
        else
                return (wint_t) _filwbuf(stream);
}
