home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / CBASE / CBASE.ZIP / CBEXPORT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-20  |  4.0 KB  |  172 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbexport.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. #include <stdio.h>
  10. /*#include <stdlib.h>*/
  11.  
  12. /* local headers */
  13. #include "cbase_.h"
  14.  
  15. /*man---------------------------------------------------------------------------
  16. NAME
  17.      cbexport - export cbase data
  18.  
  19. SYNOPSIS
  20.      #include <cbase.h>
  21.  
  22.      int cbexport(cbp, filename)
  23.      cbase_t *cbp;
  24.      const char *filename;
  25.  
  26. DESCRIPTION
  27.      The cbexport function exports all records in cbase cbp to a
  28.      printable file.  filename points to a character string that
  29.      contains the name of the file to receive the data.  If the file
  30.      does not exist, it is created.  If the file exists, it is
  31.      truncated to zero length before the data is exported.
  32.  
  33.      The exported file format is as follows:
  34.  
  35.           o Each record is terminated by a newline ('\\n').
  36.           o The fields in a record are delimited by vertical
  37.             bars ('|').
  38.           o Each field contains only printable characters.
  39.           o If a field contains the field delimiter
  40.             character, that character is replaced with \\F.
  41.           o The individual elements of array data types are
  42.             exported as individual fields.
  43.  
  44.      cbexport will fail if one or more of the following is true:
  45.  
  46.      [EINVAL]       cbp is not a valid cbase pointer.
  47.      [EINVAL]       filename is the NULL pointer.
  48.      [CBELOCK]      cbp is not read locked.
  49.      [CBENOPEN]     cbp is not open.
  50.      [CBEPRFILE]    Error writing to printable file.
  51.  
  52. DIAGNOSTICS
  53.      Upon successful completion, a value of 0 is returned.  Otherwise,
  54.      a value of -1 is returned, and errno set to indicate the error.
  55.  
  56. ------------------------------------------------------------------------------*/
  57. int cbexport(cbp, filename)
  58. cbase_t *cbp;
  59. const char *filename;
  60. {
  61.     int    rs    = 0;
  62.     int    terrno    = 0;
  63.     FILE *    fp    = NULL;
  64.     void *    buf    = NULL;
  65.     int    field    = 0;
  66.     size_t    fldos = 0;
  67.     size_t    fldlen    = 0;
  68.     int    fldtype    = 0;
  69.  
  70.     /* validate arguments */
  71.     if (!cb_valid(cbp) || filename == NULL) {
  72.         errno = EINVAL;
  73.         return -1;
  74.     }
  75.  
  76.     /* check if not read locked */
  77.     if (!(cbp->flags & CB_RDLCK)) {
  78.         errno = CBELOCK;
  79.         return -1;
  80.     }
  81.  
  82.     /* truncate or create file for writing */
  83.     fp = fopen(filename, "w");
  84.     if (fp == NULL) {
  85.         return -1;
  86.     }
  87.  
  88.     /* check if cbp is empty */
  89.     if (cbreccnt(cbp) == 0) {
  90.         if (fclose(fp) == EOF) {
  91.             CBEPRINT;
  92.             return -1;
  93.         }
  94.         errno = 0;
  95.         return 0;
  96.     }
  97.  
  98.     /* position record cursor to first record */
  99.     if (cbrecfirst(cbp) == -1) {
  100.         CBEPRINT;
  101.         terrno = errno;
  102.         fclose(fp);
  103.         errno = terrno;
  104.         return -1;
  105.     }
  106.  
  107.     /* export all records to printable file */
  108.     buf = calloc((size_t)1, cbrecsize(cbp));
  109.     if (buf == NULL) {
  110.         CBEPRINT;
  111.         fclose(fp);
  112.         errno = ENOMEM;
  113.         return -1;
  114.     }
  115.     while (cbrcursor(cbp) != NULL) {
  116.         if (cbgetr(cbp, buf) == -1) {    /* read current record */
  117.             CBEPRINT;
  118.             terrno = errno;
  119.             free(buf);
  120.             fclose(fp);
  121.             errno = terrno;
  122.             return -1;
  123.         }
  124.         /* output each field of current record */
  125.         for (field = 0; field < cbp->fldc; ++field) {
  126.             fldos = cbp->fldv[field].offset;
  127.             fldlen = cbp->fldv[field].len;
  128.             fldtype = cbp->fldv[field].type;
  129.             /* write field to printable file */
  130.             if ((*cbexpv[fldtype])(fp, (char *)buf + fldos, fldlen) == -1) {
  131.                 CBEPRINT;
  132.                 free(buf);
  133.                 fclose(fp);
  134.                 errno = CBEPRFILE;
  135.                 return -1;
  136.             }
  137.             if (field < cbp->fldc - 1) {
  138.                 rs = fputc(EXPFLDDLM, fp);
  139.             } else {
  140.                 rs = fputc(EXPRECDLM, fp);
  141.             }
  142.             if (rs == EOF) {
  143.                 CBEPRINT;
  144.                 terrno = errno;
  145.                 free(buf);
  146.                 fclose(fp);
  147.                 errno = terrno;
  148.                 return -1;
  149.             }
  150.         }
  151.         if (cbrecnext(cbp) == -1) {
  152.             CBEPRINT;
  153.             terrno = errno;
  154.             free(buf);
  155.             fclose(fp);
  156.             errno = terrno;
  157.             return -1;
  158.         }
  159.     }
  160.     free(buf);
  161.     buf = NULL;
  162.  
  163.     /* close file */
  164.     if (fclose(fp) == EOF) {
  165.         CBEPRINT;
  166.         return -1;
  167.     }
  168.  
  169.     errno = 0;
  170.     return 0;
  171. }
  172.