home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 8.ddi / IOSTRSR1.ZIP / ISTELNG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.5 KB  |  133 lines

  1. /*[]------------------------------------------------------------[]*/
  2. /*|                                                              |*/
  3. /*|     istelng.cpp                                              |*/
  4. /*|                                                              |*/
  5. /*|     Class istream                                            |*/
  6. /*|          istream& istream::operator>> ( long& )              |*/
  7. /*|                                                              |*/
  8. /*[]------------------------------------------------------------[]*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1990, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18. #include <ioconfig.h>
  19. #include <iostream.h>
  20. #include <ctype.h>
  21.  
  22. // extract decimal value from istream, given first character c
  23. // error if c is not a valid character
  24.  
  25. static unsigned long getdec(istream _FAR & i, int c)
  26. {
  27.     unsigned long l = 0;
  28.     if( ! isdigit(c) )
  29.         {
  30.         if( c == EOF )
  31.             i.clear(i.rdstate() | ios::eofbit | ios::failbit);
  32.         else
  33.             i.clear(i.rdstate() | ios::failbit);
  34.         }
  35.     else
  36.         {
  37.         do  {
  38.             l = 10 * l + c - '0';
  39.             c = i.rdbuf()->snextc();
  40.             } while( isdigit(c) );
  41.         }
  42.     return l;
  43. }
  44.  
  45.  
  46. // extract octal value from istream, given first character c
  47. // error if c is not a valid character
  48.  
  49. static unsigned long getoct(istream _FAR & i, int c)
  50. {
  51.     unsigned long l = 0;
  52.     if( c < '0'  ||  '7' < c )
  53.         {
  54.         if( c == EOF )
  55.             i.clear(i.rdstate() | ios::eofbit | ios::badbit);
  56.         else
  57.             i.clear(i.rdstate() | ios::failbit);
  58.         }
  59.     else
  60.         {
  61.         do  {
  62.             l = 8 * l + c - '0';
  63.             c = i.rdbuf()->snextc();
  64.             } while( '0' <= c  &&  c <= '7' );
  65.         }
  66.     return l;
  67. }
  68.  
  69.  
  70. // extract hex value from istream, given first character c
  71. // error if c is not a valid character
  72.  
  73. static unsigned long gethex(istream _FAR & i, int c)
  74. {
  75.     unsigned long l = 0;
  76.     if( ! isxdigit(c) )
  77.         {
  78.         if( c == EOF )
  79.             i.clear(i.rdstate() | ios::eofbit | ios::badbit);
  80.         else
  81.             i.clear(i.rdstate() | ios::failbit);
  82.         }
  83.     else
  84.         {
  85.         do  {
  86.             if( isupper(c) )
  87.                 c -= ('A' - 10);
  88.             else if( islower(c) )
  89.                 c -= ('a' - 10);
  90.             else
  91.                 c -= '0';
  92.             l = 16 * l + c;
  93.             c = i.rdbuf()->snextc();
  94.             } while( isxdigit(c) );
  95.     }
  96.     return l;
  97. }
  98.  
  99. istream _FAR & istream::operator>> (long _FAR & l)
  100. {
  101.  
  102.     if( ipfx0() )
  103.         {
  104.         unsigned long u = 0;    // result
  105.         int c = bp->sgetc();
  106.         int neg = c == '-';
  107.         if( c == '+' || neg )
  108.             c = bp->snextc();
  109.         if( flags() & ios::hex )
  110.             u = gethex(*this, c);
  111.         else if( flags() & ios::oct )
  112.             u = getoct(*this, c);
  113.         else if( (flags() & ios::dec)  ||  c != '0' )
  114.             u = getdec(*this, c);
  115.         else
  116.             {  // c == '0'
  117.             c = bp->snextc();
  118.             if( c == 'x' || c == 'X' )
  119.                 {
  120.                 c = bp->snextc();
  121.                 u = gethex(*this, c);
  122.                 }
  123.             else if( '0' <= c && c <= '7' )
  124.                 u = getoct(*this, c);
  125.             else // a lone '0'
  126.                 u = 0;
  127.             }
  128.  
  129.         l = neg ? -long(u) : long(u);
  130.         }
  131.     return *this;
  132. }
  133.