X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Fconsole.cpp;h=a03ad20747bf251c40c57f682e027f760756cb7e;hp=259f80fbdf5ca21802099d9108c779f1bb9c0f53;hb=3f7aa81d9212811b323d89949adcccda212cbed3;hpb=5b541316a8c7bbf8b812c0f1e2dbebaa6563b0ee diff --git a/source/io/console.cpp b/source/io/console.cpp index 259f80f..a03ad20 100644 --- a/source/io/console.cpp +++ b/source/io/console.cpp @@ -14,13 +14,13 @@ using namespace std; namespace { #ifdef WIN32 -DWORD stream_to_sys(Console::Stream stream) +DWORD stream_to_sys(Msp::IO::Console::Stream stream) { switch(stream) { - case INPUT: return STD_INPUT_HANDLE; - case OUTPUT: return STD_OUTPUT_HANDLE; - case ERROR: return STD_ERROR_HANDLE; + 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"); } } @@ -37,25 +37,25 @@ namespace IO { Console::Console(Stream s): stream(s) { - mode = (stream==INPUT ? M_READ : M_WRITE); + mode = (stream==CIN ? M_READ : M_WRITE); #ifdef WIN32 *handle = GetStdHandle(stream_to_sys(stream)); #else *handle = stream; - if(stream==INPUT) + if(stream==CIN) tcgetattr(*handle, &orig_attr); #endif - if(stream==INPUT) + if(stream==CIN) set_events(P_INPUT); } Console::~Console() { #ifndef WIN32 - if(stream==INPUT) + if(stream==CIN) tcsetattr(*handle, TCSADRAIN, &orig_attr); #endif } @@ -99,11 +99,21 @@ void Console::set_line_buffer(bool l) if(!SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0))) throw system_error("SetConsoleMode"); #else - // XXX ICANON does more than just set line buffering, may need a bit more thought termios t; if(tcgetattr(*handle, &t)==-1) throw system_error("tcgetattr"); t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0); + // man termios warns that VMIN and VTIME may share indices with VEOF and VEOL + if(l) + { + t.c_cc[VEOF] = orig_attr.c_cc[VEOF]; + t.c_cc[VEOL] = orig_attr.c_cc[VEOL]; + } + else + { + t.c_cc[VMIN] = 1; + t.c_cc[VTIME] = 0; + } if(tcsetattr(*handle, TCSADRAIN, &t)==-1) throw system_error("tcsetattr"); #endif @@ -114,9 +124,12 @@ void Console::get_size(unsigned &rows, unsigned &cols) check_access(M_WRITE); #ifdef WIN32 - // XXX Figure out how to do this - rows = 24; - cols = 80; + 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; #else struct winsize wsz; if(ioctl(*handle, TIOCGWINSZ, &wsz)==-1) @@ -156,23 +169,23 @@ unsigned Console::do_read(char *buf, unsigned len) Console &Console::instance(Stream s) { - static Console in(INPUT); - static Console out(OUTPUT); - static Console err(ERROR); + static Console in(CIN); + static Console out(COUT); + static Console err(CERR); switch(s) { - case INPUT: return in; - case OUTPUT: return out; - case ERROR: return err; + case CIN: return in; + case COUT: return out; + case CERR: return err; } throw invalid_argument("Console::instance"); } -Console &cin = Console::instance(Console::INPUT); -Console &cout = Console::instance(Console::OUTPUT); -Console &cerr = Console::instance(Console::ERROR); +Console &cin = Console::instance(Console::CIN); +Console &cout = Console::instance(Console::COUT); +Console &cerr = Console::instance(Console::CERR); } // namespace IO } // namespace Msp