// xiosbase internal header (from "ios")
#pragma once
#ifndef _XIOSBASE_
#define _XIOSBASE_
#ifndef RC_INVOKED
#include "xlocale"

 #if _HAS_CPP0X
 #include "system_error"
 #endif /* _HAS_CPP0X */

 #include "share.h"

 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)

 #pragma warning(disable: 4412)

 #define _OPENPROT	_SH_DENYNO

 #pragma push_macro("new")
 #undef new

_STD_BEGIN
 #define _IOSskipws		0x0001
 #define _IOSunitbuf	0x0002
 #define _IOSuppercase	0x0004
 #define _IOSshowbase	0x0008
 #define _IOSshowpoint	0x0010
 #define _IOSshowpos	0x0020
 #define _IOSleft		0x0040
 #define _IOSright		0x0080
 #define _IOSinternal	0x0100
 #define _IOSdec		0x0200
 #define _IOSoct		0x0400
 #define _IOShex		0x0800
 #define _IOSscientific	0x1000
 #define _IOSfixed		0x2000

 #define _IOShexfloat	0x3000	// added with TR1/

 #define _IOSboolalpha	0x4000
 #define _IOS_Stdio		0x8000

 #define _IOS_Nocreate	0x40
 #define _IOS_Noreplace	0x80
 #define _IOSbinary		0x20

		// TEMPLATE CLASS _Iosb
template<class _Dummy>
	class _Iosb
	{	// define templatized bitmask/enumerated types, instantiate on demand
public:
	enum _Dummy_enum {_Dummy_enum_val = 1};	// don't ask
	enum _Fmtflags
		{	// constants for formatting options
		_Fmtmask = 0xffff, _Fmtzero = 0};

	static const _Fmtflags skipws = (_Fmtflags)_IOSskipws;
	static const _Fmtflags unitbuf = (_Fmtflags)_IOSunitbuf;
	static const _Fmtflags uppercase = (_Fmtflags)_IOSuppercase;
	static const _Fmtflags showbase = (_Fmtflags)_IOSshowbase;
	static const _Fmtflags showpoint = (_Fmtflags)_IOSshowpoint;
	static const _Fmtflags showpos = (_Fmtflags)_IOSshowpos;
	static const _Fmtflags left = (_Fmtflags)_IOSleft;
	static const _Fmtflags right = (_Fmtflags)_IOSright;
	static const _Fmtflags internal = (_Fmtflags)_IOSinternal;
	static const _Fmtflags dec = (_Fmtflags)_IOSdec;
	static const _Fmtflags oct = (_Fmtflags)_IOSoct;
	static const _Fmtflags hex = (_Fmtflags)_IOShex;
	static const _Fmtflags scientific = (_Fmtflags)_IOSscientific;
	static const _Fmtflags fixed = (_Fmtflags)_IOSfixed;

	static const _Fmtflags hexfloat =
		(_Fmtflags)_IOShexfloat;	// added with TR1/

	static const _Fmtflags boolalpha = (_Fmtflags)_IOSboolalpha;
	static const _Fmtflags _Stdio = (_Fmtflags)_IOS_Stdio;
	static const _Fmtflags adjustfield = (_Fmtflags)(_IOSleft
		| _IOSright | _IOSinternal);
	static const _Fmtflags basefield = (_Fmtflags)(_IOSdec
		| _IOSoct | _IOShex);
	static const _Fmtflags floatfield = (_Fmtflags)(_IOSscientific
		| _IOSfixed);

	enum _Iostate
		{	// constants for stream states
		_Statmask = 0x17};

	static const _Iostate goodbit = (_Iostate)0x0;
	static const _Iostate eofbit = (_Iostate)0x1;
	static const _Iostate failbit = (_Iostate)0x2;
	static const _Iostate badbit = (_Iostate)0x4;
	static const _Iostate _Hardfail = (_Iostate)0x10;

	enum _Openmode
		{	// constants for file opening options
		_Openmask = 0xff};

	static const _Openmode in = (_Openmode)0x01;
	static const _Openmode out = (_Openmode)0x02;
	static const _Openmode ate = (_Openmode)0x04;
	static const _Openmode app = (_Openmode)0x08;
	static const _Openmode trunc = (_Openmode)0x10;
	static const _Openmode _Nocreate = (_Openmode)_IOS_Nocreate;
	static const _Openmode _Noreplace = (_Openmode)_IOS_Noreplace;
	static const _Openmode binary = (_Openmode)_IOSbinary;

	enum _Seekdir
		{	// constants for file positioning options
		_Seekmask = 0x3};

	static const _Seekdir beg = (_Seekdir)0;
	static const _Seekdir cur = (_Seekdir)1;
	static const _Seekdir end = (_Seekdir)2;

	enum
		{	// constant for default file opening protection
		_Openprot = _OPENPROT};
	};

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::skipws;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::unitbuf;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::uppercase;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::showbase;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::showpoint;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::showpos;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::left;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::right;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::internal;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::dec;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::oct;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::hex;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::scientific;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::fixed;

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags
		_Iosb<_Dummy>::hexfloat;	// added with TR1

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::boolalpha;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::_Stdio;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::adjustfield;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::basefield;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Fmtflags _Iosb<_Dummy>::floatfield;

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::goodbit;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::eofbit;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::failbit;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::badbit;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Iostate _Iosb<_Dummy>::_Hardfail;

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::in;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::out;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::ate;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::app;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::trunc;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::_Nocreate;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::_Noreplace;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Openmode _Iosb<_Dummy>::binary;

template<class _Dummy>
	const typename _Iosb<_Dummy>::_Seekdir _Iosb<_Dummy>::beg;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Seekdir _Iosb<_Dummy>::cur;
template<class _Dummy>
	const typename _Iosb<_Dummy>::_Seekdir _Iosb<_Dummy>::end;

		// CLASS ios_base
class ios_base
	: public _Iosb<int>
	{	// base class for ios
public:
	_BITMASK(_Fmtflags, fmtflags);
	_BITMASK(_Iostate, iostate);
	_BITMASK(_Openmode, openmode);
	_BITMASK(_Seekdir, seekdir);

	typedef _STD streamoff streamoff;
	typedef _STD streampos streampos;

	enum event
		{	// constants for ios events
		erase_event, imbue_event, copyfmt_event};

	typedef void (__cdecl *event_callback)(event, ios_base&, int);
	typedef unsigned int io_state, open_mode, seek_dir;

			// CLASS failure
	class failure

 #if _HAS_CPP0X
		: public system_error
		{	// base of all iostreams exceptions
	public:
		explicit failure(const string &_Message,
			const error_code& _Errcode = make_error_code(io_errc::stream))
			: system_error(_Errcode, _Message)
			{	// construct with message
			}

		explicit failure(const char *_Message,
			const error_code& _Errcode = make_error_code(io_errc::stream))
			: system_error(_Errcode, _Message)
			{	// construct with message
			}

 #else /* _HAS_CPP0X */
		: public runtime_error
		{	// base of all iostreams exceptions
	public:
		explicit failure(const string &_Message)
			: runtime_error(_Message)
			{	// construct with message
			}

		explicit failure(const char *_Message)
			: runtime_error(_Message)
			{	// construct with message
			}
 #endif /* _HAS_CPP0X */

 #if _HAS_EXCEPTIONS

 #else /* _HAS_EXCEPTIONS */
	protected:
		virtual void _Doraise() const
			{	// report the exception
			_RAISE(*this);
			}
 #endif /* _HAS_EXCEPTIONS */
		};

			// CLASS Init
	class Init
		{	// controller for standard-stream initialization
	public:
		Init()
			{	// initialize standard streams on first construction
			_Init_ctor(this);
			}

		~Init()
			{	// flush standard streams on last destruction
			_Init_dtor(this);
			}

	private:
		static void __cdecl _Init_ctor(Init *);
		static void __cdecl _Init_dtor(Init *);

		static int _Init_cnt;	// net ctor count

		static int& __cdecl _Init_cnt_func();
		};

	ios_base& operator=(const ios_base& _Right)
		{	// assign state and format stuff from _Right
		if (this != &_Right)
			{	// worth doing
			_Mystate = _Right._Mystate;
			copyfmt(_Right);
			}
		return (*this);
		}

	operator void *() const
		{	// test if any stream operation has failed
		return (fail() ? 0 : (void *)this);
		}

	bool operator!() const
		{	// test if no stream operation has failed
		return (fail());
		}

	void clear(iostate _State, bool _Reraise)
		{	// set state, possibly reraise exception
		_Mystate = (iostate)(_State & _Statmask);
		if ((_Mystate & _Except) == 0)
			;
		else if (_Reraise)
			_RERAISE;
		else if (_Mystate & _Except & badbit)
			_THROW_NCEE(failure, "ios_base::badbit set");
		else if (_Mystate & _Except & failbit)
			_THROW_NCEE(failure, "ios_base::failbit set");
		else
			_THROW_NCEE(failure, "ios_base::eofbit set");
		}

	void clear(iostate _State = goodbit)
		{	// set state to argument
		clear(_State, false);
		}

	void clear(io_state _State)
		{	// set state to argument, old-style
		clear((iostate)_State);
		}

	iostate rdstate() const
		{	// return stream state
		return (_Mystate);
		}

	void setstate(iostate _State, bool _Exreraise)
		{	// merge in state argument, possibly reraise exception
		if (_State != goodbit)
			clear((iostate)((int)rdstate() | (int)_State), _Exreraise);
		}

	void setstate(iostate _State)
		{	// merge in state argument
		if (_State != goodbit)
			clear((iostate)((int)rdstate() | (int)_State), false);
		}

	void setstate(io_state _State)
		{	// merge in state argument, old style
		setstate((iostate)_State);
		}

	bool good() const
		{	// test if no state bits are set
		return (rdstate() == goodbit);
		}

	bool eof() const
		{	// test if eofbit is set in stream state
		return ((int)rdstate() & (int)eofbit);
		}

	bool fail() const
		{	// test if badbit or failbit is set in stream state
		return (((int)rdstate()
			& ((int)badbit | (int)failbit)) != 0);
		}

	bool bad() const
		{	// test if badbit is set in stream state
		return (((int)rdstate() & (int)badbit) != 0);
		}

	iostate exceptions() const
		{	// return exception mask
		return (_Except);
		}

	void exceptions(iostate _Newexcept)
		{	// set exception mask to argument
		_Except = (iostate)((int)_Newexcept & (int)_Statmask);
		clear(_Mystate);
		}

	void exceptions(io_state _State)
		{	// set exception mask to argument, old style
		exceptions((iostate)_State);
		}

	fmtflags flags() const
		{	// return format flags
		return (_Fmtfl);
		}

	fmtflags flags(fmtflags _Newfmtflags)
		{	// set format flags to argument
		fmtflags _Oldfmtflags = _Fmtfl;
		_Fmtfl = (fmtflags)((int)_Newfmtflags & (int)_Fmtmask);
		return (_Oldfmtflags);
		}

	fmtflags setf(fmtflags _Newfmtflags)
		{	// merge in format flags argument
		ios_base::fmtflags _Oldfmtflags = _Fmtfl;
		_Fmtfl = (fmtflags)((int)_Fmtfl
			| (int)_Newfmtflags & (int)_Fmtmask);
		return (_Oldfmtflags);
		}

	fmtflags setf(fmtflags _Newfmtflags, fmtflags _Mask)
		{	// merge in format flags argument under mask argument
		ios_base::fmtflags _Oldfmtflags = _Fmtfl;
		_Fmtfl = (fmtflags)(((int)_Fmtfl & (int)~_Mask)
			| ((int)_Newfmtflags & (int)_Mask & (int)_Fmtmask));
		return (_Oldfmtflags);
		}

	void unsetf(fmtflags _Mask)
		{	// clear format flags under mask argument
		_Fmtfl = (fmtflags)((int)_Fmtfl & (int)~_Mask);
		}

	streamsize precision() const
		{	// return precision
		return (_Prec);
		}

	streamsize precision(streamsize _Newprecision)
		{	// set precision to argument
		streamsize _Oldprecision = _Prec;
		_Prec = _Newprecision;
		return (_Oldprecision);
		}

	streamsize width() const
		{	// return width
		return (_Wide);
		}

	streamsize width(streamsize _Newwidth)
		{	// set width to argument
		streamsize _Oldwidth = _Wide;
		_Wide = _Newwidth;
		return (_Oldwidth);
		}

	locale getloc() const
		{	// get locale
		return (*_Ploc);
		}

	locale imbue(const locale& _Loc)
		{	// set locale to argument
		locale _Oldlocale = *_Ploc;
		*_Ploc = _Loc;
		_Callfns(imbue_event);
		return (_Oldlocale);
		}

	static int __cdecl xalloc()
		{	// allocate new iword/pword index
		_BEGIN_LOCK(_LOCK_STREAM)	// lock thread to ensure atomicity
			return (_Index++);
		_END_LOCK()
		}

	long& iword(int _Idx)
		{	// return reference to long element
		return (_Findarr(_Idx)._Lo);
		}

	void *& pword(int _Idx)
		{	// return reference to pointer element
		return (_Findarr(_Idx)._Vp);
		}

	void register_callback(event_callback _Pfn,
		int _Idx)
		{	// register event handler
		_Calls = _NEW_CRT _Fnarray(_Idx, _Pfn, _Calls);
		}

	ios_base& copyfmt(const ios_base& _Other)
		{	// copy format stuff
		if (this != &_Other)
			{	// copy all but _Mystate
			_Tidy();
			*_Ploc = *_Other._Ploc;
			_Fmtfl = _Other._Fmtfl;
			_Prec = _Other._Prec;
			_Wide = _Other._Wide;
			_Iosarray *_Ptr = _Other._Arr;

			for (_Arr = 0; _Ptr != 0; _Ptr = _Ptr->_Next)
				if (_Ptr->_Lo != 0 || _Ptr->_Vp != 0)
					{	// copy over nonzero array values
					iword(_Ptr->_Index) = _Ptr->_Lo;
					pword(_Ptr->_Index) = _Ptr->_Vp;
					}

			for (_Fnarray *_Pfa = _Other._Calls; _Pfa != 0;
				_Pfa = _Pfa->_Next)
				register_callback(_Pfa->_Pfn, _Pfa->_Index);

			_Callfns(copyfmt_event);	// call callbacks
			exceptions(_Other._Except);	// cause any throw at end
			}
		return (*this);
		}

	static bool __cdecl sync_with_stdio(bool _Newsync = true)
		{	// set C/C++ synchronization flag from argument
		_BEGIN_LOCK(_LOCK_STREAM)	// lock thread to ensure atomicity
			const bool _Oldsync = _Sync;
			_Sync = _Newsync;
			return (_Oldsync);
		_END_LOCK()
		}

	void swap(ios_base& _Right)
		{	// swap with _Right
		if (this != &_Right)
			{	// different, do the move
			_STD swap(_Mystate, _Right._Mystate);
			_STD swap(_Except, _Right._Except);
			_STD swap(_Fmtfl, _Right._Fmtfl);
			_STD swap(_Prec, _Right._Prec);
			_STD swap(_Wide, _Right._Wide);

			_STD swap(_Arr, _Right._Arr);
			_STD swap(_Calls, _Right._Calls);
			_STD swap(_Ploc, _Right._Ploc);
			}
		}

	virtual ~ios_base()	// destroy the object
		{	// destroy the object
		_Ios_base_dtor(this);
		}

	static void __cdecl _Addstd(ios_base *);	// add standard stream

	size_t _Stdstr;	// if > 0 index of standard stream to suppress destruction

protected:
	ios_base()
		{	// default constructor
		}

	void _Init()
		{	// initialize a new ios_base
		_Ploc = 0;
		_Stdstr = 0;
		_Except = goodbit;
		_Fmtfl = (fmtflags)(skipws | dec);
		_Prec = 6;
		_Wide = 0;
		_Arr = 0;
		_Calls = 0;
		clear(goodbit);
		_Ploc = _NEW_CRT locale;
		}

private:
			// STRUCT _Iosarray
	struct _Iosarray
		{	// list element for open-ended sparse array of longs/pointers
	public:
		_Iosarray(int _Idx, _Iosarray *_Link)
			: _Next(_Link), _Index(_Idx), _Lo(0), _Vp(0)
			{	// construct node for index _Idx and link it in
			}

		_Iosarray *_Next;	// pointer to next node
		int _Index;	// index of this node
		long _Lo;	// stored long value
		void *_Vp;	// stored pointer value
		};

			// STRUCT _Fnarray
	struct _Fnarray
		{	// list element for open-ended sparse array of event handlers
		_Fnarray(int _Idx, event_callback _Pnew, _Fnarray *_Link)
			: _Next(_Link), _Index(_Idx), _Pfn(_Pnew)
			{	// construct node for index _Idx and link it in
			}

		_Fnarray *_Next;	// pointer to next node
		int _Index;	// index of this node
		event_callback _Pfn;	// pointer to event handler
		};

	void _Callfns(event _Ev)
		{	// call all event handlers, reporting event
		for (_Fnarray *_Pfa = _Calls; _Pfa != 0; _Pfa = _Pfa->_Next)
			(*_Pfa->_Pfn)(_Ev, *this, _Pfa->_Index);
		}

	_Iosarray& _Findarr(int _Idx)
		{	// locate or make a variable array element
		_Iosarray *_Ptr1, *_Ptr2;

		for (_Ptr1 = _Arr, _Ptr2 = 0; _Ptr1 != 0; _Ptr1 = _Ptr1->_Next)
			if (_Ptr1->_Index == _Idx)
				return (*_Ptr1);	// found element, return it
			else if (_Ptr2 == 0 && _Ptr1->_Lo == 0 && _Ptr1->_Vp == 0)
				_Ptr2 = _Ptr1;	// found recycling candidate

		if (_Ptr2 != 0)
			{	// recycle existing element
			_Ptr2->_Index = _Idx;
			return (*_Ptr2);
			}

		_Arr = _NEW_CRT _Iosarray(_Idx, _Arr);	// make a new element
		return (*_Arr);
		}

	void _Tidy()
		{	// discard storage for an ios_base
		_Callfns(erase_event);
		_Iosarray *_Ptr1, *_Ptr2;

		for (_Ptr1 = _Arr; _Ptr1 != 0; _Ptr1 = _Ptr2)
			{	// delete array element
			_Ptr2 = _Ptr1->_Next;
			_DELETE_CRT(_Ptr1);
			}
		_Arr = 0;

		_Fnarray *_Pfa1, *_Pfa2;
		for (_Pfa1 = _Calls; _Pfa1 != 0; _Pfa1 = _Pfa2)
			{	// delete callback element
			_Pfa2 = _Pfa1->_Next;
			_DELETE_CRT(_Pfa1);
			}
		_Calls = 0;
		}

	iostate _Mystate;	// stream state
	iostate _Except;	// exception mask
	fmtflags _Fmtfl;	// format flags
	streamsize _Prec;	// field precision
	streamsize _Wide;	// field width
	_Iosarray *_Arr;	// pointer to first node of long/pointer array
	_Fnarray *_Calls;	// pointer to first node of call list
	locale *_Ploc;	// pointer to locale

	static int _Index;
	static bool _Sync;

	static void __cdecl _Ios_base_dtor(ios_base *);
	};

_BITMASK_OPS(ios_base::_Fmtflags)
_BITMASK_OPS(ios_base::_Iostate)
_BITMASK_OPS(ios_base::_Openmode)
_BITMASK_OPS(ios_base::_Seekdir)
_STD_END

 #pragma pop_macro("new")

 #pragma warning(pop)
 #pragma pack(pop)

#endif /* RC_INVOKED */
#endif /* _XIOSBASE_ */

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