/
exc.cpp
124 lines (110 loc) · 2.49 KB
/
exc.cpp
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
/*
* This file is part of bino, a 3D video player.
*
* Copyright (C) 2006, 2007, 2008, 2009, 2010
* Martin Lambers <marlam@marlam.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <cstring>
#include <cerrno>
#include "msg.h"
#include "exc.h"
const char *exc::_fallback_str = strerror(ENOMEM);
exc::exc() throw ()
: _fallback(false), _str(), _sys_errno(0)
{
}
exc::exc(const std::string &what, int sys_errno) throw ()
: _fallback(false), _sys_errno(sys_errno)
{
try
{
_str = what;
}
catch (...)
{
_fallback = true;
_sys_errno = ENOMEM;
}
if (!empty())
{
msg::dbg("Exception: %s", _str.c_str());
}
}
exc::exc(int sys_errno) throw ()
: _fallback(false), _sys_errno(sys_errno)
{
try
{
_str = strerror(_sys_errno);
}
catch (...)
{
_fallback = true;
_sys_errno = ENOMEM;
}
if (!empty())
{
msg::dbg("Exception: %s", _str.c_str());
}
}
exc::exc(const exc &e) throw ()
: _fallback(e._fallback), _sys_errno(e._sys_errno)
{
try
{
_str = e._str;
}
catch (...)
{
_fallback = true;
_sys_errno = ENOMEM;
}
}
exc::exc(const std::exception &e) throw ()
: _fallback(false), _sys_errno(0)
{
// TODO: Avoid the crappy what() strings; ideally translate them to errnos.
// E.g. std::bad_alloc -> ENOMEM.
try
{
_str = e.what();
}
catch (...)
{
_fallback = true;
_sys_errno = ENOMEM;
}
if (!empty())
{
msg::dbg("Exception: %s", _str.c_str());
}
}
exc::~exc() throw ()
{
}
bool exc::empty() const throw ()
{
return (_str.length() == 0 && _sys_errno == 0 && !_fallback);
}
int exc::sys_errno() const throw ()
{
return _sys_errno;
}
const char *exc::what() const throw ()
{
return (_fallback ? _fallback_str : _str.c_str());
}