|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <conio.h>
|
| #include <corecrt_internal_lowio.h>
|
| #include <stdio.h>
|
| #include <stdlib.h>
|
| #include <string.h>
|
| #include <wchar.h>
|
|
|
|
|
|
|
| namespace
|
| {
|
| struct CharPair
|
| {
|
| unsigned char LeadChar;
|
| unsigned char SecondChar;
|
| };
|
| }
|
|
|
|
|
| static wint_t wchbuf = WEOF;
|
|
|
| extern "C" extern intptr_t __dcrt_lowio_console_input_handle;
|
|
|
| extern "C" CharPair const* __cdecl _getextendedkeycode(KEY_EVENT_RECORD*);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" wint_t __cdecl _getwch()
|
| {
|
| __acrt_lock(__acrt_conio_lock);
|
| wint_t result = 0;
|
| __try
|
| {
|
| result = _getwch_nolock();
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_conio_lock);
|
| }
|
| return result;
|
| }
|
|
|
|
|
| extern "C" wint_t __cdecl _getwche()
|
| {
|
| __acrt_lock(__acrt_conio_lock);
|
| wint_t result = 0;
|
| __try
|
| {
|
| result = _getwche_nolock();
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_conio_lock);
|
| }
|
| return result;
|
| }
|
|
|
|
|
|
|
| extern "C" wint_t __cdecl _getwch_nolock()
|
| {
|
|
|
|
|
| if (wchbuf != WEOF)
|
| {
|
| wchar_t const buffered_wchar = static_cast<wchar_t>(wchbuf & 0xFFFF);
|
| wchbuf = WEOF;
|
| return buffered_wchar;
|
| }
|
|
|
|
|
|
|
| if (__dcrt_lowio_ensure_console_input_initialized() == FALSE)
|
| return WEOF;
|
|
|
|
|
| DWORD old_console_mode;
|
| __dcrt_get_input_console_mode(&old_console_mode);
|
| __dcrt_set_input_console_mode(0);
|
| wint_t result = 0;
|
| __try
|
| {
|
| for ( ; ; )
|
| {
|
|
|
| INPUT_RECORD input_record;
|
| DWORD num_read;
|
| if (__dcrt_read_console_input(&input_record, 1, &num_read) == FALSE)
|
| {
|
| result = WEOF;
|
| __leave;
|
| }
|
|
|
| if (num_read == 0)
|
| {
|
| result = WEOF;
|
| __leave;
|
| }
|
|
|
|
|
| if (input_record.EventType == KEY_EVENT && input_record.Event.KeyEvent.bKeyDown)
|
| {
|
|
|
| wchar_t const c = static_cast<wchar_t>(input_record.Event.KeyEvent.uChar.UnicodeChar);
|
| if (c != 0)
|
| {
|
| result = c;
|
| __leave;
|
| }
|
|
|
|
|
|
|
| CharPair const* const cp = _getextendedkeycode(&input_record.Event.KeyEvent);
|
| if (cp != nullptr)
|
| {
|
| wchbuf = cp->SecondChar;
|
| result = cp->LeadChar;
|
| __leave;
|
| }
|
| }
|
| }
|
| }
|
| __finally
|
| {
|
|
|
| __dcrt_set_input_console_mode(old_console_mode);
|
| }
|
| return result;
|
| }
|
|
|
|
|
|
|
| extern "C" wint_t __cdecl _getwche_nolock()
|
| {
|
|
|
|
|
| if (wchbuf != WEOF)
|
| {
|
| wchar_t const buffered_wchar = static_cast<wchar_t>(wchbuf & 0xFFFF);
|
| wchbuf = WEOF;
|
| return buffered_wchar;
|
| }
|
|
|
|
|
|
|
| wchar_t const gotten_wchar = _getwch_nolock();
|
| if (gotten_wchar == WEOF)
|
| return WEOF;
|
|
|
| if (_putwch_nolock(gotten_wchar) == WEOF)
|
| return WEOF;
|
|
|
| return gotten_wchar;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" wint_t __cdecl _ungetwch(wint_t const c)
|
| {
|
| __acrt_lock(__acrt_conio_lock);
|
| wint_t result = 0;
|
| __try
|
| {
|
| result = _ungetwch_nolock(c);
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_conio_lock);
|
| }
|
| return result;
|
| }
|
|
|
|
|
|
|
| extern "C" wint_t __cdecl _ungetwch_nolock(wint_t const c)
|
| {
|
|
|
| if (c == WEOF || wchbuf != WEOF)
|
| return static_cast<wint_t>(EOF);
|
|
|
| wchbuf = (c & 0xFF);
|
| return wchbuf;
|
| }
|
|
|