diff options
Diffstat (limited to 'db/cxx/cxx_except.cpp')
-rw-r--r-- | db/cxx/cxx_except.cpp | 151 |
1 files changed, 64 insertions, 87 deletions
diff --git a/db/cxx/cxx_except.cpp b/db/cxx/cxx_except.cpp index 2710af30b..22eb0eae9 100644 --- a/db/cxx/cxx_except.cpp +++ b/db/cxx/cxx_except.cpp @@ -1,68 +1,20 @@ /*- * See the file LICENSE for redistribution information. * - * Copyright (c) 1997-2003 + * Copyright (c) 1997-2004 * Sleepycat Software. All rights reserved. + * + * $Id: cxx_except.cpp,v 11.28 2004/09/22 03:34:48 bostic Exp $ */ #include "db_config.h" -#ifndef lint -static const char revid[] = "$Id: cxx_except.cpp,v 11.21 2003/05/19 15:38:51 mjc Exp $"; -#endif /* not lint */ - #include <string.h> #include <errno.h> #include "db_cxx.h" #include "dbinc/cxx_int.h" -// tmpString is used to create strings on the stack -// -class tmpString -{ -public: - tmpString(const char *str1, - const char *str2 = 0, - const char *str3 = 0, - const char *str4 = 0, - const char *str5 = 0); - ~tmpString() { delete [] s_; } - operator const char *() { return (s_); } - -private: - char *s_; -}; - -tmpString::tmpString(const char *str1, - const char *str2, - const char *str3, - const char *str4, - const char *str5) -{ - size_t len = strlen(str1); - if (str2) - len += strlen(str2); - if (str3) - len += strlen(str3); - if (str4) - len += strlen(str4); - if (str5) - len += strlen(str5); - - s_ = new char[len+1]; - - strcpy(s_, str1); - if (str2) - strcat(s_, str2); - if (str3) - strcat(s_, str3); - if (str4) - strcat(s_, str4); - if (str5) - strcat(s_, str5); -} - // Note: would not be needed if we can inherit from exception // It does not appear to be possible to inherit from exception // with the current Microsoft library (VC5.0). @@ -80,66 +32,99 @@ static char *dupString(const char *s) // // //////////////////////////////////////////////////////////////////////// -DbException::~DbException() +DbException::~DbException() throw() { - if (what_) - delete [] what_; + delete [] what_; } DbException::DbException(int err) : err_(err) , env_(0) { - what_ = dupString(db_strerror(err)); + describe(0, 0); } DbException::DbException(const char *description) : err_(0) , env_(0) { - what_ = dupString(tmpString(description)); + describe(0, description); } -DbException::DbException(const char *prefix, int err) +DbException::DbException(const char *description, int err) : err_(err) , env_(0) { - what_ = dupString(tmpString(prefix, ": ", db_strerror(err))); + describe(0, description); } -DbException::DbException(const char *prefix1, const char *prefix2, int err) +DbException::DbException(const char *prefix, const char *description, int err) : err_(err) , env_(0) { - what_ = dupString(tmpString(prefix1, ": ", prefix2, ": ", - db_strerror(err))); + describe(prefix, description); } DbException::DbException(const DbException &that) -: err_(that.err_) +: __DB_STD(exception)() +, what_(dupString(that.what_)) +, err_(that.err_) , env_(0) { - what_ = dupString(that.what_); } DbException &DbException::operator = (const DbException &that) { if (this != &that) { err_ = that.err_; - if (what_) - delete [] what_; - what_ = 0; // in case new throws exception + delete [] what_; what_ = dupString(that.what_); } return (*this); } +void DbException::describe(const char *prefix, const char *description) +{ + char msgbuf[1024], *p, *end; + + p = msgbuf; + end = msgbuf + sizeof(msgbuf) - 1; + + if (prefix != NULL) { + strncpy(p, prefix, (p < end) ? end - p: 0); + p += strlen(prefix); + strncpy(p, ": ", (p < end) ? end - p: 0); + p += 2; + } + if (description != NULL) { + strncpy(p, description, (p < end) ? end - p: 0); + p += strlen(description); + if (err_ != 0) { + strncpy(p, ": ", (p < end) ? end - p: 0); + p += 2; + } + } + if (err_ != 0) { + strncpy(p, db_strerror(err_), (p < end) ? end - p: 0); + p += strlen(db_strerror(err_)); + } + + /* + * If the result was too long, the buffer will not be null-terminated, + * so we need to fix that here before duplicating it. + */ + if (p >= end) + *end = '\0'; + + what_ = dupString(msgbuf); +} + int DbException::get_errno() const { return (err_); } -const char *DbException::what() const +const char *DbException::what() const throw() { return (what_); } @@ -161,7 +146,7 @@ void DbException::set_env(DbEnv *env) //////////////////////////////////////////////////////////////////////// static const char *memory_err_desc = "Dbt not large enough for available data"; -DbMemoryException::~DbMemoryException() +DbMemoryException::~DbMemoryException() throw() { } @@ -171,25 +156,12 @@ DbMemoryException::DbMemoryException(Dbt *dbt) { } -DbMemoryException::DbMemoryException(const char *description) -: DbException(description, ENOMEM) -, dbt_(0) -{ -} - DbMemoryException::DbMemoryException(const char *prefix, Dbt *dbt) : DbException(prefix, memory_err_desc, ENOMEM) , dbt_(dbt) { } -DbMemoryException::DbMemoryException(const char *prefix1, const char *prefix2, - Dbt *dbt) -: DbException(prefix1, prefix2, ENOMEM) -, dbt_(dbt) -{ -} - DbMemoryException::DbMemoryException(const DbMemoryException &that) : DbException(that) , dbt_(that.dbt_) @@ -217,7 +189,7 @@ Dbt *DbMemoryException::get_dbt() const // // //////////////////////////////////////////////////////////////////////// -DbDeadlockException::~DbDeadlockException() +DbDeadlockException::~DbDeadlockException() throw() { } @@ -245,7 +217,7 @@ DbDeadlockException // // //////////////////////////////////////////////////////////////////////// -DbLockNotGrantedException::~DbLockNotGrantedException() +DbLockNotGrantedException::~DbLockNotGrantedException() throw() { delete lock_; } @@ -258,13 +230,18 @@ DbLockNotGrantedException::DbLockNotGrantedException(const char *prefix, , op_(op) , mode_(mode) , obj_(obj) +, lock_(new DbLock(lock)) , index_(index) { - lock_ = new DbLock(lock); } DbLockNotGrantedException::DbLockNotGrantedException(const char *description) : DbException(description, DB_LOCK_NOTGRANTED) +, op_(DB_LOCK_GET) +, mode_(DB_LOCK_NG) +, obj_(NULL) +, lock_(NULL) +, index_(0) { } @@ -275,7 +252,7 @@ DbLockNotGrantedException::DbLockNotGrantedException op_ = that.op_; mode_ = that.mode_; obj_ = that.obj_; - lock_ = new DbLock(*that.lock_); + lock_ = (that.lock_ != NULL) ? new DbLock(*that.lock_) : NULL; index_ = that.index_; } @@ -287,7 +264,7 @@ DbLockNotGrantedException op_ = that.op_; mode_ = that.mode_; obj_ = that.obj_; - lock_ = new DbLock(*that.lock_); + lock_ = (that.lock_ != NULL) ? new DbLock(*that.lock_) : NULL; index_ = that.index_; } return (*this); @@ -324,7 +301,7 @@ int DbLockNotGrantedException::get_index() const // // //////////////////////////////////////////////////////////////////////// -DbRunRecoveryException::~DbRunRecoveryException() +DbRunRecoveryException::~DbRunRecoveryException() throw() { } |