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

  1. /*
  2.  *    Copyright (c) 1989-1992 Citadel Software, Inc.
  3.  *    All Rights Reserved
  4.  */
  5.  
  6. /* #ident    "@(#)cbkeypre.c    1.7 - 93/01/01" */
  7.  
  8. #include <port.h>
  9.  
  10. /* standard headers */
  11. #include <errno.h>
  12. #ifdef AC_STDLIB
  13. #include <stdlib.h>
  14. #endif
  15. #ifdef AC_STRING
  16. #include <string.h>
  17. #endif
  18.  
  19. /* library headers */
  20. #include <blkio.h>
  21. #include <btree.h>
  22. #include <lseq.h>
  23. #include <xtend.h>
  24.  
  25. /* local headers */
  26. #include "cbase_.h"
  27.  
  28. /*man---------------------------------------------------------------------------
  29. NAME
  30.      cbkeyprev - previous cbase key
  31.  
  32. SYNOPSIS
  33.      #include <cbase.h>
  34.  
  35.      int cbkeyprev(cbp, field)
  36.      cbase_t *cbp;
  37.      int field;
  38.  
  39. DESCRIPTION
  40.      The cbkeyprev function retreats the key cursor of the specified
  41.      field in cbase cbp by one.  The record cursor of cbp is
  42.      positioned to the record associated with that key.  Other key
  43.      cursors are not affected.
  44.  
  45.      cbkeyprev will fail if one or more of the following is true:
  46.  
  47.      [EINVAL]       cbp is not a valid cbase pointer.
  48.      [EINVAL]       field is not a valid field number for
  49.                     cbase cbp.
  50.      [CBELOCK]      cbp is not locked.
  51.      [CBENKEY]      field is not a key.
  52.      [CBENOPEN]     cbp is not open.
  53.  
  54. SEE ALSO
  55.      cbkcursor, cbkeyfirst, cbkeylast, cbkeynext, cbrecprev.
  56.  
  57. DIAGNOSTICS
  58.      Upon successful completion, a value of 0 is returned.  Otherwise,
  59.      a value of -1 is returned, and errno set to indicate the error.
  60.  
  61. ------------------------------------------------------------------------------*/
  62. #ifdef AC_PROTO
  63. int cbkeyprev(cbase_t *cbp, int field)
  64. #else
  65. int cbkeyprev(cbp, field)
  66. cbase_t *cbp;
  67. int field;
  68. #endif
  69. {
  70.     void *    buf    = NULL;
  71.     cbrpos_t cbrpos    = NIL;
  72.     lspos_t    lspos    = NIL;
  73.  
  74.     /* validate arguments */
  75.     if (!cb_valid(cbp)) {
  76.         errno = EINVAL;
  77.         return -1;
  78.     }
  79.  
  80.     /* check if not open */
  81.     if (!(cbp->flags & CBOPEN)) {
  82.         errno = CBENOPEN;
  83.         return -1;
  84.     }
  85.  
  86.     /* validate arguments */
  87.     if (field < 0 || field >= cbp->fldc) {
  88.         errno = EINVAL;
  89.         return -1;
  90.     }
  91.  
  92.     /* check if field not a key */
  93.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  94.         errno = CBENKEY;
  95.         return -1;
  96.     }
  97.  
  98.     /* check if not locked */
  99.     if (!(cbp->flags & CBLOCKS)) {
  100.         errno = CBELOCK;
  101.         return -1;
  102.     }
  103.  
  104.     /* set key cursor to previous key */
  105.     if (btprev(cbp->btpv[field]) == -1) {
  106.         CBERRLOG;
  107.         return -1;
  108.     }
  109.  
  110.     /* check if cursor is null */
  111.     if (btcursor(cbp->btpv[field]) == NULL) {
  112.         /* set record cursor to null */
  113.         if (lssetcur(cbp->lsp, NULL) == -1) {
  114.             CBERRLOG;
  115.             return -1;
  116.         }
  117.         return 0;
  118.     }
  119.  
  120.     /* get record position */
  121.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  122.         CBERRLOG;
  123.         errno = CBEFATAL;
  124.         return -1;
  125.     }
  126.     buf = xcalloc((size_t)1, btkeysize(cbp->btpv[field]));
  127.     if (buf == NULL) {
  128.         CBERRLOG;
  129.         errno = ENOMEM;
  130.         return -1;
  131.     }
  132.     if (btgetk(cbp->btpv[field], buf) == -1) {
  133.         CBERRLOG;
  134.         xfree(buf);
  135.         return -1;
  136.     }
  137.     memcpy(&cbrpos, (char *)buf + cbp->fldv[field].len, sizeof(cbrpos));
  138.     xfree(buf);
  139.     buf = NULL;
  140.  
  141.     /* set record cursor */
  142.     lspos = cbrpos;
  143.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  144.         CBERRLOG;
  145.         return -1;
  146.     }
  147.  
  148.     return 0;
  149. }
  150.