]> git.tdb.fi Git - libs/core.git/commitdiff
Add a dedicated exception class for a bad seek operation
authorMikko Rasa <tdb@tdb.fi>
Sun, 4 Nov 2012 20:44:05 +0000 (22:44 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 4 Nov 2012 20:44:05 +0000 (22:44 +0200)
source/io/memory.cpp
source/io/seekable.cpp
source/io/seekable.h

index f64b4be9e52edcb6fcae3bb94c99d69595fd4e80..37954cca5bea6bb95a62a5afdfc094009584ba83 100644 (file)
@@ -116,7 +116,7 @@ SeekOffset Memory::seek(SeekOffset off, SeekType type)
                throw invalid_argument("Memory::seek");
 
        if(new_pos<begin || new_pos>end)
-               throw out_of_range("Memory::seek");
+               throw bad_seek(off, type);
 
        pos = new_pos;
        eof_flag = false;
index 0c3b0b955f162e0d5d3b926cd7379b7aeab2e4f5..a75cf33e67725fe6e978f98d927681cce59e16d5 100644 (file)
@@ -1,11 +1,12 @@
 #ifndef WIN32
 #define _LARGEFILE64_SOURCE
+#include <cerrno>
 #include <unistd.h>
 #else
 #include <windows.h>
 #endif
-#include <stdexcept>
 #include <msp/core/systemerror.h>
+#include <msp/strings/format.h>
 #include "handle.h"
 #include "handle_private.h"
 #include "seekable.h"
@@ -43,6 +44,14 @@ int sys_seek_type(SeekType st)
 namespace Msp {
 namespace IO {
 
+bad_seek::bad_seek(SeekOffset offset, SeekType type):
+       runtime_error(type==S_BEG ? lexical_cast(offset) :
+               type==S_CUR ? format("CUR%+d", offset) :
+               type==S_END ? format("END%+d", offset) :
+               format("SeekType(%d)", type))
+{ }
+
+
 SeekOffset sys_seek(Handle &handle, SeekOffset offset, SeekType type)
 {
 #ifdef WIN32
@@ -59,7 +68,12 @@ SeekOffset sys_seek(Handle &handle, SeekOffset offset, SeekType type)
 #else
        off64_t ret = lseek64(*handle, offset, sys_seek_type(type));
        if(ret==(off64_t)-1)
-               throw system_error("lseek64");
+       {
+               if(errno==EINVAL)
+                       throw bad_seek(offset, type);
+               else
+                       throw system_error("lseek64");
+       }
 
        return ret;
 #endif
index 1fc3f89ed58576fd73c1b68527b51f80133c1f1d..f0170b40f5af126de2c641431d7a2033cc1f1415 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_IO_SEEKABLE_H_
 #define MSP_IO_SEEKABLE_H_
 
+#include <stdexcept>
 #include <msp/core/inttypes.h>
 #include "base.h"
 
@@ -19,6 +20,14 @@ enum SeekType
 };
 
 
+class bad_seek: public std::runtime_error
+{
+public:
+       bad_seek(SeekOffset, SeekType);
+       virtual ~bad_seek() throw() { }
+};
+
+
 class Seekable: public Base
 {
 protected: