// ostream standard header
#ifndef _OSTREAM_
#define _OSTREAM_
#include <ios>
_STD_BEGIN

		// I/O EXCEPTION MACROS

 #if _HAS_EXCEPTIONS
 #define _TRY_IO_BEGIN	_TRY_BEGIN	/* begin try block */

 #define _CATCH_IO_END	_CATCH_ALL	/* catch block for _Myios */ \
	_Myios::setstate(ios_base::badbit, true);	/* set badbit and rethrow */ \
	_CATCH_END

 #define _CATCH_IO_(x)	_CATCH_ALL	/* catch block for basic_ios x */ \
	(x).setstate(ios_base::badbit, true);	/* set badbit and rethrow */ \
	_CATCH_END

 #else /* _HAS_EXCEPTIONS */
 #define _TRY_IO_BEGIN	{	/* begin try block */

 #define _CATCH_IO_END	}	/* catch block for _Myios */

 #define _CATCH_IO_(x)	}	/* catch block for basic_ios x */
 #endif /* _HAS_EXCEPTIONS */

		// TEMPLATE CLASS basic_ostream
export
template<class _Elem,
	class _Traits>
	class basic_ostream
		: virtual public basic_ios<_Elem, _Traits>
	{	// control insertions into a stream buffer
public:
	typedef basic_ostream<_Elem, _Traits> _Myt;
	typedef basic_ios<_Elem, _Traits> _Myios;
	typedef basic_streambuf<_Elem, _Traits> _Mysb;
	typedef ostreambuf_iterator<_Elem, _Traits> _Iter;
	typedef num_put<_Elem, _Iter> _Nput;

 #if _HAS_TRADITIONAL_IOSTREAMS
	explicit basic_ostream(basic_streambuf<_Elem, _Traits> *_Strbuf = 0,

 #else /* _HAS_TRADITIONAL_IOSTREAMS */
	explicit basic_ostream(basic_streambuf<_Elem, _Traits> *_Strbuf,
 #endif /* _HAS_TRADITIONAL_IOSTREAMS */

		bool _Isstd = false)
		{	// construct from a stream buffer pointer
		_Myios::init(_Strbuf, _Isstd);
		}

	basic_ostream(_Uninitialized, bool _Addit = true)
		{	// construct uninitialized
		if (_Addit)
			ios_base::_Addstd();	// suppress for basic_iostream
		}

	virtual ~basic_ostream()
		{	// destroy the object
		}

	typedef typename _Traits::int_type int_type;
	typedef typename _Traits::pos_type pos_type;
	typedef typename _Traits::off_type off_type;

	class _Sentry_base
		{	// stores thread lock and reference to output stream
	public:
		_Sentry_base(_Myt& _Ostr)
			: _Myostr(_Ostr)
			{	// lock the stream buffer, if there
			if (_Myostr.rdbuf() != 0)
				_Myostr.rdbuf()->_Lock();
			}

		~_Sentry_base()
			{	// destroy after unlocking
			if (_Myostr.rdbuf() != 0)
				_Myostr.rdbuf()->_Unlock();
			}

		_Myt& _Myostr;	// the output stream, for _Unlock call at destruction
		};

	class sentry
		: public _Sentry_base
		{	// stores thread lock and state of stream
	public:
		explicit sentry(_Myt& _Ostr)
			: _Sentry_base(_Ostr)
			{	// construct locking and testing stream
			if (_Ostr.good() && _Ostr.tie() != 0)
				_Ostr.tie()->flush();
			_Ok = _Ostr.good();	// store test only after flushing tie
			}

		~sentry()
			{	// destroy the object

 #if _HAS_EXCEPTIONS
			if (!_XSTD uncaught_exception())
				this->_Myostr._Osfx();
			}

 #else /* _HAS_EXCEPTIONS */
			this->_Myostr._Osfx();
		}
 #endif /* _HAS_EXCEPTIONS */

		operator bool() const
			{	// test if stream state okay
			return (_Ok);
			}

	private:
		sentry(const sentry&);	// not defined
		sentry& operator=(const sentry&);	// not defined

		bool _Ok;	// true if stream state okay at construction
		};

	bool opfx()
		{	// test stream state and flush tie stream as needed (retained)
		if (ios_base::good() && _Myios::tie() != 0)
			_Myios::tie()->flush();
		return (ios_base::good());
		}

	void osfx()
		{	// perform any wrapup (retained)
		_Osfx();
		}

	void _Osfx()
		{	// perform any wrapup
		_TRY_BEGIN
		if (ios_base::flags() & ios_base::unitbuf)
			flush();	// flush stream as needed
		_CATCH_ALL
		_CATCH_END
		}

	_Myt& operator<<(_Myt& (*_Pfn)(_Myt&))
		{	// call basic_ostream manipulator
		_DEBUG_POINTER(_Pfn);
		return ((*_Pfn)(*this));
		}

	_Myt& operator<<(_Myios& (*_Pfn)(_Myios&))
		{	// call basic_ios manipulator
		_DEBUG_POINTER(_Pfn);
		(*_Pfn)(*(_Myios *)this);
		return (*this);
		}

	_Myt& operator<<(ios_base& (*_Pfn)(ios_base&))
		{	// call ios_base manipulator
		_DEBUG_POINTER(_Pfn);
		(*_Pfn)(*(ios_base *)this);
		return (*this);
		}

	_Myt& operator<<(_Bool _Val);

	_Myt& operator<<(short _Val);

	_Myt& operator<<(unsigned short _Val);

	_Myt& operator<<(int _Val);

	_Myt& operator<<(unsigned int _Val);

	_Myt& operator<<(long _Val);

	_Myt& operator<<(unsigned long _Val);

 #ifdef _LONGLONG
	_Myt& operator<<(_LONGLONG _Val);

	_Myt& operator<<(_ULONGLONG _Val);
 #endif /* _LONGLONG */

	_Myt& operator<<(float _Val);

	_Myt& operator<<(double _Val);

	_Myt& operator<<(long double _Val);

	_Myt& operator<<(const void *_Val);

	_Myt& operator<<(_Mysb *_Strbuf);

	_Myt& put(_Elem _Ch);

	_Myt& write(const _Elem *_Str,
		streamsize _Count);

	_Myt& flush()
		{	// flush output stream
		ios_base::iostate _State = ios_base::goodbit;
		if (!ios_base::fail() && _Myios::rdbuf()->pubsync() == -1)
			_State |= ios_base::badbit;	// sync failed
		_Myios::setstate(_State);
		return (*this);
		}

	_Myt& seekp(pos_type _Pos)
		{	// set output stream position to _Pos
		if (!ios_base::fail()
			&& (off_type)_Myios::rdbuf()->pubseekpos(_Pos,
				ios_base::out) == _BADOFF)
			_Myios::setstate(ios_base::failbit);
		return (*this);
		}

	_Myt& seekp(off_type _Off, ios_base::seekdir _Way)
		{	// change output stream position by _Off, according to _Way
		if (!ios_base::fail()
			&& (off_type)_Myios::rdbuf()->pubseekoff(_Off, _Way,
				ios_base::out) == _BADOFF)
			_Myios::setstate(ios_base::failbit);
		return (*this);
		}

	pos_type tellp()
		{	// return output stream position
		if (!ios_base::fail())
			return (_Myios::rdbuf()->pubseekoff(0,
				ios_base::cur, ios_base::out));
		else
			return (pos_type(_BADOFF));
		}
	};


		// INSERTERS
export
template<class _Elem,
	class _Traits>
	basic_ostream<_Elem, _Traits>& operator<<(
		basic_ostream<_Elem, _Traits>& _Ostr, const char *_Val);

export
template<class _Elem,
	class _Traits>
	basic_ostream<_Elem, _Traits>& operator<<(
		basic_ostream<_Elem, _Traits>& _Ostr, char _Ch);

export
template<class _Traits>
	basic_ostream<char, _Traits>& operator<<(
		basic_ostream<char, _Traits>& _Ostr,
		const char *_Val);

export
template<class _Traits>
	basic_ostream<char, _Traits>& operator<<(
		basic_ostream<char, _Traits>& _Ostr, char _Ch);

export
template<class _Elem,
	class _Traits>
	basic_ostream<_Elem, _Traits>& operator<<(
		basic_ostream<_Elem, _Traits>& _Ostr, const _Elem *_Val);

export
template<class _Elem,
	class _Traits>
	basic_ostream<_Elem, _Traits>& operator<<(
		basic_ostream<_Elem, _Traits>& _Ostr, _Elem _Ch);

template<class _Traits> inline
	basic_ostream<char, _Traits>& operator<<(
		basic_ostream<char, _Traits>& _Ostr, const signed char *_Val)
	{	// insert a signed char NTBS
	return (_Ostr << (const char *)_Val);
	}

template<class _Traits> inline
	basic_ostream<char, _Traits>& operator<<(
		basic_ostream<char, _Traits>& _Ostr, signed char _Ch)
	{	// insert a signed char
	return (_Ostr << (char)_Ch);
	}

template<class _Traits> inline
	basic_ostream<char, _Traits>& operator<<(
		basic_ostream<char, _Traits>& _Ostr, const unsigned char *_Val)
	{	// insert an unsigned char NTBS
	return (_Ostr << (const char *)_Val);
	}

template<class _Traits> inline
	basic_ostream<char, _Traits>& operator<<(
		basic_ostream<char, _Traits>& _Ostr, unsigned char _Ch)
	{	// insert an unsigned char
	return (_Ostr << (char)_Ch);
	}

		// MANIPULATORS
template<class _Elem,
	class _Traits> inline
	basic_ostream<_Elem, _Traits>&
		endl(basic_ostream<_Elem, _Traits>& _Ostr)
	{	// insert newline and flush stream
	_Ostr.put(_Ostr.widen('\n'));
	_Ostr.flush();
	return (_Ostr);
	}

template<class _Elem,
	class _Traits> inline
	basic_ostream<_Elem, _Traits>&
		ends(basic_ostream<_Elem, _Traits>& _Ostr)
	{	// insert null character
	_Ostr.put(_Elem());
	return (_Ostr);
	}

template<class _Elem,
	class _Traits> inline
	basic_ostream<_Elem, _Traits>&
		flush(basic_ostream<_Elem, _Traits>& _Ostr)
	{	// flush stream
	_Ostr.flush();
	return (_Ostr);
	}

_STD_END
#endif /* _OSTREAM_ */

/*
 * Copyright (c) 1992-2006 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
V5.01:1566 */
