summaryrefslogtreecommitdiff
path: root/bdb/cxx/cxx_except.cpp
blob: a62e21a767d4c3d57d7c051e9f27d31db8f973e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997, 1998, 1999, 2000
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
static const char revid[] = "$Id: cxx_except.cpp,v 11.7 2000/09/21 15:05:45 dda Exp $";
#endif /* not lint */

#include <string.h>

#include "db_cxx.h"
#include "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)
	{
		int 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);
	}
	~tmpString()                      { delete [] s_; }
	operator const char *()           { return (s_); }

private:
	char *s_;
};

// 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).
//
static char *dupString(const char *s)
{
	char *r = new char[strlen(s)+1];
	strcpy(r, s);
	return (r);
}

////////////////////////////////////////////////////////////////////////
//                                                                    //
//                            DbException                             //
//                                                                    //
////////////////////////////////////////////////////////////////////////

DbException::~DbException()
{
	if (what_)
		delete [] what_;
}

DbException::DbException(int err)
:	err_(err)
{
	what_ = dupString(db_strerror(err));
}

DbException::DbException(const char *description)
:	err_(0)
{
	what_ = dupString(tmpString(description));
}

DbException::DbException(const char *prefix, int err)
:	err_(err)
{
	what_ = dupString(tmpString(prefix, ": ", db_strerror(err)));
}

DbException::DbException(const char *prefix1, const char *prefix2, int err)
:	err_(err)
{
	what_ = dupString(tmpString(prefix1, ": ", prefix2, ": ", db_strerror(err)));
}

DbException::DbException(const DbException &that)
:	err_(that.err_)
{
	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
		what_ = dupString(that.what_);
	}
	return (*this);
}

int DbException::get_errno() const
{
	return (err_);
}

const char *DbException::what() const
{
	return (what_);
}