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

  1. /***
  2. * istrgint.cpp - definitions for istream class core integer routines
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Definitions of member function getint() for istream class.
  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::getint(char * buffer) - get an int
  20. *
  21. *Purpose:
  22. *       Get an int from stream.
  23. *
  24. *Entry:
  25. *       char * buffer   = area for number to be copied.
  26. *
  27. *Exit:
  28. *       Returns base for conversion: (0, 2, 8, or 16).
  29. *       If successful, buffer[] contains the number, followed by \0.
  30. *
  31. *Exceptions:
  32. *       Sets ios::failbit on error forming number.
  33. *       Sets ios::badbit on error after failbit
  34. *       Sets ios::eofbit if at EOF at return
  35. *
  36. *******************************************************************************/
  37. int     istream::getint(char * buffer)  // returns length
  38. {
  39.     int base, i;
  40.     int c;
  41.     int fDigit = 0;
  42.     int bindex = 1;
  43.  
  44.     if (x_flags & ios::dec)
  45.         base = 10;
  46.     else if (x_flags & ios::hex)
  47.         base = 16;
  48.     else if (x_flags & ios::oct)
  49.         base = 8;
  50.     else
  51.         base = 0;
  52.  
  53.     if (ipfx(0))
  54.         {
  55.         c=bp->sgetc();
  56.         for (i = 0; i<MAXLONGSIZ-1; buffer[i] = (char)c,c=bp->snextc(),i++)
  57.             {
  58.             if (c==EOF)
  59.                 {
  60.                 state |= ios::eofbit;
  61.                 break;
  62.                 }
  63.             if (!i)
  64.                 {
  65.                 if ((c=='-') || (c=='+'))
  66.                     {
  67.                     bindex++;
  68.                     continue;
  69.                     }
  70.                 }
  71.             if ((i==bindex) && (buffer[i-1]=='0'))
  72.                 {
  73.                 if (((c=='x') || (c=='X')) && ((base==0) || (base==16)))
  74.                     {
  75.                     base = 16;  // simplifies matters
  76.                     fDigit = 0;
  77.                     continue;
  78.                     }
  79.                 else if (base==0)
  80.                     {
  81.                     base = 8;
  82.                     }
  83.                 }
  84.  
  85.  
  86.             // now simply look for a digit and set fDigit if found else break
  87.  
  88.             if (base==16)
  89.                 {
  90.                 if (!isxdigit(c))
  91.                     break;
  92.                 }
  93.             else if ((!isdigit(c)) || ((base==8) && (c>'7')))
  94.                 break;
  95.  
  96.             fDigit++;
  97.             }
  98.         if (!fDigit)
  99.             {
  100.                 state |= ios::failbit;
  101.                 while (i--)
  102.                     {
  103.                     if(bp->sputbackc(buffer[i])==EOF)
  104.                         {
  105.                         state |= ios::badbit;
  106.                         break;
  107.                         }
  108.                     else
  109.                         state &= ~(ios::eofbit);
  110.                     }
  111.                 i=0;
  112.                 }
  113.         // buffer contains a valid number or '\0'
  114.         buffer[i] = '\0';
  115.         isfx();
  116.         }
  117.     if (i==MAXLONGSIZ)
  118.         {
  119.         state |= ios::failbit;
  120.         }
  121.     return base;
  122. }
  123.