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

📄 pidbtree.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/DB/PIDBTree.cpp,v $
 * $Date: 2003/05/13 18:41:56 $
 *
 Description:
\*____________________________________________________________________________*/
//$SourceTop:$

#include <assert.h>
#include <string.h>
#include <ctype.h>

#include "PIDBTree.h"
#include "AutoDel.h"
#include "StrToken.h"

/*____________________________________________________________________________*\
 *
 Description:
	Some compilers want dummy copy constructors.
	Here are null references to enable dummy copy constructors.
\*____________________________________________________________________________*/
static TypeFrame *__pFrame = 0;
static TypeFrame &__tFrame = *__pFrame;

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void *PIDBTree::AddString( void *pV )
{
	return DuplicateString( (const char *)pV );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PIDBTree::DestructTree( void *pV )
{
	PI_DELETE( (_PIDB *)pV );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
#define INDENT  "|   "
void PIDBTree::RenderTree( ostream &os, const void *pV, int iLevel )
{
	PIDBTree *pDB = (PIDBTree *)pV;
	assert( pDB );
	int iLastType = pDB->iLastType;
	for(int i=0; i<=iLastType; i++)
		{
		const TypeFrame &tFrame = pDB->GetTypeFrame( i );
		PIString sTypeName( tFrame.pName );
		if ( sTypeName==PIString::Empty() )
			{
			pi_ltoa( i, 10, sTypeName );
			};
		_PIDBIterator *pIter = pDB->GetIterator( i, 0, 0 );
		for(; pIter->AtValidElement(); pIter->Next() )
			{
			os << endl;
			for(int j=iLevel; j; j--)
				{ os << INDENT; };
			const char *pKey;
			void *pValue = pIter->Current( &pKey );
			PIString sSubtypeName;
			if ( i==PIDBTYPE_USER )
				{ sSubtypeName = ((UserDBType *)pValue)-> GetTypeFrame().pName; };
			os << "<" << sTypeName << sSubtypeName << "|" << (pKey?pKey:"")
				<< ">";
			if ( tFrame.fnRender )
				{ (tFrame.fnRender)( os, pValue, iLevel+1 ); }
			else
				{ os << (void *)pValue; };
			};
		PI_DELETE( pIter );
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PIDBTree::RenderString( ostream &os, const void *pV, int )
{
	if ( pV )
		{
		os << (const char *)pV;
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PIDBTree::DestructString( void *pV )
{
	PI_DELETE( [] pV );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PIDBTree::RenderOpaque( ostream &os, const void *pV, int )
{
	os << (void *)pV;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PIDBTree::RenderUser( ostream &os, const void *pV, int iLevel )
{
	if ( pV )
		{
		UserDBType &tUser = *( (UserDBType *)pV );
		if ( tUser.GetTypeFrame().fnRender )
			{ (tUser.GetTypeFrame().fnRender)(os, tUser.GetValue(), iLevel); }
		else
			{ os << (void *)pV; };
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void PIDBTree::DestructUser( void *pV )
{
	PI_DELETE( (UserDBType *)pV );
}

/*____________________________________________________________________________*\
 *
 Global declarations:
	Type frames for the built-in types.
\*____________________________________________________________________________*/
TypeFrame PIDBTree::aTypeFrames[ PIDBTree::NUM_TYPES ] =
{
	{ 
		"DBTree",
		"Program database tree",
		0,
		PIDBTree::RenderTree,
		PIDBTree::DestructTree
	},
	{ 
		"String",
		"Program database string",
		PIDBTree::AddString,
		PIDBTree::RenderString,
		PIDBTree::DestructString
	},
	{
		"RFC822",
		"RFC822 compliant string",
		PIDBTree::AddString,
		PIDBTree::RenderString,
		PIDBTree::DestructString
	},
	{
		"Opaque",
		"Pointer to internal data structure",
		0,
		PIDBTree::RenderOpaque,
		0
	},
	{
		"User/",
		"User defined type with typeframe",
		0,
		PIDBTree::RenderUser,
		PIDBTree::DestructUser	
	}
};

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class KeyValuePair
{
private:
	const char *pKey;
	void *pValue;

public:
	KeyValuePair( const char *pTheKey, void *pTheValue )
		:	pKey(pTheKey), pValue(pTheValue)	{};
	inline const char *Key()					{ return pKey; };
	inline void *Value()						{ return pValue; };
	inline void Delete( void (* fnDelete )(void *) )
		{ if (fnDelete) { (fnDelete)(pValue); };  PI_DELETE( this ); }; 
	inline void *Detach()
		{ void *pTmp = pValue; pValue = 0; PI_DELETE( this ); return pTmp; };
	inline void Replace( void *pNew, void (* fnDelete )(void *) )
		{ if (fnDelete) { (fnDelete)(pValue); };  pValue = pNew; };

};

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class PIDBTreePropagatesIterator : public _PIDBIterator
{
private:
	_PIDBIterator *pIterator;
	_PIDB *pParent;
	int iType;
	const char *pKey;
	int iFlags;

public:
	PIDBTreePropagatesIterator( _PIDB *pTheParent, int iTheType,
			const char *pTheKey, _PIDBIterator *pTheIterator, int iTheFlags )
		:	pIterator( pTheIterator ),
			pParent( pTheParent ),
			iType( iTheType ),
			pKey( pTheKey ),
			iFlags( iTheFlags )
		{
		};

	virtual ~PIDBTreePropagatesIterator()
		{
		PI_DELETE( pIterator );
		};

	virtual bool AtValidElement()
		{
		assert( pIterator );
		if ( (!pIterator->AtValidElement()) && pParent )
			{
			PI_DELETE( pIterator );
			pIterator = pParent->GetIterator( iType, pKey, iFlags );
			pParent = pParent->GetParent();
			return AtValidElement();
			};
		return pIterator->AtValidElement();
		};

	virtual void *Current( const char **ppKey )
		{
		assert( pIterator );
		return pIterator->Current( ppKey );
		};

	/*
		Remove the entry at the current index 
	*/
	virtual void RemoveCurrent()
		{
		assert( pIterator );
		if ( !pIterator ) { return; };
		pIterator->RemoveCurrent();
		};

	/*
		Detach and return the current element
	*/
	virtual void *DetachCurrent()
		{
		assert( pIterator );
		if ( !pIterator ) { return 0; };
		return pIterator->DetachCurrent();
		};

	/*
		Replace the current element with the specified one
	*/
	virtual void ReplaceCurrent( void *pNew )
		{
		assert( pIterator );
		if ( !pIterator ) { return; };
		pIterator->ReplaceCurrent( pNew );
		};

	/*
		Advance the contained iterator to the next element. If the
		contained iterator is exhausted and its an iterator for the
		current tree and this tree has a parent, then make the 
		contained interator be the iterator for the parent.
	*/
	virtual void Next()			
		{
		assert( pIterator );
		pIterator->Next();
		};
};

/*____________________________________________________________________________*\
 *
 Class:
 Description:
\*____________________________________________________________________________*/
class PIDBTreeAnyKeyIterator : public _PIDBIterator
{
protected:
	DblList *aLists;
	int iNumLists;
	int iCurrentList;
	DblListIterator iIter;
	const TypeFrame &tTypeFrame;

	/*
		If the iterator is pointing at an empty list then initialise
		the iterator to point at the next list
	*/
	inline void AdvanceToValidKey()
		{
		if ( !iIter.BadIndex() )
			{ return; };
		for(; iCurrentList<iNumLists; iCurrentList++ )
			{
			iIter = aLists[iCurrentList];
			if ( !iIter.BadIndex() )
				{
				iCurrentList++;
				return;
				};
			};
		};

	/* --- forbid copy constructor --- */
	PIDBTreeAnyKeyIterator( const PIDBTreeAnyKeyIterator &iI )
	:	aLists( 0 ),
		iNumLists( 0 ),
		iCurrentList( 0 ),
		iIter( iI.aLists[0] ),
		tTypeFrame( __tFrame )
		{ assert( 0 ); };

public:
	PIDBTreeAnyKeyIterator( DblList *aTheLists, int iTheNumLists,
			const TypeFrame &tTheTypeFrame )
	:	aLists( aTheLists ),
		iNumLists( iTheNumLists ),
		iCurrentList( 0 ),
		iIter( aTheLists[0] ),
		tTypeFrame( tTheTypeFrame )

⌨️ 快捷键说明

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