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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbcreate.c    1.1 - 89/08/31" */
  5.  
  6. #include <blkio.h>
  7. #include <btree.h>
  8. #include <errno.h>
  9. #include <lseq.h>
  10. #include "cbase_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      cbcreate - create a cbase
  15.  
  16. SYNOPSIS
  17.      #include <cbase.h>
  18.  
  19.      int cbcreate(cbname, recsize, fields, fldcnt)
  20.      char *cbname;
  21.      size_t recsize;
  22.      cbfield_t fields[];
  23.      size_t fldcnt;
  24.  
  25. DESCRIPTION
  26.      The cbcreate function creates the cbase named by cbname.
  27.  
  28.      cbname points to a character string that contains the name of the
  29.      cbase to be created.
  30.  
  31.      recsize specifies the size of the records in the cbase.
  32.  
  33.      fields is an array of field definition structures.  The list of field
  34.      definitions must be in order of occurrence in the record.  cbfield_t
  35.      is defined as follows:
  36.  
  37.           typedef struct {
  38.                size_t offset;        /* offset of field in record *\/
  39.                size_t size;          /* size of field *\/
  40.                int type;             /* type of field *\/
  41.                int flags;            /* flags *\/
  42.                char filename[FILENAME_MAX]; /* name of file to store keys in *\/
  43.           } cbfield_t;
  44.  
  45.      flags values are constructed by OR-ing flags from the following list:
  46.           CBFKEY         Field is to be a key.
  47.           CBFUNIQ        Only for use with CBFKEY.  Indicates that
  48.                          key value should be unique.
  49.  
  50.      fldcnt specifies the number of fields in each record.
  51.  
  52.      cbcreate will fail if one or more of the following is true:
  53.  
  54.      [EEXIST]       Either the record file or one of
  55.                     the key files exists.
  56.      [EINVAL]       cbname is the NULL pointer.
  57.      [EINVAL]       recsize is less than sizeof(lspos_t).
  58.      [EINVAL]       fldcnt is not in the range [1..CBFIELD_MAX].
  59.      [EINVAL]       keycnt is not in the range [0..CBKEY_MAX].
  60.      [EINVAL]       There is a field overlap in the field
  61.                     definition list.
  62.      [EINVAL]       fields specifies an invalid type.
  63.      [EINVAL]       keys specifies a non-existent field.
  64.  
  65. SEE ALSO
  66.      cbopen.
  67.  
  68. DIAGNOSTICS
  69.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  70.      value of -1 is returned, and errno set to indicate the error.
  71.  
  72. ------------------------------------------------------------------------------*/
  73. int cbcreate(cbname, recsize, fields, fldcnt)
  74. char *    cbname;
  75. size_t    recsize;
  76. cbfield_t fields[];
  77. size_t    fldcnt;
  78. {
  79.     int    rs    = 0;
  80.     int    i    = 0;
  81.  
  82.     errno = 0;
  83.  
  84.     /* validate arguments */
  85.     if (cbname == NULL) {
  86.         errno = EINVAL;
  87.         return -1;
  88.     }
  89.     if (recsize < sizeof(lspos_t)) {
  90.         errno = EINVAL;
  91.         return -1;
  92.     }
  93.     if (!cb_fdlvalid(fields, fldcnt, recsize)) {
  94.         errno = EINVAL;
  95.         return -1;
  96.     }
  97.  
  98.     /* create lseq file for records */
  99.     rs = lscreate(cbname, recsize);
  100.     if (rs == -1) {
  101.         if ((errno != EEXIST)) CBEPRINT;
  102.         return -1;
  103.     }
  104.  
  105.     /* create btree files for keys */
  106.     for (i = 0; i < fldcnt; i++) {
  107.         if (fields[i].flags & CBFKEY) {
  108.             rs = btcreate(fields[i].filename, CBM, fields[i].size + sizeof(cbrpos_t));
  109.             if (rs == -1) {
  110.                 if ((errno != EEXIST)) CBEPRINT;
  111.                 return -1;
  112.             }
  113.         }
  114.     }
  115.  
  116.     errno = 0;
  117.     return 0;
  118. }
  119.