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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btcreate.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. /*man---------------------------------------------------------------------------
  12. NAME
  13.      btcreate - create a btree
  14.  
  15. SYNOPSIS
  16.      int btcreate(filename, m, keysize)
  17.      char *filename;
  18.      size_t m;
  19.      size_t keysize;
  20.  
  21. DESCRIPTION
  22.      The btcreate function creates the file named by filename as a btree.
  23.      m is the degree of the tree and keysize is the size of the keys.
  24.  
  25.      filename points to a character string that contains the name of the
  26.      file to be createed.
  27.  
  28.      m is the degree of the tree and keysize is the size of the keys.
  29.  
  30.      btcreate will fail if one or more of the following is true:
  31.  
  32.      [EINVAL]       filename is the NULL pointer.
  33.      [EINVAL]       m is less than 3.
  34.      [EINVAL]       keysize is less than 1.
  35.      [BTEMFILE]     Too many open btrees.  The maximum
  36.                     is defined as BTOPEN_MAX in btree.h.
  37.  
  38. SEE ALSO
  39.      btopen.
  40.  
  41. DIAGNOSTICS
  42.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  43.      value of -1 is returned, and errno set to indicate the error.
  44.  
  45. ------------------------------------------------------------------------------*/
  46. int btcreate(filename, m, keysize)
  47. char * filename;
  48. size_t m;
  49. size_t keysize;
  50. {
  51.     int        rs    = 0;
  52.     int        terrno    = 0;
  53.     btree_t *    btp    = NULL;
  54.  
  55.     errno = 0;
  56.  
  57.     /* validate input parameters */
  58.     if ((filename == NULL) || (m < 3) || (keysize < 1)) {
  59.         errno = EINVAL;
  60.         return -1;
  61.     }
  62.  
  63.     /* find free slot in btb table */
  64.     for (btp = btb; btp < (btb + BTOPEN_MAX); btp++) {
  65.         if (!(btp->flags & BTOPEN)) {
  66.             break;        /* found */
  67.         }
  68.     }
  69.     if (btp >= btb + BTOPEN_MAX) {
  70.         errno = BTEMFILE;
  71.         return -1;        /* no free slots */
  72.     }
  73.  
  74.     /* load btree_t structure */
  75.     btp->bthdr.flh = 0;
  76.     btp->bthdr.m = m;
  77.     btp->bthdr.keysize = keysize;
  78.     btp->bthdr.flags = 0;
  79.     btp->bthdr.root = 0;
  80.     btp->bthdr.first = 0;
  81.     btp->bthdr.last = 0;
  82.     btp->bthdr.keycnt = 0;
  83.     btp->bthdr.height = 0;
  84.     btp->flags = BTREAD | BTWRITE;
  85.     btp->cmp_p = memcmp;
  86.     btp->cbtpos.node = 0;            /* cursor */
  87.     btp->cbtpos.key = 0;
  88.     rs = bt_alloc(btp);
  89.     if (rs == -1) {
  90.         BTEPRINT;
  91.         memset((void *)btp, 0, sizeof(btb[0]));
  92.         btp->flags = 0;
  93.         return -1;
  94.     }
  95.  
  96.     /* create file */
  97.     btp->bp = bopen(filename, "c", sizeof(bthdr_t), (size_t)1, (size_t)0);
  98.     if (btp->bp == NULL) {
  99.         if (errno != EEXIST) BTEPRINT;
  100.         bt_free(btp);
  101.         memset((void *)btp, 0, sizeof(btb[0]));
  102.         btp->flags = 0;
  103.         return -1;
  104.     }
  105.  
  106.     /* write header to file */
  107.     rs = bputh(btp->bp, (void *)&btp->bthdr);    /* header */
  108.     if (rs == -1) {
  109.         BTEPRINT;
  110.         terrno = errno;
  111.         bclose(btp->bp);
  112.         bt_free(btp);
  113.         memset((void *)btp, 0, sizeof(btb[0]));
  114.         btp->flags = 0;
  115.         errno = terrno;
  116.         return -1;
  117.     }
  118.  
  119.     /* close btp */
  120.     rs = btclose(btp);
  121.     if (rs == -1) {
  122.         BTEPRINT;
  123.         bt_free(btp);
  124.         return -1;
  125.     }
  126.  
  127.     errno = 0;
  128.     return 0;
  129. }
  130.