home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BTREE.ZIP
/
BTOPEN.C
< prev
next >
Wrap
Text File
|
1989-08-31
|
5KB
|
168 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "btopen.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <errno.h>
/* #include <string.h> */
#include "btree_.h"
/* btree control structure table definition */
btree_t btb[BTOPEN_MAX];
/*man---------------------------------------------------------------------------
NAME
btopen - open a btree
SYNOPSIS
btree_t *btopen(filename, type, cmp_p)
char *filename;
char *type;
int (*cmp_p)();
DESCRIPTION
The btopen function opens the file named by filename as a btree. A
pointer to the btree_t structure associated with the file is returned.
filename points to a character string that contains the name of the
file to be opened.
type is a character string having one of the following values:
"r" open for reading
"r+" open for update (reading and writing)
cmp_p is a pointer to the user written comparison function which
defines the sort sequence for the btree. This function must be of
the form
int f(const void *p1, const void *p2, size_t n);
where p1 and p2 point to the two keys to bey compared and n is the
key size. The return must be less than, equal to, or greater than
zero if p1 is less than, equal to, or greater than p2, respectively.
If cmp_p is NULL then the memcmp function is used as the default.
btopen will fail if one or more of the following is true:
[EINVAL] filename is the NULL pointer.
[EINVAL] type is not "r" or "r+".
[ENOENT] The named btree file does not exist.
[BTECORRUPT] The named file is corrupt.
[BTEEOF] No file header.
[BTEMFILE] Too many open btrees. The maximum
is defined as BTOPEN_MAX in btree.h.
SEE ALSO
btclose, btcreate.
DIAGNOSTICS
btopen returns a NULL pointer on failure, and errno is set to
indicate the error.
NOTES
Since there is no mechanism by which the address of a function
may be stored in a file, it is the responsibility of the
programmer to supply the same key comparison function each time
the file is opened. Use of a different comparison function
will yield unpredictable results and corrupt the file if a write
is attempted.
------------------------------------------------------------------------------*/
btree_t *btopen(filename, type, cmp_p)
char * filename;
char * type;
int (*cmp_p)();
{
int rs = 0;
int terrno = 0;
btree_t * btp = NULL;
errno = 0;
/* validate input parameters */
if ((filename == NULL) || (type == NULL)) {
errno = EINVAL;
return NULL;
}
/* find free slot in btb table */
for (btp = btb; btp < (btb + BTOPEN_MAX); btp++) {
if (!(btp->flags & BTOPEN)) {
break; /* found */
}
}
if (btp >= btb + BTOPEN_MAX) {
errno = BTEMFILE;
return NULL; /* no free slots */
}
/* open file */
if (strcmp(type, BT_READ) == 0) {
btp->flags = BTREAD;
} else if (strcmp(type, BT_RDWR) == 0) {
btp->flags = BTREAD | BTWRITE;
} else {
errno = EINVAL;
return NULL;
}
btp->bp = bopen(filename, type, sizeof(bthdr_t), (size_t)1, (size_t)0);
if (btp->bp == NULL) {
if ((errno != ENOENT) && (errno != EACCES)) BTEPRINT;
memset((void *)btp, 0, sizeof(btb[0]));
btp->flags = 0;
return NULL;
}
/* load btree_t structure */
rs = bgeth(btp->bp, (void *)&btp->bthdr); /* header */
if (rs == -1) {
BTEPRINT;
terrno = errno;
bclose(btp->bp);
memset((void *)btp, 0, sizeof(btb[0]));
btp->flags = 0;
errno = terrno;
return NULL;
}
if (btp->bthdr.flags & BTHMOD) { /* corrupt file */
bclose(btp->bp);
memset((void *)btp, 0, sizeof(btb[0]));
btp->flags = 0;
errno = BTECORRUPT;
return NULL;
}
btp->cmp_p = cmp_p; /* key comparison fct */
if (btp->cmp_p == NULL) {
btp->cmp_p = memcmp;
}
btp->cbtpos.node = 0; /* cursor */
btp->cbtpos.key = 0;
rs = bt_alloc(btp); /* current node and search path */
if (rs == -1) {
BTEPRINT;
terrno = errno;
bclose(btp->bp);
memset((void *)btp, 0, sizeof(btb[0]));
btp->flags = 0;
errno = terrno;
return NULL;
}
/* set up buffering */
rs = bsetvbuf(btp->bp, NULL, bt_blksize(btp), BTBUFCNT);
if (rs == -1) {
BTEPRINT;
terrno = errno;
bt_free(btp);
bclose(btp->bp);
memset((void *)btp, 0, sizeof(btb[0]));
btp->flags = 0;
errno = terrno;
return NULL;
}
errno = 0;
return btp;
}