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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbopen.c    1.2 - 89/11/08" */
  5.  
  6. #include <blkio.h>
  7. #include <btree.h>
  8. #include <errno.h>
  9. #include <lseq.h>
  10. /*#include <string.h>*/
  11. #include "cbase_.h"
  12.  
  13. /* cbase control structure table definition */
  14. cbase_t cbb[CBOPEN_MAX];
  15.  
  16. /* cbase record position comparison function */
  17. int cbrposcmp(p1, p2, n)
  18. void *p1;
  19. void *p2;
  20. int n;
  21. {
  22.     cbrpos_t rpos1 = 0;
  23.     cbrpos_t rpos2 = 0;
  24.  
  25.     memcpy(&rpos1, p1, sizeof(rpos1));
  26.     memcpy(&rpos2, p2, sizeof(rpos2));
  27.     if (rpos1 < rpos2) {
  28.         return -1;
  29.     } else if (rpos1 > rpos2) {
  30.         return 1;
  31.     }
  32.  
  33.     return 0;
  34. }
  35.  
  36. static btfield_t btfldv[] = {
  37.     {0, 0, NULL, BT_FASC},
  38.     {0, sizeof(cbrpos_t), cbrposcmp, BT_FASC}
  39. };
  40.  
  41. /*man---------------------------------------------------------------------------
  42. NAME
  43.      cbopen - open a cbase
  44.  
  45. SYNOPSIS
  46.      #include <cbase.h>
  47.  
  48.      cbase_t *cbopen(cbname, type, fldc, fldv)
  49.      const char *cbname;
  50.      char *type;
  51.      int fldc;
  52.      const cbfield_t fldv[];
  53.  
  54. DESCRIPTION
  55.      The cbopen function opens the cbase named by cbname.  A pointer
  56.      to the cbase_t control structure associated with the cbase is
  57.      returned.
  58.  
  59.      type is a character string having one of the following values:
  60.  
  61.           "r"            open for reading
  62.           "r+"           open for update (reading and writing)
  63.  
  64.      See cbcreate for explanation of the field count fldc and the
  65.      field definition list fldv.
  66.  
  67.      cbopen will fail if one or more of the following is true:
  68.  
  69.      [EINVAL]       cbname is the NULL pointer.
  70.      [EINVAL]       type is not "r" or "r+".
  71.      [EINVAL]       fldc is less than 1.
  72.      [EINVAL]       fldv is the NULL pointer.
  73.      [EINVAL]       fldv contains an invalid field
  74.                     definition.
  75.      [CBECORRUPT]   A file in the named cbase is corrupt.
  76.      [CBEMFILE]     Too many open cbases.  The maximum
  77.                     is defined as CBOPEN_MAX in <cbase.h>.
  78.  
  79. SEE ALSO
  80.      cbclose, cbcreate.
  81.  
  82. DIAGNOSTICS
  83.      On failure cbopen returns a NULL pointer, and errno set to
  84.      indicate the error.
  85.  
  86. NOTES
  87.      The same field count and field definition list with which the
  88.      cbase was created must be used each time the cbase is opened.
  89.      Otherwise the results are undefined.
  90.  
  91. ------------------------------------------------------------------------------*/
  92. cbase_t *cbopen(cbname, type, fldc, fldv)
  93. CONST char *cbname;
  94. char *type;
  95. int fldc;
  96. CONST cbfield_t fldv[];
  97. {
  98.     int terrno = 0;
  99.     cbase_t *cbp = NULL;
  100.     int i = 0;
  101.  
  102.     /* validate arguments */
  103.     if ((cbname == NULL) || (type == NULL) || (fldc < 1) || (fldv == NULL)) {
  104.         errno = EINVAL;
  105.         return NULL;
  106.     }
  107.  
  108.     /* find free slot in cbb table */
  109.     for (cbp = cbb; cbp < (cbb + CBOPEN_MAX); cbp++) {
  110.         if (!(cbp->flags & CBOPEN)) {
  111.             break;        /* found */
  112.         }
  113.     }
  114.     if (cbp >= cbb + CBOPEN_MAX) {
  115.         errno = CBEMFILE;
  116.         return NULL;        /* no free slots */
  117.     }
  118.  
  119.     /* open record file */
  120.     if (strcmp(type, CB_READ) == 0) {
  121.         cbp->flags = CBREAD;
  122.     } else if (strcmp(type, CB_RDWR) == 0) {
  123.         cbp->flags = CBREAD | CBWRITE;
  124.     } else {
  125.         errno = EINVAL;
  126.         return NULL;
  127.     }
  128.     cbp->lsp = lsopen(cbname, type);
  129.     if (cbp->lsp == NULL) {
  130.         if (errno == LSECORRUPT) errno = CBECORRUPT;
  131.         if ((errno != ENOENT) && (errno != CBECORRUPT)) CBEPRINT;
  132.         memset(cbp, 0, sizeof(*cbb));
  133.         cbp->flags = 0;
  134.         return NULL;
  135.     }
  136.  
  137.     /* validate arguments */
  138.     if (!cb_fvalid(lsrecsize(cbp->lsp), fldc, fldv)) {
  139.         lsclose(cbp->lsp);
  140.         memset(cbp, 0, sizeof(*cbb));
  141.         cbp->flags = 0;
  142.         errno = EINVAL;
  143.         return NULL;
  144.     }
  145.  
  146.     /* copy field definitions into cbase structure */
  147.     cbp->fldc = fldc;
  148.     cbp->fldv = NULL;
  149.     cbp->btpv = NULL;
  150.     if (cb_alloc(cbp) == -1) {
  151.         terrno = errno;
  152.         lsclose(cbp->lsp);
  153.         memset(cbp, 0, sizeof(*cbb));
  154.         cbp->flags = 0;
  155.         errno = terrno;
  156.         return NULL;
  157.     }
  158.     memcpy(cbp->fldv, fldv, cbp->fldc * sizeof(*cbp->fldv));
  159.  
  160.     /* open key files */
  161.     for (i = 0; i < cbp->fldc; i++) {
  162.         if (cbp->fldv[i].flags & CB_FKEY) {
  163.             btfldv[1].offset = btfldv[0].len = cbp->fldv[i].len;
  164.             btfldv[0].cmp = cbcmpv[cbp->fldv[i].type];
  165.             cbp->btpv[i] = btopen(cbp->fldv[i].filename, type, 2, btfldv);
  166.             if (cbp->btpv[i] == NULL) {
  167.                 if ((errno != ENOENT) && (errno != BTECORRUPT)) CBEPRINT;
  168.                 if (errno == BTECORRUPT) errno = CBECORRUPT;
  169.                 terrno = errno;
  170.                 for (i--; i >= 0; i--) {
  171.                     if (cbp->fldv[i].flags & CB_FKEY) {
  172.                         btclose(cbp->btpv[i]);
  173.                     }
  174.                 }
  175.                 lsclose(cbp->lsp);
  176.                 cb_free(cbp);
  177.                 memset(cbp, 0, sizeof(*cbb));
  178.                 cbp->flags = 0;
  179.                 errno = terrno;
  180.                 return NULL;
  181.             }
  182.         }
  183.     }
  184.  
  185.     errno = 0;
  186.     return cbp;
  187. }
  188.