| 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 "mednafen.h" |
| 19 | #include "error.h" |
| 20 | #include <string.h> |
| 21 | #include <stdarg.h> |
| 22 | #include "../libretro.h" |
| 23 | |
| 24 | extern retro_log_printf_t log_cb; |
| 25 | |
| 26 | MDFN_Error::MDFN_Error() throw() |
| 27 | { |
| 28 | abort(); |
| 29 | } |
| 30 | |
| 31 | MDFN_Error::MDFN_Error(int errno_code_new, const char *format, ...) throw() |
| 32 | { |
| 33 | errno_code = errno_code_new; |
| 34 | |
| 35 | va_list ap; |
| 36 | va_start(ap, format); |
| 37 | vsnprintf(error_message, 4096, format, ap); |
| 38 | va_end(ap); |
| 39 | |
| 40 | if (log_cb) |
| 41 | log_cb(RETRO_LOG_ERROR, "%s\n", error_message); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | MDFN_Error::MDFN_Error(const ErrnoHolder &enh) |
| 46 | { |
| 47 | errno_code = enh.Errno(); |
| 48 | |
| 49 | error_message = strdup(enh.StrError()); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | MDFN_Error::~MDFN_Error() throw() |
| 54 | { |
| 55 | if(error_message) |
| 56 | { |
| 57 | free(error_message); |
| 58 | error_message = NULL; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | MDFN_Error::MDFN_Error(const MDFN_Error &ze_error) throw() |
| 63 | { |
| 64 | if(ze_error.error_message) |
| 65 | error_message = strdup(ze_error.error_message); |
| 66 | else |
| 67 | error_message = NULL; |
| 68 | |
| 69 | errno_code = ze_error.errno_code; |
| 70 | } |
| 71 | |
| 72 | MDFN_Error& MDFN_Error::operator=(const MDFN_Error &ze_error) throw() |
| 73 | { |
| 74 | char *new_error_message = ze_error.error_message ? strdup(ze_error.error_message) : NULL; |
| 75 | int new_errno_code = ze_error.errno_code; |
| 76 | |
| 77 | if(error_message) |
| 78 | free(error_message); |
| 79 | |
| 80 | error_message = new_error_message; |
| 81 | errno_code = new_errno_code; |
| 82 | |
| 83 | return(*this); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | const char * MDFN_Error::what(void) const throw() |
| 88 | { |
| 89 | if(!error_message) |
| 90 | return("Error allocating memory for the error message!"); |
| 91 | |
| 92 | return(error_message); |
| 93 | } |
| 94 | |
| 95 | int MDFN_Error::GetErrno(void) const throw() |
| 96 | { |
| 97 | return(errno_code); |
| 98 | } |
| 99 | |
| 100 | static const char *srr_wrap(int ret, const char *local_strerror) |
| 101 | { |
| 102 | if(ret == -1) |
| 103 | return("ERROR IN strerror_r()!!!"); |
| 104 | |
| 105 | return(local_strerror); |
| 106 | } |
| 107 | |
| 108 | static const char *srr_wrap(const char *ret, const char *local_strerror) |
| 109 | { |
| 110 | if(ret == NULL) |
| 111 | return("ERROR IN strerror_r()!!!"); |
| 112 | |
| 113 | return(ret); |
| 114 | } |
| 115 | |
| 116 | void ErrnoHolder::SetErrno(int the_errno) |
| 117 | { |
| 118 | local_errno = the_errno; |
| 119 | |
| 120 | if(the_errno == 0) |
| 121 | local_strerror[0] = 0; |
| 122 | else |
| 123 | { |
| 124 | strncpy(local_strerror, strerror(the_errno), 255); |
| 125 | |
| 126 | local_strerror[255] = 0; |
| 127 | } |
| 128 | } |
| 129 |