source: trunk/libdjvu/GException.cpp @ 15

Last change on this file since 15 was 15, checked in by Eugene Romanenko, 15 years ago

needed libs update

File size: 7.6 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: GException.cpp,v 1.14 2004/08/06 15:11:29 leonb Exp $
55// $Name: release_3_5_16 $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64#include <stdlib.h>
65#include <stdio.h>
66#include <string.h>
67#include "GException.h"
68#include "DjVuMessageLite.h"
69#include "debug.h"
70
71
72#ifdef HAVE_NAMESPACES
73namespace DJVU {
74# ifdef NOT_DEFINED // Just to fool emacs c++ mode
75}
76#endif
77#endif
78
79
80// - Author: Leon Bottou, 05/1997
81
82GException::GException() 
83  : cause(0), file(0), func(0), line(0), source(GException::GINTERNAL)
84{
85}
86
87const char * const
88GException::outofmemory = ERR_MSG("GException.outofmemory");
89
90GException::GException(const GException & exc) 
91  : file(exc.file), func(exc.func), line(exc.line), source(exc.source)
92{
93  if (exc.cause && exc.cause!=outofmemory) 
94    {
95      char *s = new char[strlen(exc.cause)+1];
96      strcpy(s, exc.cause);
97      cause = s;
98    }
99  else
100    {
101      cause = exc.cause;
102    }
103}
104
105GException::GException (const char *xcause, const char *file, int line,
106   const char *func, const source_type xsource)
107  : file(file), func(func), line(line), source(xsource)
108{
109  // good place to set a breakpoint and DEBUG message too.
110  // It'd hard to track exceptions which seem to go from nowhere
111#ifdef DEBUG_MSG
112  DEBUG_MSG("GException::GException(): cause=" << (xcause ? xcause : "unknown") << "\n");
113#endif
114  if (xcause && xcause!=outofmemory) 
115    {
116      char *s = new char[strlen(xcause)+1];
117      strcpy(s, xcause);
118      cause = s;
119    }
120  else
121    {
122      cause = xcause;
123    }
124}
125
126GException::~GException(void)
127{
128  if (cause && cause!=outofmemory ) 
129    delete [] const_cast<char*>(cause); 
130  cause=file=func=0;
131}
132
133GException & 
134GException::operator=(const GException & exc)
135{
136  if (cause && cause!=outofmemory) 
137    delete [] const_cast<char*>(cause);
138  cause = 0;
139  file = exc.file;
140  func = exc.func;
141  line = exc.line;
142  source=exc.source;
143  if (exc.cause && exc.cause!=outofmemory) 
144    {
145      char *s = new char[strlen(exc.cause)+1];
146      strcpy(s, exc.cause);
147      cause = s;
148    }
149  else
150    {
151      cause = exc.cause;
152    }
153  return *this;
154}
155
156void
157GException::perror(void) const
158{
159  fflush(0);
160  DjVuPrintErrorUTF8("*** ");
161  DjVuMessageLite::perror(get_cause());
162  if (file && line>0)
163    DjVuPrintErrorUTF8("*** (%s:%d)\n", file, line);   
164  else if (file)
165    DjVuPrintErrorUTF8("*** (%s)\n", file);
166  if (func)
167    DjVuPrintErrorUTF8("*** '%s'\n", func);   
168  DjVuPrintErrorUTF8("\n");
169}
170
171const char* 
172GException::get_cause(void) const
173{
174  if (! cause)
175    return "Invalid exception";
176  return cause;
177}
178
179int
180GException::cmp_cause(const char s1[] , const char s2[])
181{
182  int retval;
183  if(! s2 || !s2[0])
184  {
185    retval=(s1&&s1[0])?1:(-1);
186  }else if(! s1 || !s1[0])
187  {
188    retval=(-1);
189  }else
190  {
191    const char *end_s1=strpbrk(s1,"\t\n");
192    const int n1=end_s1?(int)((size_t)end_s1-(size_t)s1):strlen(s1);
193    const char *end_s2=strpbrk(s1,"\t\n");
194    const int n2=end_s2?(int)((size_t)end_s2-(size_t)s2):strlen(s2);
195    retval=(n1==n2)?strncmp(s1,s2,n1):strcmp(s1,s2);
196  }
197  return retval;
198}
199
200int
201GException::cmp_cause(const char s2[]) const
202{
203  return cmp_cause(cause,s2);
204}
205
206#ifdef USE_EXCEPTION_EMULATION
207
208GExceptionHandler *GExceptionHandler::head = 0;
209
210void
211GExceptionHandler::emthrow(const GException &gex)
212{
213  if (head)
214    {
215      head->current = gex;
216      longjmp(head->jump, -1);
217    }
218  else
219    {
220      DjVuPrintErrorUTF8("\n*** Unhandled exception");
221      gex.perror();
222      abort();
223    }
224}
225
226#else // ! USE_EXCEPTION_EMULATION
227
228static int abort_on_exception = 0;
229
230void 
231#ifndef NO_LIBGCC_HOOKS
232GExceptionHandler::exthrow(const GException &ex)
233#else
234GExceptionHandler::exthrow(const GException ex)
235#endif /* NO_LIBGCC_HOOKS */
236{
237  if (abort_on_exception) 
238    abort();
239  throw ex;
240}
241
242void 
243GExceptionHandler::rethrow(void)
244{
245  if (abort_on_exception) 
246    abort();
247  throw;
248}
249
250#endif
251
252
253
254// ------ MEMORY MANAGEMENT HANDLER
255
256#ifndef NEED_DJVU_MEMORY
257// This is not activated when C++ memory management
258// is overidden.  The overriding functions handle
259// memory exceptions by themselves.
260# if defined(_MSC_VER)
261// Microsoft is different!
262static int throw_memory_error(size_t) { G_THROW(GException::outofmemory); return 0; }
263static int (*old_handler)(size_t) = _set_new_handler(throw_memory_error);
264# else // !_MSC_VER
265// Standard C++
266static void throw_memory_error() { G_THROW(GException::outofmemory); }
267#  if !defined(WIN32) && !defined(__CYGWIN32__) && !defined(OS2)
268#   ifdef HAVE_STDINCLUDES
269static void (*old_handler)() = std::set_new_handler(throw_memory_error);
270#   else
271static void (*old_handler)() = set_new_handler(throw_memory_error);
272#   endif // HAVE_STDINCLUDES
273#  endif // ! WIN32
274# endif // !_MSC_VER
275#endif // !NEED_DJVU_MEMORY
276
277
278#ifdef HAVE_NAMESPACES
279}
280# ifndef NOT_USING_DJVU_NAMESPACE
281using namespace DJVU;
282# endif
283#endif
284
Note: See TracBrowser for help on using the repository browser.