home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / CBASE.ZIP / CBKEYLAS.C < prev    next >
Text File  |  1989-08-31  |  3KB  |  137 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbkeylas.c    1.1 - 89/08/31" */
  5.  
  6. #include <blkio.h>
  7. #include <btree.h>
  8. #include <errno.h>
  9. #include <lseq.h>
  10. /* #include <stdlib.h> */
  11. /* #include <string.h> */
  12. #include "cbase_.h"
  13.  
  14. /*man---------------------------------------------------------------------------
  15. NAME
  16.      cbkeylast - last cbase key
  17.  
  18. SYNOPSIS
  19.      #include <cbase.h>
  20.  
  21.      int cbkeylast(cbp, field)
  22.      cbase_t *cbp;
  23.      int field;
  24.  
  25. DESCRIPTION
  26.      The cbkeylast function positions a key cursor for cbase cbp to the
  27.      last key.  The record cursor of cbp is positioned to the record
  28.      associated with that key.
  29.  
  30.      field is the field for which to set the key cursor.
  31.  
  32.      cbkeylast will fail if one or more of the following is true:
  33.  
  34.      [EINVAL]       cbp is not a valid cbase pointer.
  35.      [EINVAL]       field is less than 1 or greater than
  36.                     the number of fields defined for cbp.
  37.      [CBELOCK]      cbp is not locked.
  38.      [CBENKEY]      field is not a key.
  39.      [CBENOPEN]     cbp is not open.
  40.      [CBENREC]      cbp is empty.
  41.  
  42. SEE ALSO
  43.      cbkeyfirst, cbkeynext, cbkeyprev, cbreccnt, cbreclast.
  44.  
  45. DIAGNOSTICS
  46.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  47.      value of -1 is returned, and errno set to indicate the error.
  48.  
  49. ------------------------------------------------------------------------------*/
  50. int cbkeylast(cbp, field)
  51. cbase_t * cbp;
  52. int       field;
  53. {
  54.     int        rs    = 0;
  55.     void *        buf    = NULL;
  56.     cbrpos_t    cbrpos    = 0;
  57.     lspos_t        lspos    = 0;
  58.  
  59.     errno = 0;
  60.  
  61.     /* validate arguments */
  62.     if (!cb_valid(cbp)) {
  63.         errno = EINVAL;
  64.         return -1;
  65.     }
  66.     if ((field < 1) || (field > cbp->fldcnt)) {
  67.         errno = EINVAL;
  68.         return -1;
  69.     }
  70.  
  71.     /* check if not open */
  72.     if (!(cbp->flags & CBOPEN)) {
  73.         errno = CBENOPEN;
  74.         return -1;
  75.     }
  76.  
  77.     /* check if field not a key */
  78.     if (!(cbp->fields[field - 1].flags & CBFKEY)) {
  79.         errno = CBENKEY;
  80.         return -1;
  81.     }
  82.  
  83.     /* check if not locked */
  84.     if (!(cbp->flags & CBLOCKS)) {
  85.         errno = CBELOCK;
  86.         return -1;
  87.     }
  88.  
  89.     /* check if cbase empty */
  90.     if (lsreccnt(cbp->lsp) == 0) {
  91.         errno = CBENREC;
  92.         return -1;
  93.     }
  94.  
  95.     /* set key cursor to last key */
  96.     rs = btlast(cbp->btp[field - 1]);
  97.     if (rs == -1) {
  98.         CBEPRINT;
  99.         return -1;
  100.     }
  101.  
  102.     /* get record position */
  103.     if (btkeysize(cbp->btp[field - 1]) !=
  104.             (cbp->fields[field - 1].size + sizeof(lspos_t))) {
  105.         CBEPRINT;
  106.         errno = CBEPANIC;
  107.         return -1;
  108.     }
  109.     buf = calloc(1, btkeysize(cbp->btp[field - 1]));
  110.     if (buf == NULL) {
  111.         CBEPRINT;
  112.         errno = ENOMEM;
  113.         return -1;
  114.     }
  115.     rs = btgetk(cbp->btp[field - 1], buf);
  116.     if (rs == -1) {
  117.         CBEPRINT;
  118.         free(buf);
  119.         return -1;
  120.     }
  121.     memcpy((void *)&cbrpos, (void *)((char *)buf +
  122.                 cbp->fields[field - 1].size), sizeof(cbrpos));
  123.     free(buf);
  124.     buf = NULL;
  125.  
  126.     /* set record cursor */
  127.     lspos = cbrpos;
  128.     rs = lssetcur(cbp->lsp, &lspos);
  129.     if (rs == -1) {
  130.         CBEPRINT;
  131.         return -1;
  132.     }
  133.  
  134.     errno = 0;
  135.     return 0;
  136. }
  137.