]> git.tdb.fi Git - libs/core.git/blobdiff - source/io/seekable.cpp
Add a dedicated exception class for a bad seek operation
[libs/core.git] / source / io / seekable.cpp
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