From 86e0004bd195bc7f7478cf736871339bddf38127 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 11 Jun 2011 00:01:02 +0300 Subject: [PATCH] Add an intermediate Seekable interface class --- source/io/file.cpp | 12 +++++----- source/io/file.h | 12 ++++------ source/io/memory.h | 9 ++++--- source/io/seek.h | 19 --------------- source/io/{seek.cpp => seekable.cpp} | 4 ++-- source/io/seekable.h | 35 ++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 40 deletions(-) delete mode 100644 source/io/seek.h rename source/io/{seek.cpp => seekable.cpp} (87%) create mode 100644 source/io/seekable.h 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.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/seek.cpp b/source/io/seekable.cpp similarity index 87% rename from source/io/seek.cpp rename to source/io/seekable.cpp index c0b65ca..101676e 100644 --- a/source/io/seek.cpp +++ b/source/io/seekable.cpp @@ -2,7 +2,7 @@ #include #endif #include -#include "seek.h" +#include "seekable.h" using namespace std; @@ -27,7 +27,7 @@ int sys_seek_type(SeekType st) return SEEK_END; #endif - throw invalid_argument("Invalid seek type"); + throw invalid_argument("sys_seek_type"); } } // namespace IO 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 -- 2.45.2