Source code of Windows XP (NT5)
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.

467 lines
13 KiB

  1. // ostream standard header
  2. #ifndef _OSTREAM_
  3. #define _OSTREAM_
  4. #include <ios>
  5. #ifdef _MSC_VER
  6. #pragma pack(push,8)
  7. #endif /* _MSC_VER */
  8. // I/O exception macros
  9. #define _TRY_IO_BEGIN _TRY_BEGIN
  10. #define _CATCH_IO_END _CATCH_ALL \
  11. setstate(ios_base::badbit, true); _CATCH_END
  12. #define _CATCH_IO_(x) _CATCH_ALL \
  13. (x).setstate(ios_base::badbit, true); _CATCH_END
  14. _STD_BEGIN
  15. // TEMPLATE CLASS basic_ostream
  16. template<class _E, class _Tr = char_traits<_E> >
  17. class basic_ostream : virtual public basic_ios<_E, _Tr> {
  18. public:
  19. typedef basic_ostream<_E, _Tr> _Myt;
  20. typedef basic_ios<_E, _Tr> _Myios;
  21. typedef basic_streambuf<_E, _Tr> _Mysb;
  22. typedef ostreambuf_iterator<_E, _Tr> _Iter;
  23. typedef num_put<_E, _Iter> _Nput;
  24. explicit basic_ostream(basic_streambuf<_E, _Tr> *_S,
  25. bool _Isstd = false, bool _Doinit = true)
  26. {if (_Doinit)
  27. init(_S, _Isstd); }
  28. basic_ostream(_Uninitialized)
  29. {_Addstd(); }
  30. virtual ~basic_ostream()
  31. {}
  32. class sentry {
  33. public:
  34. explicit sentry(_Myt& _Os)
  35. : _Ok(_Os.opfx()), _Ostr(_Os) {}
  36. ~sentry()
  37. {if (!uncaught_exception())
  38. _Ostr.osfx(); }
  39. operator bool() const
  40. {return (_Ok); }
  41. private:
  42. bool _Ok;
  43. _Myt& _Ostr;
  44. };
  45. bool opfx()
  46. {if (good() && tie() != 0)
  47. tie()->flush();
  48. return (good()); }
  49. void osfx()
  50. {if (flags() & unitbuf)
  51. flush(); }
  52. _Myt& operator<<(_Myt& (__cdecl *_F)(_Myt&))
  53. {return ((*_F)(*this)); }
  54. _Myt& operator<<(_Myios& (__cdecl *_F)(_Myios&))
  55. {(*_F)(*(_Myios *)this);
  56. return (*this); }
  57. _Myt& operator<<(ios_base& (__cdecl *_F)(ios_base&))
  58. {(*_F)(*(ios_base *)this);
  59. return (*this); }
  60. _Myt& operator<<(_Bool _X)
  61. {iostate _St = goodbit;
  62. const sentry _Ok(*this);
  63. if (_Ok)
  64. {const _Nput& _Fac = _USE(getloc(), _Nput);
  65. _TRY_IO_BEGIN
  66. if (_Fac.put(_Iter(rdbuf()), *this,
  67. fill(), _X).failed())
  68. _St |= badbit;
  69. _CATCH_IO_END }
  70. setstate(_St);
  71. return (*this); }
  72. _Myt& operator<<(short _X)
  73. {iostate _St = goodbit;
  74. const sentry _Ok(*this);
  75. if (_Ok)
  76. {const _Nput& _Fac = _USE(getloc(), _Nput);
  77. fmtflags _Bfl = flags() & basefield;
  78. long _Y = (_Bfl == oct || _Bfl == hex)
  79. ? (long)(unsigned short)_X : (long)_X;
  80. _TRY_IO_BEGIN
  81. if (_Fac.put(_Iter(rdbuf()), *this,
  82. fill(), _Y).failed())
  83. _St |= badbit;
  84. _CATCH_IO_END }
  85. setstate(_St);
  86. return (*this); }
  87. _Myt& operator<<(unsigned short _X)
  88. {iostate _St = goodbit;
  89. const sentry _Ok(*this);
  90. if (_Ok)
  91. {const _Nput& _Fac = _USE(getloc(), _Nput);
  92. _TRY_IO_BEGIN
  93. if (_Fac.put(_Iter(rdbuf()), *this,
  94. fill(), (unsigned long)_X).failed())
  95. _St |= badbit;
  96. _CATCH_IO_END }
  97. setstate(_St);
  98. return (*this); }
  99. _Myt& operator<<(int _X)
  100. {iostate _St = goodbit;
  101. const sentry _Ok(*this);
  102. if (_Ok)
  103. {const _Nput& _Fac = _USE(getloc(), _Nput);
  104. fmtflags _Bfl = flags() & basefield;
  105. long _Y = (_Bfl == oct || _Bfl == hex)
  106. ? (long)(unsigned int)_X : (long)_X;
  107. _TRY_IO_BEGIN
  108. if (_Fac.put(_Iter(rdbuf()), *this,
  109. fill(), _Y).failed())
  110. _St |= badbit;
  111. _CATCH_IO_END }
  112. setstate(_St);
  113. return (*this); }
  114. _Myt& operator<<(unsigned int _X)
  115. {iostate _St = goodbit;
  116. const sentry _Ok(*this);
  117. if (_Ok)
  118. {const _Nput& _Fac = _USE(getloc(), _Nput);
  119. _TRY_IO_BEGIN
  120. if (_Fac.put(_Iter(rdbuf()), *this,
  121. fill(), (unsigned long)_X).failed())
  122. _St |= badbit;
  123. _CATCH_IO_END }
  124. setstate(_St);
  125. return (*this); }
  126. _Myt& operator<<(long _X)
  127. {iostate _St = goodbit;
  128. const sentry _Ok(*this);
  129. if (_Ok)
  130. {const _Nput& _Fac = _USE(getloc(), _Nput);
  131. _TRY_IO_BEGIN
  132. if (_Fac.put(_Iter(rdbuf()), *this,
  133. fill(), _X).failed())
  134. _St |= badbit;
  135. _CATCH_IO_END }
  136. setstate(_St);
  137. return (*this); }
  138. _Myt& operator<<(unsigned long _X)
  139. {iostate _St = goodbit;
  140. const sentry _Ok(*this);
  141. if (_Ok)
  142. {const _Nput& _Fac = _USE(getloc(), _Nput);
  143. _TRY_IO_BEGIN
  144. if (_Fac.put(_Iter(rdbuf()), *this,
  145. fill(), _X).failed())
  146. _St |= badbit;
  147. _CATCH_IO_END }
  148. setstate(_St);
  149. return (*this); }
  150. #ifdef _WIN64
  151. _Myt& operator<<(__int64 _X)
  152. {iostate _St = goodbit;
  153. const sentry _Ok(*this);
  154. if (_Ok)
  155. {const _Nput& _Fac = _USE(getloc(), _Nput);
  156. _TRY_IO_BEGIN
  157. if (_Fac.put(_Iter(rdbuf()), *this,
  158. fill(), _X).failed())
  159. _St |= badbit;
  160. _CATCH_IO_END }
  161. setstate(_St);
  162. return (*this); }
  163. _Myt& operator<<(unsigned __int64 _X)
  164. {iostate _St = goodbit;
  165. const sentry _Ok(*this);
  166. if (_Ok)
  167. {const _Nput& _Fac = _USE(getloc(), _Nput);
  168. _TRY_IO_BEGIN
  169. if (_Fac.put(_Iter(rdbuf()), *this,
  170. fill(), _X).failed())
  171. _St |= badbit;
  172. _CATCH_IO_END }
  173. setstate(_St);
  174. return (*this); }
  175. #endif // _WIN64
  176. _Myt& operator<<(float _X)
  177. {iostate _St = goodbit;
  178. const sentry _Ok(*this);
  179. if (_Ok)
  180. {const _Nput& _Fac = _USE(getloc(), _Nput);
  181. _TRY_IO_BEGIN
  182. if (_Fac.put(_Iter(rdbuf()), *this,
  183. fill(), (double)_X).failed())
  184. _St |= badbit;
  185. _CATCH_IO_END }
  186. setstate(_St);
  187. return (*this); }
  188. _Myt& operator<<(double _X)
  189. {iostate _St = goodbit;
  190. const sentry _Ok(*this);
  191. if (_Ok)
  192. {const _Nput& _Fac = _USE(getloc(), _Nput);
  193. _TRY_IO_BEGIN
  194. if (_Fac.put(_Iter(rdbuf()), *this,
  195. fill(), _X).failed())
  196. _St |= badbit;
  197. _CATCH_IO_END }
  198. setstate(_St);
  199. return (*this); }
  200. _Myt& operator<<(long double _X)
  201. {iostate _St = goodbit;
  202. const sentry _Ok(*this);
  203. if (_Ok)
  204. {const _Nput& _Fac = _USE(getloc(), _Nput);
  205. _TRY_IO_BEGIN
  206. if (_Fac.put(_Iter(rdbuf()), *this,
  207. fill(), _X).failed())
  208. _St |= badbit;
  209. _CATCH_IO_END }
  210. setstate(_St);
  211. return (*this); }
  212. _Myt& operator<<(const void *_X)
  213. {iostate _St = goodbit;
  214. const sentry _Ok(*this);
  215. if (_Ok)
  216. {const _Nput& _Fac = _USE(getloc(), _Nput);
  217. _TRY_IO_BEGIN
  218. if (_Fac.put(_Iter(rdbuf()), *this,
  219. fill(), _X).failed())
  220. _St |= badbit;
  221. _CATCH_IO_END }
  222. setstate(_St);
  223. return (*this); }
  224. _Myt& operator<<(_Mysb *_Pb)
  225. {iostate _St = goodbit;
  226. bool _Copied = false;
  227. const sentry _Ok(*this);
  228. if (_Ok && _Pb != 0)
  229. for (int_type _C = _Tr::eof(); ; _Copied = true)
  230. {_TRY_BEGIN
  231. _C = _Tr::eq_int_type(_Tr::eof(), _C)
  232. ? _Pb->sgetc() : _Pb->snextc();
  233. _CATCH_ALL
  234. setstate(failbit);
  235. _RERAISE;
  236. _CATCH_END
  237. if (_Tr::eq_int_type(_Tr::eof(),_C))
  238. break;
  239. _TRY_IO_BEGIN
  240. if (_Tr::eq_int_type(_Tr::eof(),
  241. rdbuf()->sputc(_Tr::to_char_type(_C))))
  242. {_St |= badbit;
  243. break; }
  244. _CATCH_IO_END }
  245. width(0);
  246. setstate(!_Copied ? _St | failbit : _St);
  247. return (*this); }
  248. _Myt& put(_E _X)
  249. {iostate _St = goodbit;
  250. const sentry _Ok(*this);
  251. if (!_Ok)
  252. _St |= badbit;
  253. else
  254. {_TRY_IO_BEGIN
  255. if (_Tr::eq_int_type(_Tr::eof(),
  256. rdbuf()->sputc(_X)))
  257. _St |= badbit;
  258. _CATCH_IO_END }
  259. setstate(_St);
  260. return (*this); }
  261. _Myt& write(const _E *_S, streamsize _N)
  262. {iostate _St = goodbit;
  263. const sentry _Ok(*this);
  264. if (!_Ok)
  265. _St |= badbit;
  266. else
  267. {_TRY_IO_BEGIN
  268. if (rdbuf()->sputn(_S, _N) != _N)
  269. _St |= badbit;
  270. _CATCH_IO_END }
  271. setstate(_St);
  272. return (*this); }
  273. _Myt& flush()
  274. {iostate _St = goodbit;
  275. if (!fail() && rdbuf()->pubsync() == -1)
  276. _St |= badbit;
  277. setstate(_St);
  278. return (*this); }
  279. _Myt& seekp(pos_type _P)
  280. {if (!fail())
  281. rdbuf()->pubseekpos(_P, out);
  282. return (*this); }
  283. _Myt& seekp(off_type _O, ios_base::seekdir _W)
  284. {if (!fail())
  285. rdbuf()->pubseekoff(_O, _W, out);
  286. return (*this); }
  287. pos_type tellp()
  288. {if (!fail())
  289. return (rdbuf()->pubseekoff(0, cur, out));
  290. else
  291. return (streampos(_BADOFF)); }
  292. };
  293. #ifdef _DLL
  294. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  295. extern template class _CRTIMP basic_ostream<char, char_traits<char> >;
  296. extern template class _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >;
  297. #pragma warning(default:4231) /* restore previous warning */
  298. #endif // _DLL
  299. // INSERTERS
  300. template<class _E, class _Tr> inline
  301. basic_ostream<_E, _Tr>& __cdecl operator<<(
  302. basic_ostream<_E, _Tr>& _O, const _E *_X)
  303. {typedef basic_ostream<_E, _Tr> _Myos;
  304. ios_base::iostate _St = ios_base::goodbit;
  305. size_t _N = _Tr::length(_X);
  306. size_t _M = _O.width() <= 0 || _O.width() <= _N
  307. ? 0 : _O.width() - _N;
  308. const _Myos::sentry _Ok(_O);
  309. if (!_Ok)
  310. _St |= ios_base::badbit;
  311. else
  312. {_TRY_IO_BEGIN
  313. if ((_O.flags() & ios_base::adjustfield)
  314. != ios_base::left)
  315. for (; 0 < _M; --_M)
  316. if (_Tr::eq_int_type(_Tr::eof(),
  317. _O.rdbuf()->sputc(_O.fill())))
  318. {_St |= ios_base::badbit;
  319. break; }
  320. if (_St == ios_base::goodbit
  321. && _O.rdbuf()->sputn(_X, (int)_N) != _N)
  322. _St |= ios_base::badbit;
  323. if (_St == ios_base::goodbit)
  324. for (; 0 < _M; --_M)
  325. if (_Tr::eq_int_type(_Tr::eof(),
  326. _O.rdbuf()->sputc(_O.fill())))
  327. {_St |= ios_base::badbit;
  328. break; }
  329. _O.width(0);
  330. _CATCH_IO_(_O) }
  331. _O.setstate(_St);
  332. return (_O); }
  333. template<class _E, class _Tr> inline
  334. basic_ostream<_E, _Tr>& __cdecl operator<<(
  335. basic_ostream<_E, _Tr>& _O, _E _C)
  336. {typedef basic_ostream<_E, _Tr> _Myos;
  337. ios_base::iostate _St = ios_base::goodbit;
  338. const _Myos::sentry _Ok(_O);
  339. if (_Ok)
  340. {size_t _M = _O.width() <= 1 ? 0 : _O.width() - 1;
  341. _TRY_IO_BEGIN
  342. if ((_O.flags() & ios_base::adjustfield)
  343. != ios_base::left)
  344. for (; _St == ios_base::goodbit && 0 < _M; --_M)
  345. if (_Tr::eq_int_type(_Tr::eof(),
  346. _O.rdbuf()->sputc(_O.fill())))
  347. _St |= ios_base::badbit;
  348. if (_St == ios_base::goodbit
  349. && _Tr::eq_int_type(_Tr::eof(),
  350. _O.rdbuf()->sputc(_C)))
  351. _St |= ios_base::badbit;
  352. for (; _St == ios_base::goodbit && 0 < _M; --_M)
  353. if (_Tr::eq_int_type(_Tr::eof(),
  354. _O.rdbuf()->sputc(_O.fill())))
  355. _St |= ios_base::badbit;
  356. _CATCH_IO_(_O) }
  357. _O.width(0);
  358. _O.setstate(_St);
  359. return (_O); }
  360. template<class _E, class _Tr> inline
  361. basic_ostream<_E, _Tr>& __cdecl operator<<(
  362. basic_ostream<_E, _Tr>& _O, const signed char *_X)
  363. {return (_O << (const char *)_X); }
  364. template<class _E, class _Tr> inline
  365. basic_ostream<_E, _Tr>& __cdecl operator<<(
  366. basic_ostream<_E, _Tr>& _O, const signed char _C)
  367. {return (_O << (char)_C); }
  368. template<class _E, class _Tr> inline
  369. basic_ostream<_E, _Tr>& __cdecl operator<<(
  370. basic_ostream<_E, _Tr>& _O, const unsigned char *_X)
  371. {return (_O << (const char *)_X); }
  372. template<class _E, class _Tr> inline
  373. basic_ostream<_E, _Tr>& __cdecl operator<<(
  374. basic_ostream<_E, _Tr>& _O, const unsigned char _C)
  375. {return (_O << (char)_C); }
  376. template<class _E, class _Tr> inline
  377. basic_ostream<_E, _Tr>& __cdecl operator<<(
  378. basic_ostream<_E, _Tr>& _O, const signed short *_X)
  379. {return (_O << (const wchar_t *)_X); }
  380. // MANIPULATORS
  381. template<class _E, class _Tr> inline
  382. basic_ostream<_E, _Tr>&
  383. __cdecl endl(basic_ostream<_E, _Tr>& _O)
  384. {_O.put(_O.widen('\n'));
  385. _O.flush();
  386. return (_O); }
  387. _CRTIMP inline basic_ostream<char, char_traits<char> >&
  388. __cdecl endl(basic_ostream<char, char_traits<char> >& _O)
  389. {_O.put('\n');
  390. _O.flush();
  391. return (_O); }
  392. _CRTIMP inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  393. __cdecl endl(basic_ostream<wchar_t,
  394. char_traits<wchar_t> >& _O)
  395. {_O.put('\n');
  396. _O.flush();
  397. return (_O); }
  398. template<class _E, class _Tr> inline
  399. basic_ostream<_E, _Tr>&
  400. __cdecl ends(basic_ostream<_E, _Tr>& _O)
  401. {_O.put(_E('\0'));
  402. return (_O); }
  403. _CRTIMP inline basic_ostream<char, char_traits<char> >&
  404. __cdecl ends(basic_ostream<char, char_traits<char> >& _O)
  405. {_O.put('\0');
  406. return (_O); }
  407. _CRTIMP inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  408. __cdecl ends(basic_ostream<wchar_t,
  409. char_traits<wchar_t> >& _O)
  410. {_O.put('\0');
  411. return (_O); }
  412. template<class _E, class _Tr> inline
  413. basic_ostream<_E, _Tr>&
  414. __cdecl flush(basic_ostream<_E, _Tr>& _O)
  415. {_O.flush();
  416. return (_O); }
  417. _CRTIMP inline basic_ostream<char, char_traits<char> >&
  418. __cdecl flush(basic_ostream<char, char_traits<char> >& _O)
  419. {_O.flush();
  420. return (_O); }
  421. _CRTIMP inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  422. __cdecl flush(basic_ostream<wchar_t,
  423. char_traits<wchar_t> >& _O)
  424. {_O.flush();
  425. return (_O); }
  426. #ifdef _DLL
  427. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  428. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  429. basic_ostream<char, char_traits<char> >&, const char *);
  430. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  431. basic_ostream<char, char_traits<char> >&, char);
  432. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  433. basic_ostream<char, char_traits<char> >&, const signed char *);
  434. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  435. basic_ostream<char, char_traits<char> >&, const signed char);
  436. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  437. basic_ostream<char, char_traits<char> >&, const unsigned char *);
  438. extern template _CRTIMP basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  439. basic_ostream<char, char_traits<char> >&, const unsigned char);
  440. extern template _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  441. basic_ostream<wchar_t, char_traits<wchar_t> >&, const wchar_t *);
  442. extern template _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  443. basic_ostream<wchar_t, char_traits<wchar_t> >&, wchar_t);
  444. extern template _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  445. basic_ostream<wchar_t, char_traits<wchar_t> >&, const signed short *);
  446. #pragma warning(default:4231) /* restore previous warning */
  447. #endif // _DLL
  448. _STD_END
  449. #ifdef _MSC_VER
  450. #pragma pack(pop)
  451. #endif /* _MSC_VER */
  452. #endif /* _OSTREAM_ */
  453. /*
  454. * Copyright (c) 1994 by P.J. Plauger. ALL RIGHTS RESERVED.
  455. * Consult your license regarding permissions and restrictions.
  456. */