home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / recio213.zip / testchp.c < prev    next >
C/C++ Source or Header  |  1995-09-05  |  6KB  |  221 lines

  1. /* testchp.c - tests character delimited put functions */
  2. /* recio version 2.13, release September 4, 1995 */
  3. /* Copyright (C) 1994-1995, William Pierpoint */
  4.  
  5. #include <ctype.h>
  6. #include <errno.h>
  7. #include <limits.h>
  8. #include <float.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <io.h>
  13.  
  14. #include "recio.h"
  15.  
  16. /* errors to stderr */
  17. #define errout  stdout
  18.  
  19. enum {RESET, INCR, REPORT};
  20.  
  21. /****************************************************************************/
  22. void                         /* return nothing                              */
  23.     warnttl(                 /* warning totals                              */
  24.         int action,          /* action (0=reset, 1=increment, 2=report)     */
  25.         int warnum)          /* warning number                              */
  26. /****************************************************************************/
  27. {
  28.   static unsigned int widthcnt=0;
  29.   static unsigned int noregcnt=0;
  30.   static unsigned int timefaults=0;
  31.  
  32.   switch (action) {
  33.   case RESET:
  34.     widthcnt=0;
  35.     timefaults=0;
  36.     return;
  37.   case INCR:
  38.     switch(warnum) {
  39.     case R_WWIDTH: 
  40.       widthcnt++; 
  41.       return;
  42.     case R_WNOREG: 
  43.       noregcnt++; 
  44.       return;
  45.     case R_WFAULT:
  46.       timefaults++;
  47.       return;
  48.     }
  49.     return;
  50.   case REPORT:
  51.     fprintf(errout, "\n");
  52.     if (widthcnt) 
  53.       fprintf(errout, "WARNING -- %s -- %u times\n", 
  54.        rstrwarning(R_WWIDTH), widthcnt);
  55.     if (noregcnt) 
  56.       fprintf(errout, "WARNING -- %s\n", rstrwarning(R_WNOREG));
  57.     if (timefaults) 
  58.       fprintf(errout, "WARNING -- Time conversion failed %u times\n",
  59.        timefaults);
  60.     return;
  61.   }
  62. }
  63.  
  64. /****************************************************************************/
  65. void                         /* returns nothing                             */
  66.     rwarnfn(                 /* recio callback warning function             */
  67.         REC *rp)             /* record pointer                              */
  68. /****************************************************************************/
  69. {
  70.   if (risvalid(rp)) warnttl(INCR, rwarning(rp));
  71. }
  72.  
  73. /****************************************************************************/
  74. void                         /* returns nothing                             */
  75.      errnofn(                /* errno callback error function               */
  76.         void)                /* no parameters                               */
  77. /****************************************************************************/
  78. {
  79.   switch (errno) {
  80.  
  81.   /* non-fatal errors */
  82.   case EACCES:
  83.   case EMFILE:
  84.     fprintf(errout, "ERROR: %s\n", strerror(errno));
  85.     break;
  86.  
  87.   /* fatal errors (EINVAL, ENOMEM) */
  88.   default:
  89.     fprintf(errout, "FATAL ERROR: %s\n", strerror(errno));
  90.     exit(EXIT_FAILURE);
  91.     break;
  92.   }
  93. }
  94.  
  95. /****************************************************************************/
  96. void                         /* returns nothing                             */
  97.     rerrfn(                  /* recio callback error function               */
  98.         REC *rp)             /* record pointer                              */
  99. /****************************************************************************/
  100. {
  101.   if (risvalid(rp)) {
  102.   
  103.     /* determine cause of error */
  104.     switch (rerror(rp)) {
  105.  
  106.     case R_ENOPUT:   /* could not write data */
  107.       fprintf(errout, "ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
  108.       break;
  109.  
  110.     /* fatal errors (R_EINVMOD, R_EINVAL, R_ENOMEM) */
  111.     default:
  112.       fprintf(errout, "FATAL ERROR %s -- %s\n", rnames(rp), rerrstr(rp));
  113.       exit(EXIT_FAILURE);
  114.       break;
  115.     }
  116.   
  117.   /* invalid record pointer */
  118.   } else {
  119.     errnofn();
  120.   }
  121. }
  122.  
  123. /****************************************************************************
  124. main
  125. *****************************************************************************/
  126. int main()
  127. {
  128.   int j, k;                     /* loop indices */
  129.   int i;                        /* integer field */
  130.   unsigned int ui;              /* unsigned integer field */
  131.   long l;                       /* long field */
  132.   unsigned long ul;             /* unsigned long field */
  133.   float f;                      /* float field */
  134.   double d;                     /* double field */
  135.   int ch;                       /* character field */
  136.   char *str = NULL;             /* string field */
  137.   char str1[]="A";              /* string consisting of one letter */
  138.   struct tm t;                  /* broken-down time */
  139.   time_t time;                  /* time field */
  140.   
  141.   /* install error and warning functions */
  142.   rinit(rerrfn, rwarnfn);
  143.   rsettmfmt(recout, "%m/%d/%Y %H:%M:%S");
  144.   
  145.   /* if output not redirected */
  146.   if (isatty(fileno(stdout))) {
  147.     /* print instructions */
  148.     puts("TESTCHP version 2.13 Copyright (C) 1994-1995, William Pierpoint");
  149.     puts("Tests recio character delimited put functions.");
  150.   }
  151.  
  152.   for (k=0; k<=1; k++) {
  153.     switch (k) {
  154.     case 1:
  155.       rsetfldch(recout, ',');
  156.       rsettxtch(recout, '"');
  157.       break;
  158.     }
  159.  
  160.     /* initialize data */
  161.     i = -1;
  162.     ui = 1;
  163.     l = -1L;
  164.     ul = 1L;
  165.     f = 1.111111;
  166.     d = 1.111111111111111111111;
  167.     ch = 'a';
  168.     if (str) *str='\0';
  169.     *str1 = toupper(ch);
  170.     scpys(str, str1);
  171.     t = sftotm("1/1/1970 00:00:00", "%m/%d/%Y %H:%M:%S");
  172.  
  173.     /* loop through data */
  174.     for (j=0; j<11; j++) {
  175.  
  176.       rputi(recout,  i);
  177.       rputui(recout, ui);
  178.       rputl(recout,  l);
  179.       rputul(recout, ul);
  180.       rputf(recout,  f);
  181.       rputd(recout,  d);
  182.       rputc(recout,  ch);
  183.       rputs(recout,  str);
  184.  
  185.       time=tmtotime(t); 
  186.       /* if conversion error */
  187.       if (time == (time_t) -1 ) {
  188.           rsetwarn(recout, R_WFAULT);
  189.       }
  190.       rputt(recout,  time);
  191.  
  192.       i  *= 10;
  193.       ui *= 10;
  194.       l  *= 10L;
  195.       ul *= 10L;
  196.       f  *= 10.;
  197.       d  *= 10.;
  198.       ch += 1;
  199.       *str1 = toupper(ch);
  200.       scats(str, str1);
  201.       t.tm_year += 10;
  202.  
  203.       rputrec(recout);
  204.     }
  205.   }
  206.   
  207.   /* free dynamic string fields */
  208.   free (str);
  209.   
  210.   /* check stream for warnings */
  211.   if (rwarning(recout)) warnttl(REPORT, NULL);
  212.  
  213.   /* check stream for error */
  214.   if (rerror(recout)) { 
  215.     fprintf(errout, "\nERROR %s -- %s\n", 
  216.      rnames(recout), rerrstr(recout));
  217.     exit (EXIT_FAILURE);
  218.   }
  219.   return EXIT_SUCCESS;
  220. }
  221.