-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathswhxx.cpp
112 lines (92 loc) · 2.27 KB
/
swhxx.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
/*
Swephelp
Copyright 2007-2020 Stanislas Marquis <[email protected]>
Swephelp 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 2 of
the License, or (at your option) any later version.
Swephelp 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 Swephelp. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include "swhxx.h"
#include "swhxx.hpp"
swh::ErrorBase::ErrorBase()
:
m_error(NULL)
{
}
swh::ErrorBase::~ErrorBase()
{
if (m_error)
delete m_error;
}
const char* swh::ErrorBase::error() const
{
return m_error ? m_error->c_str() : NULL;
}
const char* swhxx_get_error(void* o)
{
return ((swh::ErrorBase*)o)->error();
}
void swh::ErrorBase::error(const char* s)
{
if (!s)
return clearError();
if (!m_error) {
m_error = new (std::nothrow) string(s);
if (!m_error) {
fputs("nomem", stderr);
exit(1);
}
}
else
*m_error = s;
}
void swh::ErrorBase::errorFormat(const char* fmt, ...)
{
va_list ap;
char err[512];
memset(err, 0, 512);
assert(fmt);
va_start(ap, fmt);
vsnprintf(err, 511, fmt, ap);
va_end(ap);
error(err);
}
bool swh::ErrorBase::hasError() const
{
return m_error ? true : false;
}
int swhxx_has_error(void* o)
{
return ((swh::ErrorBase*)o)->hasError() == true ? 1 : 0;
}
void swh::ErrorBase::clearError()
{
if (m_error) {
delete m_error;
m_error = NULL;
}
}
void swhxx_clear_error(void* o)
{
((swh::ErrorBase*)o)->clearError();
}
string swh::replaceAll(string str, const string& from, const string& to)
{
size_t pos = 0;
while((pos = str.find(from, pos)) != string::npos) {
str.replace(pos, from.length(), to);
pos += to.length();
}
return str;
}
/* vi: set fenc=utf-8 ff=unix et sw=4 ts=4 */