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

  1. /*****************************************************************************
  2.    MODULE: rcgetf.c
  3.   PURPOSE: recio column delimited floating point input functions
  4. COPYRIGHT: (C) 1994-1995, William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.13
  8.   RELEASE: September 4, 1995
  9. *****************************************************************************/
  10.  
  11. #include <ctype.h>
  12. #include <errno.h>
  13. #include <float.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #include "recio.h"
  19.  
  20. extern int _risready(REC *rp, int mode);
  21. extern char *_rfldstr(REC *rp, size_t len);
  22. extern char *_rerrs(REC *rp, int errnum);
  23.  
  24. #define rcol(rp) (rp->r_colno)
  25.  
  26. /****************************************************************************/
  27. static double                /* return floating point number                */
  28.     _rcgetd(                 /* get flt pt number from col delim stream     */
  29.         REC   *rp,           /* pointer to record stream                    */
  30.         size_t begcol,       /* field inclusive beginning column            */
  31.         size_t endcol,       /* field inclusive ending column               */
  32.         double negmin,       /* inclusive valid negative minimum value      */
  33.         double negmax,       /* inclusive valid negative maximum value      */
  34.         double posmin,       /* inclusive valid positive minimum value      */
  35.         double posmax)       /* inclusive valid positive maximum value      */
  36. /****************************************************************************/
  37. {
  38.     double result=0.0;       /* result to return */
  39.     double val;              /* conversion value */
  40.     char *fldptr;            /* pointer to field string */
  41.     char *endptr;            /* pointer to first invalid field char */
  42.     char *fldp;              /* another pointer to field string */
  43.  
  44.     if (_risready(rp, R_READ)) { 
  45.       if (endcol >= begcol && begcol >= rbegcolno(rp)) { 
  46.         rcol(rp) = begcol - rbegcolno(rp); 
  47.         fldptr = _rfldstr(rp, endcol-begcol+1); 
  48.         if (fldptr) { 
  49.           for (;;) { 
  50.             for (fldp=fldptr; *fldp; fldp++) {if (!isspace(*fldp)) break;} 
  51.             if (*fldp) { 
  52.               endptr = fldptr; 
  53.               errno = 0; 
  54.               val = strtod(fldptr, &endptr); 
  55.               while (isspace(*endptr)) endptr++; 
  56.               if (errno==ERANGE || !*endptr) { 
  57.                 if (!errno) { 
  58.                   if (!val || (val >= negmin && val <= negmax) || 
  59.                               (val >= posmin && val <= posmax)) { 
  60.                         result = val;
  61.                         goto done;
  62.                   }
  63.                 } /* data out of range */ 
  64.                 fldptr = _rerrs(rp, R_ERANGE); 
  65.                 if (fldptr) { continue; } else { goto done; } 
  66.               } /* invalid data */ 
  67.               fldptr = _rerrs(rp, R_EINVDAT); 
  68.               if (fldptr) { continue; } else { goto done; } 
  69.             } /* missing data */ 
  70.             fldptr = _rerrs(rp, R_EMISDAT); 
  71.             if (fldptr) { continue; } else { goto done; } 
  72.           } 
  73.         }
  74.       } /* column args reversed or tried to start before first column */
  75.       rseterr(rp, R_EINVAL);
  76.     }
  77. done: 
  78.     return result; 
  79. }
  80.  
  81. /****************************************************************************/
  82. /* column delimited floating point input functions                          */
  83. /****************************************************************************/
  84. float rcgetf(REC *rp, size_t begcol, size_t endcol)
  85. {
  86.     return (float) _rcgetd(rp, begcol, endcol, 
  87.      -FLT_MAX, -FLT_MIN, FLT_MIN, FLT_MAX);
  88. }
  89.  
  90. double rcgetd(REC *rp, size_t begcol, size_t endcol)
  91. {
  92.     return _rcgetd(rp, begcol, endcol, 
  93.      -DBL_MAX, -DBL_MIN, DBL_MIN, DBL_MAX);
  94. }
  95.