home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / istrgdbl.cpp < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  101 lines

  1. /***
  2. * istrgdbl.cpp - definitions for istream class core double routine
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Definitions of member function for istream getdouble().
  8. *       [AT&T C++]
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <internal.h>
  14. #include <ctype.h>
  15. #include <iostream.h>
  16. #pragma hdrstop
  17.  
  18. /***
  19. *int istream::getdouble(char * buffer, int buflen) - get a double
  20. *
  21. *Purpose:
  22. *       Get a double from stream.
  23. *
  24. *Entry:
  25. *       char * buffer   = area for number to be copied.
  26. *       int buflen      = max. length of buffer
  27. *
  28. *Exit:
  29. *       Returns 0 if fatal error
  30. *       Otherwise, returns length of buffer filled.
  31. *       Sets ios::failbit on error forming number.
  32. *       If successful, buffer[] contains the number, followed by \0.
  33. *
  34. *Exceptions:
  35. *
  36. *******************************************************************************/
  37. int     istream::getdouble(char * buffer, int buflen)   // returns length
  38. {
  39.     int c;
  40.     int i = 0;
  41.     int fDigit = 0;     // true if legal digit encountered
  42.     int fDecimal=0;     // true if '.' encountered or no longer valid
  43.     int fExp=0;         // true if 'E' or 'e' encounted
  44.  
  45.     if (ipfx(0))
  46.         {
  47.         c=bp->sgetc();
  48.         for (; i<buflen; buffer[i] = (char)c,c=bp->snextc(),i++)
  49.             {
  50.             if (c==EOF)
  51.                 {
  52.                 state |= ios::eofbit;
  53.                 break;
  54.                 }
  55.             if ((!i) || (fExp==1))
  56.                 {
  57.                 if ((c=='-') || (c=='+'))
  58.                     {
  59.                     continue;
  60.                     }
  61.                 }
  62.             if ((c=='.') && (!fExp) && (!fDecimal))
  63.                 {
  64.                 fDecimal++;
  65.                 continue;
  66.                 }
  67.             if (((c=='E') || (c=='e')) && (!fExp))
  68.                 {
  69.                 fDecimal++;     // can't allow decimal now
  70.                 fExp++;
  71.                 continue;
  72.                 }
  73.             if (!isdigit(c))
  74.                 break;
  75.             if (fExp)
  76.                 fExp++;
  77.             else
  78.                 fDigit++;
  79.             }
  80.         if (fExp==1)            // E or e with no number after it
  81.             {
  82.             if (bp->sputbackc(buffer[i])!=EOF)
  83.                 {
  84.                 i--;
  85.                 state &= ~(ios::eofbit);
  86.                 }
  87.             else
  88.                 {
  89.                 state |= ios::failbit;
  90.                 }
  91.             }
  92.         if ((!fDigit) || (i==buflen))
  93.             state |= ios::failbit;
  94.  
  95.         // buffer contains a valid number or '\0'
  96.         buffer[i] = '\0';
  97.         isfx();
  98.         }
  99.     return i;
  100. }
  101.