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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbinscur.c    1.2 - 89/11/08" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. #include <lseq.h>
  9. /*#include <stddef.h>*/
  10. /*#include <stdlib.h>*/
  11. /*#include <string.h>*/
  12. #include "cbase_.h"
  13.  
  14. /*man---------------------------------------------------------------------------
  15. NAME
  16.      cbinscur - insert into cbase after current record
  17.  
  18. SYNOPSIS
  19.      #include <cbase.h>
  20.  
  21.      int cbinscur(cbp, buf)
  22.      cbase_t *cbp;
  23.      void *buf;
  24.  
  25. DESCRIPTION
  26.      The cbinscur function inserts the record pointed to by buf into
  27.      cbase cbp.  The record is inserted after the current record.  The
  28.      record cursor and all the key cursors are set to the inserted
  29.      record.
  30.  
  31.      cbinscur will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       cbp is not a valid cbase pointer.
  34.      [EINVAL]       buf is the NULL pointer.
  35.      [CBEDUP]       A field in the record pointed to by buf
  36.                     contains an illegal duplicate key.
  37.      [CBELOCK]      cbp is not write locked.
  38.      [CBENOPEN]     cbp is not open.
  39.  
  40. SEE ACBO
  41.      cbdelete, lssearch.
  42.  
  43. DIAGNOSTICS
  44.      Upon successful completion, a value of 0 is returned.  Otherwise,
  45.      a value of -1 is returned, and errno set to indicate the error.
  46.  
  47. ------------------------------------------------------------------------------*/
  48. int cbinscur(cbp, buf)
  49. cbase_t *cbp;
  50. void *buf;
  51. {
  52.     void *buf2 = NULL;
  53.     cbrpos_t cbrpos = 0;
  54.     lspos_t lspos = 0;
  55.     int i = 0;
  56.     int found = 0;
  57.  
  58.     /* validate arguments */
  59.     if (!cb_valid(cbp) || (buf == NULL)) {
  60.         errno = EINVAL;
  61.         return -1;
  62.     }
  63.  
  64.     /* check if not open */
  65.     if (!(cbp->flags & CBOPEN)) {
  66.         errno = CBENOPEN;
  67.         return -1;
  68.     }
  69.  
  70.     /* check if not write locked */
  71.     if (!(cbp->flags & CBWRLCK)) {
  72.         errno = CBELOCK;
  73.         return -1;
  74.     }
  75.  
  76.     /* check for illegal duplicate keys */
  77.     if (cbgetrcur(cbp, &cbrpos) == -1) {    /* save current record pos */
  78.         CBEPRINT;
  79.         return -1;
  80.     }
  81.     for (i = 0; i < cbp->fldc; i++) {
  82.         if (cbp->fldv[i].flags & CB_FKEY) {
  83.             if (cbp->fldv[i].flags & CB_FUNIQ) {
  84.                 buf2 = calloc((size_t)1, cbp->fldv[i].len);
  85.                 if (buf2 == NULL) {
  86.                     CBEPRINT;
  87.                     errno = ENOMEM;
  88.                     return -1;
  89.                 }
  90.                 memcpy(buf2, ((char *)buf + cbp->fldv[i].offset), cbp->fldv[i].len);
  91.                 found = cbkeysrch(cbp, i, buf2);
  92.                 if (found == -1) {
  93.                     CBEPRINT;
  94.                     free(buf2);
  95.                     return -1;
  96.                 }
  97.                 if (found == 1) {
  98.                     free(buf2);
  99.                     errno = CBEDUP;
  100.                     return -1;
  101.                 }
  102.                 free(buf2);
  103.                 buf2 = NULL;
  104.             }
  105.         }
  106.     }
  107.     if (cbsetrcur(cbp, &cbrpos) == -1) {    /* restore record pos */
  108.         CBEPRINT;
  109.         return -1;
  110.     }
  111.  
  112.     /* insert record */
  113.     if (lsinscur(cbp->lsp, buf) == -1) {
  114.         CBEPRINT;
  115.         return -1;
  116.     }
  117.  
  118.     /* get position of inserted record */
  119.     if (lsgetcur(cbp->lsp, &lspos) == -1) {
  120.         CBEPRINT;
  121.         return -1;
  122.     }
  123.     cbrpos = lspos;
  124.  
  125.     /* insert keys */
  126.     for (i = 0; i < cbp->fldc; i++) {
  127.         if (cbp->fldv[i].flags & CB_FKEY) {
  128.             /* construct (key, record postion) pair */
  129.             if (btkeysize(cbp->btpv[i]) != (cbp->fldv[i].len + sizeof(cbrpos_t))) {
  130.                 CBEPRINT;
  131.                 errno = CBEPANIC;
  132.                 return -1;
  133.             }
  134.             buf2 = calloc((size_t)1, cbp->fldv[i].len + sizeof(cbrpos_t));
  135.             if (buf2 == NULL) {
  136.                 CBEPRINT;
  137.                 errno = ENOMEM;
  138.                 return -1;
  139.             }
  140.             memcpy(buf2, ((char *)buf + cbp->fldv[i].offset), cbp->fldv[i].len);
  141.             memcpy(((char *)buf2 + cbp->fldv[i].len), &cbrpos, sizeof(cbrpos_t));
  142.             /* insert key */
  143.             if (btinsert(cbp->btpv[i], buf2) == -1) {
  144.                 CBEPRINT;
  145.                 free(buf2);
  146.                 return -1;
  147.             }
  148.             free(buf2);
  149.             buf2 = NULL;
  150.         }
  151.     }
  152.  
  153.     errno = 0;
  154.     return 0;
  155. }
  156.