⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 streambuf

📁 C语言库函数的原型,有用的拿去
💻
📖 第 1 页 / 共 2 页
字号:
// streambuf standard header
#pragma once
#ifndef _STREAMBUF_
#define _STREAMBUF_
#ifndef RC_INVOKED
#include <xiosbase>

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

 #pragma push_macro("new")
 #undef new

_STD_BEGIN
		// TEMPLATE CLASS basic_streambuf
template<class _Elem,
	class _Traits>
	class basic_streambuf
	{	// control read/write buffers
	typedef basic_streambuf<_Elem, _Traits> _Myt;

protected:
	__CLR_OR_THIS_CALL basic_streambuf()
		: _Plocale(_NEW_CRT locale)
		{	// construct with no buffers
		_Init();
		}

	__CLR_OR_THIS_CALL basic_streambuf(_Uninitialized)
		: _Mylock(_Noinit)
		{	// construct uninitialized
		}

	__CLR_OR_THIS_CALL basic_streambuf(const _Myt& _Right)
		: _Plocale(_NEW_CRT locale(_Right.getloc()))
		{	// construct by copying _Right
		_Init();
		setp(_Right.pbase(), _Right.pptr(), _Right.epptr());
		setg(_Right.eback(), _Right.gptr(), _Right.egptr());
		}

	_Myt& __CLR_OR_THIS_CALL operator=(const _Myt& _Right)
		{	// assign from _Right
		if (this != &_Right)
			{	// different, worth copying
			setp(_Right.pbase(), _Right.pptr(), _Right.epptr());
			setg(_Right.eback(), _Right.gptr(), _Right.egptr());
			pubimbue(_Right.getloc());
			}
		return (*this);
		}

	void __CLR_OR_THIS_CALL swap(_Myt& _Right)
		{	// swap with _Right
		if (this != &_Right)
			{	// different, worth swapping
			_Elem *_Tfirst = pbase();
			_Elem *_Tnext = pptr();
			_Elem *_Tend = epptr();
			setp(_Right.pbase(), _Right.pptr(), _Right.epptr());
			_Right.setp(_Tfirst, _Tnext, _Tend);

			_Tfirst = eback();
			_Tnext = gptr();
			_Tend = egptr();
			setg(_Right.eback(), _Right.gptr(), _Right.egptr());
			_Right.setg(_Tfirst, _Tnext, _Tend);

			locale _Oldlocale = pubimbue(_Right.getloc());
			_Right.pubimbue(_Oldlocale);
			}
		}

public:
	typedef _Elem char_type;
	typedef _Traits traits_type;

	virtual __CLR_OR_THIS_CALL ~basic_streambuf()
		{	// destroy the object
		_DELETE_CRT(_Plocale);
		}

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

	pos_type __CLR_OR_THIS_CALL pubseekoff(off_type _Off,
		ios_base::seekdir _Way,
		ios_base::openmode _Mode = ios_base::in | ios_base::out)
		{	// change position by _Off, according to _Way, _Mode
		return (seekoff(_Off, _Way, _Mode));
		}

	pos_type __CLR_OR_THIS_CALL pubseekoff(off_type _Off,
		ios_base::seek_dir _Way,
		ios_base::open_mode _Mode)
		{	// change position by _Off, according to _Way, _Mode (old style)
		return (pubseekoff(_Off, (ios_base::seekdir)_Way,
			(ios_base::openmode)_Mode));
		}

	pos_type __CLR_OR_THIS_CALL pubseekpos(pos_type _Pos,
		ios_base::openmode _Mode = ios_base::in | ios_base::out)
		{	// change position to _Pos, according to _Mode
		return (seekpos(_Pos, _Mode));
		}

	pos_type __CLR_OR_THIS_CALL pubseekpos(pos_type _Pos,
		ios_base::open_mode _Mode)
		{	// change position to _Pos, according to _Mode (old style)
		return (seekpos(_Pos, (ios_base::openmode)_Mode));
		}

	_Myt *__CLR_OR_THIS_CALL pubsetbuf(_Elem *_Buffer,
		streamsize _Count)
		{	// offer _Buffer to external agent
		return (setbuf(_Buffer, _Count));
		}

	locale __CLR_OR_THIS_CALL pubimbue(const locale &_Newlocale)
		{	// set locale to argument
		locale _Oldlocale = *_Plocale;
		imbue(_Newlocale);
		*_Plocale = _Newlocale;
		return (_Oldlocale);
		}

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

	streamsize __CLR_OR_THIS_CALL in_avail()
		{	// return count of buffered input characters
		streamsize _Res = _Gnavail();
		return (0 < _Res ? _Res : showmanyc());
		}

	int __CLR_OR_THIS_CALL pubsync()
		{	// synchronize with external agent
		return (sync());
		}

	int_type __CLR_OR_THIS_CALL sbumpc()
		{	// get a character and point past it
		return (0 < _Gnavail()
			? _Traits::to_int_type(*_Gninc()) : uflow());
		}

	int_type __CLR_OR_THIS_CALL sgetc()
		{	// get a character and don't point past it
		return (0 < _Gnavail()
			? _Traits::to_int_type(*gptr()) : underflow());
		}

	streamsize __CLR_OR_THIS_CALL sgetn(_Elem *_Ptr,
		streamsize _Count)
		{	// get up to _Count characters into array beginning at _Ptr
		return (xsgetn(_Ptr, _Count));
		}

	int_type __CLR_OR_THIS_CALL snextc()
		{	// point to next character and return it
		return (1 < _Gnavail()
			? _Traits::to_int_type(*_Gnpreinc())
			: _Traits::eq_int_type(_Traits::eof(), sbumpc())
				? _Traits::eof() : sgetc());
		}

	int_type __CLR_OR_THIS_CALL sputbackc(_Elem _Ch)
		{	// put back _Ch
		return (gptr() != 0 && eback() < gptr()
			&& _Traits::eq(_Ch, gptr()[-1])
			? _Traits::to_int_type(*_Gndec())
			: pbackfail(_Traits::to_int_type(_Ch)));
		}

	void __CLR_OR_THIS_CALL stossc()
		{	// point past a character
		if (0 < _Gnavail())
			_Gninc();
		else
			uflow();
		}

	int_type __CLR_OR_THIS_CALL sungetc()
		{	// back up one position
		return (gptr() != 0 && eback() < gptr()
			? _Traits::to_int_type(*_Gndec()) : pbackfail());
		}

	int_type __CLR_OR_THIS_CALL sputc(_Elem _Ch)
		{	// put a character
		return (0 < _Pnavail()
			? _Traits::to_int_type(*_Pninc() = _Ch)
			: overflow(_Traits::to_int_type(_Ch)));
		}

	streamsize __CLR_OR_THIS_CALL sputn(const _Elem *_Ptr,
		streamsize _Count)
		{	// put _Count characters from array beginning at _Ptr
		return (xsputn(_Ptr, _Count));
		}

	virtual void __CLR_OR_THIS_CALL _Lock()
		{	// set the thread lock
		_Mylock._Lock();
		}

	virtual void __CLR_OR_THIS_CALL _Unlock()
		{	// clear the thread lock
		_Mylock._Unlock();
		}

protected:
	_Elem *__CLR_OR_THIS_CALL eback() const
		{	// return beginning of read buffer
		return (*_IGfirst);
		}

	_Elem *__CLR_OR_THIS_CALL gptr() const
		{	// return current position in read buffer
		return (*_IGnext);
		}

	_Elem *__CLR_OR_THIS_CALL pbase() const
		{	// return beginning of write buffer
		return (*_IPfirst);
		}

	_Elem *__CLR_OR_THIS_CALL pptr() const
		{	// return current position in write buffer
		return (*_IPnext);
		}

	_Elem *__CLR_OR_THIS_CALL egptr() const
		{	// return end of read buffer
		return (*_IGnext + *_IGcount);
		}

	void __CLR_OR_THIS_CALL gbump(int _Off)
		{	// alter current position in read buffer by _Off
		*_IGcount -= _Off;
		*_IGnext += _Off;
		}

	void __CLR_OR_THIS_CALL setg(_Elem *_First, _Elem *_Next, _Elem *_Last)
		{	// set pointers for read buffer
		*_IGfirst = _First;
		*_IGnext = _Next;
		*_IGcount = (int)(_Last - _Next);
		}

	_Elem *__CLR_OR_THIS_CALL epptr() const
		{	// return end of write buffer
		return (*_IPnext + *_IPcount);
		}

	_Elem *__CLR_OR_THIS_CALL _Gndec()
		{	// decrement current position in read buffer
		++*_IGcount;
		return (--*_IGnext);
		}

	_Elem *__CLR_OR_THIS_CALL _Gninc()
		{	// increment current position in read buffer
		--*_IGcount;
		return ((*_IGnext)++);
		}

	_Elem *__CLR_OR_THIS_CALL _Gnpreinc()
		{	// preincrement current position in read buffer
		--*_IGcount;
		return (++(*_IGnext));
		}

	streamsize __CLR_OR_THIS_CALL _Gnavail() const
		{	// count number of available elements in read buffer
		return (*_IGnext != 0 ? *_IGcount : 0);
		}

	void __CLR_OR_THIS_CALL pbump(int _Off)
		{	// alter current position in write buffer by _Off
		*_IPcount -= _Off;
		*_IPnext += _Off;
		}

	void __CLR_OR_THIS_CALL setp(_Elem *_First, _Elem *_Last)
		{	// set pointers for write buffer
		*_IPfirst = _First;
		*_IPnext = _First;
		*_IPcount = (int)(_Last - _First);
		}

	void __CLR_OR_THIS_CALL setp(_Elem *_First, _Elem *_Next, _Elem *_Last)
		{	// set pointers for write buffer, extended version
		*_IPfirst = _First;
		*_IPnext = _Next;
		*_IPcount = (int)(_Last - _Next);
		}

	_Elem *__CLR_OR_THIS_CALL _Pninc()
		{	// increment current position in write buffer
		--*_IPcount;
		return ((*_IPnext)++);
		}

	streamsize __CLR_OR_THIS_CALL _Pnavail() const
		{	// count number of available positions in write buffer
		return (*_IPnext != 0 ? *_IPcount : 0);
		}

	void __CLR_OR_THIS_CALL _Init()
		{	// initialize buffer parameters for no buffers
		_IGfirst = &_Gfirst;
		_IPfirst = &_Pfirst;
		_IGnext = &_Gnext;
		_IPnext = &_Pnext;
		_IGcount = &_Gcount;
		_IPcount = &_Pcount;
		setp(0, 0);
		setg(0, 0, 0);
		}

	void __CLR_OR_THIS_CALL _Init(_Elem **_Gf, _Elem **_Gn, int *_Gc,
		_Elem **_Pf, _Elem **_Pn, int *_Pc)
		{	// initialize buffer parameters as specified
		_IGfirst = _Gf;
		_IPfirst = _Pf;
		_IGnext = _Gn;
		_IPnext = _Pn;
		_IGcount = _Gc;
		_IPcount = _Pc;
		}

	virtual int_type __CLR_OR_THIS_CALL overflow(int_type = _Traits::eof())
		{	// put a character to stream (always fail)
		return (_Traits::eof());
		}

	virtual int_type __CLR_OR_THIS_CALL pbackfail(int_type = _Traits::eof())

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -