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.

474 lines
14 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. #ifdef __FORCE_INSTANCE
  295. template class _CRTIMP2 basic_ostream<char, char_traits<char> >;
  296. template class _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >;
  297. #else // __FORCE_INSTANCE
  298. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  299. extern template class _CRTIMP2 basic_ostream<char, char_traits<char> >;
  300. extern template class _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >;
  301. #pragma warning(default:4231) /* restore previous warning */
  302. #endif // __FORCE_INSTANCE
  303. #endif // _DLL
  304. // INSERTERS
  305. template<class _E, class _Tr> inline
  306. basic_ostream<_E, _Tr>& __cdecl operator<<(
  307. basic_ostream<_E, _Tr>& _O, const _E *_X)
  308. {typedef basic_ostream<_E, _Tr> _Myos;
  309. ios_base::iostate _St = ios_base::goodbit;
  310. size_t _N = _Tr::length(_X);
  311. size_t _M = _O.width() <= 0 || _O.width() <= _N
  312. ? 0 : _O.width() - _N;
  313. const _Myos::sentry _Ok(_O);
  314. if (!_Ok)
  315. _St |= ios_base::badbit;
  316. else
  317. {_TRY_IO_BEGIN
  318. if ((_O.flags() & ios_base::adjustfield)
  319. != ios_base::left)
  320. for (; 0 < _M; --_M)
  321. if (_Tr::eq_int_type(_Tr::eof(),
  322. _O.rdbuf()->sputc(_O.fill())))
  323. {_St |= ios_base::badbit;
  324. break; }
  325. if (_St == ios_base::goodbit
  326. && _O.rdbuf()->sputn(_X, (int)_N) != _N)
  327. _St |= ios_base::badbit;
  328. if (_St == ios_base::goodbit)
  329. for (; 0 < _M; --_M)
  330. if (_Tr::eq_int_type(_Tr::eof(),
  331. _O.rdbuf()->sputc(_O.fill())))
  332. {_St |= ios_base::badbit;
  333. break; }
  334. _O.width(0);
  335. _CATCH_IO_(_O) }
  336. _O.setstate(_St);
  337. return (_O); }
  338. template<class _E, class _Tr> inline
  339. basic_ostream<_E, _Tr>& __cdecl operator<<(
  340. basic_ostream<_E, _Tr>& _O, _E _C)
  341. {typedef basic_ostream<_E, _Tr> _Myos;
  342. ios_base::iostate _St = ios_base::goodbit;
  343. const _Myos::sentry _Ok(_O);
  344. if (_Ok)
  345. {size_t _M = _O.width() <= 1 ? 0 : _O.width() - 1;
  346. _TRY_IO_BEGIN
  347. if ((_O.flags() & ios_base::adjustfield)
  348. != ios_base::left)
  349. for (; _St == ios_base::goodbit && 0 < _M; --_M)
  350. if (_Tr::eq_int_type(_Tr::eof(),
  351. _O.rdbuf()->sputc(_O.fill())))
  352. _St |= ios_base::badbit;
  353. if (_St == ios_base::goodbit
  354. && _Tr::eq_int_type(_Tr::eof(),
  355. _O.rdbuf()->sputc(_C)))
  356. _St |= ios_base::badbit;
  357. for (; _St == ios_base::goodbit && 0 < _M; --_M)
  358. if (_Tr::eq_int_type(_Tr::eof(),
  359. _O.rdbuf()->sputc(_O.fill())))
  360. _St |= ios_base::badbit;
  361. _CATCH_IO_(_O) }
  362. _O.width(0);
  363. _O.setstate(_St);
  364. return (_O); }
  365. template<class _E, class _Tr> inline
  366. basic_ostream<_E, _Tr>& __cdecl operator<<(
  367. basic_ostream<_E, _Tr>& _O, const signed char *_X)
  368. {return (_O << (const char *)_X); }
  369. template<class _E, class _Tr> inline
  370. basic_ostream<_E, _Tr>& __cdecl operator<<(
  371. basic_ostream<_E, _Tr>& _O, const signed char _C)
  372. {return (_O << (char)_C); }
  373. template<class _E, class _Tr> inline
  374. basic_ostream<_E, _Tr>& __cdecl operator<<(
  375. basic_ostream<_E, _Tr>& _O, const unsigned char *_X)
  376. {return (_O << (const char *)_X); }
  377. template<class _E, class _Tr> inline
  378. basic_ostream<_E, _Tr>& __cdecl operator<<(
  379. basic_ostream<_E, _Tr>& _O, const unsigned char _C)
  380. {return (_O << (char)_C); }
  381. template<class _E, class _Tr> inline
  382. basic_ostream<_E, _Tr>& __cdecl operator<<(
  383. basic_ostream<_E, _Tr>& _O, const signed short *_X)
  384. {return (_O << (const wchar_t *)_X); }
  385. // MANIPULATORS
  386. template<class _E, class _Tr> inline
  387. basic_ostream<_E, _Tr>&
  388. __cdecl endl(basic_ostream<_E, _Tr>& _O)
  389. {_O.put(_O.widen('\n'));
  390. _O.flush();
  391. return (_O); }
  392. _CRTIMP2 inline basic_ostream<char, char_traits<char> >&
  393. __cdecl endl(basic_ostream<char, char_traits<char> >& _O)
  394. {_O.put('\n');
  395. _O.flush();
  396. return (_O); }
  397. _CRTIMP2 inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  398. __cdecl endl(basic_ostream<wchar_t,
  399. char_traits<wchar_t> >& _O)
  400. {_O.put('\n');
  401. _O.flush();
  402. return (_O); }
  403. template<class _E, class _Tr> inline
  404. basic_ostream<_E, _Tr>&
  405. __cdecl ends(basic_ostream<_E, _Tr>& _O)
  406. {_O.put(_E('\0'));
  407. return (_O); }
  408. _CRTIMP2 inline basic_ostream<char, char_traits<char> >&
  409. __cdecl ends(basic_ostream<char, char_traits<char> >& _O)
  410. {_O.put('\0');
  411. return (_O); }
  412. _CRTIMP2 inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  413. __cdecl ends(basic_ostream<wchar_t,
  414. char_traits<wchar_t> >& _O)
  415. {_O.put('\0');
  416. return (_O); }
  417. template<class _E, class _Tr> inline
  418. basic_ostream<_E, _Tr>&
  419. __cdecl flush(basic_ostream<_E, _Tr>& _O)
  420. {_O.flush();
  421. return (_O); }
  422. _CRTIMP2 inline basic_ostream<char, char_traits<char> >&
  423. __cdecl flush(basic_ostream<char, char_traits<char> >& _O)
  424. {_O.flush();
  425. return (_O); }
  426. _CRTIMP2 inline basic_ostream<wchar_t, char_traits<wchar_t> >&
  427. __cdecl flush(basic_ostream<wchar_t,
  428. char_traits<wchar_t> >& _O)
  429. {_O.flush();
  430. return (_O); }
  431. #ifdef _DLL
  432. #ifndef CRTDLL2
  433. #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
  434. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  435. basic_ostream<char, char_traits<char> >&, const char *);
  436. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  437. basic_ostream<char, char_traits<char> >&, char);
  438. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  439. basic_ostream<char, char_traits<char> >&, const signed char *);
  440. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  441. basic_ostream<char, char_traits<char> >&, const signed char);
  442. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  443. basic_ostream<char, char_traits<char> >&, const unsigned char *);
  444. extern template _CRTIMP2 basic_ostream<char, char_traits<char> >& __cdecl operator<<(
  445. basic_ostream<char, char_traits<char> >&, const unsigned char);
  446. extern template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  447. basic_ostream<wchar_t, char_traits<wchar_t> >&, const wchar_t *);
  448. extern template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  449. basic_ostream<wchar_t, char_traits<wchar_t> >&, wchar_t);
  450. extern template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >& __cdecl operator<<(
  451. basic_ostream<wchar_t, char_traits<wchar_t> >&, const signed short *);
  452. #pragma warning(default:4231) /* restore previous warning */
  453. #endif // CRTDLL2
  454. #endif // _DLL
  455. _STD_END
  456. #ifdef _MSC_VER
  457. #pragma pack(pop)
  458. #endif /* _MSC_VER */
  459. #endif /* _OSTREAM_ */
  460. /*
  461. * Copyright (c) 1994 by P.J. Plauger. ALL RIGHTS RESERVED.
  462. * Consult your license regarding permissions and restrictions.
  463. */