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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - istreami.cpp
  3.  * Class istream member functions for extracting integral types
  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. /*
  17.     To reduce the total amount of code, shorts and ints are extracted into
  18.     a long, and the result stored in the proper type.  If long arithmetic
  19.     creates an unacceptable overhead, you can duplicate the code with
  20.     the obvious modifications for shorts or ints.
  21. */
  22.  
  23. #include <iostream.h>
  24. #include <ctype.h>
  25.  
  26.  
  27. istream& istream::operator>> (short& i)
  28. {
  29.     long l;
  30.     *this >> l;
  31.     if( ! fail() )
  32.     i = (short) l;
  33.     return *this;
  34. }
  35.  
  36.  
  37. istream& istream::operator>> (unsigned short& i)
  38. {
  39.     unsigned long l;
  40.     *this >> l;
  41.     if( ! fail() )
  42.     i = (unsigned short) l;
  43.     return *this;
  44. }
  45.  
  46.  
  47. istream& istream::operator>> (int& i)
  48. {
  49.     long l;
  50.     *this >> l;
  51.     if( ! fail() )
  52.     i = (int) l;
  53.     return *this;
  54. }
  55.  
  56.  
  57. istream& istream::operator>> (unsigned int& i)
  58. {
  59.     unsigned long l;
  60.     *this >> l;
  61.     if( ! fail() )
  62.     i = (unsigned int) l;
  63.     return *this;
  64. }
  65.  
  66.  
  67. istream& istream::operator>> (unsigned long& u)
  68. {
  69.     long l;
  70.     *this >> l;
  71.     if( ! fail() )
  72.     u = l;
  73.     return *this;
  74. }
  75.  
  76.  
  77. /*
  78.  * This is the actual code which does the extraction
  79.  */
  80.  
  81. // extract decimal value from istream, given first character c
  82. // error if c is not a valid character
  83. static unsigned long getdec(istream& i, int c)
  84. {
  85.     unsigned long l = 0;
  86.     if( ! isdigit(c) ) {
  87.     if( c == EOF )
  88.         i.clear(i.rdstate() | ios::eofbit | ios::badbit);
  89.     else
  90.         i.clear(i.rdstate() | ios::failbit);
  91.     }
  92.     else {
  93.     do {
  94.         l = 10 * l + c - '0';
  95.         c = i.rdbuf()->snextc();
  96.     } while( isdigit(c) );
  97.     }
  98.     return l;
  99. }
  100.  
  101.  
  102. // extract octal value from istream, given first character c
  103. // error if c is not a valid character
  104. static unsigned long getoct(istream& i, int c)
  105. {
  106.     unsigned long l = 0;
  107.     if( c < '0'  ||  '7' < c ) {
  108.     if( c == EOF )
  109.         i.clear(i.rdstate() | ios::eofbit | ios::badbit);
  110.     else
  111.         i.clear(i.rdstate() | ios::failbit);
  112.     }
  113.     else {
  114.     do {
  115.         l = 8 * l + c - '0';
  116.         c = i.rdbuf()->snextc();
  117.     } while( '0' <= c  &&  c <= '7' );
  118.     }
  119.     return l;
  120. }
  121.  
  122.  
  123. // extract hex value from istream, given first character c
  124. // error if c is not a valid character
  125. static unsigned long gethex(istream& i, int c)
  126. {
  127.     unsigned long l = 0;
  128.     if( ! isxdigit(c) ) {
  129.     if( c == EOF )
  130.         i.clear(i.rdstate() | ios::eofbit | ios::badbit);
  131.     else
  132.         i.clear(i.rdstate() | ios::failbit);
  133.     }
  134.     else {
  135.     do {
  136.         if( isupper(c) ) c -= 'A';
  137.         else if( islower(c) ) c -= 'a';
  138.         else c -= '0';
  139.         l = 16 * l + c;
  140.         c = i.rdbuf()->snextc();
  141.     } while( isxdigit(c) );
  142.     }
  143.     return l;
  144. }
  145.  
  146.  
  147. istream& istream::operator>> (long& l)
  148. {
  149.  
  150.     if( ipfx0() ) {
  151.     unsigned long u = 0;    // result
  152.     int c = bp->sgetc();
  153.     int neg = c == '-';
  154.     if( c == '+' || neg )
  155.         c = bp->snextc();
  156.     if( flags() & ios::hex ) 
  157.         u = gethex(*this, c);
  158.     else if( flags() & ios::oct )
  159.         u = getoct(*this, c);
  160.     else if( (flags() & ios::dec)  ||  c != '0' )
  161.         u = getdec(*this, c);
  162.     else {    // c == '0'
  163.         c = bp->snextc();
  164.         if( c == 'x' || c == 'X' ) {
  165.         c = bp->snextc();
  166.         u = gethex(*this, c);
  167.         }
  168.         else if( '0' <= c && c <= '7' )
  169.         u = getoct(*this, c);
  170.         else // a lone '0'
  171.         u = 0;
  172.     }
  173.  
  174.     l = neg ? -long(u) : long(u);
  175.     }
  176.     return *this;
  177. }
  178.