home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR41 / CBASE11.ZIP / LSSEARCH.C < prev    next >
C/C++ Source or Header  |  1993-01-01  |  4KB  |  145 lines

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