
/**	@file	dk3types.h	Type definitions for the dk3 library.
 */

#ifndef DK3_TYPES_H_INCLUDED
/** Avoid multiple file inclusions. */
#define DK3_TYPES_H_INCLUDED 1

#include <dk3conf.h>

#if DK3_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if DK3_HAVE_SIGNAL_H
#include <signal.h>
#endif
#if DK3_HAVE_WCHAR_H
#include <wchar.h>
#endif
#if DK3_HAVE_TERMIOS_H
#include <termios.h>
#endif
#if DK3_HAVE_SYS_TERMIOS_H
#include <sys/termios.h>
#endif
#if DK3_HAVE_SYS_TTOLD_H
#include <sys/ttold.h>
#endif
#if DK3_HAVE_ZLIB_H
#include <zlib.h>
#endif

#ifndef DK3_CHAR_SIZE
#if DK3_ON_WINDOWS && (defined(_UNICODE))
/**	Specify default character size 16 bit.
*/
#define	DK3_CHAR_SIZE	2
#else
/**	Specify default character size 8 bit.
*/
#define	DK3_CHAR_SIZE	1
#endif
#endif


/*
	Simple data types.
*/

/**	A 8-bit unsigned int  type.
*/
typedef unsigned char	dk3_u8_t;

/**	An 8-bit signed int type.
*/
typedef char dk3_i8_t;

/**	A 8-bit character type.
*/
typedef char dk3_c8_t;

/**	A 16-bit unsigned int type.
*/
typedef	unsigned short	dk3_u16_t;

/**	A 16-bit signed int type.
*/
typedef short dk3_i16_t;

/**	A 16 bit character type (used on Windows).
*/
#if DK3_SIZEOF_WCHAR_T == 2
typedef wchar_t		dk3_c16_t;
#else
typedef unsigned short	dk3_c16_t;
#endif

/**	A 32-bit character/unsigned int type (wchar_t on UNIX, not used).
*/
typedef unsigned long	dk3_u32_t;

/**	A 32-bit signed int type.
*/
typedef long dk3_i32_t;

/**	A 32-bit character type (used i.e. by X11).
*/
#if DK3_SIZEOF_WCHAR_T == 4
typedef	wchar_t	      dk3_c32_t;
#else
typedef unsigned long dk3_c32_t;
#endif



#if DK3_CHAR_SIZE > 1
#if defined(DK3_SIZEOF_WCHAR_T)
#if DK3_CHAR_SIZE == DK3_SIZEOF_WCHAR_T
/**	Text character type.
*/
typedef	wchar_t		dkChar;
/**	Macro to switch text literals between 8-bit and 16-bit text.
*/
#define	dkT(s)		L ## s
/**	Macro to switch the main function between 8 and 16 bit.
*/
#define	DK3_MAIN	int wmain(int argc, wchar_t *argv[])
#else
/* if DK3_CHAR_SIZE == DK3_SIZEOF_WCHAR_T */
#if DK3_CHAR_SIZE > 2
/**	Text character type.
*/
typedef	dk3_c32_t	dkChar;
#error	"Can not use prefix L to specify dkChar string literals!"
#error	"Can not define DK3_MAIN to dkChar!"
#else
/* if DK3_CHAR_SIZE > 2 */
/**	Text character type.
*/
typedef	dk3_c16_t	dkChar;
#error	"Can not use prefix L to specify dkChar string literals!"
#error	"Can not define DK3_MAIN to dkChar!"
#endif
/* if DK3_CHAR_SIZE > 2 */
#endif
/* if DK3_CHAR_SIZE == DK3_SIZEOF_WCHAR_T */
#else
/* if defined(DK3_SIZEOF_WCHAR_T) */
#error	"Missing definition for DK3_SIZEOF_WCHAR_T!"
#endif
/* if defined(DK3_SIZEOF_WCHAR_T) */
#if !DK3_ON_WINDOWS
#error "Usable support for UNICODE/MBCS is available on Windows platforms only!"
#endif
/* if !DK3_ON_WINDOWS */
#else
/* if DK3_CHAR_SIZE > 1 */
/* +++ DK3_CHAR_SIZE == 1 */
/**	Text character type.
*/
typedef	char		dkChar;
/**	Macro to switch the main function between 8 and 16 bit.
*/
#define	dkT(s)		s
/**	Macro to switch the main function between 8 and 16 bit.
*/
#define	DK3_MAIN	int main(int argc, char *argv[])
/* --- DK3_CHAR_SIZE == 1 */
#endif
/* if DK3_CHAR_SIZE > 1 */



/*
	In the following section we define the longest possible integer
	type (64-bit if possible, 32-bit otherwise).
*/

#if DK3_HAVE_LONG_LONG
/**	Longest available integer type.
*/
typedef long long		dk3_im_t;
/**	Longest unsigned available integer type.
*/
typedef unsigned long long	dk3_um_t;
/**	Maximum unsigned long value on this system.
*/
#define	DK3_MAX_UINTMAX	0xFFFFFFFFFFFFFFFFULL
/**	Maximum long value on this system.
*/
#define DK3_MAX_INTMAX	0x7FFFFFFFFFFFFFFFLL
/**	0 value for longest unsigned integer.
*/
#define	DK3_0_UINTMAX	0ULL
/**	0 value for longest integer.
*/
#define	DK3_0_INTMAX	0LL
/**	Maximum value for longest integer expressed as double.
*/
#define DK3_MAX_INT_AS_DOUBLE	9223372036854775807.0
/**	Maximum value for longest unsigned expressed as double.
*/
#define DK3_MAX_UINT_AS_DOUBLE	18446744073709551615.0
/**	First bit (MSB) of largest unsigned int as double.
*/
#define DK3_MAX_UINT_FIRST_BIT_AS_DOUBLE	9223372036854775808.0
/**	First bit (MSB) of largest signed int as double.
*/
#define DK3_MAX_INT_FIRST_BIT_AS_DOUBLE	4611686018427387904.0
/**	The most significant bit of the largest unsigned int.
*/
#define DK3_MAX_UINT_FIRST_BIT	0x8000000000000000ULL
/**	The most significant bit of the largest signed int.
*/
#define DK3_MAX_INT_FIRST_BIT	0x4000000000000000LL
/**	Most significant bit of the largest int.
*/
#define DK3_MAX_INT_MSB		((long long)(0x8000000000000000ULL))
/**	0.
*/
#define DK3_MAX_UINT_0	0ULL
/**	1.
*/
#define DK3_MAX_UINT_1	1ULL
/**	2.
*/
#define DK3_MAX_UINT_2	2ULL
/**	0.
*/
#define DK3_MAX_INT_0	0LL
/**	1 as max int.
*/
#define DK3_MAX_INT_1	1LL
/**	2.
*/
#define	DK3_MAX_INT_2	2LL
/**	-1 as max int.
*/
#define DK3_MAX_INT_MINUS_1	-1LL
#else

#if DK3_ON_WINDOWS
/**	Longest available integer type.
*/
typedef	__in64			dk3_im_t;
/**	Longest unsigned available integer type.
*/
typedef unsigned __int64	dk3_um_t;
/**	Maximum unsigned long value on this system.
*/
#define	DK3_MAX_UINTMAX	0xFFFFFFFFFFFFFFFFULL
/**	Maximum long value on this system.
*/
#define DK3_MAX_INTMAX	0x7FFFFFFFFFFFFFFFLL
/**	0 value for longest unsigned integer.
*/
#define	DK3_0_UINTMAX	0ULL
/**	0 value for longest integer.
*/
#define	DK3_0_INTMAX	0LL
/**	Maximum value for longest integer expressed as double.
*/
#define DK3_MAX_INT_AS_DOUBLE	9223372036854775807.0
/**	Maximum value for longest unsigned expressed as double.
*/
#define DK3_MAX_UINT_AS_DOUBLE	18446744073709551615.0
/**	First bit (MSB) of largest unsigned int as double.
*/
#define DK3_MAX_UINT_FIRST_BIT_AS_DOUBLE	9223372036854775808.0
/**	First bit (MSB) of largest signed int as double.
*/
#define DK3_MAX_INT_MSB		((long long)(0x8000000000000000ULL))
#define DK3_MAX_INT_FIRST_BIT_AS_DOUBLE	4611686018427387904.0
/**	The most significant bit of the largest unsigned int.
*/
#define DK3_MAX_UINT_FIRST_BIT	0x8000000000000000ULL
/**	The most significant bit of the largest signed int.
*/
/**	Most significant bit of the largest int.
*/
#define DK3_MAX_INT_MSB		((long long)(0x8000000000000000ULL))
#define DK3_MAX_INT_FIRST_BIT	0x4000000000000000LL
/**	0.
*/
#define DK3_MAX_UINT_0	0ULL
/**	1.
*/
#define DK3_MAX_UINT_1	1ULL
/**	2.
*/
#define	DK3_MAX_UINT_2	2ULL
/**	0.
*/
#define DK3_MAX_INT_0	0LL
/**	1 as max int.
*/
#define DK3_MAX_INT_1	1LL
/**	2.
*/
#define	DK3_MAX_INT_2	2LL
/**	-1 as max int.
*/
#define DK3_MAX_INT_MINUS_1	-1LL
#else
/**	Longest available integer type.
*/
typedef long			dk3_im_t;
/**	Longest unsigned available integer type.
*/
typedef unsigned long		dk3_um_t;
/**	Maximum unsigned long value on this system.
*/
#define	DK3_MAX_UINTMAX	0xFFFFFFFFUL
/**	Maximum long value on this system.
*/
#define DK3_MAX_INTMAX	0x7FFFFFFFL
/**	0 value for longest unsigned integer.
*/
#define	DK3_0_UINTMAX	0UL
/**	0 value for longest integer.
*/
#define	DK3_0_INTMAX	0L
/**	Maximum value for longest integer expressed as double.
*/
#define DK3_MAX_INT_AS_DOUBLE	2147483647.0
/**	Maximum value for longest unsigned expressed as double.
*/
#define DK3_MAX_UINT_AS_DOUBLE	4294967295.0
/**	First bit (MSB) of largest unsigned int as double.
*/
#define DK3_MAX_UINT_FIRST_BIT_AS_DOUBLE	2147483648.0
/**	First bit (MSB) of largest signed int as double.
*/
#define DK3_MAX_INT_FIRST_BIT_AS_DOUBLE	1073741824.0
/**	The most significant bit of the largest unsigned int.
*/
#define DK3_MAX_UINT_FIRST_BIT	0x80000000UL
/**	The most significant bit of the largest signed int.
*/
#define DK3_MAX_INT_FIRST_BIT	0x40000000L
/**	Most significant bit of the largest int.
*/
#define DK3_MAX_INT_MSB		((long)(0x80000000UL))
/**	0.
*/
#define DK3_MAX_UINT_0	0UL
/**	1.
*/
#define DK3_MAX_UINT_1	1UL
/**	2.
*/
#define	DK3_MAX_UINT_2	2UL
/**	0.
*/
#define DK3_MAX_INT_0	0L
/**	1 as max int.
*/
#define DK3_MAX_INT_1	1L
/**	2.
*/
#define	DK3_MAX_INT_2	2L
/**	-1 as max int.
*/
#define DK3_MAX_INT_MINUS_1	-1L
#endif
#endif



/* Values for long and unsigned long */
#if DK3_SIZEOF_LONG > 2
#if DK3_SIZEOF_LONG > 4
/**	Maximum unsigned long value expressed as double.
*/
#define	DK3_UL_MAX_AS_DOUBLE		18446744073709551615.0
/**	First bit of unsigned long expressed as double.
*/
#define	DK3_UL_FIRST_BIT_AS_DOUBLE	9223372036854775808.0
/**	Maximum long value expressed as double.
*/
#define	DK3_L_MAX_AS_DOUBLE		9223372036854775807.0
/**	First bit of long variables expressed as double.
*/
#define	DK3_L_FIRST_BIT_AS_DOUBLE	4611686018427387904.0
/**	Maximum unsigned long value.
*/
#define DK3_UL_MAX			0xFFFFFFFFFFFFFFFFUL
/**	First bit of unsigned long.
*/
#define	DK3_UL_FIRST_BIT		0x8000000000000000UL
/**	Maximum long value.
*/
#define	DK3_L_MAX			0x7FFFFFFFFFFFFFFFL
/**	First bit of long value.
*/
#define	DK3_L_FIRST_BIT			0x4000000000000000L
#else
/**	Maximum unsigned long value expressed as double.
*/
#define	DK3_UL_MAX_AS_DOUBLE		4294967295.0
/**	First bit of unsigned long expressed as double.
*/
#define	DK3_UL_FIRST_BIT_AS_DOUBLE	2147483647.0
/**	Maximum long value expressed as double.
*/
#define	DK3_L_MAX_AS_DOUBLE		2147483647.0
/**	First bit of long variables expressed as double.
*/
#define	DK3_L_FIRST_BIT_AS_DOUBLE	1073741824.0
/**	Maximum unsigned long value.
*/
#define DK3_UL_MAX			0xFFFFFFFFUL
/**	First bit of unsigned long.
*/
#define	DK3_UL_FIRST_BIT		0x80000000UL
/**	Maximum long value.
*/
#define	DK3_L_MAX			0x7FFFFFFFL
/**	First bit of long value.
*/
#define	DK3_L_FIRST_BIT			0x40000000L
#endif
#else
/**	Maximum unsigned long value expressed as double.
*/
#define	DK3_UL_MAX_AS_DOUBLE		65535.0
/**	First bit of unsigned long expressed as double.
*/
#define	DK3_UL_FIRST_BIT_AS_DOUBLE	32768.0
/**	Maximum long value expressed as double.
*/
#define	DK3_L_MAX_AS_DOUBLE		32767.0
/**	First bit of long variables expressed as double.
*/
#define	DK3_L_FIRST_BIT_AS_DOUBLE	16384.0
/**	Maximum unsigned long value.
*/
#define DK3_UL_MAX			0xFFFFUL
/**	First bit of unsigned long.
*/
#define	DK3_UL_FIRST_BIT		0x8000UL
/**	Maximum long value.
*/
#define	DK3_L_MAX			0x7FFFL
/**	First bit of long value.
*/
#define	DK3_L_FIRST_BIT			0x4000L
#endif



/* Values for size_t */
#if DK3_SIZEOF_SIZE_T	> 2
#if DK3_SIZEOF_SIZE_T	> 4
/**	Maximum value for size_t data type.
*/
#define	DK3_SIZE_T_MAX	0xFFFFFFFFFFFFFFFFULL
#else
/**	Maximum value for size_t data type.
*/
#define	DK3_SIZE_T_MAX	0xFFFFFFFFUL
#endif
#else
/**	Maximum value for size_t data type.
*/
#define	DK3_SIZE_T_MAX	0xFFFFU
#endif



/** Maximum value for double variables. */
#define DK3_MAX_DOUBLE 1.7e308

/** Maximum value for long double variables. */
#if DK3_HAVE_LONG_DOUBLE && (DK3_SIZEOF_LONG_DOUBLE > DK3_SIZEOF_DOUBLE)
#define	DK3_MAX_LONG_DOUBLE	1.7e308L
#else
#define DK3_MAX_LONG_DOUBLE	1.7e308
#endif

/**	Timer.
*/

#if DK3_ON_WINDOWS
/**	Portable type to save a timestamp.
*/
typedef	__time64_t	dk3_time_t;
#else
#if (defined(DK3_HAVE_SYS_TIME_H) || defined(DK3_HAVE_TIME_H)) && defined(DK3_HAVE_TIME)
/**	Portable type to save a timestamp.
*/
typedef	time_t		dk3_time_t;
#else
#error "No mechanism known to obtain current time!"
#endif
#endif



#if DK3_HAVE_LONG_DOUBLE && (DK3_SIZEOF_LONG_DOUBLE > DK3_SIZEOF_DOUBLE)
/**	Most precise double type.
*/
typedef long double	dk3_double_t;
#else
/**	Most precise double type.
*/
typedef double		dk3_double_t;
#endif



/*
	Structured data types.
*/



/**	Timestamp.
*/
typedef struct _dk3_time_t_ {
  /**	Year.
  */
  int	Y;
  /**	Month.
  */
  int	M;
  /**	Day in month.
  */
  int	D;
  /**	Hours.
  */
  int	h;
  /**	Minutes.
  */
  int	m;
  /**	Seconds.
  */
  int	s;
} dk3_tm_t;



/**	File information.
	When retrieving information about a symbolic link the dk3sf_stat()
	function retrieves information about both the link destination and
	the link itself.
*/
typedef struct {
  /**	Creation timestamp.
  */
  dk3_time_t	cre;
  /**	Modification timestamp.
  */
  dk3_time_t	mod;
  /**	Last access timestamp.
  */
  dk3_time_t	acc;
  /**	Link creation timestamp.
  */
  dk3_time_t	lcre;
  /**	Link modification timestamp.
  */
  dk3_time_t	lmod;
  /**	Link last access timestamp.
  */
  dk3_time_t	lacc;
  /**	User ID.
  */
  dk3_um_t	u;
  /**	Group ID.
  */
  dk3_um_t	g;
  /**	Device number.
  */
  dk3_um_t	device;
  /**	Relative device number.
  */
  dk3_um_t	rdev;
  /**	Inode number.
  */
  dk3_um_t	inode;
  /**	Number of hard links to inode.
  */
  dk3_um_t	nlink;
  /**	File size in bytes.
  */
  dk3_um_t	sz;
  /**	Link user ID.
  */
  dk3_um_t	lu;
  /**	Link group ID.
  */
  dk3_um_t	lg;
  /**	Link device number.
  */
  dk3_um_t	ldevice;
  /**	Link relative device number.
  */
  dk3_um_t	lrdev;
  /**	Link inode number.
  */
  dk3_um_t	linode;
  /**	Number of hard links to the link.
  */
  dk3_um_t	lnlink;
#if DK3_ON_WINDOWS
  long		dwReparse;
#endif
  /**	File type, see @ref filetypes.
  */
  int		ft;
  /**	Permissions, see @ref filepermissions.
  */
  int		perm;
  /**	Link file type.
  */
  int		lft;
  /**	Link permissions.
  */
  int		lperm;
  /**	Link file size.
  */
  dk3_um_t	lsz;
  /**	Additional information about file, see @ref statadditional.
  */
  unsigned char	ai;
#if DK3_ON_WINDOWS
  /**	Reparse point information.
  */
  char		cReparse;
#endif
} dk3_stat_t;




/**	Evaluation result for an object.
*/
typedef union {
    /**	Evaluation to double.
    */
    double d;
    /**	Evaluation to float.
    */
    float f;
    /**	Evaluation to long.
    */
    long l;
    /**	Evaluation to unsigned long.
    */
    unsigned long ul;  
    /**	Evaluation to int.
    */
    int i;
    /**	Evaluation to unsigned int.
    */
    unsigned int ui;  
    /**	Evaluation to short.
    */
    short s;
    /**	Evaluation to unsigned short.
    */
    unsigned short us;
    /**	Evaluation to char.
    */
    char c;
    /**	Evaluation to unsigned char.
    */
    unsigned char uc;
} dk3_object_eval_res_t;


/**	Internal node for dk3_sto_t.
	This structure is used internally by the dksto module.
*/
struct _dk3_sto_node_ {
  /**	Object evaluation of @a o.
  */
  dk3_object_eval_res_t	   v;
  /**	Parent node in tree.
  */
  struct _dk3_sto_node_ *p;
  /**	Left child node (tree) or preceeding node (list).
  */
  struct _dk3_sto_node_ *l;
  /**	Right child node (tree) or following node (list).
  */
  struct _dk3_sto_node_ *r;
  /**	Object to store.
  */
  void                    *o;
  /**	Balance flag (used in trees only).
  */
  short                    b;
  /**	Direction to walk in tree.
  */
  short                    w;	/**< Direction to walk. */
};

/** Internal node for dk3_sto_t.
*/
typedef struct _dk3_sto_node_ dk3_sto_node_t;

/** Pointer to internal node for dk3_sto_t.
*/
typedef dk3_sto_node_t *dk3_sto_node_p;



/**	Evaluation functions.
	These function type are used to evaluate and compare objects
	stored in a sorted dk3_sto_t.
*/

/** Object evaluation function for sorted storage.
*/
typedef char dk3_fct_eval_c_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef unsigned char dk3_fct_eval_uc_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef short dk3_fct_eval_s_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef unsigned short dk3_fct_eval_us_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef int dk3_fct_eval_i_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef unsigned int dk3_fct_eval_ui_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef long dk3_fct_eval_l_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef unsigned long dk3_fct_eval_ul_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef float dk3_fct_eval_f_t(void *obj, int crit);

/** Object evaluation function for sorted storage.
*/
typedef double dk3_fct_eval_d_t(void *obj, int crit);

/** Object comparison function for sorted storage.
    When comparing to objects while inserting a new object into
    a storage both @a o1 and @a o2 are object pointers.
    When this function is called from dk3sto_it_find_like() the
    @a o1 argument is an object pointer from the storage,
    @a o2 is the pointer passed to dk3sto_it_find_like().
*/
typedef int dk3_fct_comp_t(void *o1, void *o2, int crit);



/**	Choice for object evaluation function.
*/
typedef union {
  /** Evaluation function returning a char value.
  */
  dk3_fct_eval_c_t *c;
  /** Evaluation function returning an unsigned char value.
  */
  dk3_fct_eval_uc_t *uc;
  /** Evaluation function returning a short value.
  */
  dk3_fct_eval_s_t *s;
  /** Evaluation function returning an unsigned short value.
  */
  dk3_fct_eval_us_t *us;
  /** Evaluation function returning an int value.
  */
  dk3_fct_eval_i_t *i;
  /** Evaluation function returning an unsigned int value.
  */
  dk3_fct_eval_ui_t *ui;
  /** Evaluation function returning a long value.
  */
  dk3_fct_eval_l_t *l;
  /** Evaluation function returning an unsigned long value.
  */
  dk3_fct_eval_ul_t*ul;
  /** Evaluation function returning a float value.
  */
  dk3_fct_eval_f_t *f;
  /** Evaluation function returning a double value.
  */
  dk3_fct_eval_d_t *d;
  /** Function to compare two objects.
  */
  dk3_fct_comp_t *comp;
} dk3_object_eval_fct_t;



/**	Object storage.
	A storage can be used to store pointers to objects.
*/
typedef struct _dk3_sto_t_ {
  /**	Comparison or evaluation function.
  */
  dk3_object_eval_fct_t	e;
  /**	Application structure for diagnostics.
  */
  void		  	*app;
  /**	Critical path for delete operations.
  */
  dk3_sto_node_p 	*d;
  /**	Root element (tree) or left element (list).
  */
  dk3_sto_node_t 	*r;
  /**	Double-linked list of iterators.
  */
  void              	*i;
  /**	Algorithm selection for comparison or evaluation.
  */
  int 		     	h;
  /**	Criteria for comparison or evaluation.
  */
  int                	c;
  /**	Flag: Use tree, 1=tree, 0=list.
  */
  int		     	t;
  /**	Path length of critical path (number of elements).
  */
  short              	l;
} dk3_sto_t;



/**	Storage iterator.
	This structure can be used to iterate through a dk3_sto_t.
*/
struct _dk3_sto_iterator_ {
  /**	Preceeding iterator in iterator linked list. 
  */
  struct _dk3_sto_iterator_ *l;
  /**	Following iterator in iterator linked list.
  */
  struct _dk3_sto_iterator_ *r;
  /**	Storage owning this iterator.
  */
  dk3_sto_t                *s;
  /**	Current node.
  */
  dk3_sto_node_t           *c;
};
/**	Storage iterator. Can be used to iterate through a dk3_sto_t. */
typedef struct _dk3_sto_iterator_ dk3_sto_it_t;



/**	Directory contents.
*/
typedef struct _dk3_dir_t_ {
  /**	Stat buffer for current entry to process.
  */
  dk3_stat_t		stb;
  /**	Storage containing the directory names.
  */
  dk3_sto_t		*sdi;
  /**	Iterator for directory names storage.
  */
  dk3_sto_it_t		*idi;
  /**	Storage containing file names.
  */
  dk3_sto_t		*sfi;
  /**	Iterator for file names storage.
  */
  dk3_sto_it_t		*ifi;
  /**	Directory name.
  */
  dkChar const		*dirname;
  /**	Full path of current entry to process.
  */
  dkChar		*fullname;
  /**	Short name of current entry to process.
  */
  dkChar		*shortname;
  /**	Application used for diagnostics.
  */
  void			*app;
  /**	Number of directories found.
  */
  dk3_um_t		ndir;
  /**	Number of files found.
  */
  dk3_um_t		nfi;
} dk3_dir_t;



/**	Arguments in a stream command API call.
*/
typedef struct {
  /**	Low-level data.
  */
  void	*d;
  /**	Buffer for bytes to read/write.
  */
  char	*b;
  /**	Buffer size (number of bytes to read/write).
  */
  size_t	sz;
} dk3_stream_api_arg_t;



/**	Results of a stream command API call.
*/
typedef struct {
  /**	Number of bytes read.
  */
  size_t	sz;
  /**	Error code DK3_ERROR_xxx, see @ref errorcodes.
  */
  int		ec;
} dk3_stream_api_result_t;



/**	Stream command API.
*/
typedef struct _dk3_stream_api_t_ {
  /**	API call arguments.
  */
  dk3_stream_api_arg_t	arg;
  /**	API call results.
  */
  dk3_stream_api_result_t	res;
  /**	Command to execute, DK3_STREAM_API_xxx, see @ref streamapicommands.
  */
  int	cmd;
  /**	Success flag, 1=success, 0=error.
  */
  int	suc;
} dk3_stream_api_t;

/**	Stream API function type.
*/
typedef void dk3_stream_api_fct_t(dk3_stream_api_t *);

/**	Generic I/O stream.
*/
typedef struct _dk3_stream_t_ {
  /**	Optional application structure for diagnostics.
  */
  void			*app;
  /**	Low-level data.
  */
  void			*d;
  /**	Stream API function.
  */
  dk3_stream_api_fct_t	*f;
  /**	Input buffer.
  */
  char			*ib;
  /**	Output buffer.
  */
  char			*ob;
  /**	Number of bytes written.
  */
  dk3_um_t		wr;
  /**	Size of input buffer.
  */
  size_t		sz_ib;
  /**	Bytes available in input buffer.
  */
  size_t		av_ib;
  /**	Number of bytes already used from input buffer.
  */
  size_t		us_ib;
  /**	Size of output buffer.
  */
  size_t		sz_ob;
  /**	Number of bytes already placed in output buffer.
  */
  size_t		us_ob;
  /**	Output encoding, see @ref fileenc.
  */
  int			oe;
  /**	Usage flags, see @ref streamuseflags.
  */
  int			fl;
  /**	Flag: End of input found.
  */
  int			fe;
  /**	Flag: Reading 0 bytex indicates end of input.
  */
  int			ze;
  /**	Flag: Flush output buffer at end of line.
  */
  int			lf;
  /**	Flag: Error occured.
  */
  int			er;
  /**	Last error occured, see @ref errorcodes.
  */
  int			ec;
} dk3_stream_t;



/**	Application structure.
*/
typedef struct _dk3_app_t_ {
  /**	Messages array, either localized messages or default texts.
  */
  dkChar const * const	*msg;
  /**	Input file name currently processed (used in error messages).
  */
  dkChar const		*n_sourcefile;
  /**	Command line arguments.
  */
  dkChar const * 	*argv;
  /**	Application name.
  */
  dkChar const		*n_app;
  /**	Name of executable file for process.
  */
  dkChar const		*n_execfile;
  /**	Directory bin (directory for binaries - bindir).
  */
  dkChar const		*n_bindir;
  /**	Directory etc (system configuration directory - sysconfdir).
  */
  dkChar const		*n_etcdir;
  /**	Directory share (data root directory - datarootdir).
  */
  dkChar const		*n_sharedir;
  /**	Directory var (local state directory - vardir).
  */
  dkChar const		*n_vardir;
  /**	Login name of process owner.
  */
  dkChar const		*n_logname;
  /**	Home directory of process owner.
  */
  dkChar const		*n_homedir;
  /**	Aplication group name.
  */
  dkChar const		*n_appgroup;
  /**	File name for log file.
  */
  dkChar const		*n_logfilename;
  /**	Directory for temporary files.
  */
  dkChar const		*n_tmpdir;
  /**	Users language.
  */
  dkChar const		*n_language;
  /**	Users region.
  */
  dkChar const		*n_region;
  /**	Language/region setting string from environment or registry.
  */
  dkChar const		*n_langstr;
  /**	Host name.
  */
  dkChar const		*n_hostname;
  /**	Container for message catalogs.
  */
  dk3_sto_t		*s_mc;
  /**	Iterator through message catalogs container.
  */
  dk3_sto_it_t		*i_mc;
  /**	Container for preferences read from command line.
  */
  dk3_sto_t		*s_cmdprefs;
  /**	Iterator through command line preferences container.
  */
  dk3_sto_it_t		*i_cmdprefs;
  /**	Container for self-set preferences.
  */
  dk3_sto_t		*s_selfprefs;
  /**	Iterator through self-set preferences container.
  */
  dk3_sto_it_t		*i_selfprefs;
  /**	Container for system-wide preferences.
  */
  dk3_sto_t		*s_sysprefs;
  /**	Iterator through system-wide preferences container.
  */
  dk3_sto_it_t		*i_sysprefs;
  /**	Container for users constant preferences.
  */
  dk3_sto_t		*s_constprefs;
  /**	Iterator through users constant preferences container.
  */
  dk3_sto_it_t		*i_constprefs;
  /**	Container for users variable preferences.
  */
  dk3_sto_t		*s_varprefs;
  /**	Iterator through users variable preferences container.
  */
  dk3_sto_it_t		*i_varprefs;
  /**	Line number in current input file (used in error messages).
  */
  unsigned long		sourceline;
  /**	Name part of next temporary file name.
  */
  unsigned long		ul_tmpname;
  /**	Suffix part of next temporary file name.
  */
  unsigned		u_tmpsuffix;
  /**	Application type, see @ref apptypes.
  */
  int	app_type;
  /**	Error coce, report delayed after finishing application initialization.
  */
  int	de;
  /**	Log level required for reporting to stderr, see @ref loglevels.
  */
  int	ll_stderr;
  /**	Log level required for reporting to file, see @ref loglevels.
  */
  int	ll_file;
  /**	Log level required to keep log file, see @ref loglevels.
  */
  int	ll_file_keep;
  /**	Log level required to keep temporary files, see @ref loglevels.
  */
  int	ll_tmp_keep;
  /**	Most relevant log level found, see @ref loglevels.
  */
  int	ll_highest;
  /**	Carry flag, too many temporary file names requested.
  */
  int	i_tmpcarry;
  /**	Number of command line arguments.
  */
  int	argc;
  /**	Internal encoding, used for strings in memory, see @ref stringenc.
  */
  int	i_encoding;
  /**	Encoding for output files, see @ref fileenc.
  */
  int	i_output_encoding;
  /**	Encoding expected on stdin, see @ref fileenc.
  */
  int	i_stdin_input_encoding;
  /**	Encoding expected when reading from files, see @ref fileenc.
  */
  int	i_file_input_encoding;
  /**	Flag: Unconfigure application (delete users variable preferences)
	when finishing the application.
  */
  int	f_unconfigure;
  /**	Flag: Changes applied to preferences, need to save when finishing
	the application.
  */
  int	f_prefschanged;
  /**	Flag: Setup completed, we can write log messages.
  */
  int	f_readytolog;
  /**	Flag: First attempt to log to file.
  */
  int	f_first_file_log;
  /**	Flag: Already attempted to initialize PRNG.
  */
  int	f_rand_initialized;
  /**	Flag: PRNG was initialized successfully.
  */
  int	f_rand_success;
  /**	PRNG type to use, see @ref rndtypes.
  */
  int	rand_type;
} dk3_app_t;



/**	File search result.
*/
typedef struct _dk3_search_t_ {
  /**	Container for all file names found.
  */
  dk3_sto_t	*s_fn;
  /**	Iterator for all file names container.
  */
  dk3_sto_it_t	*i_fn;
  /**	Application structure, used for diagnostics.
  */
  dk3_app_t	*app;
  /**	Number of next file to add.
  */
  unsigned long	nf;
  /**	Flag: Inverted search order (most relevant files first).
  */
  int		inverted;
} dk3_search_t;



/**	Bit field.
*/
typedef struct _dk3_bf_t_ {
  /**	Data bytes to store the bits.
  */
  unsigned char	*data;
  /**	Optional application structure for diagnostics.
  */
  dk3_app_t	*app;
  /**	Number of bits to save.
  */
  size_t	nbits;
} dk3_bf_t;



/**	Bit matrix.
*/
typedef struct _dk3_bm_t_ {
  /**	Data bytes to store the bits.
  */
  unsigned char	**data;
  /**	Optional application structure for diagnostics.
  */
  dk3_app_t	*app;
  /**	Number of bits per row.
  */
  size_t	columns;
  /**	Number of rows.
  */
  size_t	rows;
} dk3_bm_t;



/**	Information about one package needed for an encoding.
*/
typedef struct {
  char const	*name;	/**< Package name. */
  char		 used;	/**< Flag: Package used. */
} dk3_uc2lat_pkg_t;

/**	Pointer to a package information.
*/
typedef dk3_uc2lat_pkg_t *dk3_uc2lat_pkg_ptr;


#if 0
/**	Group of 256 characters for encoding from U32 to LaTeX.
*/
typedef struct {
#if 0
  char const		**a;	/**< In all modes. */
  char const		**t;	/**< In text mode. */
  char const		**m;	/**< In math mode. */
  dk3_uc2lat_pkg_t	**p;	/**< Packages required for characters. */
  dk3_c32_t		g;	/**< Group number. */
#endif
  int dummy;
} dk3_uc2lat_group_t;
#endif


/**	Directory entry for UC to LaTeX.
*/
typedef struct {
  /**	Container to store ranges.
  */
  dk3_sto_t		*s_ran;
  /**	Iterator through ranges container.
  */
  dk3_sto_it_t		*i_ran;
  /**	Short file name containing data for this directory entry.
  */
  dkChar const		*sn;
  /**	Flag: Data was loaded.
  */
  unsigned char		loaded;
} dk3_uc2lat_dir_t;



/**	Range of UC characters.
*/
typedef struct {
  /**	LaTeX encodings for both text and math mode.
  */
  char const		**a;
  /**	LaTeX encodings for text mode only.
  */
  char const		**t;
  /**	LaTeX encodings for math mode only.
  */
  char const		**m;
  /**	Descriptions texts for character.
  */
  dkChar const		**dsc;
  /**	Package collections required for character.
  */
  dk3_uc2lat_pkg_t	**p;
  /**	Directory entry responsible for this range.
  */
  dk3_uc2lat_dir_t	*dir;
  /**	First character in range.
  */
  dk3_c32_t		start;
  /**	Last character in range.
  */
  dk3_c32_t		end;
  /**	Flag: Initialization of range attempted, no second attempt.
  */
  unsigned char		ia;
} dk3_uc2lat_range_t;



/**	Data for U32 to LaTeX conversion.
*/
typedef struct {
#if 0
  dk3_app_t	*app;	/**< Application structure. */
  dk3_sto_t	*s;	/**< Storage of dk3_uc2lat_group_t. */
  dk3_sto_it_t	*i;	/**< Iterator through storage. */
  dkChar const	*d;	/**< Directory. */
  dk3_sto_t	*s_p;	/**< Storage of packages. */
  dk3_sto_it_t	*i_p;	/**< Iterator through packages storage. */
#endif
  /**	Application structure for diagnostics and file search.
  */
  dk3_app_t	*app;
  /**	Name of data directory containing the LaTeX encodings.
  */
  dkChar const	*dir;
  /**	Allocated buffer for characters directly passed through.
  */
  char		*buf;
  /**	Container for ranges.
  */
  dk3_sto_t	*s_ran;
  /**	Iterator through ranges container.
  */
  dk3_sto_it_t	*i_ran;
  /**	Container for directories.
  */
  dk3_sto_t	*s_dir;
  /**	Iterator for directories container.
  */
  dk3_sto_it_t	*i_dir;
  /**	Containter for packages.
  */
  dk3_sto_t	*s_pkg;
  /**	Iterator through packages container.
  */
  dk3_sto_it_t	*i_pkg;
  /**	Cache: Last range used.
  */
  dk3_uc2lat_range_t	*rca;
  /**	Size of buffer @a buf.
  */
  size_t	 szbuf;
  /**	Flag: Load descriptions for characters too (normally the
	descriptions are ignored).
  */
  int		 f_dsc;
  /**	Flag: Generate UTF-8 output.
  */
  int		 f_utf8;
} dk3_uc2lat_t;



/**	Handler function for one line.
	@param	obj	Object to modify using the line.
	@param	il	Input line to process.
	@return	1=OK, 0=error, can continue, -1=error, abort processing.
*/
typedef int  dk3_line_handler_t(void *obj, dkChar *il);



/**	Struct for processing a file line by line.
*/
typedef struct {
  /**	Application structure for diagnostics.
  */
  dk3_app_t		*app;
  /**	Data object to modify while processing input lines.
  */
  void			*obj;
  /**	Handler function.
  */
  dk3_line_handler_t	*hf;
  /**	Buffer for input lines.
  */
  dkChar		*buf;
  /**	Current insertion position for next character to add.
  */
  dkChar		*cp;
  /**	Size of input buffer @a buf.
  */
  size_t		 szbuf;
  /**	Number of characters used in @a buf.
  */
  size_t		 bufus;
  /**	System encoding, see @ref stringenc.
  */
  int			 se;
} dk3_line_handler_data_t;



/**	One option.
*/
typedef struct {
  /**	Short option character.
  */
  dkChar 		 so;
  /**	Long option text.
  */
  dkChar const		*lo;
  /**	Flag: Option needs an argument.
  */
  int			 na;
} dk3_option_t;



/**	Options set.
*/
typedef struct {
  /**	Application structure for diagnostics.
  */
  dk3_app_t		 *app;
  /**	Options to find in command line arguments.
  */
  dk3_option_t const	 *options;
  /**	Array of flags, one for each options, whether or not the option was
	found.
  */
  int		 	 *found;
  /**	Array for option arguments.
  */
  dkChar const		**optargs;
  /**	Array of normal arguments (no options and no option arguments).
  */
  dkChar const		**args;
  /**	Further options.
  */
  dkChar const		**fo;
  /**	Short option to introduce further options.
  */
  dkChar		  foc;
  /**	Long option to introduce further options.
  */
  dkChar const		 *focl;
  /**	Number of elements in @a options.
  */
  size_t		  szoptions;
  /**	Number of normal arguments available.
  */
  int			  argsav;
  /**	Number of normal arguments used (found).
  */
  int			  argsused;
  /**	Number of further options available.
  */
  int			  foav;
  /**	Number of further options found.
  */
  int			  fou;
  /**	Error code of last error occured, see @ref errorcodes.
  */
  int			  ec;
} dk3_option_set_t;



/**	Output filter cell details for ASCII/Hex encoding.
*/
typedef struct {
  /**	Number of characters already printed in current line.
  */
  size_t			nl;
} dk3_of_cell_ah_details_t;

/**	Output filter cell details for ASCII85 encoding.
*/
typedef struct {
  /**	32-bit value built up by for bytes.
  */
  unsigned long			val;
  /**	Number of characters merged into @a val.
  */
  size_t			nc;
  /**	Number of characters already printed in current line.
  */
  size_t			nl;
} dk3_of_cell_a85_details_t;



/**	Output filter cell details for run-length compression.
*/
typedef struct {
  /**	Data buffer.
  */
  unsigned char		*bu;
  /**	Number of bytes used in buffer @a bu.
  */
  size_t		 used;
  /**	Number of same bytes repeated.
  */
  size_t		 same;
  /**	Previous character processed.
  */
  unsigned char		 pc;
  /**	Flag: Have a previous character.
  */
  unsigned char		 hc;
  /**	Flag: In a run.
  */
  unsigned char		 run;
} dk3_of_cell_rl_details_t;



#if DK3_HAVE_ZLIB_H
/**	Output filter cell details for flate compression.
*/
typedef struct {
  /**	zlib stream.
  */
  z_stream		*zs;
  /**	Input buffer.
  */
  Bytef			*ibu;
  /**	Output buffer.
  */
  Bytef			*obu;
  /**	Number of input bytes used.
  */
  uLong			 iused;
  /**	Flag: Everything ok, no errors yet.
  */
  int			 ok;
} dk3_of_cell_flate_details_t;
#endif


/**	Output filter cell details.
*/
typedef union {
#if DK3_HAVE_ZLIB_H
    /**	Flate compression data.
    */
    dk3_of_cell_flate_details_t	flate;
#endif
    /**	Run-length compression data.
    */
    dk3_of_cell_rl_details_t	psrl;
    /**	ASCII85 encoding data.
    */
    dk3_of_cell_a85_details_t	a85;
    /**	ASCII-Hex encoding data.
    */
    dk3_of_cell_ah_details_t	ah;
} dk3_of_cell_details_t;



/**	Output filter cell.
*/
typedef struct _dk3_of_cell_t_ {
  /**	Compression/encoding algorithm-specific data.
  */
  dk3_of_cell_details_t		data;
  /**	Next cell downwards.
  */
  struct _dk3_of_cell_t_	*down;
  /**	Next cell upwards.
  */
  struct _dk3_of_cell_t_	*up;
  /**	Output filter owning this cell.
  */
  void				*of;	/**< Output filter. */
  /**	Output filter cell type DK3_OF_CELL_TYPE_xxx,
	see @ref outputfiltercelltypes.
  */
  int				 type;
} dk3_of_cell_t;



/**	Output filter on top of a stream.
*/
typedef struct {
  /**	Application structure for diagnostics.
  */
  dk3_app_t		*app;
  /**	Output stream to receive the filtered data.
  */
  dk3_stream_t		*os;
  /**	Bottom filter cell (next to the stream).
  */
  dk3_of_cell_t		*bottom;
  /**	Top filter cell (input goes to here).
  */
  dk3_of_cell_t		*top;
  /**	Number of cells.
  */
  size_t		 nCells;
  /**	Number of bits used in the @a bits variable.
  */
  size_t		 nBits;
  /**	The bits stored so far.
  */
  unsigned char		 bits;
  /**	Flag set for output filter.
  */
  unsigned char		 flags;
} dk3_of_t;



#if 0

/**	One datum stored in the in-memory database.
*/
typedef struct {
  /**	Start address of data.
  */
  void		*data;
  /**	Data size (number of bytes).
  */
  size_t	 size;
} dk3_memdb_datum_t;



/**	In-memory database.
*/
typedef struct {
  /**	Container for the key/value pairs.
  */
  dk3_sto_t		*st;
  /**	Iterator through key/value pair container.
  */
  dk3_sto_it_t		*it;
  /**	Application structure for diagnostics.
  */
  dk3_app_t		*app;
  /**	File name of database on disk.
  */
  dkChar const		*fn;
  /**	Start of key.
  */
  char			*kb;
  /**	Start of value.
  */
  char			*vb;
  /**	Input line buffer.
  */
  char			*il;
  /**	Binary key size.
  */
  size_t		 szkb;
  /**	Text key size.
  */
  size_t		 szkt;
  /**	Binary value size.
  */
  size_t		 szvb;
  /**	Text value size.
  */
  size_t		 szvt;
  /**	Text line size.
  */
  size_t		 szl;
  /**	Error code of last error occured, see @ref errorcodes.
  */
  int			 ec;
  /**	Flag: Dirty (synchronization needed).
  */
  int			 f_dt;
} dk3_memdb_t;

#endif



/**	Data to store in a database as key or value.
*/
typedef struct {
  /**	Data bytes.
  */
  void		*dt;
  /**	Number of bytes.
  */
  size_t	 sz;
} dk3_datum_t;



/**	Details for a memory database.
*/
typedef struct {
  /**	Container for key/value pairs.
  */
  dk3_sto_t		*s_mem;
  /**	Container iterator.
  */
  dk3_sto_it_t		*i_mem;
} dk3_dbi_mem_details_t;



/**	Details for Berkeley DB database.
*/
typedef struct {
  /**	The BDB database.
  */
  void			*dbptr;
} dk3_dbi_bdb_details_t;



/**	Details for NDBM database.
*/
typedef struct {
  /**	8-bit character file name, only used if dkChar is 16 bit or above.
  */
  void			*dbptr;
} dk3_dbi_ndbm_details_t;



/**	Database type specific details.
*/
typedef union {
  /**	Memory database.
  */
  dk3_dbi_mem_details_t		mem;
  /**	Berkeley database details.
  */
  dk3_dbi_bdb_details_t		bdb;
  /**	NDBM database details.
  */
  dk3_dbi_ndbm_details_t	ndbm;
} dk3_dbi_details_t;



/**	Database interface.
*/
typedef struct {
  /**	Database type specific details.
   */
  dk3_dbi_details_t	details;
  /**	Application structure for diagnostics.
  */
  dk3_app_t		*app;
  /**	File name for database on disk.
  */
  dkChar const		*fn;
  /**	Database type, DK3_DB_TYPE_xxx, see @ref databasetypes.
  */
  int			tp;
  /**	Flag: Database opened for writing.
  */
  int			wr;
  /**	Flag: Database modified, sync to file necessary.
  */
  int			mod;
  /**	Flag: Write error messages for keys not found.
  */
  int			rfk;
  /**	Error code for last error occured.
  */
  int			ec;
} dk3_dbi_t;



/**	BIF coordinates type.
	Must include negative values as predictor operators may
	try to obtain values in row and column -1.
*/
typedef long	dk3_bif_coord_t;



/**	BIF pixel value type.
*/
typedef unsigned short dk3_bif_pixel_t;



/**	Pixel resample information.
*/
typedef struct {
  /**	Shift width (number of bits).
  */
  size_t		shiftw;
  /**	Bits per component in input values.
  */
  size_t		insize;
  /**	Bits per component in output values.
  */
  size_t		outsize;
  /**	Bitmask for and operation.
  */
  dk3_bif_pixel_t	andmask;
  /**	Bitmask for or operation.
  */
  dk3_bif_pixel_t	ormask;
  /**	Maximum output value.
  */
  dk3_bif_pixel_t	maxval;
  /**	Bit-shift direction, 1=increase, 0=unchanged, -1=decrease.
  */
  int			direction;
} dk3_pixel_resample_t;



/**	One frame in bitmap image file.
*/
typedef struct {
  /**	X resolution.
  */
  double		 xres;
  /**	Y resolution.
  */
  double		 yres;
  /**	Bitmap type specific data per frame.
  */
  void			*implspec;
  /**	Frame width.
  */
  dk3_bif_coord_t	 w;
  /**	Frame height.
  */
  dk3_bif_coord_t	 h;
  /**	Bits per component for frame found in file.
  */
  size_t		 bits;
  /**	Number of non-redundant bits in @ bits.
  */
  size_t		 realbits;
  /**	Frame number.
  */
  size_t		 n;		/**< Frame number. */
  /**	Color space, DK3_COLOR_SPACE_xxx, see @ref colorspaces.
  */
  int			 cs;
  /**	Flag: Really colored, 1=colored, 0=gray.
  */
  int			 realcolor;
  /**	Flag: Really found alpha data.
  */
  int			 realalpha;
  /**	Flag: Background color defined for frame.
  */
  int			 bg;
  /**	Background red found in file.
  */
  dk3_bif_pixel_t	 bgr;
  /**	Background green found in file.
  */
  dk3_bif_pixel_t	 bgg;
  /**	Background blue found in file.
  */
  dk3_bif_pixel_t	 bgb;
  /**	Background gray found in file.
  */
  dk3_bif_pixel_t	 bggr;
} dk3_bif_frame_t;



/**	Bitmap image file.
*/
typedef struct {
  /**	Structure to upsample or downsample pixel values.
  */
  dk3_pixel_resample_t	 pr;
  /**	Application structure for diagnostics.
  */
  dk3_app_t		*app;
  /**	Container for frames.
  */
  dk3_sto_t		*s_frames;
  /**	Iterator through frames container.
  */
  dk3_sto_it_t		*i_frames;
  /**	Current frame to process.
  */
  dk3_bif_frame_t	*cf;
  /**	Bitmap image type specific data per file.
  */
  void			*implspec;
  /**	Name of temporary file (if any), may be NULL.
  */
  char const		*tmpfn;
  /**	Temporary file (if any), may be NULL.
  */
  FILE			*tmpfipo;
  /**	Number of bitmap image file within process.
  */
  unsigned long		 n;
  /**	Number of frames available.
  */
  size_t		 n_frames;
  /**	Bits per component requested for output.
  */
  size_t		 bits;
  /**	BIF image type DK3_BIF_IMAGE_TYPE_xxx, see @ref bifimagetypes.
  */
  int			 bt;
  /**	Flag: Force use of requested background over bg found in file.
  */
  int			 bg;
  /**	Flag: Remove temporary file when closing the structure.
  */
  int			 remtmpf;
  /**	Background red requested.
  */
  dk3_bif_pixel_t	 bgr;
  /**	Background green requested.
  */
  dk3_bif_pixel_t	 bgg;
  /**	Background blue requested.
  */
  dk3_bif_pixel_t	 bgb;
} dk3_bif_t;



/**	Paper size description. All sizes are specified in PS points.
*/
typedef struct {
  /**	Width.
  */
  double	w;
  /**	Height.
  */
  double	h;
  /**	Left border (inner border for duplex without tumble).
  */
  double	i;
  /**	Right border (outer border for duplex without tumble).
  */
  double	o;
  /**	Top border (inner border for duplex with tumble).
  */
  double	t;
  /**	Bottom bofder (outer border for duplex with tumble).
  */
  double	b;
} dk3_paper_size_t;



/**	Named paper size.
*/
typedef struct {
  /**	Size.
  */
  dk3_paper_size_t		 size;
  /**	Size name.
  */
  dkChar const		*name;
} dk3_named_paper_size_t;



/**	Collection of multiple named paper sizes.
*/
typedef struct {
  /**	Application structure for diagnostics and file search.
  */
  dk3_app_t		*app;
  /**	Container of named paper sizes.
  */
  dk3_sto_t		*s_sizes;
  /**	Iterator through named paper sizes container.
  */
  dk3_sto_it_t		*i_sizes;
} dk3_paper_size_collection_t;






/**	Area to place an image.
*/
typedef struct {
  /**	Left x.
  */
  double	xl;
  /**	Right x.
  */
  double	xr;
  /**	Top y.
  */
  double	yt;
  /**	Bottom y.
  */
  double	yb;
} dk3_image_area_t;



/**	Area to place a PS/PDF image.
*/
typedef union {
  /**	Paper size for creating a document.
  */
  dk3_paper_size_t	ps;
  /**	Image area to use when converting Fig to PS.
  */
  dk3_image_area_t	ia;
} dk3_bm_eps_area_t;



/**	Object start position.
*/
typedef struct {
  /**	Object number.
  */
  unsigned long		objno;

  /**	Start position in file.
  */
  dk3_um_t		startpos;
} dk3_pdf_position_t;



/**	XObject representing one image or alpha mask.
*/
typedef struct {
  /**	Name of temporary file with contents.
  */
  dkChar const		*tempfilename;

  /**	Xobject for alpha mask.
  */
  void			*mask;

  /**	Stream length.
  */
  dk3_um_t		slgt;

  /**	Image width (number of pixels).
  */
  dk3_bif_coord_t	width;

  /**	Image height (number of pixels).
  */
  dk3_bif_coord_t	height;

  /**	Object number for XObject.
  */
  unsigned long		objno;

  /**	XObject type DK3_PDF_XO_TYPE_xxx, see @ref pdfxobjecttype.
  */
  int			xot;

  /**	Procedure for object, DK3_BMEPS_xxx, see @ref bmepscompression.
  */
  int			procedure;

  /**	Bits per component.
  */
  int			bpc;

  /**	Flag: Stream is DCT (1) or flate (0).
  */
  int			dct;

  /**	Predictor Dk3_COMPRESSIONPREDICTOR_xxx, see @ref flatepredictors.
  */
  int			pred;

  /**	Flag: Adobe marker was found in CMYK DCT file.
  */
  int			adobe_marker_found;

  /**	Flag: Used by current page.
  */
  int			used;

  /**	Flag: Interpolate this object.
  */
  int			interpolate;
} dk3_pdf_xobject_t;



/**	One PDF page.
*/
typedef struct {
  /**	Name of temporary file for page description stream.
  */
  dkChar const		*tempfilename;

  /**	Temporary file.
  */
  FILE			*tempfile;

  /**	Stream to write to temporary file.
  */
  dk3_stream_t		*tempstream;

  /**	Output filter, used to write to temporary file.
  */
  dk3_of_t		*tempof;

  /**	Collection of all XObjects used by this page.
  */
  dk3_sto_t		*s_xobjects;

  /**	Iterator through XObjects collection.
  */
  dk3_sto_it_t		*i_xobjects;

  /**	Stream length.
  */
  dk3_um_t		slgt;

  /**	PDF object number.
  */
  unsigned long		objno;

  /**	Page number.
  */
  unsigned long		pageno;

  /**	Media box x0.
  */
  long			mbx0;

  /**	Media box x1.
  */
  long			mbx1;

  /**	Media box y0.
  */
  long			mby0;

  /**	Media box y1.
  */
  long			mby1;
} dk3_pdf_page_t;



/**	Structure used to write a PDF file.
*/
typedef struct {
  /**	Application structure, used to find temporary file names.
  */
  dk3_app_t		*app;

  /**	XObject storage (bitmaps).
  */
  dk3_sto_t		*s_xobjects;

  /**	Iterator through XObject storage.
  */
  dk3_sto_it_t		*i_xobjects;

  /**	Pages storage.
  */
  dk3_sto_t		*s_pages;

  /**	Iterator through pages storage.
  */
  dk3_sto_it_t		*i_pages;

  /**	Current page.
  */
  dk3_pdf_page_t	*cp;

  /**	Next XObject object number.
  	First Xobject is object number 4 as we have info object,
	root object and pages collection object as 1, 2, and 3.
  */
  unsigned long		nextobject;

  /**	Next page number.
  */
  unsigned long		nextpage;

  /**	Media box x0 for next page.
  */
  long			mbx0;

  /**	Media box x1 for next page.
  */
  long			mbx1;

  /**	Media box y0 for next page.
  */
  long			mby0;

  /**	Media box y1 for next page.
  */
  long			mby1;

  /**	Flag: Document mode (1) or image/object (0).
  */
  int			documentMode;

} dk3_pdf_t;


/**	Options to convert a bitmap image to PS/PDF.
*/
typedef struct {
  /**	Image area.
  */
  dk3_bm_eps_area_t	ima;

  /**	Resolution in dpi.
  */
  double		resolution;

  /**	Application structure for diagnostics, may be NULL.
  */
  dk3_app_t		*app;

  /**	Output driver DK3_BMEPS_DRIVER_xxx, see @ref bmepsdrivers.
  */
  int			dr;

  /**	Run mode DK3_BMEPS_MODE_xxx, see @ref dkbmmodes.
  */
  int			mode;

  /**	Size conversion mode.
  */
  int			szmode;

  /**	Compression predictor for flate compression,
	DK3_COMPRESSION_PREDICTOR_xxx, see @ref flatepredictors.
  */
  int			pred;

  /**	Image origin position DK3_IMAGE_ORIGIN, see @ref imageorigin.
  */
  int			io;

  /**	Flag: Draft mode.
  */
  int			draft;

  /**	Flag: Printout in duplex.
  */
  int			duplex;

  /**	Flag: Printout in tumble mode (like a calendar).
  */
  int			tumble;

  /**	PS level (2 or 3).
  */
  int			psl;

  /**	Flag: Write DSC comments.
  */
  int			dsc;

  /**	Flag: Allow direct DCT data transfer.
  */
  int			dct;

  /**	Flag: Keep aspect ratio.
  */
  int			kar;

  /**	Requested/suggested background red, 8 bits per pixel.
  */
  int			bgr;

  /**	Requested/suggested background green, 8 bits per pixel.
  */
  int			bgg;

  /**	Requested/suggested background blue, 8 bits per pixel.
  */
  int			bgb;

  /**	Flag: Enforce this background over bg color found in file.
  */
  int			fbg;

  /**	Flag: Image interpolation enabled.
  */
  int			ip;

  /**	Flag: Image interpolation enabled for JPEG files too.
  */
  int			jip;

  /**	Flag: Make mode.
  */
  int			make;

  /**	Write colored output.
  */
  int			color;

  /**	Flag: Reduce bits per component.
  */
  int			rbpc;

  /**	Flag: Write to stdout.
  */
  int			to_stdout;
} dk3_bm_eps_options_t;



/**	Internal conversion job structure.
*/
typedef struct {

  /**	Conversion option set.
  */
  dk3_bm_eps_options_t		*opt;

  /**	Localized message texts.
  */
  dkChar const * const		*msg;

  /**	Source file name.
  */
  dkChar const			*sfn;

  /**	Destination file name.
  */
  dkChar const			*dfn;

  /**	Destination file.
  */
  FILE				*dfile;

  /**	PDF writer structure.
  */
  dk3_pdf_t			*pdf;

  /**	Bitmap image file.
  */
  dk3_bif_t			*bif;

  /**	Communication object.
  */
  void				*pComm;

  /**	Translation x.
  */
  double			 x_translate;

  /**	Translation y.
  */
  double			 y_translate;

  /**	Scale x.
  */
  double			 x_scale;

  /**	Scale y.
  */
  double			 y_scale;

  /**	Left x coordinate (used for the draft).
  */
  double			 x_0;

  /**	Right x coordinate (used for the draft).
  */
  double			 x_1;

  /**	Lower y coordinate (used for the draft).
  */
  double			 y_0;

  /**	Upper y coordinate (used for the draft).
  */
  double			 y_1;

  /**	Image width (bounding box).
  */
  long				 x_size;

  /**	Image width (bounding box).
  */
  long				 y_size;

  /**	Number of bits per component.
  */
  size_t			 bpc;

  /**	Number of pages to produce.
  */
  size_t			 nPages;

  /**	Number of current page.
  */
  size_t			 currentPage;

  /**	Minimum value for progress bar.
  */
  int				 minpb;

  /**	Maximum value for progress bar.
  */
  int				 maxpb;

  /**	Image file type.
  */
  int				 biftype;

  /**	Flag: Must translate coordinates.
  */
  int				 do_translate;

  /**	Rotation (multiples of 90 degree).
  */
  int				 do_rotate;

  /**	Use floating point numbers for scaling.
  */
  int				 do_float_scale;

  /**	Flag: Must do header and footer.
  */
  int				 do_header_footer;

  /**	Output procedure DK3_BMEPS_xxx, see @ref bmepscompression.
  */
  int				 procedure;

} dk3_bm_conversion_job_t;



/**	Information about one font.
*/
typedef struct {
  char const		*psFontName;	/**< PS font name. */
  char const		*texFontName;	/**< LaTeX font name. */
  char const		*texFamilyName;	/**< LaTeX font family name. */
  char const		*gsFontName;	/**< GhostScript font name. */
  char const		*gsFileOld;	/**< Old GhostScript font file name. */
  char const		*gsFileNew;	/**< New GhostScript font file name. */
  char const		*psFamilyName;	/**< PS font family name. */
  char const		*svgFamilyName;	/**< SVG font family name. */
  char const		*svgFontId;	/**< SVG font identifier. */
  char const * const	*replacements;	/**< Replacement family names. */
  int			 features;	/**< Font features (bold/italic). */
  int			 svgFeatures;	/**< Font features when used in SVG. */
  int			 svgFontWeight;	/**< Font weight. */
} dk3_font_t;



/**	Bounding box data.
*/
typedef struct {
  double	xmin;	/**< Minimum x. */
  double	xmax;	/**< Maximum x. */
  double	ymin;	/**< Minimum y. */
  double	ymax;	/**< Maximum y. */
  int		xused;	/**< Flag: x-value specified. */
  int		yused;	/**< Flag: y-value specified. */
} dk3_bb_t;



/**	Coordinates transformation-
*/
typedef struct {
  double	mx;	/**< X factor. */
  double	nx;	/**< X summand. */
  double	my;	/**< Y factor. */
  double	ny;	/**< Y summand. */
} dk3_ct_2d_t;



/**	Color specified as RGB.
*/
typedef struct {
  double	r;	/**< Red. */
  double	g;	/**< Green. */
  double	b;	/**< Blue. */
} dk3_rgb_color_t;



/**	Coordinates system transformation to place an image.
	Order of operations is translate, rotate, scale.
*/
typedef struct {
  double	x_translate;	/**< X translation. */
  double	y_translate;	/**< Y translation. */
  double	x_scale;	/**< X scale factor. */
  double	y_scale;	/**< Y scale factor. */
  int		rot;		/**< Rotation angle, multiple of 90 degrees. */
} dk3_coordinates_transformation_t;



/**	Details for a Windows printer.
*/
typedef struct {
  int			dummy;		/**< No details for Windows. */
} dk3_printer_windows_details_t;



/**	Details for LPRng/LPD queue.
*/
typedef struct {
  dkChar const		*hostname;	/**< Destination host name. */
  dkChar const		*queuename;	/**< Destination queue name. */
  double		 to_c;		/**< Timeout for connect. */
  double		 to_r;		/**< Timeout for receive. */
  double		 to_s;		/**< Timeout for send. */
} dk3_printer_lprng_details_t;



/**	Details for a socket connected printer.
*/
typedef struct {
  dkChar const		*hostname;	/**< Destination host or address. */
  unsigned short	 portno;	/**< Port no, host representation. */
  unsigned char		 ordrel;	/**< Flag: Orderly release. */
  double		 to_c;		/**< Timeout for connect. */
  double		 to_r;		/**< Timeout for receive. */
  double		 to_s;		/**< Timeout for send. */
} dk3_printer_socket_t;



/**	Details for a printer.
*/
typedef union {
  dk3_printer_windows_details_t		win;	/**< Windows printer details. */
  dk3_printer_lprng_details_t		lprng;	/**< LPRng printer details. */
  dk3_printer_socket_t			sock;	/**< Socket printer details. */
} dk3_printer_details_t;



/**	Printer.
	Windows printer ports can be a connection to a remote
	raw socket or LPD/LPRng queue. In these cases t_p and det_p
	indicate "Windows printer" and the t_s and det_s contain
	information about the backend.
	For the "clean" and "status" commands we need to access
	the backend, other operations only access the Windows printer.

*/
typedef struct {
  dk3_printer_details_t	 det_p;	/**< Primary type printer details. */
  dk3_printer_details_t	 det_s;	/**< Secondary type printer details. */
  dkChar const		*name;	/**< Printer name. */
  dkChar const		*h_snmp;	/**< Host to access for SNMP. */
  dkChar const		*c_snmp;	/**< SNMP community. */
  int			 t_p;	/**< Primary type,see @ref printertypes.  */
  int			 t_s;	/**< Secondard type, see @ref printertypes.  */
  int			 v_snmp;	/**< SNMP version. */
  int			 ps;	/**< Flag: PS printer. */
} dk3_printer_t;



/**	Queue name alias.
*/
typedef struct {
  dkChar const		*name;		/**< Alias name. */
  dk3_printer_t		*printer;	/**< Alias destination. */
} dk3_printer_alias_t;



/**	Print host.
*/
typedef struct {
  dkChar const		*name;		/**< Host name. */
  double		 to_c;		/**< Timeout for connect. */
  double		 to_r;		/**< Timeout for receive. */
  double		 to_s;		/**< Timeout for send. */
  int			 enc;		/**< Host encoding (plain or UTF-8). */
} dk3_print_host_t;



/**	Print host alias.
*/
typedef struct {
  dkChar const		*name;		/**< Alias name. */
  dk3_print_host_t	*host;		/**< Destination host. */
} dk3_print_host_alias_t;



/**	Print configuration for a computer system.
*/
typedef struct {
  dk3_app_t		*app;		/**< Application structure. */
  dkChar const * const	*msg;		/**< Localized message texts. */
  dk3_sto_t		*sPrinters;	/**< Printers storage. */
  dk3_sto_it_t		*iPrinters;	/**< Iterator through printers. */
  dk3_sto_t		*sPrintAliases;	/**< Printer aliases storage. */
  dk3_sto_it_t		*iPrintAliases;	/**< Iterator through print aliases. */
  dk3_sto_t		*sPrintHosts;	/**< Print hosts storage. */
  dk3_sto_it_t		*iPrintHosts;	/**< Iterator through hosts. */
  dk3_sto_t		*sHostAliases;	/**< Print host aliases storage. */
  dk3_sto_it_t		*iHostAliases;	/**< Iterator through host aliases. */
  dk3_printer_t		*defPrinter;	/**< Default printer. */
  dk3_print_host_t	*defHost;	/**< Default host. */
  int			 defPsl;	/**< Default PS level. */
} dk3_print_conf_t;



#if DK3_HAVE_VOLATILE
/**	The volatile keyword can be used.
*/
#define	DK3_VOLATILE	volatile
#else
/**	The volatile keyword can not be used.
*/
#define	DK3_VOLATILE	/* NIX */
#endif

typedef
#if DK3_HAVE_SIG_ATOMIC_T
/**	Atomic type for signal handlers.
*/
sig_atomic_t
#else
/**	Atomic type or int for signal handlers.
*/
int
#endif
dk3_sig_atomic_t;

/**	Pointer to dkChar.
*/
typedef dkChar		*DK3_PDKCHAR;

/**	Pointer to const dkChar.
*/
typedef	dkChar const	*DK3_PCDKCHAR;

/**	Pointer to unsigned char.
*/
typedef	unsigned char	*DK3_PUCHAR;

/**	Pointer to char.
*/
typedef char		*DK3_PCHAR;

/**	Pointer to char const.
*/
typedef char const	*DK3_PCCHAR;

/**	Return type for signal handlers.
*/
typedef	void		dk3_signal_ret_t;

/**	Signal handler function prototype.
*/
typedef	dk3_signal_ret_t	dk3_signal_fct_t(int);

/**	Signal disposition (handler function address).
*/
typedef	dk3_signal_fct_t	*dk3_signal_disp_t;

/**	Handler function for one character.
	@param	obj	Object to modify using the line.
	@param	ch	Character to process.
	@return	1=OK, 0=error, can continue, -1=error, abort processing.
*/
typedef int dk3_char_handler_t(void *obj, dk3_c32_t ch);

/**	Function to traverse a simple database.
	@param	obj	Object to modify while traversing the database.
	@param	key	Key data.
	@param	val	Value data.
	@return	1 on success, 0 on error (can continue), -1 on error (abort).
*/
typedef int dk3_db_traverse_fct_t(void *obj,dk3_datum_t *key,dk3_datum_t *val);

#endif

