]> git.tdb.fi Git - libs/core.git/commitdiff
Move some common operations to helper functions
authorMikko Rasa <tdb@tdb.fi>
Sat, 11 Jun 2011 17:09:22 +0000 (20:09 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 11 Jun 2011 17:09:22 +0000 (20:09 +0300)
source/io/console.cpp
source/io/file.cpp
source/io/handle.cpp
source/io/handle.h
source/io/pipe.cpp
source/io/serial.cpp

index a0e0c28f07cbbd506683cb92166f3def8b57b93a..442bbb0f7dc0dbcd70b1cd0da62e74f247f8c162 100644 (file)
@@ -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;
 }
index c9e5d4978c5255e418c12d7794146e2d062f0667..1b207a25971f960ef99fa5f4481a0be23239e38d 100644 (file)
@@ -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;
index d5183e5b4a349d286f9858e85c3c28234ac5970f..0e9b82aa000c9382fe954b764f36d6614710934e 100644 (file)
@@ -1,3 +1,6 @@
+#include <cerrno>
+#include <unistd.h>
+#include <msp/core/systemerror.h>
 #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
index 44b59371e56ef5262f1e0ba0dd621eb73ef0192a..24b9ee6a9e9e192b63fb4eb2230cd66050577c4c 100644 (file)
@@ -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
 
index 1b275e731667f4736929499aaad7e9a17745343b..a8487867a137d406699361a3c5d0fe2cfaa35b08 100644 (file)
@@ -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)
index 9df797109d840cc3554136035410977926bc802e..74af366856cd6ceaf99866e00d977e2108c1803c 100644 (file)
@@ -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()