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

  1. /*
  2.  *    Copyright (c) 1989-1992 Citadel Software, Inc.
  3.  *    All Rights Reserved
  4.  */
  5.  
  6. /* #ident    "@(#)rcops.c    1.7 - 93/01/01" */
  7.  
  8. #include <port.h>
  9.  
  10. /* standard headers */
  11. #include <errno.h>
  12. #ifdef AC_STDDEF
  13. #include <stddef.h>
  14. #endif
  15. #ifdef AC_STDLIB
  16. #include <stdlib.h>
  17. #endif
  18. #ifdef AC_STRING
  19. #include <string.h>
  20. #endif
  21.  
  22. /* library headers */
  23. #include <blkio.h>
  24. #include <xtend.h>
  25.  
  26. /* local headers */
  27. #include "lseq_.h"
  28.  
  29. /*man---------------------------------------------------------------------------
  30. NAME
  31.      ls_rcalloc - allocate memory for lseq record
  32.  
  33. SYNOPSIS
  34.      #include "lseq_.h"
  35.  
  36.      lsrec_t *ls_rcalloc(lsp)
  37.      lseq_t *lsp;
  38.  
  39. DESCRIPTION
  40.      The ls_rcalloc function creates a record of the appropriate
  41.      configuration for lseq lsp and initializes it.  The address of
  42.      the record created is returned.
  43.  
  44.      ls_rcalloc will fail if one or more of the following is true:
  45.  
  46.      [EINVAL]       lsp is not a valid lseq pointer.
  47.      [ENOMEM]       Not enough memory is available for
  48.                     allocation by the calling process.
  49.      [LSENOPEN]     lsp is not open.
  50.  
  51. SEE ALSO
  52.      ls_rcfree, ls_rcinit.
  53.  
  54. DIAGNOSTICS
  55.      On failure, a value of NULL is returned, and errno set to
  56.      indicate the error.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. #ifdef AC_PROTO
  60. lsrec_t *ls_rcalloc(lseq_t *lsp)
  61. #else
  62. lsrec_t *ls_rcalloc(lsp)
  63. lseq_t *lsp;
  64. #endif
  65. {
  66.     lsrec_t *lsrp = NULL;
  67. #ifdef DEBUG
  68.     /* validate arguments */
  69.     if (!ls_valid(lsp)) {
  70.         LSERRLOG;
  71.         errno = EINVAL;
  72.         return NULL;
  73.     }
  74.  
  75.     /* check if not open */
  76.     if (!(lsp->flags & LSOPEN)) {
  77.         LSERRLOG;
  78.         errno = LSENOPEN;
  79.         return NULL;
  80.     }
  81. #endif
  82.     /* allocate storage for main record structure */
  83.     /* (calloc is used throughout to automatically set all bits 0) */
  84.     lsrp = (lsrec_t *)xcalloc((size_t)1, sizeof(lsrec_t));
  85.     if (lsrp == NULL) {
  86.         LSERRLOG;
  87.         errno = ENOMEM;
  88.         return NULL;
  89.     }
  90.     lsrp->next = NIL;
  91.     lsrp->prev = NIL;
  92.     lsrp->recbuf = xcalloc((size_t)1, lsp->lshdr.recsize);
  93.     if (lsrp->recbuf == NULL) {
  94.         LSERRLOG;
  95.         xfree(lsrp);
  96.         errno = ENOMEM;
  97.         return NULL;
  98.     }
  99.  
  100.     return lsrp;
  101. }
  102.  
  103. /*man---------------------------------------------------------------------------
  104. NAME
  105.      ls_rccopy - copy lseq record
  106.  
  107. SYNOPSIS
  108.      #include "lseq_.h"
  109.  
  110.      int ls_rccopy(lsp, tlsrp, slsrp)
  111.      lseq_t *lsp;
  112.      lsrec_t *tlsrp;
  113.      const lsrec_t *slsrp;
  114.  
  115. DESCRIPTION
  116.      The ls_rccopy function makes an exact copy of source record slsrp
  117.      in target record tlsrp.
  118.  
  119.      ls_rccopy will fail if one or more of the following is true:
  120.  
  121.      [EINVAL]       lsp is not a valid lseq pointer.
  122.      [EINVAL]       tlsrp or slsrp is the NULL pointer.
  123.  
  124. DIAGNOSTICS
  125.      Upon successful completion, a value of 0 is returned.  Otherwise,
  126.      a value of -1 is returned, and errno set to indicate the error.
  127.  
  128. ------------------------------------------------------------------------------*/
  129. #ifdef AC_PROTO
  130. int ls_rccopy(lseq_t *lsp, lsrec_t *tlsrp, const lsrec_t *slsrp)
  131. #else
  132. int ls_rccopy(lsp, tlsrp, slsrp)
  133. lseq_t *lsp;
  134. lsrec_t *tlsrp;
  135. const lsrec_t *slsrp;
  136. #endif
  137. {
  138. #ifdef DEBUG
  139.     /* validate arguments */
  140.     if (!ls_valid(lsp) || tlsrp == NULL || slsrp == NULL) {
  141.         LSERRLOG;
  142.         errno = EINVAL;
  143.         return -1;
  144.     }
  145. #endif
  146.     /* copy record slsrp into tlsrp */
  147.     tlsrp->next = slsrp->next;
  148.     tlsrp->prev = slsrp->prev;
  149.     memcpy(tlsrp->recbuf, slsrp->recbuf, lsp->lshdr.recsize);
  150.  
  151.     return 0;
  152. }
  153.  
  154. /*man---------------------------------------------------------------------------
  155. NAME
  156.      ls_rcfree - free memory allocated for lseq record
  157.  
  158. SYNOPSIS
  159.      #include "lseq_.h"
  160.  
  161.      void ls_rcfree(lsrp)
  162.      lsrec_t *lsrp;
  163.  
  164. DESCRIPTION
  165.      The ls_rcfree function frees all memory allocated for lseq record
  166.      lsrp.
  167.  
  168. SEE ALSO
  169.      ls_rcalloc.
  170.  
  171. ------------------------------------------------------------------------------*/
  172. #ifdef AC_PROTO
  173. void ls_rcfree(lsrec_t *lsrp)
  174. #else
  175. void ls_rcfree(lsrp)
  176. lsrec_t *lsrp;
  177. #endif
  178. {
  179.     if (lsrp != NULL) {
  180.         if (lsrp->recbuf != NULL) {
  181.             xfree(lsrp->recbuf);
  182.             lsrp->recbuf = NULL;
  183.         }
  184.         xfree(lsrp);
  185.     }
  186.  
  187.     return;
  188. }
  189.  
  190. /*man---------------------------------------------------------------------------
  191. NAME
  192.      ls_rcget - lseq record get
  193.  
  194. SYNOPSIS
  195.      #include "lseq_.h"
  196.  
  197.      int ls_rcget(lsp, lspos, lsrp)
  198.      lseq_t *lsp;
  199.      lspos_t lspos;
  200.      lsrec_t *lsrp;
  201.  
  202. DESCRIPTION
  203.      The ls_rcget function reads the record at position lspos into the
  204.      record pointed to be lsrp.  The entire record is read, including
  205.      the links.
  206.  
  207. SEE ALSO
  208.      ls_rcput.
  209.  
  210. DIAGNOSTICS
  211.      Upon successful completion, a value of 0 is returned.  Otherwise,
  212.      a value of -1 is returned, and errno set to indicate the error.
  213.  
  214. ------------------------------------------------------------------------------*/
  215. #ifdef AC_PROTO
  216. int ls_rcget(lseq_t *lsp, lspos_t lspos, lsrec_t *lsrp)
  217. #else
  218. int ls_rcget(lsp, lspos, lsrp)
  219. lseq_t *lsp;
  220. lspos_t lspos;
  221. lsrec_t *lsrp;
  222. #endif
  223. {
  224.     void *buf = NULL;
  225. #ifdef DEBUG
  226.     /* validate arguments */
  227.     if (!ls_valid(lsp) || lsrp == NULL || lspos == NIL) {
  228.         LSERRLOG;
  229.         errno = EINVAL;
  230.         return -1;
  231.     }
  232.  
  233.     /* check if not open */
  234.     if (!(lsp->flags & LSOPEN)) {
  235.         LSERRLOG;
  236.         errno = LSENOPEN;
  237.         return -1;
  238.     }
  239. #endif
  240.     /* read record from file */
  241.     buf = xcalloc((size_t)1, ls_blksize(lsp));
  242.     if (buf == NULL) {
  243.         LSERRLOG;
  244.         errno = ENOMEM;
  245.         return -1;
  246.     }
  247.     if (bgetb(lsp->bp, (bpos_t)lspos, buf) == -1) {
  248.         LSERRLOG;
  249.         xfree(buf);
  250.         return -1;
  251.     }
  252.  
  253.     /* convert record from file format */
  254.     memcpy(lsrp, buf, offsetof(lsrec_t, recbuf));
  255.     memcpy(lsrp->recbuf, ((char *)buf + offsetof(lsrec_t, recbuf)), lsp->lshdr.recsize);
  256.  
  257.     /* free buffer */
  258.     xfree(buf);
  259.     buf = NULL;
  260.  
  261.     return 0;
  262. }
  263.  
  264. /*man---------------------------------------------------------------------------
  265. NAME
  266.      ls_rcinit - lseq record initialize
  267.  
  268. SYNOPSIS
  269.      #include "lseq_.h"
  270.  
  271.      void ls_rcinit(lsp, lsrp)
  272.      lseq_t *lsp;
  273.      lsrec_t *lsrp;
  274.  
  275. DESCRIPTION
  276.      The ls_rcinit function initializes record lsrp.
  277.  
  278. ------------------------------------------------------------------------------*/
  279. #ifdef AC_PROTO
  280. void ls_rcinit(lseq_t *lsp, lsrec_t *lsrp)
  281. #else
  282. void ls_rcinit(lsp, lsrp)
  283. lseq_t *lsp;
  284. lsrec_t *lsrp;
  285. #endif
  286. {
  287. #ifdef DEBUG
  288.     /* validate arguments */
  289.     if (!ls_valid(lsp) || lsrp == NULL) {
  290.         LSERRLOG;
  291.         return;
  292.     }
  293. #endif
  294.     /* initialize lsrp */
  295.     lsrp->next = NIL;
  296.     lsrp->prev = NIL;
  297.     if (lsrp->recbuf == NULL) {
  298.         LSERRLOG;
  299.         return;
  300.     }
  301.  
  302.     memset(lsrp->recbuf, 0, lsp->lshdr.recsize);
  303.  
  304.     return;
  305. }
  306.  
  307. /*man---------------------------------------------------------------------------
  308. NAME
  309.      ls_rcput - lseq record put
  310.  
  311. SYNOPSIS
  312.      #include "lseq_.h"
  313.  
  314.      int ls_rcput(lsp, lspos, lsrp)
  315.      lseq_t *lsp;
  316.      lspos_t lspos;
  317.      const lsrec_t *lsrp;
  318.  
  319. DESCRIPTION
  320.      The ls_rcput function writes the record pointed to by lsrp into
  321.      record position lspos.  The entire record is written, including
  322.      the links.
  323.  
  324.      ls_rcput will fail if one or more of the following is true:
  325.  
  326.      [EINVAL]       lsp is not a valid lseq pointer.
  327.      [EINVAL]       lspos is NIL.
  328.      [LSENOPEN]     lsp is not open.
  329.  
  330. SEE ALSO
  331.      ls_rcget, ls_rcputf.
  332.  
  333. DIAGNOSTICS
  334.      Upon successful completion, a value of 0 is returned.  Otherwise,
  335.      a value of -1 is returned, and errno set to indicate the error.
  336.  
  337. ------------------------------------------------------------------------------*/
  338. #ifdef AC_PROTO
  339. int ls_rcput(lseq_t *lsp, lspos_t lspos, const lsrec_t *lsrp)
  340. #else
  341. int ls_rcput(lsp, lspos, lsrp)
  342. lseq_t *lsp;
  343. lspos_t lspos;
  344. const lsrec_t *lsrp;
  345. #endif
  346. {
  347.     void *buf = NULL;
  348. #ifdef DEBUG
  349.     /* validate arguments */
  350.     if (!ls_valid(lsp) || lspos == NIL || lsrp == NULL) {
  351.         LSERRLOG;
  352.         errno = EINVAL;
  353.         return -1;
  354.     }
  355.  
  356.     /* check if not open */
  357.     if (!(lsp->flags & LSOPEN)) {
  358.         LSERRLOG;
  359.         errno = LSENOPEN;
  360.         return -1;
  361.     }
  362. #endif
  363.     /* convert record to file format */
  364.     buf = xcalloc((size_t)1, ls_blksize(lsp));
  365.     if (buf == NULL) {
  366.         LSERRLOG;
  367.         errno = ENOMEM;
  368.         return -1;
  369.     }
  370.     memcpy(buf, lsrp, offsetof(lsrec_t, recbuf));
  371.     memcpy(((char *)buf + offsetof(lsrec_t, recbuf)), lsrp->recbuf, lsp->lshdr.recsize);
  372.  
  373.     /* write record to file */
  374.     if (bputb(lsp->bp, (bpos_t)lspos, buf) == -1) {
  375.         LSERRLOG;
  376.         xfree(buf);
  377.         return -1;
  378.     }
  379.  
  380.     /* free buffer */
  381.     xfree(buf);
  382.     buf = NULL;
  383.  
  384.     return 0;
  385. }
  386.  
  387. /*man---------------------------------------------------------------------------
  388. NAME
  389.      ls_rcputf - lseq record field put
  390.  
  391. SYNOPSIS
  392.      #include "lseq_.h"
  393.  
  394.      int ls_rcputf(lsp, lspos, offset, buf, bufsize)
  395.      lseq_t *lsp;
  396.      lspos_t lspos;
  397.      size_t offset;
  398.      const void *buf;
  399.      size_t bufsize;
  400.  
  401. DESCRIPTION
  402.      The ls_rcputf function writes the field pointed to by buf into
  403.      record position lspos.  Only the field is written.
  404.  
  405.      ls_rcputf will fail if one or more of the following is true:
  406.  
  407.      [EINVAL]       lsp is not a valid lseq pointer.
  408.      [EINVAL]       lspos is NIL.
  409.      [LSEBOUND]
  410.      [LSENOPEN]
  411.  
  412. SEE ALSO
  413.      ls_rcget, ls_rcput.
  414.  
  415. DIAGNOSTICS
  416.      Upon successful completion, a value of 0 is returned.  Otherwise,
  417.      a value of -1 is returned, and errno set to indicate the error.
  418.  
  419. ------------------------------------------------------------------------------*/
  420. #ifdef AC_PROTO
  421. int ls_rcputf(lseq_t *lsp, lspos_t lspos, size_t offset, const void *buf, size_t bufsize)
  422. #else
  423. int ls_rcputf(lsp, lspos, offset, buf, bufsize)
  424. lseq_t *lsp;
  425. lspos_t lspos;
  426. size_t offset;
  427. const void *buf;
  428. size_t bufsize;
  429. #endif
  430. {
  431. #ifdef DEBUG
  432.     /* validate arguments */
  433.     if (!ls_valid(lsp) || lspos == NIL || buf == NULL || bufsize < 1) {
  434.         LSERRLOG;
  435.         errno = EINVAL;
  436.         return -1;
  437.     }
  438.  
  439.     /* check if not open */
  440.     if (!(lsp->flags & LSOPEN)) {
  441.         LSERRLOG;
  442.         errno = LSENOPEN;
  443.         return -1;
  444.     }
  445.  
  446.     /* check if record boundary crossed */
  447.     if ((offset + bufsize) > lsp->lshdr.recsize) {
  448.         LSERRLOG;
  449.         errno = LSEBOUND;
  450.         return -1;
  451.     }
  452. #endif
  453.     /* write record to file */
  454.     if (bputbf(lsp->bp, (bpos_t)lspos, offsetof(lsrec_t, recbuf) + offset, buf, bufsize) == -1) {
  455.         LSERRLOG;
  456.         return -1;
  457.     }
  458.  
  459.     return 0;
  460. }
  461.