X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fio%2Fwindows%2Fconsole.cpp;fp=source%2Fio%2Fwindows%2Fconsole.cpp;h=a331133c2bf8bec39762021e9da8f96889aed192;hb=609c9a508cfdc7b42c46c4f21d17639204165a00;hp=0000000000000000000000000000000000000000;hpb=b4806214e905752617691f851717033fd3f266c2;p=libs%2Fcore.git diff --git a/source/io/windows/console.cpp b/source/io/windows/console.cpp new file mode 100644 index 0000000..a331133 --- /dev/null +++ b/source/io/windows/console.cpp @@ -0,0 +1,72 @@ +#include +#include "console.h" +#include "handle_private.h" + +using namespace std; + +namespace { + +DWORD stream_to_sys(Msp::IO::Console::Stream stream) +{ + switch(stream) + { + case Msp::IO::Console::CIN: return STD_INPUT_HANDLE; + case Msp::IO::Console::COUT: return STD_OUTPUT_HANDLE; + case Msp::IO::Console::CERR: return STD_ERROR_HANDLE; + default: throw invalid_argument("stream_to_sys"); + } +} + +} // namespace + + +namespace Msp { +namespace IO { + +void Console::platform_init() +{ + *handle = GetStdHandle(stream_to_sys(stream)); +} + +Console::~Console() +{ } + +void Console::set_local_echo(bool e) +{ + check_access(M_READ); + + DWORD m; + GetConsoleMode(*handle, &m); + SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0)); +} + +void Console::set_line_buffer(bool l) +{ + check_access(M_READ); + + DWORD m; + if(!GetConsoleMode(*handle, &m)) + throw system_error("GetConsoleMode"); + if(!SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0))) + throw system_error("SetConsoleMode"); +} + +void Console::get_size(unsigned &rows, unsigned &cols) +{ + check_access(M_WRITE); + + CONSOLE_SCREEN_BUFFER_INFO sbi; + if(!GetConsoleScreenBufferInfo(*handle, &sbi)) + throw system_error("GetConsoleScreenBufferInfo"); + // Right/bottom coords are inclusive + rows = sbi.srWindow.Bottom+1-sbi.srWindow.Top; + cols = sbi.srWindow.Right+1-sbi.srWindow.Left; +} + +void Console::redirect(Base &other) +{ + SetStdHandle(stream_to_sys(stream), *other.get_handle(mode)); +} + +} // namespace IO +} // namespace Msp