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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - ios.cpp
  3.  * Class ios member functions
  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 <iostream.h>
  17.  
  18. union ios_user_union {
  19.     long lword;
  20.     void *pword;
  21. };
  22.  
  23. // static members of ios
  24. const long ios::basefield = ios::dec | ios::oct | ios::hex;
  25. const long ios::adjustfield = ios::left | ios::right | ios::internal;
  26. const long ios::floatfield = ios::scientific | ios::fixed;
  27. long ios::nextbit = 16;
  28. int ios::usercount = 0;
  29.  
  30.  
  31. ios::ios()
  32. {
  33.     userwords = 0;
  34. }
  35.  
  36.  
  37. ios::ios(streambuf* sb)
  38. {
  39.     userwords = 0;
  40.     init(sb);
  41. }
  42.  
  43.  
  44. void ios::init(streambuf* sbp)
  45. {
  46.     nwords = 0;
  47.     state = ospecial = 0;
  48.     ispecial = skipping;
  49.     x_flags = skipws;
  50.     x_precision = x_width = 0;
  51.     x_fill = ' ';
  52.     x_tie = 0;
  53.     bp = sbp;
  54. }
  55.  
  56.  
  57. ios::~ios()
  58. {
  59.     if( userwords )
  60.     delete userwords;
  61. }
  62.  
  63.  
  64. // static void sync_with_stdio() { } TO BE IMPLEMENTED
  65.  
  66.  
  67. ostream* ios::tie(ostream* s)
  68. {
  69.     ostream *x = x_tie;
  70.     // documentation doesn't state whether to flush old tied stream
  71.     if( x ) x->flush();
  72.     x_tie = s;
  73.     if( s ) {
  74.     ispecial |= tied;
  75.     ospecial |= tied;
  76.     }
  77.     else {
  78.     ispecial &= ~tied;
  79.     ospecial &= ~tied;
  80.     }
  81.     return x;
  82. }
  83.  
  84.  
  85. long ios::flags(long l)
  86. {
  87.     long x = x_flags;
  88.     x_flags = l;
  89.     if( l & ios::skipws )
  90.     ispecial |= skipping;
  91.     else
  92.     ispecial &= ~skipping;
  93.     return x;
  94. }
  95.  
  96.  
  97. long ios::setf(long _setbits, long _field)
  98. {
  99.     long x = x_flags & _field;
  100.     x_flags &= ~_field;
  101.     x_flags |= (_setbits & _field);
  102.     if( x_flags & ios::skipws )
  103.     ispecial |= skipping;
  104.     else
  105.     ispecial &= ~skipping;
  106.     return x;
  107. }
  108.  
  109.  
  110. long ios::setf(long l)
  111. {
  112.     long x = x_flags & l;
  113.     x_flags |= l;
  114.     if( x_flags & ios::skipws )
  115.     ispecial |= skipping;
  116.     else
  117.     ispecial &= ~skipping;
  118.     return x;
  119. }
  120.  
  121.  
  122. long ios::unsetf(long l)
  123. {
  124.     long x = x_flags & l;
  125.     x_flags &= ~l;
  126.     if( x_flags & ios::skipws )
  127.     ispecial |= skipping;
  128.     else
  129.     ispecial &= ~skipping;
  130.     return x;
  131. }
  132.  
  133.  
  134. ios& dec(ios& s)
  135. {
  136.     s.setf(ios::dec, ios::basefield);
  137.     return s;
  138. }
  139.  
  140.  
  141. ios& hex(ios& s)
  142. {
  143.     s.setf(ios::hex, ios::basefield);
  144.     return s;
  145. }
  146.  
  147.  
  148. ios& oct(ios& s)
  149. {
  150.     s.setf(ios::oct, ios::basefield);
  151.     return s;
  152. }
  153.  
  154.  
  155. void ios::clear(int i)
  156. {
  157.     state = (i & 0xFF) | (state & hardfail);
  158.     ispecial = (ispecial & ~0xFF) | state;
  159.     ospecial = (ospecial & ~0xFF) | state;
  160. }
  161.  
  162.  
  163. void ios::setstate(int b)
  164. {
  165.     state |= (b & 0xFF);
  166.     ispecial |= (b & ~(skipping | tied));
  167.     ospecial |= (b & ~tied);
  168. }
  169.  
  170.  
  171. int ios::skip(int i)
  172. {
  173.     int x = (ispecial & skipping);
  174.     if( i ) {
  175.     ispecial |= skipping;
  176.     x_flags |= skipws;
  177.     }
  178.     else {
  179.     ispecial &= ~skipping;
  180.     x_flags &= ~skipws;
  181.     }
  182.     return x != 0;
  183. }
  184.  
  185.  
  186. // supply a new flags bit
  187. long ios::bitalloc()
  188. {
  189.     if( nextbit >= 31 )
  190.     return 0;
  191.     return 1 << ++nextbit;
  192. }
  193.  
  194.  
  195. // supply a new word index for derived class use
  196. int ios::xalloc()
  197. {
  198.     return ++usercount;
  199. }
  200.  
  201.  
  202. // allocate or resize user array -- we assume i > nwords
  203. void ios::usersize(int i)
  204. {
  205.     // allocate space for new array
  206.     ios_user_union *p = new ios_user_union[i];
  207.  
  208.     if( ! p )
  209.     return;        // cannot enlarge array
  210.  
  211.     if( nwords ) {    // copy old array to new one
  212.     memcpy(p, userwords, nwords * sizeof(ios_user_union));
  213.     delete userwords;
  214.     }
  215.     userwords = p;
  216.     do {        // set new words to zero
  217.     p[nwords].lword = 0;
  218.     } while( ++nwords < i );
  219. }
  220.  
  221.  
  222. // return user word as an integer
  223. long& ios::iword(int i)
  224. {
  225.     static long l = 0;        // for error return
  226.     if( i < 1  ||  usercount < i )
  227.     return l;        // no such entry
  228.     if( i > nwords )
  229.     usersize(i);
  230.     return (i > nwords) ? l : userwords[i-1].lword;
  231. }
  232.  
  233.  
  234. // return user word as a void pointer
  235. void*& ios::pword(int i)
  236. {
  237.     static void* p = 0;        // for error return
  238.     if( i < 1  ||  usercount < i )
  239.     return p;        // no such entry
  240.     if( i > nwords )
  241.     usersize(i);
  242.     return (i > nwords) ? p : userwords[i-1].pword;
  243. }
  244.