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

  1. // ios_base -- ios_base basic members
  2. #include <new>
  3. #include <xiosbase>
  4. #include <xstddef>
  5. _STD_BEGIN
  6.  
  7. #define NSTDSTR    8    /* cin, wcin, etc. */
  8.  
  9. extern _CRTIMP2 const fpos_t _Fpz;
  10.  
  11. int ios_base::_Index = 0;
  12. bool ios_base::_Sync = true;
  13. const fpos_t _Fpz = {0};
  14.  
  15. static ios_base *stdstr[NSTDSTR + 1] = {0};
  16. static char stdopens[NSTDSTR + 1] = {0};
  17.  
  18. void ios_base::clear(iostate ns, bool ex)
  19.     {    // clear all but selected state bits
  20.     _State = (iostate)(ns & _Statmask);
  21.     if ((_State & _Except) == 0)
  22.         ;
  23.     else if (ex)
  24.         _RERAISE;
  25.     else
  26.         _THROW(failure,
  27.             _State & _Except & badbit ? "ios::badbit set"
  28.             : _State & _Except & failbit ? "ios::failbit set"
  29.             : "ios::eofbit set");
  30.     }
  31.  
  32. ios_base& ios_base::copyfmt(const ios_base& rhs)
  33.     {    // copy format info from another ios_base
  34.     if (this != &rhs)
  35.         {    // copy all but _State
  36.         _Tidy();
  37.         _Loc = rhs._Loc;
  38.         _Fmtfl = rhs._Fmtfl;
  39.         _Prec = rhs._Prec;
  40.         _Wide = rhs._Wide;
  41.         _Iosarray *p = rhs._Arr;
  42.         for (_Arr = 0; p != 0; p = p->_Next)
  43.             if (p->_Lo != 0 || p->_Vp != 0)
  44.                 {    // copy over nonzero array values
  45.                 iword(p->_Index) = p->_Lo;
  46.                 pword(p->_Index) = p->_Vp;
  47.                 }
  48.         _Callfns(copyfmt_event);
  49.         exceptions(rhs._Except);    // cause any throw at end
  50.         }
  51.     return (*this);
  52.     }
  53.  
  54. locale ios_base::imbue(const locale& _Ln)
  55.     {    // imbue a new locale into stream
  56.     locale _Lo = _Loc;
  57.     _Loc = _Ln;
  58.     _Callfns(imbue_event);
  59.     return (_Lo);
  60.     }
  61.  
  62. void ios_base::register_callback(event_callback _P, int _Idx)
  63.     {    // register a callback function
  64.     if ((_Calls = new _Fnarray(_Idx, _P, _Calls)) == 0)
  65.         _Nomemory();
  66.     }
  67.  
  68. ios_base::~ios_base()
  69.     {    // destruct an ios_base
  70.     if (0 < _Stdstr && 0 < --stdopens[_Stdstr])
  71.         return;
  72.     _Tidy();
  73.     }
  74.  
  75. void ios_base::_Callfns(event ev)
  76.     {    // call registered functions
  77.     _Fnarray *p;
  78.     for (p = _Calls; p != 0; p = p->_Next)
  79.         (*p->_Pfn)(ev, *this, p->_Index);
  80.     }
  81.  
  82. ios_base::_Iosarray& ios_base::_Findarr(int idx)
  83.     {    // locate or make a variable array element
  84.     _Iosarray *p, *q;
  85.     if (idx < 0)
  86.         _THROW(failure, "invalid ios::iword/pword index");
  87.     for (p = _Arr, q = 0; p != 0; p = p->_Next)
  88.         if (p->_Index == idx)
  89.             return (*p);
  90.         else if (q == 0 && p->_Lo == 0 && p->_Vp == 0)
  91.             q = p;
  92.     if (q != 0)
  93.         {    // recycle existing element
  94.         q->_Index = idx;
  95.         return (*q);
  96.         }
  97.     if ((_Arr = new _Iosarray(idx, _Arr)) == 0)
  98.         _Nomemory();
  99.     return (*_Arr);
  100.     }
  101.  
  102. void ios_base::_Addstd()
  103.     {    // add standard stream to destructor list
  104.     _Lockit _Lk;
  105.     for (; _Stdstr < NSTDSTR; ++_Stdstr)
  106.         if (stdstr[_Stdstr] == 0 || stdstr[_Stdstr] == this)
  107.             break;
  108.     stdstr[_Stdstr] = this;
  109.     ++stdopens[_Stdstr];
  110.     }
  111.  
  112. void ios_base::_Init()
  113.     {    // initialize a new ios_base
  114.     new (&_Loc) locale;
  115.     _Except = goodbit;
  116.     _Fmtfl = skipws | dec;
  117.     _Prec = 6;
  118.     _Wide = 0;
  119.     _Arr = 0;
  120.     _Calls = 0;
  121.     clear(goodbit);
  122.     }
  123.  
  124. void ios_base::_Tidy()
  125.     {    // discard storage for an ios_base
  126.     _Callfns(erase_event);
  127.     _Iosarray *q1, *q2;
  128.     for (q1 = _Arr; q1 != 0; q1 = q2)
  129.         q2 = q1->_Next, delete q1;
  130.     _Arr = 0;
  131.     _Fnarray *q3, *q4;
  132.     for (q3 = _Calls; q3 != 0; q3 = q4)
  133.         q4 = q3->_Next, delete q3;
  134.     _Calls = 0;
  135.     }
  136. _STD_END
  137.  
  138. /*
  139.  * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  140.  * Consult your license regarding permissions and restrictions.
  141.  */
  142.