home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / CBASE / CBASE.ZIP / CBKEYALI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-20  |  3.2 KB  |  151 lines

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