From: Mikko Rasa Date: Sun, 4 Nov 2012 20:44:05 +0000 (+0200) Subject: Add a dedicated exception class for a bad seek operation X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=1a6331551a025ddcc1772ae447c326ec70e0e107;p=libs%2Fcore.git Add a dedicated exception class for a bad seek operation --- diff --git a/source/io/memory.cpp b/source/io/memory.cpp index f64b4be..37954cc 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -116,7 +116,7 @@ SeekOffset Memory::seek(SeekOffset off, SeekType type) throw invalid_argument("Memory::seek"); if(new_posend) - throw out_of_range("Memory::seek"); + throw bad_seek(off, type); pos = new_pos; eof_flag = false; diff --git a/source/io/seekable.cpp b/source/io/seekable.cpp index 0c3b0b9..a75cf33 100644 --- a/source/io/seekable.cpp +++ b/source/io/seekable.cpp @@ -1,11 +1,12 @@ #ifndef WIN32 #define _LARGEFILE64_SOURCE +#include #include #else #include #endif -#include #include +#include #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 diff --git a/source/io/seekable.h b/source/io/seekable.h index 1fc3f89..f0170b4 100644 --- a/source/io/seekable.h +++ b/source/io/seekable.h @@ -1,6 +1,7 @@ #ifndef MSP_IO_SEEKABLE_H_ #define MSP_IO_SEEKABLE_H_ +#include #include #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: