diff --git a/Exception.h b/Exception.h index cf9ffb7..cd23c37 100644 --- a/Exception.h +++ b/Exception.h @@ -11,20 +11,36 @@ class Exception : public std::exception public: Exception(const char *cp); Exception(const std::string &str); - Exception(const char *cp, int error); - Exception(const std::string& string, int error); + virtual ~Exception() throw (); virtual const char *what(); - int error() const; + int error() const { return _error; } + +protected: + Exception(const char *cp, int error); + Exception(const std::string& string, int error); + private: int _error; std::string _string; }; +class POSIXException : public Exception { +public: + POSIXException(const char *cp, int error); + POSIXException(const std::string& string, int error); +}; + +class ProDOSException : public Exception { +public: + ProDOSException(const char *cp, int error); + ProDOSException(const std::string& string, int error); +}; + inline Exception::Exception(const char *cp): _error(0), _string(cp) @@ -49,11 +65,28 @@ inline Exception::Exception(const std::string& string, int error): { } -inline int Exception::error() const +inline POSIXException::POSIXException(const char *cp, int error) : + Exception(cp, error) { - return _error; } +inline POSIXException::POSIXException(const std::string& string, int error) : + Exception(string, error) +{ +} + +inline ProDOSException::ProDOSException(const char *cp, int error) : + Exception(cp, error) +{ +} + +inline ProDOSException::ProDOSException(const std::string& string, int error) : + Exception(string, error) +{ +} + + + } #endif \ No newline at end of file diff --git a/MappedFile.cpp b/MappedFile.cpp index b8b055d..7b078b3 100644 --- a/MappedFile.cpp +++ b/MappedFile.cpp @@ -36,7 +36,7 @@ MappedFile::MappedFile(const char *name, bool readOnly) if (fd < 0) { - throw Exception(__METHOD__ ": Unable to open file.", errno); + throw POSIXException(__METHOD__ ": Unable to open file.", errno); } // init may throw; auto_fd guarantees the file will be closed if that happens. init(fd, readOnly); @@ -63,12 +63,12 @@ MappedFile::MappedFile(const char *name, size_t size) auto_fd fd(::open(name, O_CREAT | O_TRUNC | O_RDWR, 0644)); if (fd < 0) - throw Exception(__METHOD__ ": Unable to create file.", errno); + throw POSIXException(__METHOD__ ": Unable to create file.", errno); // TODO -- is ftruncate portable? if (::ftruncate(fd, _size) < 0) { - throw Exception(__METHOD__ ": Unable to truncate file.", errno); + throw POSIXException(__METHOD__ ": Unable to truncate file.", errno); } //_map = ::mmap(NULL, _size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, _fd, 0); @@ -80,7 +80,7 @@ MappedFile::MappedFile(const char *name, size_t size) MAP_FILE | MAP_SHARED ); - if (map == MAP_FAILED) throw Exception(__METHOD__ ": Unable to map file.", errno); + if (map == MAP_FAILED) throw POSIXException(__METHOD__ ": Unable to map file.", errno); _fd = fd.release(); _map = map.release(); @@ -110,7 +110,7 @@ void MappedFile::init(int f, bool readOnly) _size = ::lseek(f, 0, SEEK_END); if (_size < 0) - throw Exception(__METHOD__ ": Unable to determine file size.", errno); + throw POSIXException(__METHOD__ ": Unable to determine file size.", errno); /* _map = ::mmap(NULL, _size, readOnly ? PROT_READ : PROT_READ | PROT_WRITE, @@ -123,7 +123,7 @@ void MappedFile::init(int f, bool readOnly) readOnly ? MAP_FILE : MAP_FILE | MAP_SHARED ); - if (map == MAP_FAILED) throw Exception(__METHOD__ ": Unable to map file.", errno); + if (map == MAP_FAILED) throw POSIXException(__METHOD__ ": Unable to map file.", errno); _fd = f; _map = map.release(); @@ -227,7 +227,7 @@ void MappedFile::sync() if (_readOnly) return; if (::msync(_map, _size, MS_SYNC) < 0) - throw Exception(__METHOD__ ": msync error.", errno); + throw POSIXException(__METHOD__ ": msync error.", errno); } void MappedFile::reset() diff --git a/RawDevice.cpp b/RawDevice.cpp index 2635143..bc19ce5 100644 --- a/RawDevice.cpp +++ b/RawDevice.cpp @@ -36,7 +36,7 @@ void RawDevice::devSize(int fd) struct dk_minfo minfo; if (::ioctl(fd, DKIOCGMEDIAINFO, &minfo) < 0) - throw Exception(__METHOD__ ": Unable to determine device size.", errno); + throw POSIXException(__METHOD__ ": Unable to determine device size.", errno); _size = minfo.dki_lbsize * minfo.dki_capacity; _blockSize = 512; // not really, but whatever. @@ -55,11 +55,11 @@ void RawDevice::devSize(int fd) uint64_t blockCount; // 64 bit if (::ioctl(fd, DKIOCGETBLOCKSIZE, &blockSize) < 0) - throw Exception(__METHOD__ ": Unable to determine block size.", errno); + throw POSIXException(__METHOD__ ": Unable to determine block size.", errno); if (::ioctl(fd, DKIOCGETBLOCKCOUNT, &blockCount) < 0) - throw Exception(__METHOD__ ": Unable to determine block count.", errno); + throw POSIXException(__METHOD__ ": Unable to determine block count.", errno); _blockSize = blockSize; _size = _blockSize * blockCount; @@ -79,7 +79,7 @@ void RawDevice::devSize(int fd) int blocks; if (::ioctl(fd, BLKGETSIZE, &blocks) < 0) - throw Exception(__METHOD__ ": Unable to determine device size.", errno); + throw POSIXException(__METHOD__ ": Unable to determine device size.", errno); _size = 512 * blocks; _blockSize = 512; // @@ -110,7 +110,7 @@ RawDevice::RawDevice(const char *name, bool readOnly) } if (fd < 0) - throw Exception(__METHOD__ ": Unable to open device.", errno); + throw POSIXException(__METHOD__ ": Unable to open device.", errno); _fd = -1; @@ -147,8 +147,10 @@ void RawDevice::read(unsigned block, void *bp) // TODO -- EINTR? if (ok != 512) - throw Exception(__METHOD__ ": Error reading block.", - ok < 0 ? errno : 0); + throw ok < 0 + ? POSIXException(__METHOD__ ": Error reading block.", errno) + : Exception(__METHOD__ ": Error reading block."); + } @@ -166,8 +168,9 @@ void RawDevice::write(unsigned block, const void *bp) size_t ok = ::pwrite(_fd, bp, 512, block * 512); if (ok != 512) - throw Exception(__METHOD__ ": Error writing block.", - ok < 0 ? errno : 0); + throw ok < 0 + ? POSIXException(__METHOD__ ": Error writing block.", errno) + : Exception(__METHOD__ ": Error writing block."); } @@ -184,5 +187,5 @@ void RawDevice::sync() if (_readOnly) return; if (::fsync(_fd) < 0) - throw Exception(__METHOD__ ": fsync error.", errno); + throw POSIXException(__METHOD__ ": fsync error.", errno); } \ No newline at end of file