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

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