• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

GException.cpp

Go to the documentation of this file.
00001 //C-  -*- C++ -*-
00002 //C- -------------------------------------------------------------------
00003 //C- DjVuLibre-3.5
00004 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
00005 //C- Copyright (c) 2001  AT&T
00006 //C-
00007 //C- This software is subject to, and may be distributed under, the
00008 //C- GNU General Public License, Version 2. The license should have
00009 //C- accompanied the software or you may obtain a copy of the license
00010 //C- from the Free Software Foundation at http://www.fsf.org .
00011 //C-
00012 //C- This program is distributed in the hope that it will be useful,
00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //C- GNU General Public License for more details.
00016 //C- 
00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
00018 //C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
00019 //C- Software authorized us to replace the original DjVu(r) Reference 
00020 //C- Library notice by the following text (see doc/lizard2002.djvu):
00021 //C-
00022 //C-  ------------------------------------------------------------------
00023 //C- | DjVu (r) Reference Library (v. 3.5)
00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
00026 //C- | 6,058,214 and patents pending.
00027 //C- |
00028 //C- | This software is subject to, and may be distributed under, the
00029 //C- | GNU General Public License, Version 2. The license should have
00030 //C- | accompanied the software or you may obtain a copy of the license
00031 //C- | from the Free Software Foundation at http://www.fsf.org .
00032 //C- |
00033 //C- | The computer code originally released by LizardTech under this
00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
00035 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 
00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
00040 //C- | General Public License.   This grant only confers the right to 
00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
00042 //C- | the extent such infringement is reasonably necessary to enable 
00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 
00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
00045 //C- | any greater extent that may be necessary to utilize further 
00046 //C- | modifications or combinations.
00047 //C- |
00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00052 //C- +------------------------------------------------------------------
00053 // 
00054 // $Id: GException.cpp,v 1.14 2004/08/06 15:11:29 leonb Exp $
00055 // $Name: release_3_5_15 $
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 // - Author: Leon Bottou, 05/1997
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   // good place to set a breakpoint and DEBUG message too. 
00110   // It'd hard to track exceptions which seem to go from nowhere
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 /* NO_LIBGCC_HOOKS */
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 // ------ MEMORY MANAGEMENT HANDLER
00255 
00256 #ifndef NEED_DJVU_MEMORY
00257 // This is not activated when C++ memory management
00258 // is overidden.  The overriding functions handle
00259 // memory exceptions by themselves.
00260 # if defined(_MSC_VER)
00261 // Microsoft is different!
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 // Standard C++
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 

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal