home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / ISTRF.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.8 KB  |  112 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - istrf.cpp
  3.  * C++ floating-point stream I/O functions for handling istreams
  4.  *-----------------------------------------------------------------------*/
  5.  
  6. /*[]------------------------------------------------------------[]*/
  7. /*|                                                              |*/
  8. /*|     Turbo C++ Run Time Library - Version 1.0                 |*/
  9. /*|                                                              |*/
  10. /*|                                                              |*/
  11. /*|     Copyright (c) 1990 by Borland International              |*/
  12. /*|     All Rights Reserved.                                     |*/
  13. /*|                                                              |*/
  14. /*[]------------------------------------------------------------[]*/
  15.  
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18. #include <stream.h>
  19.  
  20.  
  21. istream& _Cdecl istream::operator >> ( float& f )
  22. {
  23.     int ok, c;
  24.     checkskip(ok, c);
  25.     if( ok ) {
  26.     putback(c);
  27.     if( stream->file ) {
  28.         if( fscanf(stream->file, "%f", &f) != 1 )
  29.         state = _fail;
  30.     }
  31.     else {
  32.         char *ogptr = stream->gptr;
  33.         double t = strtod(stream->gptr, &(stream->gptr));
  34.         if( ogptr == stream->gptr )
  35.         state = _fail;    // no conversion
  36.         else {
  37.         f = t;
  38.         if( stream->gptr > stream->eptr )
  39.             stream->gptr = stream->eptr;
  40.         }
  41.     }
  42.     }
  43.     return *this;
  44. }
  45.  
  46.  
  47. istream& _Cdecl istream::operator >> ( double& d )
  48. {
  49.     int ok, c;
  50.     checkskip(ok, c);
  51.     if( ok ) {
  52.     putback(c);
  53.     if( stream->file ) {
  54.         if( fscanf(stream->file, "%lf", &d) != 1 )
  55.         state = _fail;
  56.     }
  57.     else {
  58.         char *ogptr = stream->gptr;
  59.         double t = strtod(stream->gptr, &(stream->gptr));
  60.         if( ogptr == stream->gptr )
  61.         state = _fail;    // no conversion
  62.         else {
  63.         d = t;
  64.         if( stream->gptr > stream->eptr )
  65.             stream->gptr = stream->eptr;
  66.         }
  67.     }
  68.     }
  69.     return *this;
  70. }
  71.  
  72.  
  73. istream& _Cdecl istream::operator >> ( long double& d )
  74. {
  75.     int ok, c;
  76.     checkskip(ok, c);    // scans off whitespace
  77.     if( ok ) {
  78.     putback(c);
  79.     if( stream->file ) {
  80.         if( fscanf(stream->file, "%Lf", &d) != 1 )
  81.         state = _fail;
  82.     }
  83.     else {
  84.         char *p = stream->gptr;
  85.         // too bad we don't have a strtoLd function
  86.         if( sscanf(p, "%Lf", &d) != 1 )
  87.         state = _fail;
  88.         else {
  89.         // skip past the value read
  90.         if( *p == '+' || *p == '-' )
  91.             ++p;
  92.         while( isdigit(*p) )
  93.             ++p;
  94.         if( *p == '.' ) {
  95.             ++p;
  96.             while( isdigit(*p) )
  97.             ++p;
  98.         }
  99.         if( *p == 'e' || *p == 'E' ) {
  100.             ++p;
  101.             if( *p == '+' || *p == '-' )
  102.             ++p;
  103.             while( isdigit(*p) )
  104.             ++p;
  105.         }
  106.         stream->gptr = (p > stream->eptr) ? stream->eptr : p;
  107.         }
  108.     }
  109.     }
  110.     return *this;
  111. }
  112.