X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fio%2Funix%2Fhandle.cpp;fp=source%2Fio%2Funix%2Fhandle.cpp;h=9546df9f579c657f61f658ee235db3e172eec7dd;hb=609c9a508cfdc7b42c46c4f21d17639204165a00;hp=0000000000000000000000000000000000000000;hpb=b4806214e905752617691f851717033fd3f266c2;p=libs%2Fcore.git diff --git a/source/io/unix/handle.cpp b/source/io/unix/handle.cpp new file mode 100644 index 0000000..9546df9 --- /dev/null +++ b/source/io/unix/handle.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#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