From: Mikko Rasa Date: Sat, 11 Jun 2011 17:09:22 +0000 (+0300) Subject: Move some common operations to helper functions X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6c40658510b68788fd5ef0488b20873b6aa32938;p=libs%2Fcore.git Move some common operations to helper functions --- diff --git a/source/io/console.cpp b/source/io/console.cpp index a0e0c28..442bbb0 100644 --- a/source/io/console.cpp +++ b/source/io/console.cpp @@ -123,17 +123,7 @@ unsigned Console::do_write(const char *buf, unsigned len) if(!(mode&M_WRITE)) throw invalid_access(M_WRITE); -#ifdef WIN32 - DWORD ret; - if(!WriteFile(*handle, buf, len, &ret, 0)) - throw system_error("WriteFile"); -#else - int ret = ::write(*handle, buf, len); - if(ret==-1) - throw system_error("write"); -#endif - - return ret; + return sys_write(handle, buf, len); } unsigned Console::do_read(char *buf, unsigned len) @@ -141,22 +131,9 @@ unsigned Console::do_read(char *buf, unsigned len) if(!(mode&M_READ)) throw invalid_access(M_READ); -#ifdef WIN32 - DWORD ret; - if(!ReadFile(*handle, buf, len, &ret, 0)) - throw system_error("ReadFile"); -#else - int ret = ::read(*handle, buf, len); - if(ret==-1) - { - if(errno==EAGAIN) - return 0; - else - throw system_error("read"); - } - else if(ret==0) + unsigned ret = sys_read(handle, buf, len); + if(ret==0) eof_flag = true; -#endif return ret; } diff --git a/source/io/file.cpp b/source/io/file.cpp index c9e5d49..1b207a2 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -96,11 +96,7 @@ void File::close() signal_flush_required.emit(); -#ifdef WIN32 - CloseHandle(*handle); -#else - ::close(*handle); -#endif + sys_close(handle); handle = Handle(); signal_closed.emit(); @@ -129,21 +125,9 @@ unsigned File::do_write(const char *buf, unsigned size) #ifdef WIN32 if(mode&M_APPEND) seek(0, S_END); - DWORD ret; - if(WriteFile(*handle, buf, size, &ret, 0)==0) - throw system_error("WriteFile"); -#else - int ret = ::write(*handle, buf, size); - if(ret==-1) - { - if(errno==EAGAIN) - return 0; - else - throw system_error("write"); - } #endif - return ret; + return sys_write(handle, buf, size); } unsigned File::do_read(char *buf, unsigned size) @@ -153,21 +137,7 @@ unsigned File::do_read(char *buf, unsigned size) if(size==0) return 0; -#ifdef WIN32 - DWORD ret; - if(ReadFile(*handle, buf, size, &ret, 0)==0) - throw system_error("ReadFile"); -#else - int ret = ::read(*handle, buf, size); - if(ret==-1) - { - if(errno==EAGAIN) - return 0; - else - throw system_error("read"); - } -#endif - + unsigned ret = sys_read(handle, buf, size); if(ret==0) { eof_flag = true; diff --git a/source/io/handle.cpp b/source/io/handle.cpp index d5183e5..0e9b82a 100644 --- a/source/io/handle.cpp +++ b/source/io/handle.cpp @@ -1,3 +1,6 @@ +#include +#include +#include #include "handle.h" #include "handle_private.h" @@ -49,5 +52,55 @@ Handle::Private &Handle::Private::operator=(H h) return *this; } + +unsigned sys_read(Handle &handle, char *buf, unsigned size) +{ +#ifdef WIN32 + DWORD ret; + if(ReadFile(*handle, buf, size, &ret, 0)==0) + throw system_error("ReadFile"); +#else + int ret = read(*handle, buf, size); + if(ret==-1) + { + if(errno==EAGAIN) + return 0; + else + throw system_error("read"); + } +#endif + + return ret; +} + +unsigned sys_write(Handle &handle, const char *buf, unsigned size) +{ +#ifdef WIN32 + DWORD ret; + if(WriteFile(*handle, buf, size, &ret, 0)==0) + throw system_error("WriteFile"); +#else + int ret = write(*handle, buf, size); + if(ret==-1) + { + if(errno==EAGAIN) + return 0; + else + throw system_error("write"); + } +#endif + + return ret; +} + +void sys_close(Handle &handle) +{ +#ifdef WIN32 + CloseHandle(*handle); +#else + close(*handle); +#endif +} + } // namespace IO } // namespace Msp diff --git a/source/io/handle.h b/source/io/handle.h index 44b5937..24b9ee6 100644 --- a/source/io/handle.h +++ b/source/io/handle.h @@ -27,6 +27,11 @@ public: operator const void *() const; }; + +unsigned sys_read(Handle &, char *, unsigned); +unsigned sys_write(Handle &, const char *, unsigned); +void sys_close(Handle &); + } // namespace IO } // namespace Msp diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 1b275e7..a848786 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -76,14 +76,9 @@ void Pipe::close() set_events(P_NONE); signal_flush_required.emit(); -#ifdef WIN32 - CloseHandle(*handle[0]); - CloseHandle(*handle[1]); -#else - ::close(*handle[0]); - ::close(*handle[1]); + sys_close(handle[0]); + sys_close(handle[1]); signal_closed.emit(); -#endif } void Pipe::set_block(bool b) @@ -105,22 +100,7 @@ unsigned Pipe::do_write(const char *buf, unsigned size) if(size==0) return 0; -#ifdef WIN32 - DWORD ret; - if(!WriteFile(*handle[1], buf, size, &ret, 0)) - throw system_error("WriteFile"); -#else - int ret = ::write(*handle[1], buf, size); - if(ret==-1) - { - if(errno==EAGAIN) - return 0; - else - throw system_error("write"); - } -#endif - - return ret; + return sys_write(handle[1], buf, size); } unsigned Pipe::do_read(char *buf, unsigned size) @@ -153,14 +133,7 @@ unsigned Pipe::do_read(char *buf, unsigned size) // Initiate another overlapped read in case someone is polling us get_event_handle(); #else - int ret = ::read(*handle[0], buf, size); - if(ret==-1) - { - if(errno==EAGAIN) - return 0; - else - throw system_error("read"); - } + unsigned ret = sys_read(handle[0], buf, size); #endif if(ret==0) diff --git a/source/io/serial.cpp b/source/io/serial.cpp index 9df7971..74af366 100644 --- a/source/io/serial.cpp +++ b/source/io/serial.cpp @@ -208,11 +208,7 @@ Serial::~Serial() void Serial::close() { -#ifdef WIN32 - CloseHandle(*handle); -#else - ::close(*handle); -#endif + sys_close(handle); } void Serial::set_block(bool b) @@ -287,22 +283,7 @@ unsigned Serial::do_write(const char *buf, unsigned size) if(size==0) return 0; -#ifdef WIN32 - DWORD ret; - if(WriteFile(*handle, buf, size, &ret, 0)==0) - throw system_error("WriteFile"); -#else - int ret = ::write(*handle, buf, size); - if(ret==-1) - { - if(errno==EAGAIN) - return 0; - else - throw system_error("write"); - } -#endif - - return ret; + return sys_write(handle, buf, size); } unsigned Serial::do_read(char *buf, unsigned size) @@ -310,22 +291,7 @@ unsigned Serial::do_read(char *buf, unsigned size) if(size==0) return 0; -#ifdef WIN32 - DWORD ret; - if(ReadFile(*handle, buf, size, &ret, 0)==0) - throw system_error("ReadFile"); -#else - int ret = ::read(*handle, buf, size); - if(ret==-1) - { - if(errno==EAGAIN) - return 0; - else - throw system_error("read"); - } -#endif - - return ret; + return sys_read(handle, buf, size); } const Handle &Serial::get_event_handle()