kviewshell
GException.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063
00064 #include <stdlib.h>
00065 #include <stdio.h>
00066 #include <string.h>
00067 #include "GException.h"
00068 #include "DjVuMessageLite.h"
00069 #include "debug.h"
00070
00071
00072 #ifdef HAVE_NAMESPACES
00073 namespace DJVU {
00074 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00075 }
00076 #endif
00077 #endif
00078
00079
00080
00081
00082 GException::GException()
00083 : cause(0), file(0), func(0), line(0), source(GException::GINTERNAL)
00084 {
00085 }
00086
00087 const char * const
00088 GException::outofmemory = ERR_MSG("GException.outofmemory");
00089
00090 GException::GException(const GException & exc)
00091 : file(exc.file), func(exc.func), line(exc.line), source(exc.source)
00092 {
00093 if (exc.cause && exc.cause!=outofmemory)
00094 {
00095 char *s = new char[strlen(exc.cause)+1];
00096 strcpy(s, exc.cause);
00097 cause = s;
00098 }
00099 else
00100 {
00101 cause = exc.cause;
00102 }
00103 }
00104
00105 GException::GException (const char *xcause, const char *file, int line,
00106 const char *func, const source_type xsource)
00107 : file(file), func(func), line(line), source(xsource)
00108 {
00109
00110
00111 #ifdef DEBUG_MSG
00112 DEBUG_MSG("GException::GException(): cause=" << (xcause ? xcause : "unknown") << "\n");
00113 #endif
00114 if (xcause && xcause!=outofmemory)
00115 {
00116 char *s = new char[strlen(xcause)+1];
00117 strcpy(s, xcause);
00118 cause = s;
00119 }
00120 else
00121 {
00122 cause = xcause;
00123 }
00124 }
00125
00126 GException::~GException(void)
00127 {
00128 if (cause && cause!=outofmemory )
00129 delete [] const_cast<char*>(cause);
00130 cause=file=func=0;
00131 }
00132
00133 GException &
00134 GException::operator=(const GException & exc)
00135 {
00136 if (cause && cause!=outofmemory)
00137 delete [] const_cast<char*>(cause);
00138 cause = 0;
00139 file = exc.file;
00140 func = exc.func;
00141 line = exc.line;
00142 source=exc.source;
00143 if (exc.cause && exc.cause!=outofmemory)
00144 {
00145 char *s = new char[strlen(exc.cause)+1];
00146 strcpy(s, exc.cause);
00147 cause = s;
00148 }
00149 else
00150 {
00151 cause = exc.cause;
00152 }
00153 return *this;
00154 }
00155
00156 void
00157 GException::perror(void) const
00158 {
00159 fflush(0);
00160 DjVuPrintErrorUTF8("*** ");
00161 DjVuMessageLite::perror(get_cause());
00162 if (file && line>0)
00163 DjVuPrintErrorUTF8("*** (%s:%d)\n", file, line);
00164 else if (file)
00165 DjVuPrintErrorUTF8("*** (%s)\n", file);
00166 if (func)
00167 DjVuPrintErrorUTF8("*** '%s'\n", func);
00168 DjVuPrintErrorUTF8("\n");
00169 }
00170
00171 const char*
00172 GException::get_cause(void) const
00173 {
00174 if (! cause)
00175 return "Invalid exception";
00176 return cause;
00177 }
00178
00179 int
00180 GException::cmp_cause(const char s1[] , const char s2[])
00181 {
00182 int retval;
00183 if(! s2 || !s2[0])
00184 {
00185 retval=(s1&&s1[0])?1:(-1);
00186 }else if(! s1 || !s1[0])
00187 {
00188 retval=(-1);
00189 }else
00190 {
00191 const char *end_s1=strpbrk(s1,"\t\n");
00192 const int n1=end_s1?(int)((size_t)end_s1-(size_t)s1):strlen(s1);
00193 const char *end_s2=strpbrk(s1,"\t\n");
00194 const int n2=end_s2?(int)((size_t)end_s2-(size_t)s2):strlen(s2);
00195 retval=(n1==n2)?strncmp(s1,s2,n1):strcmp(s1,s2);
00196 }
00197 return retval;
00198 }
00199
00200 int
00201 GException::cmp_cause(const char s2[]) const
00202 {
00203 return cmp_cause(cause,s2);
00204 }
00205
00206 #ifdef USE_EXCEPTION_EMULATION
00207
00208 GExceptionHandler *GExceptionHandler::head = 0;
00209
00210 void
00211 GExceptionHandler::emthrow(const GException &gex)
00212 {
00213 if (head)
00214 {
00215 head->current = gex;
00216 longjmp(head->jump, -1);
00217 }
00218 else
00219 {
00220 DjVuPrintErrorUTF8("\n*** Unhandled exception");
00221 gex.perror();
00222 abort();
00223 }
00224 }
00225
00226 #else // ! USE_EXCEPTION_EMULATION
00227
00228 static int abort_on_exception = 0;
00229
00230 void
00231 #ifndef NO_LIBGCC_HOOKS
00232 GExceptionHandler::exthrow(const GException &ex)
00233 #else
00234 GExceptionHandler::exthrow(const GException ex)
00235 #endif
00236 {
00237 if (abort_on_exception)
00238 abort();
00239 throw ex;
00240 }
00241
00242 void
00243 GExceptionHandler::rethrow(void)
00244 {
00245 if (abort_on_exception)
00246 abort();
00247 throw;
00248 }
00249
00250 #endif
00251
00252
00253
00254
00255
00256 #ifndef NEED_DJVU_MEMORY
00257
00258
00259
00260 # if defined(_MSC_VER)
00261
00262 static int throw_memory_error(size_t) { G_THROW(GException::outofmemory); return 0; }
00263 static int (*old_handler)(size_t) = _set_new_handler(throw_memory_error);
00264 # else // !_MSC_VER
00265
00266 static void throw_memory_error() { G_THROW(GException::outofmemory); }
00267 # if !defined(WIN32) && !defined(__CYGWIN32__) && !defined(OS2)
00268 # ifdef HAVE_STDINCLUDES
00269 static void (*old_handler)() = std::set_new_handler(throw_memory_error);
00270 # else
00271 static void (*old_handler)() = set_new_handler(throw_memory_error);
00272 # endif // HAVE_STDINCLUDES
00273 # endif // ! WIN32
00274 # endif // !_MSC_VER
00275 #endif // !NEED_DJVU_MEMORY
00276
00277
00278 #ifdef HAVE_NAMESPACES
00279 }
00280 # ifndef NOT_USING_DJVU_NAMESPACE
00281 using namespace DJVU;
00282 # endif
00283 #endif
00284