X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Ffile.cpp;h=0f5652ad9484cf460a2e266f666470c00b67dd02;hp=74930baaf497286f32925ee395641147813daf58;hb=a56e05106571f28dc3044296849ccd5127d7d859;hpb=86e0004bd195bc7f7478cf736871339bddf38127 diff --git a/source/io/file.cpp b/source/io/file.cpp index 74930ba..0f5652a 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -3,9 +3,10 @@ #include #include #endif +#include #include -#include #include "file.h" +#include "handle_private.h" using namespace std; @@ -40,8 +41,8 @@ File::File(const string &fn, Mode m, CreateMode cm) } } - handle = CreateFile(fn.c_str(), flags, 0, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0); - if(handle==INVALID_HANDLE_VALUE) + *handle = CreateFile(fn.c_str(), flags, 0, 0, create_flags, FILE_ATTRIBUTE_NORMAL, 0); + if(!handle) { int err = GetLastError(); if(err==ERROR_FILE_NOT_FOUND) @@ -71,8 +72,8 @@ File::File(const string &fn, Mode m, CreateMode cm) if(mode&M_NONBLOCK) flags |= O_NONBLOCK; - handle = ::open(fn.c_str(), flags, 0666); - if(handle==-1) + *handle = ::open(fn.c_str(), flags, 0666); + if(!handle) { int err = errno; if(err==ENOENT) @@ -81,8 +82,6 @@ File::File(const string &fn, Mode m, CreateMode cm) throw system_error(format("open(%s)", fn), err); } #endif - - set_events(P_INPUT); } File::~File() @@ -92,20 +91,14 @@ File::~File() void File::close() { - if(handle==MSP_IO_INVALID_HANDLE) + if(!handle) return; - set_events(P_NONE); - signal_flush_required.emit(); -#ifdef WIN32 - CloseHandle(handle); -#else - ::close(handle); -#endif + sys_close(handle); - handle = MSP_IO_INVALID_HANDLE; + handle = Handle(); signal_closed.emit(); } @@ -117,8 +110,8 @@ void File::set_block(bool b) if(b) mode = (mode|M_NONBLOCK); #ifndef WIN32 - int flags = fcntl(handle, F_GETFD); - fcntl(handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); + int flags = fcntl(*handle, F_GETFD); + fcntl(*handle, F_SETFL, (flags&O_NONBLOCK)|(b?0:O_NONBLOCK)); #endif } @@ -132,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) @@ -156,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; @@ -185,52 +152,31 @@ void File::sync() #ifndef WIN32 signal_flush_required.emit(); - fsync(handle); + fsync(*handle); #endif } -unsigned File::seek(int off, SeekType st) +SeekOffset File::seek(SeekOffset off, SeekType type) { check_access(M_NONE); signal_flush_required.emit(); - - int type = sys_seek_type(st); -#ifdef WIN32 - DWORD ret = SetFilePointer(handle, off, 0, type); - if(ret==INVALID_SET_FILE_POINTER) - throw system_error("SetFilePointer"); -#else - off_t ret = lseek(handle, off, type); - if(ret==(off_t)-1) - throw system_error("lseek"); -#endif - + off = sys_seek(handle, off, type); eof_flag = false; - return ret; + return off; } -unsigned File::tell() const +SeekOffset File::tell() const { check_access(M_NONE); -#ifdef WIN32 - DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT); - if(ret==INVALID_SET_FILE_POINTER) - throw system_error("SetFilePointer"); -#else - off_t ret = lseek(handle, 0, SEEK_CUR); - if(ret==(off_t)-1) - throw system_error("lseek"); -#endif - - return ret; + return sys_seek(const_cast(handle), 0, S_CUR); } void File::check_access(Mode m) const { - if(handle==MSP_IO_INVALID_HANDLE || (m && !(mode&m))) + if(!handle || (m && !(mode&m))) throw invalid_access(m); }