X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fio%2Ffile.cpp;h=c9e5d4978c5255e418c12d7794146e2d062f0667;hp=2990d5c1c728a618012b0310f5451d671957e8c7;hb=c21ab7e49852585df01b4cc19599e25a918b581b;hpb=6e0fd758970bcb5bad5e3f2454b694cc4d7b4b66 diff --git a/source/io/file.cpp b/source/io/file.cpp index 2990d5c..c9e5d49 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -3,9 +3,10 @@ #include #include #endif -#include -#include "except.h" +#include +#include #include "file.h" +#include "handle_private.h" using namespace std; @@ -15,9 +16,9 @@ namespace IO { File::File(const string &fn, Mode m, CreateMode cm) { if(!(m&M_RDWR)) - throw InvalidParameterValue("Invalid read/write mode"); + throw invalid_argument("File::File mode"); if(cm&~(C_CREATE|C_TRUNCATE)) - throw InvalidParameterValue("Invalid create mode"); + throw invalid_argument("File::File create"); mode = m; @@ -40,14 +41,14 @@ 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) - throw FileNotFound("Can't find file "+fn, fn); + throw file_not_found(fn); else - throw SystemError(format("Can't open file '%s'", fn), GetLastError()); + throw system_error(format("CreateFile(%s)", fn), err); } #else int flags = 0; @@ -71,18 +72,16 @@ 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) - throw FileNotFound("Can't find file "+fn, fn); + throw file_not_found(fn); else - throw SystemError(format("Can't open file '%s'", fn), err); + throw system_error(format("open(%s)", fn), err); } #endif - - set_events(P_INPUT); } File::~File() @@ -92,20 +91,18 @@ 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); + CloseHandle(*handle); #else - ::close(handle); + ::close(*handle); #endif - handle = MSP_IO_INVALID_HANDLE; + handle = Handle(); signal_closed.emit(); } @@ -117,8 +114,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 } @@ -133,16 +130,16 @@ unsigned File::do_write(const char *buf, unsigned size) if(mode&M_APPEND) seek(0, S_END); DWORD ret; - if(WriteFile(handle, buf, size, &ret, 0)==0) - throw SystemError("Writing to file failed", GetLastError()); + if(WriteFile(*handle, buf, size, &ret, 0)==0) + throw system_error("WriteFile"); #else - int ret = ::write(handle, buf, size); + int ret = ::write(*handle, buf, size); if(ret==-1) { if(errno==EAGAIN) return 0; else - throw SystemError("Writing to file failed", errno); + throw system_error("write"); } #endif @@ -158,16 +155,16 @@ unsigned File::do_read(char *buf, unsigned size) #ifdef WIN32 DWORD ret; - if(ReadFile(handle, buf, size, &ret, 0)==0) - throw SystemError("Reading from file failed", GetLastError()); + if(ReadFile(*handle, buf, size, &ret, 0)==0) + throw system_error("ReadFile"); #else - int ret = ::read(handle, buf, size); + int ret = ::read(*handle, buf, size); if(ret==-1) { if(errno==EAGAIN) return 0; else - throw SystemError("Reading from file failed", errno); + throw system_error("read"); } #endif @@ -185,11 +182,11 @@ void File::sync() #ifndef WIN32 signal_flush_required.emit(); - fsync(handle); + fsync(*handle); #endif } -int File::seek(int off, SeekType st) +unsigned File::seek(int off, SeekType st) { check_access(M_NONE); @@ -197,13 +194,13 @@ int File::seek(int off, SeekType st) int type = sys_seek_type(st); #ifdef WIN32 - DWORD ret = SetFilePointer(handle, off, 0, type); + DWORD ret = SetFilePointer(*handle, off, 0, type); if(ret==INVALID_SET_FILE_POINTER) - throw SystemError("Seek failed", GetLastError()); + throw system_error("SetFilePointer"); #else - int ret = lseek(handle, off, type); - if(ret==-1) - throw SystemError("Seek failed", errno); + off_t ret = lseek(*handle, off, type); + if(ret==(off_t)-1) + throw system_error("lseek"); #endif eof_flag = false; @@ -211,18 +208,18 @@ int File::seek(int off, SeekType st) return ret; } -int File::tell() const +unsigned File::tell() const { check_access(M_NONE); #ifdef WIN32 - DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT); + DWORD ret = SetFilePointer(*handle, 0, 0, FILE_CURRENT); if(ret==INVALID_SET_FILE_POINTER) - throw SystemError("Tell failed", GetLastError()); + throw system_error("SetFilePointer"); #else - int ret = lseek(handle, 0, SEEK_CUR); - if(ret==-1) - throw SystemError("Tell failed", errno); + off_t ret = lseek(*handle, 0, SEEK_CUR); + if(ret==(off_t)-1) + throw system_error("lseek"); #endif return ret; @@ -230,12 +227,8 @@ int File::tell() const void File::check_access(Mode m) const { - if(handle==MSP_IO_INVALID_HANDLE) - throw InvalidState("File is not open"); - if(m==M_READ && !(mode&M_READ)) - throw InvalidState("File is not readable"); - if(m==M_WRITE && !(mode&M_WRITE)) - throw InvalidState("File is not writable"); + if(!handle || (m && !(mode&m))) + throw invalid_access(m); } } // namespace IO