home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
CBASE.ZIP
/
CBKEYSRC.C
< prev
next >
Wrap
Text File
|
1989-08-31
|
4KB
|
168 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "cbkeysrc.c 1.1 - 89/08/31" */
#include <blkio.h>
#include <bool.h>
#include <btree.h>
#include <errno.h>
#include <lseq.h>
/* #include <stdlib.h> */
/* #include <string.h> */
#include "cbase_.h"
/*man---------------------------------------------------------------------------
NAME
cbkeysrch - search cbase key
SYNOPSIS
#include <cbase.h>
int cbkeysrch(cbp, field, buf)
cbase_t *cbp;
int field;
void *buf;
DESCRIPTION
The cbkeysrch function searches a key in cbase cbp. field is the field
to be searched. buf points to the key to be searched for. If found,
the cursor for that key is positioned to the key found. If it is not
found, the cursor for that key is positioned to the next higher key,
which may be null. The record cursor is set to the record associated
with the key marked by the key cursor.
cbkeysrch will fail if one or more of the following is true:
[EINVAL] cbp is not a valid cbase pointer.
[EINVAL] field is less than 1 or greater than
the number of keys defined for cbp.
[EINVAL] buf is the NULL pointer.
[CBELOCK] cbp is not read locked.
[CBENKEY] field is not a key.
[CBENOPEN] cbp is not open.
SEE ALSO
cbkcursor, cbkeyfirst, cbkeylast, cbkeynext, cbkeyprev.
DIAGNOSTICS
Upon successful completion, a value of 1 is returned if the key was
found or a value of 0 if it was not. Otherwise, a value of -1 is
returned and errno is set to indicate the error.
------------------------------------------------------------------------------*/
int cbkeysrch(cbp, field, buf)
cbase_t * cbp;
int field;
void * buf;
{
int rs = 0;
void * buf2 = NULL;
cbrpos_t cbrpos = 0;
lspos_t lspos = 0;
bool found = FALSE;
errno = 0;
/* validate arguments */
if ((!cb_valid(cbp)) || (buf == NULL)) {
errno = EINVAL;
return -1;
}
if ((field < 1) || (field > cbp->fldcnt)) {
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(cbp->flags & CBOPEN)) {
errno = CBENOPEN;
return -1;
}
/* check if field is a key */
if (!(cbp->fields[field - 1].flags & CBFKEY)) {
errno = CBENKEY;
return -1;
}
/* check if not read locked */
if (!(cbp->flags & CBRDLCK)) {
errno = CBELOCK;
return -1;
}
/* construct (key, record position) pair */
if (btkeysize(cbp->btp[field - 1]) !=
(cbp->fields[field - 1].size + sizeof(cbrpos_t))) {
CBEPRINT;
errno = CBEPANIC;
return -1;
}
buf2 = calloc(1, cbp->fields[field - 1].size + sizeof(cbrpos_t));
if (buf2 == NULL) {
CBEPRINT;
errno = ENOMEM;
return -1;
}
cbrpos = 0;
memcpy(buf2, buf, cbp->fields[field - 1].size);
memcpy((void *)((char *)buf2 + cbp->fields[field - 1].size),
(void *)&cbrpos, sizeof(cbrpos_t));
/* search for key */
rs = btsearch(cbp->btp[field - 1], buf2);
if (rs == -1) {
CBEPRINT;
free(buf2);
return -1;
}
/* check if cursor is null */
if (btcursor(cbp->btp[field - 1]) == NULL) {
free(buf2);
rs = lssetcur(cbp->lsp, NULL);
if (rs == -1) {
CBEPRINT;
return -1;
}
errno = 0;
return 0;
}
/* get record position */
rs = btgetk(cbp->btp[field - 1], buf2);
if (rs == -1) {
CBEPRINT;
free(buf2);
return -1;
}
memcpy((void *)&cbrpos, (void *)((char *)buf2 +
cbp->fields[field - 1].size), sizeof(cbrpos));
/* check if key found or not */
if ((*cbcmp_p[cbp->fields[field - 1].type - 1])(buf, buf2,
cbp->fields[field - 1].size) == 0) {
found = TRUE;
} else {
found = FALSE;
}
free(buf2);
buf2 = NULL;
/* set record cursor */
lspos = cbrpos;
if (lspos == 0) {
CBEPRINT;
errno = CBEPANIC;
}
rs = lssetcur(cbp->lsp, &lspos);
if (rs == -1) {
CBEPRINT;
return -1;
}
errno = 0;
return (found ? 1 : 0);
}