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.

243 lines
7.6 KiB

  1. // odsstream.h c++ std lib output stream using win32 OutputDebugString
  2. // copied from vc98 fstream
  3. // Copyright (c) Microsoft Corporation 1998.
  4. #pragma once
  5. #if !defined(DEBUG) && (defined(_DBG) || defined(DBG) || defined(_DEBUG))
  6. #define DEBUG 1
  7. #endif
  8. #if !defined(ODSSTREAM_H) && defined(DEBUG)
  9. #define ODSSTREAM_H
  10. #ifdef _MSC_VER
  11. #pragma pack(push,8)
  12. #endif /* _MSC_VER */
  13. #include <tchar.h>
  14. #include <ostream>
  15. #include <tstring.h>
  16. inline bool Debugputc(TCHAR outch) {
  17. TCHAR o[2];
  18. o[0] = outch;
  19. o[1] = 0;
  20. TCHAR *p = o;
  21. OutputDebugString(p);
  22. return true;
  23. }
  24. // TEMPLATE CLASS basic_debugbuf
  25. template<class _E, class _Tr = std::char_traits<_E> >
  26. class basic_debugbuf : public std::basic_streambuf<_E, _Tr> {
  27. public:
  28. typedef typename std::codecvt<_E, TCHAR, typename _Tr::state_type> _Cvt;
  29. typedef typename std::basic_streambuf<_E, _Tr> _Mysb;
  30. typedef basic_debugbuf<_E, _Tr> _Myt;
  31. basic_debugbuf() : _Loc(), _Mysb() {
  32. _Init();
  33. }
  34. virtual ~basic_debugbuf() {
  35. delete _Str;
  36. }
  37. protected:
  38. virtual int_type overflow(int_type _C = _Tr::eof()) {
  39. if (_Tr::eq_int_type(_Tr::eof(), _C)) {
  40. return (_Tr::not_eof(_C));
  41. } else if (pptr() != 0 && pptr() < epptr()) {
  42. *_Pninc() = _Tr::to_char_type(_C);
  43. return (_C);
  44. } else if (_Pcvt == 0) {
  45. Debugputc(_Tr::to_char_type(_C));
  46. return _C;
  47. } else {
  48. const int _NC = 8;
  49. const _E _X = _Tr::to_char_type(_C);
  50. const _E *_S;
  51. TCHAR *_D;
  52. _Str->erase();
  53. for (size_t _I = _NC; ; _I += _NC) {
  54. _Str->append(_NC, '\0');
  55. switch (_Pcvt->out(_State,
  56. &_X, &_X + 1, _S,
  57. _Str->begin(), _Str->end(), _D)) {
  58. case std::codecvt_base::partial:
  59. if (_S == &_X)
  60. return (_Tr::eof());
  61. case std::codecvt_base::ok: {// can fall through
  62. size_t _N = _D - _Str->begin();
  63. OutputDebugString(_Str->begin());
  64. return _C;
  65. }
  66. case std::codecvt_base::noconv:
  67. Debugputc(_X);
  68. return _C;
  69. default:
  70. return (_Tr::eof());
  71. }
  72. }
  73. }
  74. }
  75. virtual int_type uflow() {return (_Tr::eof());}
  76. void _Init() {
  77. static _Tr::state_type _Stinit;
  78. _Loc.locale::~locale();
  79. new (&_Loc) std::locale;
  80. _Str = 0;
  81. _Mysb::_Init();
  82. _State = _Stinit;
  83. _State0 = _Stinit;
  84. _Pcvt = 0;
  85. }
  86. void _Initcvt() {
  87. _Pcvt = (_Cvt *)&_USE(getloc(), _Cvt);
  88. _Loc = _ADDFAC(_Loc, _Pcvt);
  89. if (_Pcvt->always_noconv())
  90. _Pcvt = 0;
  91. if (_Str == 0)
  92. _Str = new string;
  93. }
  94. private:
  95. _Cvt *_Pcvt;
  96. typename _Tr::state_type _State0;
  97. typename _Tr::state_type _State;
  98. Tstring *_Str;
  99. std::locale _Loc;
  100. };
  101. template<class _E, class _Tr = std::char_traits<_E> >
  102. class basic_otstream : public std::basic_ostream<_E, _Tr> {
  103. public:
  104. typedef std::basic_ostream<_E, _Tr> MytBase;
  105. typedef std::basic_ios<_E, _Tr> _Myios;
  106. typedef std::basic_streambuf<_E, _Tr> _Mysb;
  107. typedef basic_otstream<_E, _Tr> _Myt;
  108. basic_otstream() {}
  109. explicit basic_otstream(std::basic_streambuf<_E, _Tr> *_S,
  110. bool _Isstd = false, bool _Doinit = true) : std::basic_ostream<_E, _Tr>(_S, _Isstd, _Doinit) {}
  111. virtual ~basic_otstream()
  112. {}
  113. #if 0
  114. template<class T> inline _Myt& operator<<(T i) {
  115. MytBase::operator<<(i);
  116. return *this;
  117. }
  118. #endif
  119. inline _Myt& operator<<(_Myt& (__cdecl *_F)(_Myt&)) {
  120. return ((*_F)(*this));
  121. }
  122. inline _Myt& operator<<(MytBase& (__cdecl *_F)(MytBase&)) {
  123. MytBase::operator<<(_F);
  124. return *this;
  125. }
  126. inline _Myt& operator<<(_Myios& (__cdecl *_F)(_Myios&)) {
  127. MytBase::operator<<(f);
  128. return *this;
  129. }
  130. inline _Myt& operator<<(_Mysb *b) {
  131. MytBase::operator<<(b);
  132. return *this;
  133. }
  134. inline _Myt& operator<<(const void *const i) {
  135. MytBase::operator<<(i);
  136. return *this;
  137. }
  138. inline _Myt& operator<<(float i) {
  139. MytBase::operator<<(i);
  140. return *this;
  141. }
  142. inline _Myt& operator<<(double i) {
  143. MytBase::operator<<(i);
  144. return *this;
  145. }
  146. inline _Myt& operator<<(int i) {
  147. MytBase::operator<<(i);
  148. return *this;
  149. }
  150. inline _Myt& operator<<(unsigned int i) {
  151. MytBase::operator<<(i);
  152. return *this;
  153. }
  154. inline _Myt& operator<<(long i) {
  155. MytBase::operator<<(i);
  156. return *this;
  157. }
  158. inline _Myt& operator<<(unsigned long i) {
  159. MytBase::operator<<(i);
  160. return *this;
  161. }
  162. #ifdef _UNICODE
  163. _Myt& operator<<(LPCSTR _X) {
  164. USES_CONVERSION;
  165. return operator<<(A2W(_X));
  166. }
  167. #else
  168. _Myt& operator<<(const OLECHAR *_X) {
  169. USES_CONVERSION;
  170. operator<<(W2A(_X));
  171. return *this;
  172. }
  173. #endif
  174. _Myt& operator<<(LPCTSTR _X) {
  175. std::operator<<(static_cast<MytBase&>(*this), _X);
  176. return *this;
  177. }
  178. #if 0
  179. inline _Myt& operator<<(Tstring t) {
  180. return operator<<(t.c_str());
  181. }
  182. #endif
  183. };
  184. typedef basic_otstream<TCHAR> tostream;
  185. // TEMPLATE CLASS basic_ofstream
  186. template<class _E, class _Tr = std::char_traits<_E> >
  187. class basic_odebugstream : public basic_otstream<_E, _Tr> {
  188. public:
  189. typedef basic_odebugstream<_E, _Tr> _Myt;
  190. typedef basic_debugbuf<_E, _Tr> _Myfb;
  191. basic_odebugstream()
  192. : basic_otstream<_E, _Tr>(&_Fb) {}
  193. basic_odebugstream(_Myt &mt) : basic_otstream<_E, _Tr>(mt._Fb) {}
  194. virtual ~basic_odebugstream()
  195. {}
  196. private:
  197. _Myfb _Fb;
  198. };
  199. #ifdef _MSC_VER
  200. #pragma pack(pop)
  201. #endif /* _MSC_VER */
  202. typedef basic_debugbuf<TCHAR> TdbgBuf;
  203. typedef basic_odebugstream<TCHAR> TdbgStream;
  204. // the msvc 6 crt fstream header this was copied from contained the following notice:
  205. /*
  206. * Copyright (c) 1994 by P.J. Plauger. ALL RIGHTS RESERVED.
  207. * Consult your license regarding permissions and restrictions.
  208. */
  209. #endif
  210. // end of file odsstream.h