home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR41 / CBASE11.ZIP / CBCREATE.C < prev    next >
C/C++ Source or Header  |  1993-01-01  |  4KB  |  163 lines

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