]> git.tdb.fi Git - libs/core.git/blobdiff - source/console.cpp
Style update: spaces around assignments
[libs/core.git] / source / console.cpp
index 51617d30452e54e80e4eb0154b64f264272c37b8..e860436bc4ea6c8b7150b5634ebb805c5a390010 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"
@@ -29,17 +30,17 @@ Console::Console(unsigned n)
        if(n>2)
                throw InvalidParameterValue("Invalid parameter for Console::Console");
 
-       mode=(n==0 ? M_READ : M_WRITE);
+       mode = (n==0 ? M_READ : M_WRITE);
 
 #ifdef WIN32
        switch(n)
        {
-       case 0: handle=GetStdHandle(STD_INPUT_HANDLE); break;
-       case 1: handle=GetStdHandle(STD_OUTPUT_HANDLE); break;
-       case 2: handle=GetStdHandle(STD_ERROR_HANDLE); break;
+       case 0: handle = GetStdHandle(STD_INPUT_HANDLE); break;
+       case 1: handle = GetStdHandle(STD_OUTPUT_HANDLE); break;
+       case 2: handle = GetStdHandle(STD_ERROR_HANDLE); break;
        }
 #else
-       handle=n;
+       handle = n;
 
        if(handle==0)
                tcgetattr(handle, &orig_attr);
@@ -63,8 +64,8 @@ void Console::set_block(bool b)
        // XXX Dunno how to do this in win32
        (void)b;
 #else
-       int flags=fcntl(0, F_GETFL);
-       flags=(flags&~O_NONBLOCK) | (b?0:O_NONBLOCK);
+       int flags = fcntl(0, F_GETFL);
+       flags = (flags&~O_NONBLOCK) | (b?0:O_NONBLOCK);
        fcntl(0, F_SETFL, flags);
 #endif
 }
@@ -81,7 +82,7 @@ void Console::set_local_echo(bool e)
 #else
        termios t;
        tcgetattr(0, &t);
-       t.c_lflag=(t.c_lflag&~ECHO) | (e?ECHO:0);
+       t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0);
        tcsetattr(0, TCSADRAIN, &t);
 #endif
 }
@@ -99,11 +100,28 @@ void Console::set_line_buffer(bool l)
        // XXX ICANON does more than just set line buffering, may need a bit more thought
        termios t;
        tcgetattr(0, &t);
-       t.c_lflag=(t.c_lflag&~ICANON) | (l?ICANON:0);
+       t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
        tcsetattr(0, TCSADRAIN, &t);
 #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)
@@ -146,7 +164,7 @@ unsigned Console::do_read(char *buf, unsigned len)
                        throw SystemError("Reading from console failed", errno);
        }
        else if(ret==0)
-               eof_flag=true;
+               eof_flag = true;
 #endif
 
        return ret;
@@ -168,9 +186,9 @@ Console &Console::instance(unsigned n)
        throw InvalidParameterValue("Unknown Console instance requested");
 }
 
-Console &cin=Console::instance(0);
-Console &cout=Console::instance(1);
-Console &cerr=Console::instance(2);
+Console &cin = Console::instance(0);
+Console &cout = Console::instance(1);
+Console &cerr = Console::instance(2);
 
 } // namespace IO
 } // namespace Msp