From: Mikko Rasa Date: Fri, 10 Jun 2011 21:01:02 +0000 (+0300) Subject: Add an intermediate Seekable interface class X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=86e0004bd195bc7f7478cf736871339bddf38127 Add an intermediate Seekable interface class --- diff --git a/source/io/file.cpp b/source/io/file.cpp index 727542f..74930ba 100644 --- a/source/io/file.cpp +++ b/source/io/file.cpp @@ -189,7 +189,7 @@ void File::sync() #endif } -int File::seek(int off, SeekType st) +unsigned File::seek(int off, SeekType st) { check_access(M_NONE); @@ -201,8 +201,8 @@ int File::seek(int off, SeekType st) if(ret==INVALID_SET_FILE_POINTER) throw system_error("SetFilePointer"); #else - int ret = lseek(handle, off, type); - if(ret==-1) + off_t ret = lseek(handle, off, type); + if(ret==(off_t)-1) throw system_error("lseek"); #endif @@ -211,7 +211,7 @@ int File::seek(int off, SeekType st) return ret; } -int File::tell() const +unsigned File::tell() const { check_access(M_NONE); @@ -220,8 +220,8 @@ int File::tell() const if(ret==INVALID_SET_FILE_POINTER) throw system_error("SetFilePointer"); #else - int ret = lseek(handle, 0, SEEK_CUR); - if(ret==-1) + off_t ret = lseek(handle, 0, SEEK_CUR); + if(ret==(off_t)-1) throw system_error("lseek"); #endif diff --git a/source/io/file.h b/source/io/file.h index 4711160..d59cd57 100644 --- a/source/io/file.h +++ b/source/io/file.h @@ -3,10 +3,9 @@ #include #include -#include "base.h" #include "buffered.h" #include "filtered.h" -#include "seek.h" +#include "seekable.h" namespace Msp { namespace IO { @@ -23,7 +22,7 @@ A class for reading and writing files. Non-blocking mode is not supported on Win32. */ -class File: public Base +class File: public Seekable { public: enum CreateMode @@ -56,11 +55,8 @@ protected: public: virtual void sync(); - /** Changes the read/write offset of the file. Returns the new offset. */ - virtual int seek(int, SeekType); - - /** Returns the current read/write offset of the file. */ - virtual int tell() const; + virtual unsigned seek(int, SeekType); + virtual unsigned tell() const; virtual Handle get_event_handle() { return handle; } diff --git a/source/io/memory.h b/source/io/memory.h index ba6c4d8..27fd6cb 100644 --- a/source/io/memory.h +++ b/source/io/memory.h @@ -1,13 +1,12 @@ #ifndef MSP_IO_MEMORY_H_ #define MSP_IO_MEMORY_H_ -#include "base.h" -#include "seek.h" +#include "seekable.h" namespace Msp { namespace IO { -class Memory: public Base +class Memory: public Seekable { private: char *begin; @@ -29,8 +28,8 @@ public: virtual bool getline(std::string &); virtual int get(); - unsigned seek(int, SeekType); - unsigned tell() const { return pos-begin; } + virtual unsigned seek(int, SeekType); + virtual unsigned tell() const { return pos-begin; } virtual Handle get_event_handle(); diff --git a/source/io/seek.cpp b/source/io/seek.cpp deleted file mode 100644 index c0b65ca..0000000 --- a/source/io/seek.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#ifdef WIN32 -#include -#endif -#include -#include "seek.h" - -using namespace std; - -namespace Msp { -namespace IO { - -int sys_seek_type(SeekType st) -{ -#ifdef WIN32 - if(st==S_BEG) - return FILE_BEGIN; - else if(st==S_CUR) - return FILE_CURRENT; - else if(st==S_END) - return FILE_END; -#else - if(st==S_BEG) - return SEEK_SET; - else if(st==S_CUR) - return SEEK_CUR; - else if(st==S_END) - return SEEK_END; -#endif - - throw invalid_argument("Invalid seek type"); -} - -} // namespace IO -} // namespace Msp diff --git a/source/io/seek.h b/source/io/seek.h deleted file mode 100644 index 3c6c331..0000000 --- a/source/io/seek.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef MSP_IO_SEEK_H_ -#define MSP_IO_SEEK_H_ - -namespace Msp { -namespace IO { - -enum SeekType -{ - S_BEG, - S_CUR, - S_END -}; - -extern int sys_seek_type(SeekType); - -} // namespace IO -} // namespace Msp - -#endif diff --git a/source/io/seekable.cpp b/source/io/seekable.cpp new file mode 100644 index 0000000..101676e --- /dev/null +++ b/source/io/seekable.cpp @@ -0,0 +1,34 @@ +#ifdef WIN32 +#include +#endif +#include +#include "seekable.h" + +using namespace std; + +namespace Msp { +namespace IO { + +int sys_seek_type(SeekType st) +{ +#ifdef WIN32 + if(st==S_BEG) + return FILE_BEGIN; + else if(st==S_CUR) + return FILE_CURRENT; + else if(st==S_END) + return FILE_END; +#else + if(st==S_BEG) + return SEEK_SET; + else if(st==S_CUR) + return SEEK_CUR; + else if(st==S_END) + return SEEK_END; +#endif + + throw invalid_argument("sys_seek_type"); +} + +} // namespace IO +} // namespace Msp diff --git a/source/io/seekable.h b/source/io/seekable.h new file mode 100644 index 0000000..cabb9dc --- /dev/null +++ b/source/io/seekable.h @@ -0,0 +1,35 @@ +#ifndef MSP_IO_SEEKABLE_H_ +#define MSP_IO_SEEKABLE_H_ + +#include "base.h" + +namespace Msp { +namespace IO { + +enum SeekType +{ + S_BEG, + S_CUR, + S_END +}; + +int sys_seek_type(SeekType); + + +class Seekable: public Base +{ +protected: + Seekable() { } + +public: + /** Changes the read/write offset. Returns the new offset. */ + virtual unsigned seek(int, SeekType) = 0; + + /** Returns the current read/write offset. */ + virtual unsigned tell() const = 0; +}; + +} // namespace IO +} // namespace Msp + +#endif