home *** CD-ROM | disk | FTP | other *** search
/ PC Press 1997 July / Sezamfile97_1.iso / msdos / c / cbase11.a03 / CBASE11.ZIP / LSEQ / LSINSERT.C < prev    next >
C/C++ Source or Header  |  1993-01-01  |  3KB  |  137 lines

  1. /*
  2.  *    Copyright (c) 1989-1992 Citadel Software, Inc.
  3.  *    All Rights Reserved
  4.  */
  5.  
  6. /* #ident    "@(#)lsinsert.c    1.7 - 93/01/01" */
  7.  
  8. #include <port.h>
  9.  
  10. /* standard headers */
  11. #include <errno.h>
  12. #ifdef AC_STRING
  13. #include <string.h>
  14. #endif
  15.  
  16. /* library headers */
  17. #include <blkio.h>
  18.  
  19. /* local headers */
  20. #include "lseq_.h"
  21.  
  22. /*man---------------------------------------------------------------------------
  23. NAME
  24.      lsinsert - insert record
  25.  
  26. SYNOPSIS
  27.      #include <lseq.h>
  28.  
  29.      int lsinsert(lsp, buf)
  30.      lseq_t *lsp;
  31.      const void *buf;
  32.  
  33. DESCRIPTION
  34.      The lsinsert function creates a new record following the current
  35.      record and writes the data pointed to by buf into that record.
  36.      The cursor is set to the inserted record.
  37.  
  38.      lsinsert will fail if one or more of the following is true:
  39.  
  40.      [EINVAL]       lsp is not a valid lseq pointer.
  41.      [EINVAL]       buf is the NULL pointer.
  42.      [LSELOCK]      lsp is not write locked.
  43.      [LSENOPEN]     lsp is not open.
  44.  
  45. SEE ALSO
  46.      lsdelcur, lsinsert, lssearch.
  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. #ifdef AC_PROTO
  54. int lsinsert(lseq_t *lsp, const void *buf)
  55. #else
  56. int lsinsert(lsp, buf)
  57. lseq_t *lsp;
  58. const void *buf;
  59. #endif
  60. {
  61.     int    terrno    = 0;
  62.     bpos_t    bpos    = NIL;
  63.  
  64.     /* validate arguments */
  65.     if (!ls_valid(lsp) || buf == NULL) {
  66.         errno = EINVAL;
  67.         return -1;
  68.     }
  69.  
  70.     /* check if not open */
  71.     if (!(lsp->flags & LSOPEN)) {
  72.         errno = LSENOPEN;
  73.         return -1;
  74.     }
  75.  
  76.     /* check if not write locked */
  77.     if (!(lsp->flags & LSWRLCK)) {
  78.         errno = LSELOCK;
  79.         return -1;
  80.     }
  81.  
  82.     /* build record to insert */
  83.     lsp->clsrp->prev = lsp->clspos;
  84.     if (lsp->clspos == NIL) {
  85.         lsp->clsrp->next = lsp->lshdr.first;
  86.     }
  87.     memcpy(lsp->clsrp->recbuf, buf, lsp->lshdr.recsize);
  88.  
  89.     /* get empty block from free list */
  90.     if (bflpop(lsp->bp, &bpos) == -1) {
  91.         LSERRLOG;
  92.         return -1;
  93.     }
  94.     lsp->clspos = bpos;
  95.  
  96.     /* write new record */
  97.     if (ls_rcput(lsp, lsp->clspos, lsp->clsrp) == -1) {
  98.         LSERRLOG;
  99.         terrno = errno;
  100.         bpos = lsp->clspos;
  101.         bflpush(lsp->bp, &bpos);
  102.         errno = terrno;
  103.         return -1;
  104.     }
  105.  
  106.     /* increment record count */
  107.     lsp->lshdr.reccnt++;
  108.  
  109.     /* fix links */
  110.     if (lsp->clsrp->next == NIL) {
  111.         lsp->lshdr.last = lsp->clspos;
  112.     } else {
  113.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->next, offsetof(lsrec_t, prev), &lsp->clspos, sizeof(lsp->clsrp->prev)) == -1) {
  114.             LSERRLOG;
  115.             terrno = errno;
  116.             bpos = lsp->clspos;
  117.             bflpush(lsp->bp, &bpos);
  118.             errno = terrno;
  119.             return -1;
  120.         }
  121.     }
  122.     if (lsp->clsrp->prev == NIL) {
  123.         lsp->lshdr.first = lsp->clspos;
  124.     } else {
  125.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->prev, offsetof(lsrec_t, next), &lsp->clspos, sizeof(lsp->clsrp->next)) == -1) {
  126.             LSERRLOG;
  127.             terrno = errno;
  128.             bpos = lsp->clspos;
  129.             bflpush(lsp->bp, &bpos);
  130.             errno = terrno;
  131.             return -1;
  132.         }
  133.     }
  134.  
  135.     return 0;
  136. }
  137.