Use the console handle for read/write on *nix
authorMikko Rasa <tdb@tdb.fi>
Mon, 15 Dec 2008 11:50:44 +0000 (11:50 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 15 Dec 2008 11:50:44 +0000 (11:50 +0000)
Add Console::get_size

source/console.cpp
source/console.h

index 51617d30452e54e80e4eb0154b64f264272c37b8..f606a09fcceb64406500dac6c00d9b532b108244 100644 (file)
@@ -9,6 +9,7 @@ Distributed under the LGPL
 #include <errno.h>
 #include <fcntl.h>
 #include <termios.h>
+#include <sys/ioctl.h>
 #endif
 #include <msp/core/except.h>
 #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)
index 06fa603c495a7f99950492088d32ae4b8d2d4598..d12697f6e66d54579955d35b21bdad66dd254070 100644 (file)
@@ -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);