Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.0 KiB

  1. // ios_base -- ios_base basic members
  2. #include <new>
  3. #include <xiosbase>
  4. #include <xdebug>
  5. _STD_BEGIN
  6. #define NSTDSTR 8 /* cin, wcin, cout, wcout, cerr, wcerr, clog, wclog */
  7. extern _CRTIMP2 const streamoff
  8. _BADOFF = -1; // initialize constant for bad file offset
  9. _CRTIMP2 fpos_t _Fpz = {0}; // initialize constant for beginning of file
  10. int ios_base::_Index = 0; // initialize source of unique indexes
  11. bool ios_base::_Sync = true; // initialize synchronization flag
  12. static ios_base *stdstr[NSTDSTR + 2] =
  13. {0}; // [1, NSTDSTR] hold pointers to standard streams
  14. static char stdopens[NSTDSTR + 2] =
  15. {0}; // [1, NSTDSTR] hold open counts for standard streams
  16. void ios_base::clear(iostate state, bool reraise)
  17. { // set state, possibly reraise exception
  18. _Mystate = (iostate)(state & _Statmask);
  19. if ((_Mystate & _Except) == 0)
  20. ;
  21. else if (reraise)
  22. _RERAISE;
  23. else if (_Mystate & _Except & badbit)
  24. _THROW(failure, "ios_base::badbit set");
  25. else if (_Mystate & _Except & failbit)
  26. _THROW(failure, "ios_base::failbit set");
  27. else
  28. _THROW(failure, "ios_base::eofbit set");
  29. }
  30. ios_base& ios_base::copyfmt(const ios_base& right)
  31. { // copy format stuff
  32. if (this != &right)
  33. { // copy all but _Mystate
  34. _Tidy();
  35. *_Ploc = *right._Ploc;
  36. _Fmtfl = right._Fmtfl;
  37. _Prec = right._Prec;
  38. _Wide = right._Wide;
  39. _Iosarray *p = right._Arr;
  40. for (_Arr = 0; p != 0; p = p->_Next)
  41. if (p->_Lo != 0 || p->_Vp != 0)
  42. { // copy over nonzero array values
  43. iword(p->_Index) = p->_Lo;
  44. pword(p->_Index) = p->_Vp;
  45. }
  46. for (_Fnarray *q = right._Calls; q != 0; q = q->_Next)
  47. register_callback(q->_Pfn, q->_Index); // copy callbacks
  48. _Callfns(copyfmt_event); // call callbacks
  49. exceptions(right._Except); // cause any throw at end
  50. }
  51. return (*this);
  52. }
  53. locale ios_base::imbue(const locale& loc)
  54. { // set locale to argument
  55. locale oldlocale = *_Ploc;
  56. *_Ploc = loc;
  57. _Callfns(imbue_event);
  58. return (oldlocale);
  59. }
  60. void ios_base::register_callback(event_callback pfn, int idx)
  61. { // register event handler
  62. _Calls = _NEW_CRT _Fnarray(idx, pfn, _Calls);
  63. }
  64. ios_base::~ios_base()
  65. { // destroy the object
  66. if (0 < _Stdstr && 0 < --stdopens[_Stdstr])
  67. return;
  68. _Tidy();
  69. _DELETE_CRT(_Ploc);
  70. }
  71. void ios_base::_Callfns(event ev)
  72. { // call all event handlers, reporting event
  73. for (_Fnarray *p = _Calls; p != 0; p = p->_Next)
  74. (*p->_Pfn)(ev, *this, p->_Index);
  75. }
  76. ios_base::_Iosarray& ios_base::_Findarr(int idx)
  77. { // locate or make a variable array element
  78. static _Iosarray stub(0, 0);
  79. _Iosarray *p, *q;
  80. if (idx < 0)
  81. { // handle bad index
  82. setstate(badbit);
  83. return (stub);
  84. }
  85. for (p = _Arr, q = 0; p != 0; p = p->_Next)
  86. if (p->_Index == idx)
  87. return (*p); // found element, return it
  88. else if (q == 0 && p->_Lo == 0 && p->_Vp == 0)
  89. q = p; // found recycling candidate
  90. if (q != 0)
  91. { // recycle existing element
  92. q->_Index = idx;
  93. return (*q);
  94. }
  95. _Arr = _NEW_CRT _Iosarray(idx, _Arr); // make a new element
  96. return (*_Arr);
  97. }
  98. void ios_base::_Addstd()
  99. { // add standard stream to destructor list
  100. _Lockit lock(_LOCK_STREAM);
  101. for (; ++_Stdstr < NSTDSTR; )
  102. if (stdstr[_Stdstr] == 0 || stdstr[_Stdstr] == this)
  103. break; // found a candidate
  104. stdstr[_Stdstr] = this;
  105. ++stdopens[_Stdstr];
  106. }
  107. void ios_base::_Init()
  108. { // initialize a new ios_base
  109. _Ploc = _NEW_CRT locale;
  110. _Except = goodbit;
  111. _Fmtfl = skipws | dec;
  112. _Prec = 6;
  113. _Wide = 0;
  114. _Arr = 0;
  115. _Calls = 0;
  116. clear(goodbit);
  117. }
  118. void ios_base::_Tidy()
  119. { // discard storage for an ios_base
  120. _Callfns(erase_event);
  121. _Iosarray *q1, *q2;
  122. for (q1 = _Arr; q1 != 0; q1 = q2)
  123. q2 = q1->_Next, _DELETE_CRT(q1); // delete array elements
  124. _Arr = 0;
  125. _Fnarray *q3, *q4;
  126. for (q3 = _Calls; q3 != 0; q3 = q4)
  127. q4 = q3->_Next, _DELETE_CRT(q3); // delete callback elements
  128. _Calls = 0;
  129. }
  130. _STD_END
  131. /*
  132. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  133. * Consult your license regarding permissions and restrictions.
  134. V3.10:0009 */