]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/unix/handle.cpp
Move most platform-specific code into overlay directories
[libs/core.git] / source / io / unix / handle.cpp
diff --git a/source/io/unix/handle.cpp b/source/io/unix/handle.cpp
new file mode 100644 (file)
index 0000000..9546df9
--- /dev/null
@@ -0,0 +1,57 @@
+#include <cerrno>
+#include <unistd.h>
+#include <fcntl.h>
+#include <msp/core/systemerror.h>
+#include "handle.h"
+#include "handle_private.h"
+
+namespace Msp {
+namespace IO {
+
+Handle::operator const void *() const
+{
+       return priv->handle!=-1 ? this : 0;
+}
+
+
+void sys_set_blocking(Handle &handle, bool b)
+{
+       int flags = fcntl(*handle, F_GETFD);
+       fcntl(*handle, F_SETFL, (flags&~O_NONBLOCK)|(b?0:O_NONBLOCK));
+}
+
+unsigned sys_read(Handle &handle, char *buf, unsigned size)
+{
+       int ret = read(*handle, buf, size);
+       if(ret==-1)
+       {
+               if(errno==EAGAIN)
+                       return 0;
+               else
+                       throw system_error("read");
+       }
+
+       return ret;
+}
+
+unsigned sys_write(Handle &handle, const char *buf, unsigned size)
+{
+       int ret = write(*handle, buf, size);
+       if(ret==-1)
+       {
+               if(errno==EAGAIN)
+                       return 0;
+               else
+                       throw system_error("write");
+       }
+
+       return ret;
+}
+
+void sys_close(Handle &handle)
+{
+       close(*handle);
+}
+
+} // namespace IO
+} // namespace Msp