home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / cbase.zip / CBASE10B.ZIP / CBASE.ZIP / CBINSERT.C < prev    next >
Text File  |  1989-11-08  |  2KB  |  87 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbinsert.c    1.2 - 89/11/08" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. #include <lseq.h>
  9. #include "cbase_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      cbinsert - cbase insert
  14.  
  15. SYNOPSIS
  16.      #include <cbase.h>
  17.  
  18.      int cbinsert(cbp, buf)
  19.      cbase_t *cbp;
  20.      const void *buf;
  21.  
  22. DESCRIPTION
  23.      The cbinsert function inserts the record pointed to by buf into
  24.      cbase cbp.  The record is inserted as the last record.  The
  25.      record cursor and all key cursors are set to the inserted record.
  26.  
  27.      cbinsert will fail if one or more of the following is true:
  28.  
  29.      [EINVAL]       cbp is not a valid cbase pointer.
  30.      [EINVAL]       buf is the NULL pointer.
  31.      [CBEDUP]       A field in the record pointed to by
  32.                     buf contains an illegal duplicate key.
  33.      [CBELOCK]      cbp is not write locked.
  34.      [CBENOPEN]     cbp is not open.
  35.  
  36. SEE ACBO
  37.      cbdelcur, cbinscur.
  38.  
  39. DIAGNOSTICS
  40.      Upon successful completion, a value of 0 is returned.  Otherwise,
  41.      a value of -1 is returned, and errno set to indicate the error.
  42.  
  43. ------------------------------------------------------------------------------*/
  44. int cbinsert(cbp, buf)
  45. cbase_t *cbp;
  46. CONST void *buf;
  47. {
  48.     /* validate arguments */
  49.     if (!cb_valid(cbp) || (buf == NULL)) {
  50.         errno = EINVAL;
  51.         return -1;
  52.     }
  53.  
  54.     /* check if not open */
  55.     if (!(cbp->flags & CBOPEN)) {
  56.         errno = CBENOPEN;
  57.         return -1;
  58.     }
  59.  
  60.     /* check if not write locked */
  61.     if (!(cbp->flags & CBWRLCK)) {
  62.         errno = CBELOCK;
  63.         return -1;
  64.     }
  65.  
  66.     /* set record cursor to last record */
  67.     if (lsreccnt(cbp->lsp) == 0) {
  68.         if (lssetcur(cbp->lsp, NULL) == -1) {
  69.             CBEPRINT;
  70.             return -1;
  71.         }
  72.     } else {
  73.         if (lslast(cbp->lsp) == -1) {
  74.             CBEPRINT;
  75.             return -1;
  76.         }
  77.     }
  78.  
  79.     /* insert new record */
  80.     if (cbinscur(cbp, buf) == -1) {
  81.         return -1;
  82.     }
  83.  
  84.     errno = 0;
  85.     return 0;
  86. }
  87.