home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload
/
ShartewareOverload.cdr
/
progm
/
cbase.zip
/
CBASE10B.ZIP
/
CBASE.ZIP
/
CBKEYALI.C
< prev
next >
Wrap
Text File
|
1989-11-08
|
3KB
|
146 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "cbkeyali.c 1.2 - 89/11/08" */
#include <blkio.h>
#include <btree.h>
#include <errno.h>
#include <lseq.h>
/*#include <stdlib.h>*/
/*#include <string.h>*/
#include "cbase_.h"
/*man---------------------------------------------------------------------------
NAME
cbkeyalign - align cbase key cursor
SYNOPSIS
#include <cbase.h>
int cbkeyalign(cbp, field)
cbase_t *cbp;
int field;
DESCRIPTION
The cbkeyalign function aligns a key cursor in cbase cbp with the
record cursor. The key cursor is positioned to the key
associated with the current record. Other cursors are not
affected.
cbkeyalign 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.
[CBELOCK] cbp is not locked.
[CBENKEY] field is not a key.
[CBENOPEN] cbp is not open.
SEE ALSO
cbkeyfirst, cbkeylast, cbkeynext, cbkeyprev, cbkeysrch.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise,
a value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int cbkeyalign(cbp, field)
cbase_t *cbp;
int field;
{
void *buf = NULL;
cbrpos_t cbrpos = 0;
lspos_t lspos = 0;
int found = 0;
/* validate arguments */
if (!cb_valid(cbp)) {
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 not a key */
if (!(cbp->fldv[field].flags & CB_FKEY)) {
errno = CBENKEY;
return -1;
}
/* check if not locked */
if (!(cbp->flags & CBLOCKS)) {
errno = CBELOCK;
return -1;
}
/* check if record cursor is null */
if (lscursor(cbp->lsp) == NULL) {
/* set key cursor to null */
if (btsetcur(cbp->btpv[field], NULL) == -1) {
CBEPRINT;
return -1;
}
errno = 0;
return 0;
}
/* allocate storage for key */
if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
CBEPRINT;
errno = CBEPANIC;
return -1;
}
buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
if (buf == NULL) {
CBEPRINT;
errno = ENOMEM;
return -1;
}
/* get record field */
if (cbgetrf(cbp, field, buf) == -1) {
CBEPRINT;
free(buf);
return -1;
}
/* get record position and place in key */
if (lsgetcur(cbp->lsp, &lspos) == -1) {
CBEPRINT;
free(buf);
return -1;
}
cbrpos = lspos;
memcpy(((char *)buf + cbp->fldv[field].len), &cbrpos, sizeof(cbrpos_t));
/* position key cursor */
found = btsearch(cbp->btpv[field], buf);
if (found == -1) {
CBEPRINT;
free(buf);
return -1;
}
if (found == 0) {
free(buf);
errno = CBECORRUPT;
return -1;
}
free(buf);
buf = NULL;
errno = 0;
return 0;
}