home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
cbase.zip
/
CBASE10B.ZIP
/
CBASE.ZIP
/
CBKEYSRC.C
< prev
next >
Wrap
Text File
|
1989-11-08
|
4KB
|
161 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "cbkeysrc.c 1.2 - 89/11/08" */
#include <blkio.h>
#include <bool.h>
#include <btree.h>
#include <errno.h>
#include <lseq.h>
/*#include <stddef.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;
const void *buf;
DESCRIPTION
The cbkeysrch function searches a key in cbase cbp. field is the
field to be searched. buf points to the key for which to be
searched. If found, the cursor for that key is positioned to the
found key. 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. Other key cursors are not affected.
cbkeysrch will fail if one or more of the following is true:
[EINVAL] cbp is not a valid cbase pointer.
[EINVAL] field is not a valid field number for
cbase 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 set to indicate the error.
------------------------------------------------------------------------------*/
int cbkeysrch(cbp, field, buf)
cbase_t *cbp;
int field;
CONST void *buf;
{
void *buf2 = NULL;
cbrpos_t cbrpos = 0;
lspos_t lspos = 0;
bool found = FALSE;
/* validate arguments */
if (!cb_valid(cbp) || (buf == NULL)) {
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(cbp->flags & CBOPEN)) {
errno = CBENOPEN;
return -1;
}
/* validate arguments */
if ((field < 0) || (field >= cbp->fldc)) {
errno = EINVAL;
return -1;
}
/* check if field is a key */
if (!(cbp->fldv[field].flags & CB_FKEY)) {
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->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
CBEPRINT;
errno = CBEPANIC;
return -1;
}
buf2 = calloc((size_t)1, cbp->fldv[field].len + sizeof(cbrpos_t));
if (buf2 == NULL) {
CBEPRINT;
errno = ENOMEM;
return -1;
}
cbrpos = 0;
memcpy(buf2, buf, cbp->fldv[field].len);
memcpy(((char *)buf2 + cbp->fldv[field].len), &cbrpos, sizeof(cbrpos_t));
/* search for key */
if (btsearch(cbp->btpv[field], buf2) == -1) {
CBEPRINT;
free(buf2);
return -1;
}
/* check if cursor is null */
if (btcursor(cbp->btpv[field]) == NULL) {
free(buf2);
if (lssetcur(cbp->lsp, NULL) == -1) {
CBEPRINT;
return -1;
}
errno = 0;
return 0;
}
/* get record position */
if (btgetk(cbp->btpv[field], buf2) == -1) {
CBEPRINT;
free(buf2);
return -1;
}
memcpy(&cbrpos, ((char *)buf2 + cbp->fldv[field].len), sizeof(cbrpos));
/* check if key found or not */
if ((*cbcmpv[cbp->fldv[field].type])(buf, buf2, cbp->fldv[field].len) == 0) {
found = TRUE;
} else {
found = FALSE;
}
free(buf2);
buf2 = NULL;
/* set record cursor */
lspos = cbrpos;
if (lspos == 0) {
CBEPRINT;
errno = CBEPANIC;
}
if (lssetcur(cbp->lsp, &lspos) == -1) {
CBEPRINT;
return -1;
}
errno = 0;
return (found ? 1 : 0);
}