home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / BTREE.ZIP / BTOPEN.C < prev    next >
Text File  |  1989-08-31  |  5KB  |  168 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btopen.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. /* #include <string.h> */
  9. #include "btree_.h"
  10.  
  11. /* btree control structure table definition */
  12. btree_t btb[BTOPEN_MAX];
  13.  
  14. /*man---------------------------------------------------------------------------
  15. NAME
  16.      btopen - open a btree
  17.  
  18. SYNOPSIS
  19.      btree_t *btopen(filename, type, cmp_p)
  20.      char *filename;
  21.      char *type;
  22.      int (*cmp_p)();
  23.  
  24. DESCRIPTION
  25.      The btopen function opens the file named by filename as a btree.  A
  26.      pointer to the btree_t structure associated with the file is returned.
  27.  
  28.      filename points to a character string that contains the name of the
  29.      file to be opened.
  30.  
  31.      type is a character string having one of the following values:
  32.           "r"            open for reading
  33.           "r+"           open for update (reading and writing)
  34.  
  35.      cmp_p is a pointer to the user written comparison function which
  36.      defines the sort sequence for the btree.  This function must be of
  37.      the form
  38.  
  39.           int f(const void *p1, const void *p2, size_t n);
  40.  
  41.      where p1 and p2 point to the two keys to bey compared and n is the
  42.      key size.  The return must be less than, equal to, or greater than
  43.      zero if p1 is less than, equal to, or greater than p2, respectively.
  44.      If cmp_p is NULL then the memcmp function is used as the default.
  45.  
  46.      btopen will fail if one or more of the following is true:
  47.  
  48.      [EINVAL]       filename is the NULL pointer.
  49.      [EINVAL]       type is not "r" or "r+".
  50.      [ENOENT]       The named btree file does not exist.
  51.      [BTECORRUPT]   The named file is corrupt.
  52.      [BTEEOF]       No file header.
  53.      [BTEMFILE]     Too many open btrees.  The maximum
  54.                     is defined as BTOPEN_MAX in btree.h.
  55.  
  56. SEE ALSO
  57.      btclose, btcreate.
  58.  
  59. DIAGNOSTICS
  60.      btopen returns a NULL pointer on failure, and errno is set to
  61.      indicate the error.
  62.  
  63. NOTES
  64.      Since there is no mechanism by which the address of a function
  65.      may be stored in a file, it is the responsibility of the
  66.      programmer to supply the same key comparison function each time
  67.      the file is opened.  Use of a different comparison function
  68.      will yield unpredictable results and corrupt the file if a write
  69.      is attempted.
  70.  
  71. ------------------------------------------------------------------------------*/
  72. btree_t *btopen(filename, type, cmp_p)
  73. char *  filename;
  74. char *  type;
  75. int     (*cmp_p)();
  76. {
  77.     int        rs    = 0;
  78.     int        terrno    = 0;
  79.     btree_t *    btp    = NULL;
  80.  
  81.     errno = 0;
  82.  
  83.     /* validate input parameters */
  84.     if ((filename == NULL) || (type == NULL)) {
  85.         errno = EINVAL;
  86.         return NULL;
  87.     }
  88.  
  89.     /* find free slot in btb table */
  90.     for (btp = btb; btp < (btb + BTOPEN_MAX); btp++) {
  91.         if (!(btp->flags & BTOPEN)) {
  92.             break;        /* found */
  93.         }
  94.     }
  95.     if (btp >= btb + BTOPEN_MAX) {
  96.         errno = BTEMFILE;
  97.         return NULL;        /* no free slots */
  98.     }
  99.  
  100.     /* open file */
  101.     if (strcmp(type, BT_READ) == 0) {
  102.         btp->flags = BTREAD;
  103.     } else if (strcmp(type, BT_RDWR) == 0) {
  104.         btp->flags = BTREAD | BTWRITE;
  105.     } else {
  106.         errno = EINVAL;
  107.         return NULL;
  108.     }
  109.     btp->bp = bopen(filename, type, sizeof(bthdr_t), (size_t)1, (size_t)0);
  110.     if (btp->bp == NULL) {
  111.         if ((errno != ENOENT) && (errno != EACCES)) BTEPRINT;
  112.         memset((void *)btp, 0, sizeof(btb[0]));
  113.         btp->flags = 0;
  114.         return NULL;
  115.     }
  116.  
  117.     /* load btree_t structure */
  118.     rs = bgeth(btp->bp, (void *)&btp->bthdr);    /* header */
  119.     if (rs == -1) {
  120.         BTEPRINT;
  121.         terrno = errno;
  122.         bclose(btp->bp);
  123.         memset((void *)btp, 0, sizeof(btb[0]));
  124.         btp->flags = 0;
  125.         errno = terrno;
  126.         return NULL;
  127.     }
  128.     if (btp->bthdr.flags & BTHMOD) {        /* corrupt file */
  129.         bclose(btp->bp);
  130.         memset((void *)btp, 0, sizeof(btb[0]));
  131.         btp->flags = 0;
  132.         errno = BTECORRUPT;
  133.         return NULL;
  134.     }
  135.     btp->cmp_p = cmp_p;        /* key comparison fct */
  136.     if (btp->cmp_p == NULL) {
  137.         btp->cmp_p = memcmp;
  138.     }
  139.     btp->cbtpos.node = 0;        /* cursor */
  140.     btp->cbtpos.key = 0;
  141.     rs = bt_alloc(btp);        /* current node and search path */
  142.     if (rs == -1) {
  143.         BTEPRINT;
  144.         terrno = errno;
  145.         bclose(btp->bp);
  146.         memset((void *)btp, 0, sizeof(btb[0]));
  147.         btp->flags = 0;
  148.         errno = terrno;
  149.         return NULL;
  150.     }
  151.  
  152.     /* set up buffering */
  153.     rs = bsetvbuf(btp->bp, NULL, bt_blksize(btp), BTBUFCNT);
  154.     if (rs == -1) {
  155.         BTEPRINT;
  156.         terrno = errno;
  157.         bt_free(btp);
  158.         bclose(btp->bp);
  159.         memset((void *)btp, 0, sizeof(btb[0]));
  160.         btp->flags = 0;
  161.         errno = terrno;
  162.         return NULL;
  163.     }
  164.  
  165.     errno = 0;
  166.     return btp;
  167. }
  168.