#include <cstring>
+#include <stdexcept>
#include "buffered.h"
-#include "except.h"
using namespace std;
begin=end = buf;
if(len<used)
- throw Exception("Couldn't flush all data");
+ throw runtime_error("Couldn't flush all data");
}
}
else if(cur_op==M_READ)
Handle Buffered::get_event_handle()
{
- throw Exception("Buffered doesn't support events");
+ throw logic_error("Buffered doesn't support events");
}
} // namespace IO
#include <termios.h>
#include <sys/ioctl.h>
#endif
-#include <msp/core/except.h>
+#include <msp/core/systemerror.h>
#include "console.h"
+using namespace std;
+
namespace {
#ifndef WIN32
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);
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;
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;
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
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());
+ throw system_error("WriteFile");
#else
int ret = ::write(handle, buf, len);
if(ret==-1)
- throw SystemError("Writing to console failed", errno);
+ throw system_error("write");
#endif
return ret;
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());
+ throw system_error("ReadFile");
#else
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;
case 2: return err;
}
- throw InvalidParameterValue("Unknown Console instance requested");
+ throw invalid_argument("Console::instance");
}
Console &cin = Console::instance(0);
+++ /dev/null
-#ifndef MSP_IO_EXCEPT_H_
-#define MSP_IO_EXCEPT_H_
-
-#include <msp/core/except.h>
-
-namespace Msp {
-namespace IO {
-
-class FileNotFound: public Exception
-{
-public:
- FileNotFound(const std::string &w_, const std::string &f): Exception(w_), filename(f) { }
- const std::string &get_filename() { return filename; }
- ~FileNotFound() throw() { }
-private:
- std::string filename;
-};
-
-} // namespace IO
-} // namespace Msp
-
-#endif
#include <fcntl.h>
#include <unistd.h>
#endif
+#include <msp/core/systemerror.h>
#include <msp/strings/formatter.h>
-#include "except.h"
#include "file.h"
using namespace std;
File::File(const string &fn, Mode m, CreateMode cm)
{
if(!(m&M_RDWR))
- throw InvalidParameterValue("Invalid read/write mode");
+ throw invalid_argument("File::File mode");
if(cm&~(C_CREATE|C_TRUNCATE))
- throw InvalidParameterValue("Invalid create mode");
+ throw invalid_argument("File::File create");
mode = m;
{
int err = GetLastError();
if(err==ERROR_FILE_NOT_FOUND)
- throw FileNotFound("Can't find file "+fn, fn);
+ throw file_not_found(fn);
else
- throw SystemError(format("Can't open file '%s'", fn), GetLastError());
+ throw system_error(format("CreateFile(%s)", fn), err);
}
#else
int flags = 0;
{
int err = errno;
if(err==ENOENT)
- throw FileNotFound("Can't find file "+fn, fn);
+ throw file_not_found(fn);
else
- throw SystemError(format("Can't open file '%s'", fn), err);
+ throw system_error(format("open(%s)", fn), err);
}
#endif
seek(0, S_END);
DWORD ret;
if(WriteFile(handle, buf, size, &ret, 0)==0)
- throw SystemError("Writing to file failed", GetLastError());
+ throw system_error("WriteFile");
#else
int ret = ::write(handle, buf, size);
if(ret==-1)
if(errno==EAGAIN)
return 0;
else
- throw SystemError("Writing to file failed", errno);
+ throw system_error("write");
}
#endif
#ifdef WIN32
DWORD ret;
if(ReadFile(handle, buf, size, &ret, 0)==0)
- throw SystemError("Reading from file failed", GetLastError());
+ throw system_error("ReadFile");
#else
int ret = ::read(handle, buf, size);
if(ret==-1)
if(errno==EAGAIN)
return 0;
else
- throw SystemError("Reading from file failed", errno);
+ throw system_error("read");
}
#endif
#ifdef WIN32
DWORD ret = SetFilePointer(handle, off, 0, type);
if(ret==INVALID_SET_FILE_POINTER)
- throw SystemError("Seek failed", GetLastError());
+ throw system_error("SetFilePointer");
#else
int ret = lseek(handle, off, type);
if(ret==-1)
- throw SystemError("Seek failed", errno);
+ throw system_error("lseek");
#endif
eof_flag = false;
#ifdef WIN32
DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT);
if(ret==INVALID_SET_FILE_POINTER)
- throw SystemError("Tell failed", GetLastError());
+ throw system_error("SetFilePointer");
#else
int ret = lseek(handle, 0, SEEK_CUR);
if(ret==-1)
- throw SystemError("Tell failed", errno);
+ throw system_error("lseek");
#endif
return ret;
void File::check_access(Mode m) const
{
- if(handle==MSP_IO_INVALID_HANDLE)
- throw InvalidState("File is not open");
- if(m==M_READ && !(mode&M_READ))
- throw InvalidState("File is not readable");
- if(m==M_WRITE && !(mode&M_WRITE))
- throw InvalidState("File is not writable");
+ if(handle==MSP_IO_INVALID_HANDLE || (m && !(mode&m)))
+ throw invalid_access(m);
}
} // namespace IO
#ifndef MSP_IO_FILE_H_
#define MSP_IO_FILE_H_
+#include <stdexcept>
#include <string>
#include "base.h"
#include "buffered.h"
namespace Msp {
namespace IO {
+class file_not_found: public std::runtime_error
+{
+public:
+ file_not_found(const std::string &fn): std::runtime_error(fn) { }
+};
+
+
/**
A class for reading and writing files.
#include <algorithm>
#include <cstring>
-#include "except.h"
#include "memory.h"
using namespace std;
else if(type==S_END)
new_pos = end+off;
else
- throw InvalidParameterValue("Invalid seek type");
+ throw invalid_argument("Memory::seek");
if(new_pos<begin || new_pos>end)
- throw InvalidParameterValue("Invalid seek offset");
+ throw out_of_range("Memory::seek");
pos = new_pos;
return pos-begin;
Handle Memory::get_event_handle()
{
- throw Exception("Memory doesn't support events");
+ throw logic_error("Memory doesn't support events");
}
void Memory::check_mode(Mode m) const
{
- if(m==M_WRITE)
- {
- if(!(mode&M_WRITE))
- throw InvalidState("Memory is not writable");
- if(pos==end)
- throw InvalidState("Attempt to write past end of Memory");
- }
+ if(m==M_WRITE && !(mode&M_WRITE))
+ throw invalid_access(M_WRITE);
}
} // namespace IO
--- /dev/null
+#include "mode.h"
+
+using namespace std;
+
+namespace Msp {
+namespace IO {
+
+invalid_access::invalid_access(Mode m):
+ logic_error(m==M_READ ? "read" : m==M_WRITE ? "write" : "generic")
+{ }
+
+} // namespace IO
+} // namespace Msp
#ifndef MSP_IO_MODE_H_
#define MSP_IO_MODE_H_
+#include <stdexcept>
+
namespace Msp {
namespace IO {
inline Mode operator~(Mode m)
{ return Mode(~static_cast<int>(m)); }
+
+class invalid_access: public std::logic_error
+{
+public:
+ invalid_access(Mode);
+};
+
} // namespace IO
} // namespace Msp
#include <fcntl.h>
#include <errno.h>
#endif
+#include <msp/core/systemerror.h>
#include <msp/strings/formatter.h>
#include "pipe.h"
string name = format("\\\\.\\pipe\\%u.%p", GetCurrentProcessId(), this);
handle[0] = CreateNamedPipe(name.c_str(), PIPE_ACCESS_INBOUND|FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE, 1, 1024, 1024, 0, 0);
if(handle[0]==INVALID_HANDLE_VALUE)
- throw SystemError("Unable to create pipe", GetLastError());
+ throw system_error("CreateNamedPipe");
handle[1] = CreateFile(name.c_str(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(handle[1]==INVALID_HANDLE_VALUE)
{
unsigned err = GetLastError();
CloseHandle(handle[0]);
- throw SystemError("Unable to create pipe", err);
+ throw system_error(format("CreateFile(%s)", name), err);
}
overlapped = 0;
buf_next = buffer;
#else
if(pipe(handle)==-1)
- throw SystemError("Unable to create pipe", errno);
+ throw system_error("pipe");
#endif
set_events(P_INPUT);
#ifdef WIN32
DWORD ret;
if(!WriteFile(handle[1], buf, size, &ret, 0))
- throw SystemError("Writing to pipe failed", GetLastError());
+ throw system_error("WriteFile");
#else
int ret = ::write(handle[1], buf, size);
if(ret==-1)
if(errno==EAGAIN)
return 0;
else
- throw SystemError("Writing to pipe failed", errno);
+ throw system_error("write");
}
#endif
{
DWORD ret;
if(!GetOverlappedResult(handle[0], overlapped, &ret, !buf_avail))
- throw SystemError("Reading from pipe failed", GetLastError());
+ throw system_error("GetOverlappedResult");
else
{
buf_avail += ret;
if(errno==EAGAIN)
return 0;
else
- throw SystemError("Reading from pipe failed", errno);
+ throw system_error("read");
}
#endif
{
unsigned err = GetLastError();
if(err!=ERROR_IO_PENDING)
- throw SystemError("Failed to start an overlapped read", err);
+ throw system_error("ReadFile");
}
else
{
#include <errno.h>
+#include <stdexcept>
+#include <msp/core/systemerror.h>
#include <msp/strings/formatter.h>
#include <msp/time/units.h>
-#include "except.h"
#include "base.h"
#include "poll.h"
+using namespace std;
+
namespace {
using namespace Msp;
int result = 0;
if(event&~(P_INPUT|P_PRIO|P_OUTPUT))
- throw InvalidParameterValue("Invalid poll events");
+ throw invalid_argument("sys_poll_event");
#ifndef WIN32
if(event&P_INPUT)
if(ret==WAIT_OBJECT_0)
return pe;
else if(ret==WAIT_FAILED)
- throw SystemError("Poll failed", GetLastError());
+ throw system_error("WaitForSingleObject");
return P_NONE;
#else
if(errno==EINTR)
return P_NONE;
else
- throw SystemError("Poll failed", errno);
+ throw system_error("poll");
}
return poll_event_from_sys(pfd.revents);
{
#ifdef WIN32
if(objects.size()>=MAXIMUM_WAIT_OBJECTS)
- throw InvalidState("Maximum number of wait objects reached");
+ throw logic_error("Maximum number of wait objects reached");
#endif
objects.insert(SlotMap::value_type(&obj, Slot(&obj, ev)));
int Poller::poll(const Time::TimeDelta &timeout)
{
if(timeout<Time::zero)
- throw InvalidParameterValue("Invalid timeout");
+ throw invalid_argument("Poller::poll");
return do_poll(static_cast<int>(timeout/Time::msec));
}
return 1;
}
else if(ret==WAIT_FAILED)
- throw SystemError("Poll failed", GetLastError());
+ throw system_error("WaitForMultipleObjects");
return 0;
#else
if(errno==EINTR)
return 0;
else
- throw SystemError("Poll failed", errno);
+ throw system_error("poll");
}
int n = ret;
PollEvent poll(Base &obj, PollEvent pe, const Time::TimeDelta &timeout)
{
if(timeout<Time::zero)
- throw InvalidParameterValue("Invalid timeout");
+ throw invalid_argument("poll");
return do_poll(obj, pe, static_cast<int>(timeout/Time::msec));
}
#ifdef WIN32
#include <windows.h>
#endif
-#include "except.h"
+#include <stdexcept>
#include "seek.h"
+using namespace std;
+
namespace Msp {
namespace IO {
return SEEK_END;
#endif
- throw InvalidParameterValue("Invalid seek type");
+ throw invalid_argument("Invalid seek type");
}
} // namespace IO
#include <fcntl.h>
#include <errno.h>
#endif
+#include <msp/core/systemerror.h>
#include <msp/strings/formatter.h>
-#include "except.h"
#include "serial.h"
using namespace std;
{
#ifdef WIN32
if(SetCommState(handle, &state)==0)
- throw SystemError("Cannot set serial port parameters", GetLastError());
+ throw system_error("SetCommState");
#else
if(tcsetattr(handle, TCSADRAIN, &state)==-1)
- throw SystemError("Cannot set serial port parameters", errno);
+ throw system_error("tcsetattr");
#endif
}
case 57600: speed = B57600; break;
case 115200: speed = B115200; break;
case 230400: speed = B230400; break;
- default: throw InvalidParameterValue("Invalid baud rate");
+ default: throw invalid_argument("set_baud_rate");
}
cfsetospeed(&state, speed);
case 6: flag = CS6; break;
case 7: flag = CS7; break;
case 8: flag = CS8; break;
- default: throw InvalidParameterValue("Invalid data bit count");
+ default: throw invalid_argument("set_data_bits");
}
state.c_cflag = (state.c_cflag&~CSIZE)|flag;
case Serial::NONE: state.Parity = NOPARITY; break;
case Serial::EVEN: state.Parity = EVENPARITY; break;
case Serial::ODD: state.Parity = ODDPARITY; break;
- default: throw InvalidParameterValue("Invalid parity");
+ default: throw invalid_argument("set_parity");
}
#else
tcflag_t flag;
case Serial::NONE: flag = 0; break;
case Serial::EVEN: flag = PARENB; break;
case Serial::ODD: flag = PARENB|PARODD; break;
- default: throw InvalidParameterValue("Invalid parity");
+ default: throw invalid_argument("set_parity");
}
state.c_cflag = (state.c_cflag&~(PARENB|PARODD))|flag;
{
case 1: state.StopBits = ONESTOPBIT; break;
case 2: state.StopBits = TWOSTOPBITS; break;
- default: throw InvalidParameterValue("Invalid stop bit count");
+ default: throw invalid_argument("set_stop_bits");
}
#else
tcflag_t flag;
{
case 1: flag = 0; break;
case 2: flag = CSTOPB; break;
- default: throw InvalidParameterValue("Invalid stop bit count");
+ default: throw invalid_argument("set_stop_bits");
}
state.c_cflag = (state.c_cflag&~CSTOPB)|flag;
handle = CreateFile(port.c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(handle==INVALID_HANDLE_VALUE)
- throw SystemError(format("Can't open serial port '%s'", port), GetLastError());
+ throw system_error(format("CreateFile(%s)", port));
mode = M_READ|M_WRITE;
COMMTIMEOUTS timeouts;
handle = open(port.c_str(), O_RDWR);
if(handle==-1)
- throw SystemError(format("Can't open serial port '%s'", port), errno);
+ throw system_error(format("open(%s)", port));
mode = M_READ|M_WRITE;
termios t;
unsigned i;
for(i=0; i<params.size() && isdigit(params[i]); ++i) ;
if(i+4!=params.size() || params[i]!=',')
- throw InvalidParameterValue("Invalid parameter string");
+ throw invalid_argument("Serial::set_parameters");
if(params[i+1]<'5' || params[i+1]>'8')
- throw InvalidParameterValue("Invalid data bit count");
+ throw invalid_argument("Serial::set_parameters data_bits");
if(params[i+2]!='N' && params[i+2]!='E' && params[i+2]!='O')
- throw InvalidParameterValue("Invalid parity");
+ throw invalid_argument("Serial::set_parameters parity");
if(params[i+3]!='1' && params[i+3]!='2')
- throw InvalidParameterValue("Invalid stop bit count");
+ throw invalid_argument("Serial::set_parameters stop_bits");
DeviceState state;
get_state(handle, state);
#ifdef WIN32
DWORD ret;
if(WriteFile(handle, buf, size, &ret, 0)==0)
- throw SystemError("Writing to serial port failed", GetLastError());
+ throw system_error("WriteFile");
#else
int ret = ::write(handle, buf, size);
if(ret==-1)
if(errno==EAGAIN)
return 0;
else
- throw SystemError("Writing to serial port failed", errno);
+ throw system_error("write");
}
#endif
#ifdef WIN32
DWORD ret;
if(ReadFile(handle, buf, size, &ret, 0)==0)
- throw SystemError("Reading from serial port failed", GetLastError());
+ throw system_error("ReadFile");
#else
int ret = ::read(handle, buf, size);
if(ret==-1)
if(errno==EAGAIN)
return 0;
else
- throw SystemError("Reading from serial port failed", errno);
+ throw system_error("read");
}
#endif
Handle Serial::get_event_handle()
{
#ifdef WIN32
- throw Exception("Serial port events not supported on win32 yet");
+ throw logic_error("Serial port events not supported on win32 yet");
#else
return handle;
#endif