X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ffile.cpp;fp=source%2Ffile.cpp;h=2990d5c1c728a618012b0310f5451d671957e8c7;hp=8182e4a6c70814b41b5e49c1ace92e18e1ad734d;hb=b97d4e9f86e90254ab9edef7ee62a910f6333c78;hpb=49c1f3c3ffdf318579a809f3f800442c0c76c818 diff --git a/source/file.cpp b/source/file.cpp index 8182e4a..2990d5c 100644 --- a/source/file.cpp +++ b/source/file.cpp @@ -12,15 +12,6 @@ using namespace std; namespace Msp { namespace IO { -/** -Creates a new file object and opens it. If the -create flag is true and write access is requested and the file does exist, it -is created. Otherwise a missing file is an error. - -@param fn Name of the file to open -@param m Open mode -@param cm Flags controlling creation of a new file -*/ File::File(const string &fn, Mode m, CreateMode cm) { if(!(m&M_RDWR)) @@ -94,10 +85,11 @@ File::File(const string &fn, Mode m, CreateMode cm) set_events(P_INPUT); } -/** -Closes the file. Any attempt to access the file after this will cause an -exception to be thrown. -*/ +File::~File() +{ + close(); +} + void File::close() { if(handle==MSP_IO_INVALID_HANDLE) @@ -117,10 +109,6 @@ void File::close() signal_closed.emit(); } -/** -Sets the blocking state of the file. If blocking is disabled, all operations -will return immediately, even if they can't be fully completed. -*/ void File::set_block(bool b) { check_access(M_NONE); @@ -134,88 +122,6 @@ void File::set_block(bool b) #endif } -void File::sync() -{ -#ifndef WIN32 - signal_flush_required.emit(); - - fsync(handle); -#endif -} - -/** -Seeks the file to the given byte offset. - -@param off Offset in bytes -@param st Seek type - -@return The resulting offset -*/ -int File::seek(int off, SeekType st) -{ - 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 SystemError("Seek failed", GetLastError()); -#else - int ret = lseek(handle, off, type); - if(ret==-1) - throw SystemError("Seek failed", errno); -#endif - - eof_flag = false; - - return ret; -} - -/** -Returns the current read/write offset of the file. -*/ -int File::tell() const -{ - check_access(M_NONE); - -#ifdef WIN32 - DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT); - if(ret==INVALID_SET_FILE_POINTER) - throw SystemError("Tell failed", GetLastError()); -#else - int ret = lseek(handle, 0, SEEK_CUR); - if(ret==-1) - throw SystemError("Tell failed", errno); -#endif - - return ret; -} - -File::~File() -{ - close(); -} - -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"); -} - -/** -Writes data from a buffer to the file. - -@param buf Buffer to write from. -@param size Length of data to write. - -@return The number of bytes written -*/ unsigned File::do_write(const char *buf, unsigned size) { check_access(M_WRITE); @@ -243,14 +149,6 @@ unsigned File::do_write(const char *buf, unsigned size) return ret; } -/** -Reads data from the file. - -@param buf Buffer to read data into. -@param size Maximum size of data to read. - -@return The number of bytes read, possibly zero -*/ unsigned File::do_read(char *buf, unsigned size) { check_access(M_READ); @@ -282,5 +180,63 @@ unsigned File::do_read(char *buf, unsigned size) return ret; } +void File::sync() +{ +#ifndef WIN32 + signal_flush_required.emit(); + + fsync(handle); +#endif +} + +int File::seek(int off, SeekType st) +{ + 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 SystemError("Seek failed", GetLastError()); +#else + int ret = lseek(handle, off, type); + if(ret==-1) + throw SystemError("Seek failed", errno); +#endif + + eof_flag = false; + + return ret; +} + +int File::tell() const +{ + check_access(M_NONE); + +#ifdef WIN32 + DWORD ret = SetFilePointer(handle, 0, 0, FILE_CURRENT); + if(ret==INVALID_SET_FILE_POINTER) + throw SystemError("Tell failed", GetLastError()); +#else + int ret = lseek(handle, 0, SEEK_CUR); + if(ret==-1) + throw SystemError("Tell failed", errno); +#endif + + return ret; +} + +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"); +} + } // namespace IO } // namespace Msp