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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbcreate.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 <stddef.h>*/
  11. #include <stdio.h>
  12. #include "cbase_.h"
  13.  
  14. int cbrposcmp();
  15.  
  16. static btfield_t btfldv[] = {
  17.     {0, 0, NULL, BT_FASC},
  18.     {0, sizeof(cbrpos_t), cbrposcmp, BT_FASC}
  19. };
  20.  
  21. /*man---------------------------------------------------------------------------
  22. NAME
  23.      cbcreate - create a cbase
  24.  
  25. SYNOPSIS
  26.      #include <cbase.h>
  27.  
  28.      int cbcreate(cbname, recsize, fldc, fldv)
  29.      const char *cbname;
  30.      size_t recsize;
  31.      int fldc;
  32.      const cbfield_t fldv[];
  33.  
  34. DESCRIPTION
  35.      The cbcreate function creates a cbase.  cbname points to a
  36.      character string that contains the name of the cbase to be
  37.      created.  cbname is used as the name of the data file containing
  38.      the records in the cbase.
  39.  
  40.      recsize specifies the size of the records in the cbase.
  41.  
  42.      fldc is the field count.  It specifies the number of fields in
  43.      the records stored in this cbase.  fldv is an array of field
  44.      definition structures.  fldv must have fldc elements.  The field
  45.      definition structure is defined in <cbase.h> as type cbfield_t.
  46.      It has the following members.
  47.  
  48.           size_t offset;      /* offset of field in record *\/
  49.           size_t len;         /* field length *\/
  50.           int type;           /* field data type *\/
  51.           int flags;          /* flags *\/
  52.           char filename[FILENAME_MAX + 1];
  53.                               /* name of key file *\/
  54.  
  55.      offset and len specify the location and length of the field,
  56.      respectively.  type is the data type for the field; see cbase
  57.      manual entry for a list of the predefined data types, and
  58.      the cbase Programmer's Guide for information on adding new data
  59.      types.  filename is the name of the file to be used for key
  60.      storage.  flags values are constructed by OR-ing flags from the
  61.      following list:
  62.  
  63.      CB_FKEY        Field is a key.
  64.      CB_FUNIQ       Only for use with CB_FKEY.  Indicates
  65.                     that the keys must be unique.
  66.  
  67.      The fields in the field definition list must be in order,
  68.      starting with the first field in the record.
  69.  
  70.      cbcreate will fail if one or more of the following is true:
  71.  
  72.      [EEXIST]       Either the record file or one of
  73.                     the key files exists.
  74.      [EINVAL]       cbname is the NULL pointer.
  75.      [EINVAL]       recsize is less than sizeof(cbrpos_t).
  76.      [EINVAL]       fldc is less than 1.
  77.      [EINVAL]       fldv is the NULL pointer.
  78.      [EINVAL]       fldv  contains an invalid field
  79.                     definition.
  80.  
  81. SEE ALSO
  82.      cbopen.
  83.  
  84. DIAGNOSTICS
  85.      Upon successful completion, a value of 0 is returned.  Otherwise,
  86.      a value of -1 is returned, and errno set to indicate the error.
  87.  
  88. ------------------------------------------------------------------------------*/
  89. int cbcreate(cbname, recsize, fldc, fldv)
  90. CONST char *cbname;
  91. size_t recsize;
  92. int fldc;
  93. CONST cbfield_t fldv[];
  94. {
  95.     int terrno = 0;
  96.     int i = 0;
  97.  
  98.     /* validate arguments */
  99.     if ((cbname == NULL) || (recsize < sizeof(cbrpos_t))) {
  100. CBEPRINT;
  101.         errno = EINVAL;
  102.         return -1;
  103.     }
  104.     if (!cb_fvalid(recsize, fldc, fldv)) {
  105. CBEPRINT;
  106.         errno = EINVAL;
  107.         return -1;
  108.     }
  109.  
  110.     /* create data file */
  111.     if (lscreate(cbname, recsize) == -1) {
  112.         if (errno != EEXIST) CBEPRINT;
  113.         return -1;
  114.     }
  115.  
  116.     /* create key files */
  117.     for (i = 0; i < fldc; i++) {
  118.         if (fldv[i].flags & CB_FKEY) {
  119.             btfldv[1].offset = btfldv[0].len = fldv[i].len;
  120.             btfldv[0].cmp = cbcmpv[fldv[i].type];
  121.             if (btcreate(fldv[i].filename, CBM, fldv[i].len + sizeof(cbrpos_t), 2, btfldv) == -1) {
  122.                 if (errno != EEXIST) CBEPRINT;
  123.                 terrno = errno;
  124.                 for (i--; i >= 0; i--) {    /* remove files */
  125.                     if (fldv[i].flags & CB_FKEY) {
  126.                         remove(fldv[i].filename);
  127.                     }
  128.                 }
  129.                 remove(cbname);
  130.                 errno = terrno;
  131.                 return -1;
  132.             }
  133.         }
  134.     }
  135.  
  136.     errno = 0;
  137.     return 0;
  138. }
  139.