Revision 237 (by dpavlin, 2004/03/08 17:43:12) initial import of openisis 0.9.0 vendor drop
/*
	openisis - an open implementation of the CDS/ISIS database
	Version 0.8.x (patchlevel see file Version)
	Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	see README for more information
EOH */
#ifndef LUTI_H

/*
	$Id: luti.h,v 1.30 2003/04/17 13:14:41 mawag Exp $
	common utilities for openisis lib implementation.
*/

#include <assert.h>


#include "loi.h"


extern LogLevel log_lev;
#define LOG_DO( lev ) ((lev) <= log_lev)
#define LOG_DONT( lev ) ((lev) > log_lev)

/**
	NOTE: unlike sMsg, this directly and immediatly lio_writes to file 2.
	So this is more expensive, but threadsafe.

	print some log message to stderr or some configured logdevice.
	for errors, use one of the LERR_xxx codes or
	a loglevel of SYSERR or IOERR to determine the error from errno.
	loglevels FATAL and ERROR, when used w/o error code,
	assum IDIOT and INVAL, resp.
	other logging levels do not assume an error (i.e. 0 == -LERR_OK).
	any error or loglevel code implies stream 2.
	@param code errorcode (-LERR_xxx) or loglevel as described above
	@return a non-positive code as described above
*/
extern int log_msg ( int code, const char *fmt, ... );
#ifndef NDEBUG
# define LOG_DBG log_msg
#else
#	ifdef __GNUC__
# 	define LOG_DBG( args... )
#	else
# 	define LOG_DBG (void) /* compiler should dispose statement off */
#	endif
#endif

#define LOG_OTO( to, args ) do { ret = log_msg args; goto to; } while (0)

/* content logging is usually on level LOG_TRACE */
#define LOG_STR( rec, desc ) log_str( LOG_TRACE, rec, desc )
#define LOG_HEX( mem, len ) log_hex( LOG_TRACE, mem, len )

/**
	dump a record
	@param level loglevel necessary for output to occurr.
*/
extern void log_str ( LogLevel level, int *rec, const char **desc );

/*
 * print fields of record to stderr
 * @param level if negative, print msg to stdout
 * @param delim field delimiter (default '; ')
 */
extern void log_rec (int level,
	Rec *rec, const char *msg, const char *delim);

/**
	hexdump memory
	@param level loglevel necessary for output to occurr.
*/
extern void log_hex ( LogLevel level, const void *mem, int len );

/**
	do a malloc and print error if out of memory.
*/
extern void *lmalloc ( int size );

/* more specific log functions in log.h */

/* printint: optional sign, up to 10 digits, 0 byte.
	buf must be at least 12 bytes long.
	return length w/o the 0 byte.
*/
extern int lprint ( void *buf, int i );


/* Lookup of id from name */
typedef struct OpenIsisLT *LutiLT;

/* create lookup table */
extern LutiLT luti_ltnew ();
/* destroy lookup table */
extern void luti_ltdel (LutiLT lt);
/* add entry */
extern void luti_ltadd (LutiLT lt, const char *name, int id);
/* get id for name */
extern int luti_ltget (LutiLT lt, const char *name);
/* remove entry */
extern void luti_ltrmv (LutiLT lt, const char *name);

/* get fdt from rec */
extern Fdt* luti_fdt_from_rec (Rec *rec);

/* get fd, tag, occurence from path */
extern const char* luti_parse_path (
	const char *path, const Fdt *fdt, Fd **fd, int *tag, int *occ);

/* extract embedded record from envelope */
extern Rec* luti_unwrap (Rec *env, int *pos, int tag, int dbid);

/* extract embedded record from envelope */
extern Rec* luti_getembed (
	Rec *env, const char *path, const Fdt *fdt);

/* embed record in envelope */
extern Rec* luti_wrap (Rec *env, Rec *rec, int tag);

/* append src fields to tgt */
extern Rec* luti_append (Rec *tgt, Rec *src);

/**	incr base ptr by incr*siz, if 0 < maxnum && *num < maxnum
	new allocated range is memsetted 0
	NOTE: base is address of base for reallocation (whatever_type**)
	@return index of first new allocated address in array or -1 if out of mem
*/
extern int luti_ptrincr (
	void *base, int *num, int incr, int siz, int maxnum);

/** free pointer array
*/
extern void luti_free (void **arr, int num);

/**	check whether string represents a boolean value
	@return 0 str is false,
		1 str is true,
		-1 str is something else
*/
extern int luti_true (const char *str, int len);

/**
	s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
	forced to non-negative
*/
extern int lhash ( const char *str, int len );

extern const char luti_hex[16]; /* '0'..'9','a'..'f' */

#define LUTI_H
#endif /* LUTI_H */