]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/console.cpp
Use the new Handle class to hide platform details from public headers
[libs/core.git] / source / io / console.cpp
index 9db77377f0c2267ea2997808764b607d36ffcbea..a0e0c28f07cbbd506683cb92166f3def8b57b93a 100644 (file)
@@ -4,8 +4,11 @@
 #include <termios.h>
 #include <sys/ioctl.h>
 #endif
-#include <msp/core/except.h>
+#include <msp/core/systemerror.h>
 #include "console.h"
+#include "handle_private.h"
+
+using namespace std;
 
 namespace {
 
@@ -21,22 +24,22 @@ namespace IO {
 Console::Console(unsigned n)
 {
        if(n>2)
-               throw InvalidParameterValue("Invalid parameter for Console::Console");
+               throw invalid_argument("Console::Console");
 
        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);
+       if(n==0)
+               tcgetattr(*handle, &orig_attr);
 #endif
 
        if(n==0)
@@ -47,7 +50,7 @@ Console::~Console()
 {
 #ifndef WIN32
        if(handle==0)
-               tcsetattr(handle, TCSADRAIN, &orig_attr);
+               tcsetattr(*handle, TCSADRAIN, &orig_attr);
 #endif
 }
 
@@ -57,51 +60,51 @@ void Console::set_block(bool b)
        // XXX Dunno how to do this in win32
        (void)b;
 #else
-       int flags = fcntl(0, F_GETFL);
+       int flags = fcntl(*handle, F_GETFL);
        flags = (flags&~O_NONBLOCK) | (b?0:O_NONBLOCK);
-       fcntl(0, F_SETFL, flags);
+       fcntl(*handle, F_SETFL, flags);
 #endif
 }
 
 void Console::set_local_echo(bool e)
 {
        if(!(mode&M_READ))
-               throw InvalidState("Local echo can only be set on input console");
+               throw invalid_access(M_READ);
 
 #ifdef WIN32
        DWORD m;
-       GetConsoleMode(handle, &m);
-       SetConsoleMode(handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
+       GetConsoleMode(*handle, &m);
+       SetConsoleMode(*handle, (m&~ENABLE_ECHO_INPUT) | (e?ENABLE_ECHO_INPUT:0));
 #else
        termios t;
-       tcgetattr(0, &t);
+       tcgetattr(*handle, &t);
        t.c_lflag = (t.c_lflag&~ECHO) | (e?ECHO:0);
-       tcsetattr(0, TCSADRAIN, &t);
+       tcsetattr(*handle, TCSADRAIN, &t);
 #endif
 }
 
 void Console::set_line_buffer(bool l)
 {
        if(!(mode&M_READ))
-               throw InvalidState("Line buffering can only be set on input console");
+               throw invalid_access(M_READ);
 
 #ifdef WIN32
        DWORD m;
-       GetConsoleMode(handle, &m);
-       SetConsoleMode(handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0));
+       GetConsoleMode(*handle, &m);
+       SetConsoleMode(*handle, (m&~ENABLE_LINE_INPUT) | (l?ENABLE_LINE_INPUT:0));
 #else
        // XXX ICANON does more than just set line buffering, may need a bit more thought
        termios t;
-       tcgetattr(0, &t);
+       tcgetattr(*handle, &t);
        t.c_lflag = (t.c_lflag&~ICANON) | (l?ICANON:0);
-       tcsetattr(0, TCSADRAIN, &t);
+       tcsetattr(*handle, 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");
+               throw invalid_access(M_WRITE);
 
 #ifdef WIN32
        // XXX Figure out how to do this
@@ -109,7 +112,7 @@ void Console::get_size(unsigned &rows, unsigned &cols)
        cols = 80;
 #else
        struct winsize wsz;
-       ioctl(handle, TIOCGWINSZ, &wsz);
+       ioctl(*handle, TIOCGWINSZ, &wsz);
        rows = wsz.ws_row;
        cols = wsz.ws_col;
 #endif
@@ -118,16 +121,16 @@ void Console::get_size(unsigned &rows, unsigned &cols)
 unsigned Console::do_write(const char *buf, unsigned len)
 {
        if(!(mode&M_WRITE))
-               throw InvalidState("Console is not writable");
+               throw invalid_access(M_WRITE);
 
 #ifdef WIN32
        DWORD ret;
-       if(!WriteFile(handle, buf, len, &ret, 0))
-               throw SystemError("Writing to console failed", GetLastError());
+       if(!WriteFile(*handle, buf, len, &ret, 0))
+               throw system_error("WriteFile");
 #else
-       int ret = ::write(handle, buf, len);
+       int ret = ::write(*handle, buf, len);
        if(ret==-1)
-               throw SystemError("Writing to console failed", errno);
+               throw system_error("write");
 #endif
 
        return ret;
@@ -136,20 +139,20 @@ unsigned Console::do_write(const char *buf, unsigned len)
 unsigned Console::do_read(char *buf, unsigned len)
 {
        if(!(mode&M_READ))
-               throw InvalidState("Console is not readable");
+               throw invalid_access(M_READ);
 
 #ifdef WIN32
        DWORD ret;
-       if(!ReadFile(handle, buf, len, &ret, 0))
-               throw SystemError("Reading from console failed", GetLastError());
+       if(!ReadFile(*handle, buf, len, &ret, 0))
+               throw system_error("ReadFile");
 #else
-       int ret = ::read(handle, buf, len);
+       int ret = ::read(*handle, buf, len);
        if(ret==-1)
        {
                if(errno==EAGAIN)
                        return 0;
                else
-                       throw SystemError("Reading from console failed", errno);
+                       throw system_error("read");
        }
        else if(ret==0)
                eof_flag = true;
@@ -158,11 +161,6 @@ unsigned Console::do_read(char *buf, unsigned len)
        return ret;
 }
 
-Handle Console::get_event_handle()
-{
-       return 0;
-}
-
 Console &Console::instance(unsigned n)
 {
        static Console in(0);
@@ -176,7 +174,7 @@ Console &Console::instance(unsigned n)
        case 2: return err;
        }
 
-       throw InvalidParameterValue("Unknown Console instance requested");
+       throw invalid_argument("Console::instance");
 }
 
 Console &cin = Console::instance(0);