home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
LSEQ.ZIP
/
LSSEARCH.C
< prev
next >
Wrap
Text File
|
1989-08-30
|
3KB
|
129 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "lssearch.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <bool.h>
#include <errno.h>
/* #include <string.h> */
#include "lseq_.h"
/*man---------------------------------------------------------------------------
NAME
lssearch - lseq search
SYNOPSIS
#include <lseq.h>
int lssearch(lsp, offset, buf, bufsize, cmp_p)
lseq_t *lsp;
size_t offset;
void *buf;
size_t bufsize;
int (*cmp_p)();
DESCRIPTION
The lssearch function performs a linear search through lseq lsp for
a record with a field matching the field pointed to by buf. The field
being searched begins offset characters from the beginning of the
record and is bufsize characters long. The function pointed to by
cmp_p is used to test for a match. The search begins on the record
following the current record, so before the first call to lssearch the
cursor should be set to null, and successive calls will find succesive
records with fields matching buf. If the field is matched, the cursor
is left on the record with the matching field.
The user supplied comparison function cmp_p must be of the following
form:
int f(const void *s1, const void *s2, size_t n);
where s1 and s2 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 s1 is less than, equal to, or greater than s2, respectively.
If cmp_p is NULL then the memcmp function is used as the default.
lssearch will fail if one or more of the following is true:
[EINVAL] lsp is not a valid lseq pointer.
[EINVAL] buf is the NULL pointer.
[EINVAL] bufsize is less than 1.
[LSEBOUND] offset + bufsize extends beyond the
end of the record.
[LSELOCK] lsp is not read locked.
[LSENOPEN] lsp is not open.
SEE ALSO
lscursor.
DIAGNOSTICS
Upon successful completion, a value of 1 is returned if the field was
matched 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 lssearch(lsp, offset, buf, bufsize, cmp_p)
lseq_t * lsp;
size_t offset;
void * buf;
size_t bufsize;
int (*cmp_p)();
{
int rs = 0;
bool found = FALSE;
errno = 0;
/* validate arguments */
if ((!ls_valid(lsp)) || (buf == NULL) || (bufsize < 1)) {
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(lsp->flags & LSOPEN)) {
errno = LSENOPEN;
return -1;
}
/* check if not read locked */
if (!(lsp->flags & LSRDLCK)) {
errno = LSELOCK;
return -1;
}
/* check if over record boundary */
if ((offset + bufsize) > lsp->lshdr.recsize) {
errno = LSEBOUND;
return -1;
}
/* check if cmp_p is NULL */
if (cmp_p == NULL) {
cmp_p = memcmp;
}
/* advance cursor one record */
rs = lsrecnext(lsp);
if (rs == -1) {
LSEPRINT;
return -1;
}
while (lsp->clspos != 0) {
if ((*cmp_p)((void *)((char *)lsp->clsrp->recbuf + offset), buf, bufsize) == 0) {
found = TRUE;
break;
}
rs = lsrecnext(lsp);
if (rs == -1) {
LSEPRINT;
return -1;
}
}
errno = 0;
return (found ? 1 : 0);
}