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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbdelcur.c    1.1 - 89/08/31" */
  5.  
  6. #include <errno.h>
  7. /* #include <stdlib.h> */
  8. /* #include <string.h> */
  9. #include "cbase_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      cbdelcur - delete current cbase record
  14.  
  15. SYNOPSIS
  16.      #include <cbase.h>
  17.  
  18.      int cbdelcur(cbp)
  19.      cbase_t *cbp;
  20.  
  21. DESCRIPTION
  22.      The cbdelcur function deletes the current record of cbase cbp.  The
  23.      record cursor is positioned to the record following the deleted record.
  24.  
  25.      cbdelcur will fail if one or more of the following is true:
  26.  
  27.      [EINVAL]       cbp is not a valid cbase pointer.
  28.      [CBELOCK]      cbp is not write locked.
  29.      [CBENOPEN]     cbp is not open.
  30.      [CBENREC]      The record cursor of cbp is null.
  31.  
  32. SEE ALSO
  33.      cbinscur, cbrcursor.
  34.  
  35. DIAGNOSTICS
  36.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  37.      value of -1 is returned, and errno set to indicate the error.
  38. ------------------------------------------------------------------------------*/
  39. int cbdelcur(cbp)
  40. cbase_t * cbp;
  41. {
  42.     int        rs    = 0;
  43.     lspos_t        lspos    = 0;
  44.     cbrpos_t    cbrpos    = 0;
  45.     void *        buf    = NULL;
  46.     int        i    = 0;
  47.  
  48.     errno = 0;
  49.  
  50.     /* validate arguments */
  51.     if (!cb_valid(cbp)) {
  52.         errno = EINVAL;
  53.         return -1;
  54.     }
  55.  
  56.     /* check if not open */
  57.     if (!(cbp->flags & CBOPEN)) {
  58.         errno = CBENOPEN;
  59.         return -1;
  60.     }
  61.  
  62.     /* check if not write locked */
  63.     if (!(cbp->flags & CBWRLCK)) {
  64.         errno = CBELOCK;
  65.         return -1;
  66.     }
  67.  
  68.     /* check if record cursor is null */
  69.     if (lscursor(cbp->lsp) == NULL) {
  70.         errno = CBENREC;
  71.         return -1;
  72.     }
  73.  
  74.     /* get record position */
  75.     rs = lsgetcur(cbp->lsp, &lspos);
  76.     if (rs == -1) {
  77.         CBEPRINT;
  78.         return -1;
  79.     }
  80.     cbrpos = lspos;
  81.  
  82.     /* delete keys */
  83.     for (i = 0; i < cbp->fldcnt; i++) {
  84.         if (cbp->fields[i].flags & CBFKEY) {
  85.             buf = calloc(1, cbp->fields[i].size + sizeof(cbrpos_t));
  86.             if (buf == NULL) {
  87.                 CBEPRINT;
  88.                 errno = ENOMEM;
  89.                 return -1;
  90.             }
  91.             rs = lsgetrf(cbp->lsp, cbp->fields[i].offset,
  92.                         buf, cbp->fields[i].size);
  93.             if (rs == -1) {
  94.                 CBEPRINT;
  95.                 free(buf);
  96.                 return -1;
  97.             }
  98.             memcpy((void *)((char *)buf + cbp->fields[i].size),
  99.                     (void *)&cbrpos, sizeof(cbrpos_t));
  100.             rs = btdelete(cbp->btp[i], buf);
  101.             if (rs == -1) {
  102.                 CBEPRINT;
  103.                 free(buf);
  104.                 return -1;
  105.             }
  106.             free(buf);
  107.             buf = NULL;
  108.         }
  109.     }
  110.  
  111.     /* delete current record */
  112.     rs = lsdelcur(cbp->lsp);
  113.     if (rs == -1) {
  114.         CBEPRINT;
  115.         return -1;
  116.     }
  117.  
  118.     errno = 0;
  119.     return 0;
  120. }
  121.