Category : C Source Code
Archive   : BTREE3.ZIP
Filename : BTREE.H

 
Output of file : BTREE.H contained in archive : BTREE3.ZIP
/*
btree.h
hacked by ...!gatech!kd4nc!n4hgf!wht

Copyright (C) 1988, Marcus J. Ranum, William Welch Medical Library
$Author: mjr $ $Log: btree.c,v $ Revision 1.1 88/06/01 21:35:07 mjr
Initial revision
The original code was placed in the public domain. This is not, since I
wish to retain some control over it. No restrictions are placed on
modification, or use of this code, as long as the copyrights are not
removed. There are some areas that really could use improving (like
handling free nodes better) and I hope that if someone makes
improvements they will keep me up to date on them (e-mail please, I am
not on usenet).
Marcus J. Ranum, William Welch Medical Library, 1988
[email protected] || uunet!mimsy!aplcen!osiris!welchvax!mjr

Modification History:
12-23-1989-18:03-mfl-added BT_HEAD, BT_TAIL for the _btmove function
12-10-1989-18:03-wht-make cache much larger
12-17-1988-19:52-wht-released 1.20
12-16-1988-20:38-wht-variable max key length
12-16-1988-18:22-afterlint-creation

*/

#define BT_TAIL 0 /* for _btmove function */
#define BT_HEAD 1 /* for _btmove function */
#define BT_NREC 1 /* no such record */
#define BT_EOF 2 /* end of the tree (either end) */
#define BT_ERR -1 /* something went wrong */
#define BT_DELETED 1 /* this indicates a node deleted */
#define BT_ACTIVE 0 /* this indicates a node active */
#define BT_MAGIC 0x0BB1 /* BTREE file magic number */
#define BT_KSIZ 60 /* size of keys to store (or trunc) */

/* error codes - matches bterrs */
#define BT_BAD_MAGIC 1 /* bad index file magic number */
#define BT_BAD_STACK 2 /* history stack overflow */
#define BT_BAD_ROOT 3 /* failed attempt to delete node 0 */
#define BT_BAD_KSIZ 4 /* keysize does not match file keysize */
#define BT_BAD_MEMORY 5 /* memory allocation failed */
#define BT_BAD_SBLK 6 /* bad superblock */
#define BT_BAD_WSUPER 7 /* error writing superblock */
#define BT_BAD_WNODE 8 /* error writing node */
#define BT_BAD_RNODE 9 /* error reading node */
#define BT_BAD_FLUSH 10 /* error flushing node */

#define STACK_SIZ 30 /* length of btree history stacks */
#define BTNODE struct btnode
#define BTREE struct btree
#define BT_NSIZ (sizeof(struct btnode) + bt->sblk.keysize)
#define BT_SSIZ (sizeof(struct btsuper))
#define bcopy(dest,src,len) memcpy(src,dest,len)
#define bzero(dest,len) memset(dest,0,len)

#ifdef MSDOS /* "MSDOS" is pre-defined in Microsoft 'C' */
#undef SYS5
#else
#define SYS5 1 /* default to UNIX SYS5 if not MSDOS */
#endif

#ifdef MSDOS
#include /* for S_IWRITE, S_IREAD */
#include /* for S_IWRITE, S_IREAD */
#define BT_CSIZ 16 /* # of nodes to cache (power of two) */
#define OPENMODE O_CREAT|O_RDWR|O_BINARY /* must use BINARY for DOS */
#define CREATMODE S_IREAD|S_IWRITE /* DOS only */
#endif

#ifdef SYS5 /* UNIX System Five */
#define OPENMODE O_CREAT|O_RDWR
#define CREATMODE 0660
#define BT_CSIZ 4096 /* # of nodes to cache (power of two) */
#endif

struct btnode {
long recno; /* index to external file, or whatever */
short lptr; /* left-pointer */
short rptr; /* right-pointer */
char deleted; /* deleted flag */
char balance; /* balance flag */
};

struct btnode_m {
struct btnode n; /* keep these two ... */
char key[BT_KSIZ]; /* ... fields FIRST !!! */
short node_num; /* disk node number */
short dirty; /* if true, cache item needs writing to disk */
};

struct btstack {
short ele[STACK_SIZ]; /* stack elements */
short lev[STACK_SIZ]; /* stack levels */
short sptr; /* stack pointer */
};

/* a disk resident btree super block */
struct btsuper {
short magic; /* magic number */
short keysize; /* maximum length of key */
short root; /* pointer to root node */
short free; /* pointer to next free (basically, EOF) */
short list; /* number of active records */
};

/* a memory resident btree super block */
/* including room to hold a disk super block */
struct btree {
int fd; /* file descriptor */
struct btsuper sblk; /* copy of superblock */
struct btstack rstak; /* history stack */
struct btstack lstak; /* history stack */
struct btnode_m *cache[BT_CSIZ]; /* read-only cache */
short rdonly; /* true if file opened read-only */
short slev; /* history stack level */
};

#ifdef MAIN
/* the errno for btree problems. we use negative # - */
/* so btperror can use the real UNIX errno */
char *bterrs[] = {
"No btree error",
"Bad index magic number (is this an index file?)",
"History stack overflow",
"Cannot delete node zero",
"Open keysize does not match index keysize",
"Memory allocation failure",
"Bad superblock (is this an index file?)",
"error writing superblock",
"error writing node",
"error reading node",
"error flushing node",
NULL
};

int bterrno = 0;
long cache_hits = 0;
long cache_no_hits = 0;
long lseek();

#else

extern char *bterrs[];
extern int bterrno;
extern long cache_hits;
extern long cache_no_hits;
#endif

BTREE *btopen();
void btperror();
long lseek();

/* end of btree.h */


  3 Responses to “Category : C Source Code
Archive   : BTREE3.ZIP
Filename : BTREE.H

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/