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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbkeyali.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.      cbkeyalign - align cbase key cursor
  17.  
  18. SYNOPSIS
  19.      #include <cbase.h>
  20.  
  21.      int cbkeyalign(cbp, field)
  22.      cbase_t *cbp;
  23.      int field;
  24.  
  25. DESCRIPTION
  26.      The cbkeyalign function aligns a key cursor with the record cursor.  The
  27.      key cursor is positioned to the key associated with the current record.
  28.  
  29.      field is the field for which to align the key cursor.
  30.  
  31.      cbkeyalign will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       cbp is not a valid cbase pointer.
  34.      [EINVAL]       field is less than 1 or greater than
  35.                     the number of fields defined for cbp.
  36.      [CBELOCK]      cbp is not locked.
  37.      [CBENKEY]      field is not a key.
  38.      [CBENOPEN]     cbp is not open.
  39.  
  40. SEE ALSO
  41.      cbkeyfirst, cbkeylast, cbkeynext, cbkeyprev, cbkeysrch.
  42.  
  43. DIAGNOSTICS
  44.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  45.      value of -1 is returned, and errno set to indicate the error.
  46.  
  47. ------------------------------------------------------------------------------*/
  48. int cbkeyalign(cbp, field)
  49. cbase_t * cbp;
  50. int       field;
  51. {
  52.     int        rs    = 0;
  53.     void *        buf    = NULL;
  54.     cbrpos_t    cbrpos    = 0;
  55.     lspos_t        lspos    = 0;
  56.  
  57.     errno = 0;
  58.  
  59.     /* validate arguments */
  60.     if (!cb_valid(cbp)) {
  61.         errno = EINVAL;
  62.         return -1;
  63.     }
  64.     if ((field < 1) || (field > cbp->fldcnt)) {
  65.         errno = EINVAL;
  66.         return -1;
  67.     }
  68.  
  69.     /* check if not open */
  70.     if (!(cbp->flags & CBOPEN)) {
  71.         errno = CBENOPEN;
  72.         return -1;
  73.     }
  74.  
  75.     /* check if field not a key */
  76.     if (!(cbp->fields[field - 1].flags & CBFKEY)) {
  77.         errno = CBENKEY;
  78.         return -1;
  79.     }
  80.  
  81.     /* check if not locked */
  82.     if (!(cbp->flags & CBLOCKS)) {
  83.         errno = CBELOCK;
  84.         return -1;
  85.     }
  86.  
  87.     /* check if record cursor is null */
  88.     if (lscursor(cbp->lsp) == NULL) {
  89.         /* set key cursor to null */
  90.         rs = btsetcur(cbp->btp[field - 1], NULL);
  91.         if (rs == -1) {
  92.             CBEPRINT;
  93.             return -1;
  94.         }
  95.         errno = 0;
  96.         return 0;
  97.     }
  98.  
  99.     /* allocate storage for key */
  100.     if (btkeysize(cbp->btp[field - 1]) !=
  101.             (cbp->fields[field - 1].size + sizeof(cbrpos_t))) {
  102.         CBEPRINT;
  103.         errno = CBEPANIC;
  104.         return -1;
  105.     }
  106.     buf = calloc(1, btkeysize(cbp->btp[field - 1]));
  107.     if (buf == NULL) {
  108.         CBEPRINT;
  109.         errno = ENOMEM;
  110.         return -1;
  111.     }
  112.  
  113.     /* get record field */
  114.     rs = cbgetrf(cbp, field, buf);
  115.     if (rs == -1) {
  116.         CBEPRINT;
  117.         free(buf);
  118.         return -1;
  119.     }
  120.  
  121.     /* get record position and place in key */
  122.     rs = lsgetcur(cbp->lsp, &lspos);
  123.     if (rs == -1) {
  124.         CBEPRINT;
  125.         free(buf);
  126.         return -1;
  127.     }
  128.     cbrpos = lspos;
  129.     memcpy((void *)((char *)buf + cbp->fields[field - 1].size),
  130.                 (void *)&cbrpos, sizeof(cbrpos_t));
  131.  
  132.     /* position key cursor */
  133.     rs = btsearch(cbp->btp[field - 1], buf);
  134.     if (rs == -1) {
  135.         CBEPRINT;
  136.         free(buf);
  137.         return -1;
  138.     }
  139.     if (rs == 0) {
  140.         free(buf);
  141.         errno = CBECORRUPT;
  142.         return -1;
  143.     }
  144.  
  145.     free(buf);
  146.     buf = NULL;
  147.  
  148.     errno = 0;
  149.     return 0;
  150. }
  151.