]> git.tdb.fi Git - libs/core.git/commitdiff
Add an intermediate Seekable interface class
authorMikko Rasa <tdb@tdb.fi>
Fri, 10 Jun 2011 21:01:02 +0000 (00:01 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 11 Jun 2011 11:44:21 +0000 (14:44 +0300)
source/io/file.cpp
source/io/file.h
source/io/memory.h
source/io/seek.cpp [deleted file]
source/io/seek.h [deleted file]
source/io/seekable.cpp [new file with mode: 0644]
source/io/seekable.h [new file with mode: 0644]

index 727542fe1d15baa3fbdcdc6678be9914e82d0ade..74930baaf497286f32925ee395641147813daf58 100644 (file)
@@ -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
 
index 47111609e8adb238f5bfef24132f81c6e8a9b8a2..d59cd577fc0f33e1acbcbb62bcaa9fa0bb31c141 100644 (file)
@@ -3,10 +3,9 @@
 
 #include <stdexcept>
 #include <string>
-#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; }
 
index ba6c4d87995306653d0eda5ca245b95566ffb645..27fd6cbd56aeeb1fda7b9d1260ca667c114c7ce4 100644 (file)
@@ -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 (file)
index c0b65ca..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-#ifdef WIN32
-#include <windows.h>
-#endif
-#include <stdexcept>
-#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 (file)
index 3c6c331..0000000
+++ /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 (file)
index 0000000..101676e
--- /dev/null
@@ -0,0 +1,34 @@
+#ifdef WIN32
+#include <windows.h>
+#endif
+#include <stdexcept>
+#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 (file)
index 0000000..cabb9dc
--- /dev/null
@@ -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