Blame libdjvu/DjVuGlobal.h

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
#ifndef _DJVUGLOBAL_H
Packit df99a1
#define _DJVUGLOBAL_H
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma interface
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#if defined(HAVE_STDINCLUDES)
Packit df99a1
# include <new>
Packit df99a1
#elif defined(HAVE_NEW_H)
Packit df99a1
# include <new.h>
Packit df99a1
#else
Packit df99a1
# include <new> // try standard c++ anyway!
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#ifdef _WIN32
Packit df99a1
# ifdef DJVUAPI_EXPORT
Packit df99a1
#  define DJVUAPI __declspec(dllexport)
Packit df99a1
# else
Packit df99a1
#  define DJVUAPI __declspec(dllimport)
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
#ifndef DJVUAPI
Packit df99a1
# define DJVUAPI
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
/** @name DjVuGlobal.h 
Packit df99a1
Packit df99a1
    This file is included by all include files in the DjVu reference library.
Packit df99a1
    
Packit df99a1
	If compilation symbols #NEED_DJVU_MEMORY#, #NEED_DJVU_PROGRESS# 
Packit df99a1
	or #NEED_DJVU_NAMES# are defined, this file enables 
Packit df99a1
	features which are useful for certain applications of the
Packit df99a1
    DjVu Reference Library.  These features are still experimental and
Packit df99a1
    therefore poorly documented.
Packit df99a1
    
Packit df99a1
    @memo
Packit df99a1
    Global definitions.
Packit df99a1
    @author
Packit df99a1
    L\'eon Bottou <leonb@research.att.com> -- empty file.\\
Packit df99a1
    Bill Riemers <docbill@sourceforge.net> -- real work.  */
Packit df99a1
//@{
Packit df99a1
Packit df99a1
Packit df99a1
/** @name DjVu Memory 
Packit df99a1
Packit df99a1
    This section is enabled when compilation symbol #NEED_DJVU_MEMORY# is
Packit df99a1
    defined.  Function #_djvu_memory_callback# can be used to redefine the C++
Packit df99a1
    memory allocation operators.  Some operating systems (e.g. Macintoshes)
Packit df99a1
    require very peculiar memory allocation in shared objects.  We redefine
Packit df99a1
    the operators #new# and #delete# as #STATIC_INLINE# because we do not
Packit df99a1
    want to export these redefined versions to other libraries.  */
Packit df99a1
//@{
Packit df99a1
//@}
Packit df99a1
Packit df99a1
#ifdef NEED_DJVU_MEMORY
Packit df99a1
Packit df99a1
# include "DjVu.h"
Packit df99a1
Packit df99a1
// These define the two callbacks needed for C++
Packit df99a1
typedef void djvu_delete_callback(void *);
Packit df99a1
typedef void *djvu_new_callback(size_t);
Packit df99a1
Packit df99a1
// These functions allow users to set the callbacks.
Packit df99a1
int djvu_memoryObject_callback ( djvu_delete_callback*, djvu_new_callback*);
Packit df99a1
int djvu_memoryArray_callback ( djvu_delete_callback*, djvu_new_callback*);
Packit df99a1
Packit df99a1
// We need to use this inline function in all modules, but we never want it to
Packit df99a1
// appear in the symbol table.  It seems different compilers need different
Packit df99a1
// directives to do this...
Packit df99a1
# ifndef STATIC_INLINE
Packit df99a1
#  ifdef __GNUC__
Packit df99a1
#   define STATIC_INLINE extern inline
Packit df99a1
#  else /* !__GNUC__ */
Packit df99a1
#   define STATIC_INLINE static inline
Packit df99a1
#  endif /* __GNUC__ */
Packit df99a1
# endif /* STATIC_INLINE */
Packit df99a1
Packit df99a1
// This clause is used when overriding operator new
Packit df99a1
// because the standard has slightly changed.
Packit df99a1
# if defined( __GNUC__ ) && ( __GNUC__*1000 + __GNUC_MINOR__ >= 2091 )
Packit df99a1
#  ifndef new_throw_spec
Packit df99a1
#   define new_throw_spec throw(std::bad_alloc)
Packit df99a1
#  endif /* new_throw_spec */
Packit df99a1
#  ifndef delete_throw_spec
Packit df99a1
#   define delete_throw_spec throw()
Packit df99a1
#  endif /* delete_throw_spec */
Packit df99a1
# endif /* __GNUC__ ... */
Packit df99a1
// Old style
Packit df99a1
# ifndef new_throw_spec
Packit df99a1
#  define new_throw_spec
Packit df99a1
# endif /* new_throw_spec */
Packit df99a1
# ifndef delete_throw_spec
Packit df99a1
#  define delete_throw_spec
Packit df99a1
# endif  /* delete_throw_spec */
Packit df99a1
Packit df99a1
# ifdef UNIX
Packit df99a1
extern djvu_new_callback *_djvu_new_ptr;
Packit df99a1
extern djvu_new_callback *_djvu_newArray_ptr;
Packit df99a1
extern djvu_delete_callback *_djvu_delete_ptr;
Packit df99a1
extern djvu_delete_callback *_djvu_deleteArray_ptr;
Packit df99a1
Packit df99a1
#  ifndef NEED_DJVU_MEMORY_IMPLEMENTATION
Packit df99a1
void *operator new (size_t) new_throw_spec;
Packit df99a1
void *operator new[] (size_t) new_throw_spec;
Packit df99a1
void operator delete (void *) delete_throw_spec;
Packit df99a1
void operator delete[] (void *) delete_throw_spec;
Packit df99a1
Packit df99a1
STATIC_INLINE void *
Packit df99a1
operator new(size_t sz) new_throw_spec
Packit df99a1
{ return (*_djvu_new_ptr)(sz); }
Packit df99a1
STATIC_INLINE void
Packit df99a1
operator delete(void *addr) delete_throw_spec
Packit df99a1
{ return (*_djvu_delete_ptr)(addr); }
Packit df99a1
STATIC_INLINE void *
Packit df99a1
operator new [] (size_t sz) new_throw_spec
Packit df99a1
{ return (*_djvu_newArray_ptr)(sz); }
Packit df99a1
STATIC_INLINE void
Packit df99a1
operator delete [] (void *addr) delete_throw_spec
Packit df99a1
{ return (*_djvu_deleteArray_ptr)(addr); }
Packit df99a1
#  endif /* NEED_DJVU_MEMORY_IMPLEMENTATION */
Packit df99a1
Packit df99a1
# else /* UNIX */
Packit df99a1
Packit df99a1
#  ifndef NEED_DJVU_MEMORY_IMPLEMENTATION
Packit df99a1
STATIC_INLINE void *
Packit df99a1
operator new(size_t sz) new_throw_spec
Packit df99a1
{ return _djvu_new(sz); }
Packit df99a1
inline_as_macro void
Packit df99a1
operator delete(void *addr) delete_throw_spec
Packit df99a1
{ return _djvu_delete(addr); }
Packit df99a1
inline_as_macro void *
Packit df99a1
operator new [] (size_t sz) new_throw_spec
Packit df99a1
{ return _djvu_new(sz); }
Packit df99a1
inline_as_macro void
Packit df99a1
operator delete [] (void *addr) delete_throw_spec
Packit df99a1
{ _djvu_deleteArray(addr); }
Packit df99a1
#  endif /* !NEED_DJVU_MEMORY_IMPLEMENTATION */
Packit df99a1
Packit df99a1
# endif /* UNIX */
Packit df99a1
Packit df99a1
#else
Packit df99a1
Packit df99a1
# define _djvu_free(ptr) free((ptr))
Packit df99a1
# define _djvu_malloc(siz) malloc((siz))
Packit df99a1
# define _djvu_realloc(ptr,siz) realloc((ptr),(siz))
Packit df99a1
# define _djvu_calloc(siz,items) calloc((siz),(items))
Packit df99a1
Packit df99a1
#endif /* NEED_DJVU_MEMORY */
Packit df99a1
Packit df99a1
/** @name DjVu Progress  
Packit df99a1
Packit df99a1
    This section is enabled when compilation symbol #NEED_DJVU_PROGRESS# is
Packit df99a1
    defined.  This macro setups callback function that may be used to
Packit df99a1
    implement a progress indicator for the encoding routines.  The decoding
Packit df99a1
    routines do not need such a facility because it is sufficient to monitor
Packit df99a1
    the calls to function \Ref{ByteStream::read} in class \Ref{ByteStream}.
Packit df99a1
    
Packit df99a1
    {\bf Code tracing macros} ---
Packit df99a1
    Monitoring the progress of such complex algorithms requires significant
Packit df99a1
    code support.  This is achieved by inserting {\em code tracing macros}
Packit df99a1
    in strategic regions of the code.  
Packit df99a1
    \begin{description}
Packit df99a1
    \item[DJVU_PROGRESS_TASK(name,task,nsteps)]  indicates that the current
Packit df99a1
         scope performs a task roughly divided in #nsteps# equal steps, with
Packit df99a1
	       the specified #task# string used in the callback.
Packit df99a1
    \item[DJVU_PROGRESS_RUN(name,tostep)] indicates that we are starting
Packit df99a1
         an operation which will take us to step #tostep#.  The operation
Packit df99a1
         will be considered finished when #DJVU_PROGRESS_RUN# will be called
Packit df99a1
         again with an argument greater than #tostep#.  The execution of
Packit df99a1
         this operation of course can be described by one subtask and so on.
Packit df99a1
    \end{description}
Packit df99a1
 
Packit df99a1
    {\bf Progress callback} --- Before defining the outermost task, you can
Packit df99a1
    store a callback function pointer into the static member variable
Packit df99a1
    #DjVuProgressTask::callback#.  This callback function is called
Packit df99a1
    periodically with two unsigned long arguments.  The first argument is the
Packit df99a1
    elapsed time. The second argument is the estimated total execution time.
Packit df99a1
    Both times are given in milliseconds.
Packit df99a1
Packit df99a1
    {\bf Important Note} --- This monitoring mechanism should not be used by
Packit df99a1
    multithreaded programs.  */
Packit df99a1
//@{
Packit df99a1
Packit df99a1
#ifndef HAS_DJVU_PROGRESS_CALLBACKS
Packit df99a1
# define HAS_DJVU_PROGRESS_CALLBACKS
Packit df99a1
Packit df99a1
# ifdef NEED_DJVU_PROGRESS
Packit df99a1
#  include "DjVu.h"
Packit df99a1
Packit df99a1
extern djvu_progress_callback *_djvu_progress_ptr;
Packit df99a1
Packit df99a1
#  define DJVU_PROGRESS_TASK(name,task,nsteps)  DjVuProgressTask task_##name(task,nsteps)
Packit df99a1
#  define DJVU_PROGRESS_RUN(name,tostep)   { task_##name.run(tostep); }
Packit df99a1
Packit df99a1
class DjVuProgressTask
Packit df99a1
{
Packit df99a1
public:
Packit df99a1
  class Data;
Packit df99a1
  ~DjVuProgressTask();
Packit df99a1
  DjVuProgressTask(const char *task,int nsteps);
Packit df99a1
  void run(int tostep);
Packit df99a1
  const char *task;
Packit df99a1
  static djvu_progress_callback *set_callback(djvu_progress_callback *ptr=0);
Packit df99a1
private:
Packit df99a1
  DjVuProgressTask *parent;
Packit df99a1
  int nsteps;
Packit df99a1
  int runtostep;
Packit df99a1
  unsigned long startdate;
Packit df99a1
  // Statics
Packit df99a1
  void *gdata;
Packit df99a1
  Data *data;
Packit df99a1
  // Helpers
Packit df99a1
  void signal(unsigned long curdate, unsigned long estdate);
Packit df99a1
};
Packit df99a1
Packit df99a1
# else  // ! NEED_DJVU_PROGRESS
Packit df99a1
Packit df99a1
#  define DJVU_PROGRESS_TASK(name,task,nsteps)
Packit df99a1
#  define DJVU_PROGRESS_RUN(name,step)
Packit df99a1
Packit df99a1
# endif // ! NEED_DJVU_PROGRESS
Packit df99a1
#endif // HAS_DJVU_PROGRESS_CALLBACKS
Packit df99a1
//@}
Packit df99a1
Packit df99a1
Packit df99a1
/** @name General functions.
Packit df99a1
Packit df99a1
    This section contains functions that replace some of the standard
Packit df99a1
    system calls without any other header file dependancies.
Packit df99a1
 */
Packit df99a1
Packit df99a1
#ifdef __cplusplus
Packit df99a1
# define DJVUEXTERNCAPI(x) extern "C" DJVUAPI x;
Packit df99a1
#else
Packit df99a1
# define DJVUEXTERNCAPI(x) extern DJVUAPI x
Packit df99a1
#endif
Packit df99a1
Packit df99a1
/** This replaces fprintf(stderr,...), but with UTF8 encoded strings. */
Packit df99a1
DJVUEXTERNCAPI(void DjVuPrintErrorUTF8(const char *fmt, ...));
Packit df99a1
Packit df99a1
/** This replaces fprintf(stderr,...), but with UTF8 encoded strings. */
Packit df99a1
DJVUEXTERNCAPI(void DjVuPrintErrorNative(const char *fmt, ...));
Packit df99a1
Packit df99a1
/** This replaces printf(...), but requires UTF8 encoded strings. */
Packit df99a1
DJVUEXTERNCAPI(void DjVuPrintMessageUTF8(const char *fmt, ...));
Packit df99a1
Packit df99a1
/** This replaces printf(...), but requires UTF8 encoded strings. */
Packit df99a1
DJVUEXTERNCAPI(void DjVuPrintMessageNative(const char *fmt, ...));
Packit df99a1
Packit df99a1
/** The format (fmt) and arguments define a MessageList to be looked
Packit df99a1
    up in the external messages and printed to stderr. */
Packit df99a1
DJVUEXTERNCAPI(void DjVuFormatErrorUTF8(const char *fmt, ...));
Packit df99a1
Packit df99a1
/** The format (fmt) and arguments define a MessageList to be looked
Packit df99a1
    up in the external messages and printed to stderr. */
Packit df99a1
DJVUEXTERNCAPI(void DjVuFormatErrorNative(const char *fmt, ...));
Packit df99a1
Packit df99a1
/** Prints the translation of message to stderr. */
Packit df99a1
DJVUEXTERNCAPI(void DjVuWriteError( const char *message ));
Packit df99a1
Packit df99a1
/** Prints the translation of message to stdout. */
Packit df99a1
DJVUEXTERNCAPI(void DjVuWriteMessage( const char *message ));
Packit df99a1
Packit df99a1
/** A C function to perform a message lookup. Arguments are a buffer to
Packit df99a1
  received the translated message, a buffer size (bytes), and a
Packit df99a1
  message_list. The translated result is returned in msg_buffer encoded
Packit df99a1
  in UTF-8. In case of error, msg_buffer is empty
Packit df99a1
  (i.e., msg_buffer[0] == '\0').
Packit df99a1
*/
Packit df99a1
DJVUEXTERNCAPI(void DjVuMessageLookUpUTF8(
Packit df99a1
  char *msg_buffer, const unsigned int buffer_size, 
Packit df99a1
  const char *message ));
Packit df99a1
DJVUEXTERNCAPI(void DjVuMessageLookUpNative(
Packit df99a1
  char *msg_buffer, const unsigned int buffer_size, 
Packit df99a1
  const char *message ));
Packit df99a1
Packit df99a1
/** This function sets the program name used when 
Packit df99a1
    searching for language files.
Packit df99a1
*/
Packit df99a1
DJVUEXTERNCAPI(const char *djvu_programname(const char *programname));
Packit df99a1
Packit df99a1
Packit df99a1
/** @name DjVu Names  
Packit df99a1
Packit df99a1
    This section is enabled when compilation symbol #NEED_DJVU_NAMES# is
Packit df99a1
    defined.  This section redefines class names in order to unclutter the
Packit df99a1
    name space of shared objects.  This is useful on systems which
Packit df99a1
    automatically export all global symbols when building a shared object.
Packit df99a1
    @args */
Packit df99a1
//@{
Packit df99a1
//@}
Packit df99a1
Packit df99a1
#ifdef NEED_DJVU_NAMES
Packit df99a1
/* The contents of this section may be generated by this shell command :
Packit df99a1
 * % egrep -h '^(class|struct) +[A-Z_][A-Za-z0-9_]*' *.h *.cpp |\
Packit df99a1
 *   sed -e 's:[a-z]*  *\([A-Za-z_][A-Za-z0-9_]*\).*:#define \1 DJVU_\1:g' |\
Packit df99a1
 *   sort
Packit df99a1
 */
Packit df99a1
#endif // NEED_DJVU_NAMES
Packit df99a1
Packit df99a1
//@}
Packit df99a1
Packit df99a1
#if defined(macintosh)
Packit df99a1
# define EMPTY_LOOP continue
Packit df99a1
#else
Packit df99a1
# define EMPTY_LOOP /* nop */
Packit df99a1
#endif
Packit df99a1
Packit df99a1
//  The ERR_MSG(x) macro is intended to permit automated checking of the
Packit df99a1
//  externalized error message names against the source code. It has no
Packit df99a1
//  effect on the executed program. It should be used to surround each
Packit df99a1
//  message name that will need to be looked up in the external message
Packit df99a1
//  files. In particular, it should use on all strings passed to G_THROW.
Packit df99a1
#ifndef HAS_CTRL_C_IN_ERR_MSG
Packit df99a1
# define HAS_CTRL_C_IN_ERR_MSG 1
Packit df99a1
#endif
Packit df99a1
#ifndef ERR_MSG
Packit df99a1
# if HAS_CTRL_C_IN_ERR_MSG
Packit df99a1
// This hack allows for the coexistence of internationalized
Packit df99a1
// and non-internationalized code.  All internationalized error
Packit df99a1
// message names are prefixed with a ctrl-c.  Only these will
Packit df99a1
// be looked for in the message files.  Messages that do no 
Packit df99a1
// start with a ctrl-c will remain untranslated.
Packit df99a1
#  define ERR_MSG(x) "\003" x
Packit df99a1
# else
Packit df99a1
#  define ERR_MSG(x) x
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#endif /* _DJVUGLOBAL_H_ */
Packit df99a1
Packit df99a1