/*
----------------------------------------------------------------------
utility standard header
----------------------------------------------------------------------

         +----------------------------------------------------+
         |  Worldwide Copyright (c) Byte Designs Ltd (2001)   |    
         |                                                    |
         |               20568 - 32 Avenue                    |
         |               LANGLEY, BC                          |
         |               V3A 4P5 CANADA                       |
         |                                                    |
         |            Sales: sales@bytedesigns.com            |
         |          Support: support@bytedesigns.com          |
         | Phone: (604) 534 0722      Fax: (604) 534 2601     |
         +----------------------------------------------------+
                            Version 6.12
 
----------------------------------------------------------------------
*/


/* header files -------------------------------------------------- */

#include <stdlib.h>                     /* system headers */
#include <stdio.h>
#include <string.h>

#define ISINTERNAL 1

#include <isbase.h>                     /* base structures */

extern char *iscopyright;
extern char *isversnumber;
extern char *is_errlist[];

/* base library internal definitions ----------------------------- */

#define TRUE    1
#define FALSE   0

#define SETBIT( F, O )  ( F |  ( 1 << ( O ) ) )
#define UNSBIT( F, O )  ( F & ~( 1 << ( O ) ) )
#define BITSET( F, O )  ( F &  ( 1 << ( O ) ) )

#define ISREALLOC(S,P,O,N) ( struct S * )is_realloc( ( char * )P, O, N )
#define ISALLOC(S)  ( struct S * )is_malloc( sizeof( struct S ) )
#define ISFREE(S,P) ( struct S * )is_free( ( char * )P )

#define ISNODEMAX   ( isam->idxlen - 4 )

#define ISFAIL(I,M) ( ! isEntry( I, M ) || setjmp( I->trap ) )
  
#define ISFATAL     is_fatal( "fatal isam error %s(%d)", __FILE__, __LINE__ )


/* path handling ------------------------------------------------- */

#define ISPATH      ( isam->path[isam->curidx] )
#define ISKDSC      ( isam->path[isam->curidx]->kdsc )

#define ISTAIL      ( ISPATH->tail )
#define TAIL        path->tail

#define ISEMPTY(P)  ( P->tail->used == 2 )
 

/* specialised machine independant integers ---------------------- */

#define LDRECD(P)       ( ldMint( P, 4 ) & 0x7FFFFFFFL )
#define LDDUP(P,L)      ( ldMint( P, L ) )
#define STDUP(P,L,D)    ( stMint( P, L, D ) )


/* structure specific flags and state values --------------------- */

#define ISNOPRIM    0                   /* primary is natural order */
#define ISNOTNEXT   1                   /* read/start next dislocate */
#define ISNOTPREV   2                   /* read/start prev dislocate */
#define ISWRLOCK    3                   /* lock records on write */
#define ISPRECIOUS  4                   /* do not reuse data slots */
#define ISLOCKCHECK 5                   /* check locked on update/delete */
#define ISNOCURRENT 6			/* current is not viable */
#define ISVIRTUAL   7                   /* virtual close in istrans */
#define ISTXNOPEN   8                   /* new open in istrans */

/* IsamNode */
#define ISNDREF     0                   /* image must be refreshed */
#define ISNDOK      1                   /* image is current */
#define ISNDUPD     2                   /* image has been updated */

/* typedefs */

typedef struct IsamPath IsPath;
typedef struct IsamNode IsNode;
typedef struct IsamFree IsFree;


/* base library internal prototypes ------------------------------ */

#ifdef __cplusplus
extern "C" {
#endif

/* isadmin.c */
int isAdmOpen ISD3((IsFile *isam));
int isAdmCheck ISD3((IsFile *isam));
int isAdmClose ISD3((IsFile *isam));
int isAdmDupData ISD3((IsFile *isam, long recnum, int wait));
IsFile *isAdmVopen ISD3((char *name, int mode));
IsFile *isAdmVclose ISD3((IsFile *isam));
int isAdmTxnFile ISD3((int txnfd, int pid));

#if( ISAUDIT )
int isAudInit ISD3((IsFile *isam));
int isAudHead ISD3((IsFile *isam, char *code, long recnum));
int isAudVLen ISD3((IsFile *isam, int len));
int isAudBody ISD3((IsFile *isam, char *data, int len));
int isAudDone ISD3((IsFile *isam));
#endif

#if( ISTHREADED )
int isAdmDupLock ISD3((IsFile *isam, int mode));
int isAdmDupDrop ISD3((IsFile *isam));
void isAdmCrit ISD3((IsFile *isam, int mode));
void isAdmSafe ISD3((IsFile *isam, int mode));
void isAdmFail ISD3((IsFile *isam));
#else
# define isAdmDupLock(I,M)
# define isAdmDupDrop(I)
# define isAdmCrit(I,M)
# define isAdmSafe(I,M)
# define isAdmFail(I)
#endif

/* iscomp.c */
int isCompKey ISD3((char *dest, char *old, char *new, int flags, int len));
int isCompNext ISD3((IsNode *node, int flags, int klen, int dlen, int off));
int isCompScan ISD3((char *image, char *value, int start, int stop, int flags, int klen, int dlen));

/* isdatio.c */
int isGetData ISD3((IsFile *isam, char *pad, long recnum));
int isPutData ISD3((IsFile *isam, char *pad, long recnum));
int isDelCheck ISD3((IsFile *isam, long recnum));
int isDelData ISD3((IsFile *isam, long recnum));

/* isentry.c */
int isEntry ISD3((struct IsamFile *isam, int workmode));
int isDelta ISD3((struct IsamFile *isam));
int isFail ISD3((struct IsamFile *isam, int iserrno, int iserrio, char stat ));
void is_fatal ISD3((char *fmt,...));
void isSetStat ISD3((struct IsamFile *isam, char stat));

/* isfree.c */
long isGetFree ISD3((IsFile *isam, int which));
int isPutFree ISD3((IsFile *isam, int which, long recnum));
long isFreeCount ISD3((IsFile *isam, int which, int (*call )(char *node )));
int isFreeClear ISD3((IsFile *isam, int mode));
int isFreeWrite ISD3((IsFile *isam));
int isFreeFree ISD3((IsFile *isam));
int isFreeDrop ISD3((IsFile *isam, int which));

/* isgrow.c */
int isTreeInsert ISD3((IsFile *isam, IsPath *path, char *key, long recnum));

/* ishead.c */
int isMakeHead ISD3((IsFile *isam));
int isLoadHead ISD3((IsFile *isam));
int isHeadWrite ISD3((IsFile *isam));
int isHeadKdsc ISD3((IsFile *isam));
int isHeadFree ISD3((IsFile *isam, int which, IsFree *list));

/* isidxio.c */
int isRefNode ISD3((IsFile *isam, IsNode *node));
int isUpdNode ISD3((IsFile *isam, IsNode *node));
int isGetNode ISD3((IsFile *isam, char *image, long idxrec));
int isPutNode ISD3((IsFile *isam, char *image, long idxrec));
int isMarkNode ISD3((IsFile *isam, char *image, int low, int high));

/* isindex.c */
int isLoadIndexes ISD3((IsFile *isam));
int isMakeIndex ISD3((IsFile *isam, struct keydesc *kdsc));
int isDropIndex ISD3((IsFile *isam, int idx));
int isTestIndex ISD3((IsFile *isam, IsKdsc *kdsc));
int isFindIndex ISD3((IsFile *isam, IsKdsc *kdsc));
int isFreeIndex ISD3((IsFile *isam, int idx));

/* isidxdel.c isbuild.c */
int isWipeIdx ISD3((IsFile *isam, int idx));
int isFillIdx ISD3((IsFile *isam, int idx));

/* iskey.c */
int isKeyMake ISD3((IsPath *path, char *dest, char *pad));
int isKeyLoad ISD3((IsPath *path, char *dest, char *key));
int isKeyCmp ISD3((IsPath *path, char *one, char *two));
int isKeyMatch ISD3((IsPath *path, char *one, char *two));
int isKeyNull ISD3((IsPath *path, char *key));

/* islocate.c */
int isLocate ISD3((IsFile *isam, char *pad, int mode));
int isEdge ISD3((IsFile *isam, IsPath *path, int mode));
int isWalk ISD3((IsFile *isam, IsPath *path, int mode));
int isFind ISD3((IsFile *isam, IsPath *path, char *key, int mode));
long isCurrent ISD3((IsFile *isam));

/* islock.c */
int isLockOpen ISD3((IsFile *isam));
int isLockExcl ISD3((IsFile *isam));
int isDropExcl ISD3((IsFile *isam));
int isDropFile ISD3((IsFile *isam));
int isLockRead ISD3((IsFile *isam));
int isLockWrite ISD3((IsFile *isam));
int isDropLock ISD3((IsFile *isam));
int isLocked ISD3((IsFile *isam, long recnum));
int isLockTest ISD3((IsFile *isam, long recnum));
int isLockData ISD3((IsFile *isam, long recnum, int wait));
int isDropData ISD3((IsFile *isam, long recnum));
int isLockDall ISD3((IsFile *isam));
int isDropDall ISD3((IsFile *isam));

/* islkinfo.c */
#if( ISLKINFO == 1 )
int isLKplaced ISD3(( IsFile *isam, long record ));
int isLKfailed ISD3(( IsFile *isam, long record ));
int isLKreleased ISD3(( IsFile *isam, long record ));
int isLKrelall ISD3(( IsFile *isam ));
#endif

/* ismif.c */
long ldMint ISD3((char *pad, int len));
int stMint ISD3((char *pad, int len, long val));
int ld_int ISD3((char *pad));
int st_int ISD3((int value, char *pad));
long ld_long ISD3((char *pad));
int st_long ISD3((long value, char *pad));

/* isnode.c */
IsNode *isMakeNode ISD3((IsFile *isam, IsPath *path));
IsNode *isFreeNode ISD3((IsNode *node));
int isInitInfo ISD3((IsFile *isam, IsNode *node));
int isNodeWalk ISD3((IsPath *path, int mode));
int isNodeMatch ISD3((IsPath *path, char *target));
int isNodeGreat ISD3((IsPath *path, char *target));

/* ispath.c */
int isPathIns ISD3((IsFile *isam, IsPath *path, long idxrec));
int isPathSplit ISD3((IsFile *isam, IsPath *path));
int isPathAdd ISD3((IsFile *isam, IsPath *path, long idxrec));
int isPathDown ISD3((IsFile *isam, IsPath *path));
int isPathClear ISD3((IsFile *isam, int mode));
int isPathWrite ISD3((IsFile *isam, int idx));
int isPathFree ISD3((IsFile *isam, IsPath *path));

/* isprune.c */
int isTreeDelete ISD3((IsFile *isam, IsPath *path, char *key, long rec));

/* issystem.c */
int is_create ISD3((IsFile *isam, int which, char *name));
int is_open ISD3((IsFile *isam, int which, char *name));
int is_read ISD3((IsFile *isam, int which, char *pad, int len, long off));
int is_write ISD3((IsFile *isam, int which, char *pad, int len, long off));
unsigned long is_size ISD3((IsFile *isam, int which));
unsigned long is_tell ISD3((IsFile *isam, int which));
int is_close ISD3((IsFile *isam, int which));
int is_unlink ISD3((IsFile *isam, int which, char *name));
int is_rename ISD3((IsFile *isam, int which, char *old, char *new));
int is_lock ISD3((IsFile *isam, int which, long off, long len, int mode));
int is_lock ISD3((IsFile *isam, int which, long off, long len, int wait));
int is_drop ISD3((IsFile *isam, int which, long off, long len));
long is_getpid ISD3((IsFile *isam));
long is_getuid ISD3((IsFile *isam));
char *is_malloc ISD3((int len));
char *is_realloc ISD3((char *ptr, int old, int new));
char *is_free ISD3((char *ptr));
void is_memset ISD3((char *dest, int val, int len));

/* issquash.c */
int isDsquash ISD3((char *data, char *vcbuf, int datlen));
int isDexpand ISD3((char *dest, char *vcbuf));

/* istrans.c */
int isLGtest ISD3((IsFile *isam));
int isLGbuild ISD3((int code, IsFile *isam, IsKdsc *key));
int isLGerase ISD3((int code, char *name));
int isLGrename ISD3((int code, char *old, char *new));
int isLGfile ISD3((int code, IsFile *isam));
int isLGindex ISD3((int code, IsFile *isam, IsKdsc *key));
int isLGuniq ISD3((int code, IsFile *isam, long uniq));
int isLGdata ISD3((int code, IsFile *isam, long recnum, char *data));
int isLGupdate ISD3((int code, IsFile *isam, long recnum, char *old, char *new));
IsFile *isLGvopen ISD3((char *name, int mode));
int isLGvclose ISD3((IsFile *isam));

/* istree.c */
int isTreeFirst ISD3((IsFile *isam, IsPath *path));
int isTreeLast ISD3((IsFile *isam, IsPath *path));
int isTreeCurr ISD3((IsFile *isam, IsPath *path));
int isTreePrev ISD3((IsFile *isam, IsPath *path));
int isTreeNext ISD3((IsFile *isam, IsPath *path));
int isTreeMatch ISD3((IsFile *isam, IsPath *path, char *target));
int isTreeGreat ISD3((IsFile *isam, IsPath *path, char *target));

/* isvarlen.c */
#if( ISVARIABLE )
int isVLinit ISD3((IsFile *isam));
int isVLdone ISD3((IsFile *isam));
int isVLread ISD3((IsFile *isam, char *data));
int isVLwrite ISD3((IsFile *isam, char *data));
int isVLdel ISD3((IsFile *isam));
#endif

/* iswork.c */
int dbdec ISD3((FILE *s, char *pad, int l));
int dbhex ISD3((FILE *s, char *pad, int l));
int dball ISD3((FILE *s, char *p, int l));
int showindexes ISD3((struct IsamFile *isam));
int showkey ISD3((struct keydesc *kdsc));
int showinfo ISD3((struct IsamFile *isam));
int checkflat ISD3((IsFile *isam, IsPath *path, IsNode *node));
int checknode ISD3((IsFile *isam, IsPath *path, IsNode *node, int start));
int showpath ISD3((IsPath *path));

#ifdef __cplusplus
};
#endif
