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 >
Text File  |  1989-08-30  |  3KB  |  129 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "lssearch.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <bool.h>
  8. #include <errno.h>
  9. /* #include <string.h> */
  10. #include "lseq_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      lssearch - lseq search
  15.  
  16. SYNOPSIS
  17.      #include <lseq.h>
  18.  
  19.      int lssearch(lsp, offset, buf, bufsize, cmp_p)
  20.      lseq_t *lsp;
  21.      size_t offset;
  22.      void *buf;
  23.      size_t bufsize;
  24.      int (*cmp_p)();
  25.  
  26. DESCRIPTION
  27.      The lssearch function performs a linear search through lseq lsp for
  28.      a record with a field matching the field pointed to by buf.  The field
  29.      being searched begins offset characters from the beginning of the
  30.      record and is bufsize characters long.  The function pointed to by
  31.      cmp_p is used to test for a match.  The search begins on the record
  32.      following the current record, so before the first call to lssearch the
  33.      cursor should be set to null, and successive calls will find succesive
  34.      records with fields matching buf.  If the field is matched, the cursor
  35.      is left on the record with the matching field.
  36.  
  37.      The user supplied comparison function cmp_p must be of the following
  38.      form:
  39.  
  40.           int f(const void *s1, const void *s2, size_t n);
  41.  
  42.      where s1 and s2 point to the two keys to bey compared and n is the
  43.      key size.  The return must be less than, equal to, or greater than
  44.      zero if s1 is less than, equal to, or greater than s2, respectively.
  45.      If cmp_p is NULL then the memcmp function is used as the default.
  46.  
  47.      lssearch will fail if one or more of the following is true:
  48.  
  49.      [EINVAL]       lsp is not a valid lseq pointer.
  50.      [EINVAL]       buf is the NULL pointer.
  51.      [EINVAL]       bufsize is less than 1.
  52.      [LSEBOUND]     offset + bufsize extends beyond the
  53.                     end of the record.
  54.      [LSELOCK]      lsp is not read locked.
  55.      [LSENOPEN]     lsp is not open.
  56.  
  57. SEE ALSO
  58.      lscursor.
  59.  
  60. DIAGNOSTICS
  61.      Upon successful completion, a value of 1 is returned if the field was
  62.      matched or a value of 0 if it was not.  Otherwise, a value of -1 is
  63.      returned and errno is set to indicate the error.
  64.  
  65. ------------------------------------------------------------------------------*/
  66. int lssearch(lsp, offset, buf, bufsize, cmp_p)
  67. lseq_t * lsp;
  68. size_t   offset;
  69. void *   buf;
  70. size_t   bufsize;
  71. int      (*cmp_p)();
  72. {
  73.     int    rs    = 0;
  74.     bool    found    = FALSE;
  75.  
  76.     errno = 0;
  77.  
  78.     /* validate arguments */
  79.     if ((!ls_valid(lsp)) || (buf == NULL) || (bufsize < 1)) {
  80.         errno = EINVAL;
  81.         return -1;
  82.     }
  83.  
  84.     /* check if not open */
  85.     if (!(lsp->flags & LSOPEN)) {
  86.         errno = LSENOPEN;
  87.         return -1;
  88.     }
  89.  
  90.     /* check if not read locked */
  91.     if (!(lsp->flags & LSRDLCK)) {
  92.         errno = LSELOCK;
  93.         return -1;
  94.     }
  95.  
  96.     /* check if over record boundary */
  97.     if ((offset + bufsize) > lsp->lshdr.recsize) {
  98.         errno = LSEBOUND;
  99.         return -1;
  100.     }
  101.  
  102.     /* check if cmp_p is NULL */
  103.     if (cmp_p == NULL) {
  104.         cmp_p = memcmp;
  105.     }
  106.  
  107.     /* advance cursor one record */
  108.     rs = lsrecnext(lsp);
  109.     if (rs == -1) {
  110.         LSEPRINT;
  111.         return -1;
  112.     }
  113.  
  114.     while (lsp->clspos != 0) {
  115.         if ((*cmp_p)((void *)((char *)lsp->clsrp->recbuf + offset), buf, bufsize) == 0) {
  116.             found = TRUE;
  117.             break;
  118.         }
  119.         rs = lsrecnext(lsp);
  120.         if (rs == -1) {
  121.             LSEPRINT;
  122.             return -1;
  123.         }
  124.     }
  125.  
  126.     errno = 0;
  127.     return (found ? 1 : 0);
  128. }
  129.