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

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