From: Mikko Rasa Date: Mon, 15 Dec 2008 11:50:44 +0000 (+0000) Subject: Use the console handle for read/write on *nix X-Git-Tag: io-1.1~3 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=41ea778b16825c3c701c6600a76eaf628831f028;p=libs%2Fcore.git Use the console handle for read/write on *nix Add Console::get_size --- diff --git a/source/console.cpp b/source/console.cpp index 51617d3..f606a09 100644 --- a/source/console.cpp +++ b/source/console.cpp @@ -9,6 +9,7 @@ Distributed under the LGPL #include #include #include +#include #endif #include #include "console.h" @@ -104,6 +105,23 @@ void Console::set_line_buffer(bool l) #endif } +void Console::get_size(unsigned &rows, unsigned &cols) +{ + if(!(mode&M_WRITE)) + throw InvalidState("Size can only be queried from an output console"); + +#ifdef WIN32 + // XXX Figure out how to do this + rows=24; + cols=80; +#else + struct winsize wsz; + ioctl(handle, TIOCGWINSZ, &wsz); + rows=wsz.ws_row; + cols=wsz.ws_col; +#endif +} + Handle Console::get_event_handle() { return 0; @@ -119,7 +137,7 @@ unsigned Console::do_write(const char *buf, unsigned len) if(!WriteFile(handle, buf, len, &ret, 0)) throw SystemError("Writing to console failed", GetLastError()); #else - int ret=::write(1, buf, len); + int ret=::write(handle, buf, len); if(ret==-1) throw SystemError("Writing to console failed", errno); #endif @@ -137,7 +155,7 @@ unsigned Console::do_read(char *buf, unsigned len) if(!ReadFile(handle, buf, len, &ret, 0)) throw SystemError("Reading from console failed", GetLastError()); #else - int ret=::read(0, buf, len); + int ret=::read(handle, buf, len); if(ret==-1) { if(errno==EAGAIN) diff --git a/source/console.h b/source/console.h index 06fa603..d12697f 100644 --- a/source/console.h +++ b/source/console.h @@ -39,6 +39,11 @@ public: */ void set_line_buffer(bool); + /** + Retrieves the size of the Console. Can only be used on an output Console. + */ + void get_size(unsigned &rows, unsigned &cols); + virtual Handle get_event_handle(); protected: virtual unsigned do_write(const char *, unsigned);