Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/error.cpp
2 views
1
/* Mednafen - Multi-system Emulator
2
*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*/
17
18
#include <string.h>
19
#include <stdarg.h>
20
#include <stdio.h>
21
22
#include "emuware/emuware.h"
23
#include "error.h"
24
25
MDFN_Error::MDFN_Error() noexcept
26
{
27
abort();
28
}
29
30
MDFN_Error::MDFN_Error(int errno_code_new, const char *format, ...) noexcept
31
{
32
errno_code = errno_code_new;
33
34
error_message = NULL;
35
36
int size = 128;
37
for(;;) {
38
va_list ap;
39
va_start(ap, format);
40
error_message = (char*)malloc(size);
41
size *= 2;
42
int ret = vsprintf(error_message, format, ap);
43
va_end(ap);
44
if(ret>=0)
45
break;
46
free(error_message);
47
}
48
}
49
50
51
MDFN_Error::MDFN_Error(const ErrnoHolder &enh)
52
{
53
errno_code = enh.Errno();
54
55
int size = 128;
56
for(;;) {
57
error_message = (char*)malloc(size);
58
size *= 2;
59
int ret = sprintf("%s", enh.StrError());
60
if(ret>=0)
61
break;
62
free(error_message);
63
}
64
}
65
66
67
MDFN_Error::~MDFN_Error() noexcept
68
{
69
if(error_message)
70
{
71
free(error_message);
72
error_message = NULL;
73
}
74
}
75
76
MDFN_Error::MDFN_Error(const MDFN_Error &ze_error) noexcept
77
{
78
if(ze_error.error_message)
79
error_message = strdup(ze_error.error_message);
80
else
81
error_message = NULL;
82
83
errno_code = ze_error.errno_code;
84
}
85
86
MDFN_Error& MDFN_Error::operator=(const MDFN_Error &ze_error) noexcept
87
{
88
char *new_error_message = ze_error.error_message ? strdup(ze_error.error_message) : NULL;
89
int new_errno_code = ze_error.errno_code;
90
91
if(error_message)
92
free(error_message);
93
94
error_message = new_error_message;
95
errno_code = new_errno_code;
96
97
return(*this);
98
}
99
100
101
const char * MDFN_Error::what(void) const noexcept
102
{
103
if(!error_message)
104
return("Error allocating memory for the error message!");
105
106
return(error_message);
107
}
108
109
int MDFN_Error::GetErrno(void) const noexcept
110
{
111
return(errno_code);
112
}
113
114
static const char *srr_wrap(int ret, const char *local_strerror)
115
{
116
if(ret == -1)
117
return("ERROR IN strerror_r()!!!");
118
119
return(local_strerror);
120
}
121
122
static const char *srr_wrap(const char *ret, const char *local_strerror)
123
{
124
if(ret == NULL)
125
return("ERROR IN strerror_r()!!!");
126
127
return(ret);
128
}
129
130
void ErrnoHolder::SetErrno(int the_errno)
131
{
132
local_errno = the_errno;
133
134
if(the_errno == 0)
135
local_strerror[0] = 0;
136
else
137
{
138
#ifdef HAVE_STRERROR_R
139
const char *retv;
140
141
retv = srr_wrap(strerror_r(the_errno, local_strerror, 256), local_strerror);
142
143
if(retv != local_strerror)
144
strncpy(local_strerror, retv, 255);
145
146
#else // No strerror_r :(
147
148
strncpy(local_strerror, strerror(the_errno), 255);
149
150
#endif
151
152
local_strerror[255] = 0;
153
}
154
}
155
156
157