source: trunk/libdjvu/GException.cpp @ 426

Last change on this file since 426 was 280, checked in by rbri, 12 years ago

DJVU plugin: djvulibre updated to version 3.5.22

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