Blame libdjvu/GException.cpp

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma implementation
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#include <stddef.h>
Packit df99a1
#include <stdlib.h>
Packit df99a1
#include <stdio.h>
Packit df99a1
#include <string.h>
Packit df99a1
#include "GException.h"
Packit df99a1
#include "DjVuMessageLite.h"
Packit df99a1
#include "debug.h"
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
namespace DJVU {
Packit df99a1
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
Packit df99a1
}
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
// - Author: Leon Bottou, 05/1997
Packit df99a1
Packit df99a1
GException::GException() 
Packit df99a1
  : cause(0), file(0), func(0), line(0), source(GException::GINTERNAL)
Packit df99a1
{
Packit df99a1
}
Packit df99a1
Packit df99a1
const char * const
Packit df99a1
GException::outofmemory = ERR_MSG("GException.outofmemory");
Packit df99a1
Packit df99a1
GException::GException(const GException & exc) 
Packit df99a1
  : file(exc.file), func(exc.func), line(exc.line), source(exc.source)
Packit df99a1
{
Packit df99a1
  if (exc.cause && exc.cause!=outofmemory) 
Packit df99a1
    {
Packit df99a1
      char *s = new char[strlen(exc.cause)+1];
Packit df99a1
      strcpy(s, exc.cause);
Packit df99a1
      cause = s;
Packit df99a1
    }
Packit df99a1
  else
Packit df99a1
    {
Packit df99a1
      cause = exc.cause;
Packit df99a1
    }
Packit df99a1
}
Packit df99a1
Packit df99a1
GException::GException (const char *xcause, const char *file, int line,
Packit df99a1
   const char *func, const source_type xsource)
Packit df99a1
  : file(file), func(func), line(line), source(xsource)
Packit df99a1
{
Packit df99a1
  // good place to set a breakpoint and DEBUG message too. 
Packit df99a1
  // It'd hard to track exceptions which seem to go from nowhere
Packit df99a1
#ifdef DEBUG_MSG
Packit df99a1
  DEBUG_MSG("GException::GException(): cause=" << (xcause ? xcause : "unknown") << "\n");
Packit df99a1
#endif
Packit df99a1
  if (xcause && xcause!=outofmemory) 
Packit df99a1
    {
Packit df99a1
      char *s = new char[strlen(xcause)+1];
Packit df99a1
      strcpy(s, xcause);
Packit df99a1
      cause = s;
Packit df99a1
    }
Packit df99a1
  else
Packit df99a1
    {
Packit df99a1
      cause = xcause;
Packit df99a1
    }
Packit df99a1
}
Packit df99a1
Packit df99a1
GException::~GException(void)
Packit df99a1
{
Packit df99a1
  if (cause && cause!=outofmemory ) 
Packit df99a1
    delete [] const_cast<char*>(cause); 
Packit df99a1
  cause=file=func=0;
Packit df99a1
}
Packit df99a1
Packit df99a1
GException & 
Packit df99a1
GException::operator=(const GException & exc)
Packit df99a1
{
Packit df99a1
  if (cause && cause!=outofmemory) 
Packit df99a1
    delete [] const_cast<char*>(cause);
Packit df99a1
  cause = 0;
Packit df99a1
  file = exc.file;
Packit df99a1
  func = exc.func;
Packit df99a1
  line = exc.line;
Packit df99a1
  source=exc.source;
Packit df99a1
  if (exc.cause && exc.cause!=outofmemory) 
Packit df99a1
    {
Packit df99a1
      char *s = new char[strlen(exc.cause)+1];
Packit df99a1
      strcpy(s, exc.cause);
Packit df99a1
      cause = s;
Packit df99a1
    }
Packit df99a1
  else
Packit df99a1
    {
Packit df99a1
      cause = exc.cause;
Packit df99a1
    }
Packit df99a1
  return *this;
Packit df99a1
}
Packit df99a1
Packit df99a1
void
Packit df99a1
GException::perror(void) const
Packit df99a1
{
Packit df99a1
  fflush(0);
Packit df99a1
  DjVuPrintErrorUTF8("*** ");
Packit df99a1
  DjVuMessageLite::perror(get_cause());
Packit df99a1
  if (file && line>0)
Packit df99a1
    DjVuPrintErrorUTF8("*** (%s:%d)\n", file, line);    
Packit df99a1
  else if (file)
Packit df99a1
    DjVuPrintErrorUTF8("*** (%s)\n", file);
Packit df99a1
  if (func)
Packit df99a1
    DjVuPrintErrorUTF8("*** '%s'\n", func);    
Packit df99a1
  DjVuPrintErrorUTF8("\n");
Packit df99a1
}
Packit df99a1
Packit df99a1
const char* 
Packit df99a1
GException::get_cause(void) const
Packit df99a1
{
Packit df99a1
  if (! cause)
Packit df99a1
    return "Invalid exception";
Packit df99a1
  return cause;
Packit df99a1
}
Packit df99a1
Packit df99a1
int
Packit df99a1
GException::cmp_cause(const char s1[] , const char s2[])
Packit df99a1
{
Packit df99a1
  int retval;
Packit df99a1
  if(! s2 || !s2[0])
Packit df99a1
  {
Packit df99a1
    retval=(s1&&s1[0])?1:(-1);
Packit df99a1
  }else if(! s1 || !s1[0])
Packit df99a1
  {
Packit df99a1
    retval=(-1);
Packit df99a1
  }else
Packit df99a1
  {
Packit df99a1
    const char *end_s1=strpbrk(s1,"\t\n");
Packit df99a1
    const int n1=end_s1?(int)((size_t)end_s1-(size_t)s1):strlen(s1);
Packit df99a1
    const char *end_s2=strpbrk(s1,"\t\n");
Packit df99a1
    const int n2=end_s2?(int)((size_t)end_s2-(size_t)s2):strlen(s2);
Packit df99a1
    retval=(n1==n2)?strncmp(s1,s2,n1):strcmp(s1,s2);
Packit df99a1
  }
Packit df99a1
  return retval;
Packit df99a1
}
Packit df99a1
Packit df99a1
int
Packit df99a1
GException::cmp_cause(const char s2[]) const
Packit df99a1
{
Packit df99a1
  return cmp_cause(cause,s2);
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
// ------ MEMORY MANAGEMENT HANDLER
Packit df99a1
Packit df99a1
#ifndef NEED_DJVU_MEMORY
Packit df99a1
// This is not activated when C++ memory management
Packit df99a1
// is overidden.  The overriding functions handle
Packit df99a1
// memory exceptions by themselves.
Packit df99a1
static void throw_memory_error() { G_THROW(GException::outofmemory); }
Packit df99a1
# if defined(_WIN32) || defined(__CYGWIN32__) || defined(OS2)
Packit df99a1
static void (*old_handler)() = std::set_new_handler(throw_memory_error);
Packit df99a1
# else 
Packit df99a1
#   ifdef HAVE_STDINCLUDES
Packit df99a1
static void (*old_handler)() = std::set_new_handler(throw_memory_error);
Packit df99a1
#   else
Packit df99a1
static void (*old_handler)() = set_new_handler(throw_memory_error);
Packit df99a1
#  endif // HAVE_STDINCLUDES
Packit df99a1
# endif // ! WIN32
Packit df99a1
#endif // !NEED_DJVU_MEMORY
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
}
Packit df99a1
# ifndef NOT_USING_DJVU_NAMESPACE
Packit df99a1
using namespace DJVU;
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1