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

📄 slldb.cpp

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

 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/SLL/SLLDB.cpp,v $
 * $Date: 2003/05/13 18:42:16 $
 *
 Description:
\*____________________________________________________________________________*/
//$SourceTop:$

#include <stdlib.h>
#include <ctype.h>
#include "SLLDB.h"
#include "SLLLex.h"
#include "AutoDel.h"
#include "PIStrStr.h"

#define SLL_EOF_CHAR	((unsigned char)-1)

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
SLLDB::SLLDB( _PIDB *pDB, const char *pName )
:   PIConfigDB( pDB, pName )
{
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:	static
 Description:
\*____________________________________________________________________________*/
void SLLDB::RenderNode( ostream &os, const void *pV, int )
{
	assert( pV );
	((const SLLBase *)pV)->Render( os );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:	static
 Description:
\*____________________________________________________________________________*/
void SLLDB::DestructNode( void *pV )
{
	PI_DELETE( (SLLBase *)pV );
}

/*____________________________________________________________________________*\
 *
 Description:
	Type frames for some user defined types
\*____________________________________________________________________________*/
TypeFrame SLLDB::tRootNodeTypeFrame =
{
	"SLLRoot",
	"Root SLL List Node",
	0,
	SLLDB::RenderNode,
	SLLDB::DestructNode	
}; 

TypeFrame SLLDB::tNodeTypeFrame =
{
	"SLLNode",
	"SLL List Node",
	0,
	SLLDB::RenderNode,
	0
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
inline int iswhite( unsigned char c )
{
	return (isspace(c)) || c=='\n' || c=='\r' || c=='\t';
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool _SLLDB_NextString(
	_PIDBIterator *pIter, const char **ppCurrentLine, int *piPosition )
{
	for(;;)
		{
		assert( pIter && ppCurrentLine && piPosition );
		if ( !pIter->AtValidElement() ) { return false; };
		UserDBType *pUser = (UserDBType *)pIter->Current( 0 );
		PIConfigDBString *pS = (PIConfigDBString *)(
			pUser ? ((const char *)pUser->GetValue()) : 0 );
		pIter->Next();
		if ( !pS ) { return false; };
		const char *pLine = pS->GetString();
		if ( !pLine ) { return false; };
		if ( *pLine ) 
			{
			*ppCurrentLine = pLine;
			*piPosition = 0;
			return true;
			};
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns SLL_EOF_CHAR on EOF, otherwise the next character.
\*____________________________________________________________________________*/
unsigned char _SLLDB_NextChar( _PIDBIterator *pIter,
	const char **ppCurrentLine, int *piPosition )
{
	assert( pIter && ppCurrentLine && *ppCurrentLine && piPosition );
	for(;;)
		{
		int iPos = *piPosition;
		unsigned char c = (*ppCurrentLine)[ iPos++ ];
		*piPosition = iPos;
		if ( c ) { return c; };
		if ( !_SLLDB_NextString( pIter, ppCurrentLine, piPosition ) )
			{ return SLL_EOF_CHAR; };
		return ' ';
		};
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
#define SLL_EOF			0
#define SLL_ERROR		1
#define SLL_OPENPAREN	2
#define SLL_CLOSEPAREN	3
#define SLL_STRING		4
#define SLL_NUMBER		5
#define SLL_FUNCTION	6
int SLLDB::ReadElem(
	_PIDBIterator *pIter,
	const char **ppCurrentLine,	
	int *piPosition,
	unsigned char &cLookAhead,
	SLLBase **ppElem )
{
	PIString sString;	/* for strings, identifiers and numbers */
	enum { BEGIN, IN_FUNCTION, IN_STRING, IN_NUMBER } eState = BEGIN;
	for(;;)
		{
		unsigned char c;
		if ( cLookAhead!=SLL_EOF_CHAR )
			{ c=cLookAhead; cLookAhead=SLL_EOF_CHAR; }
		else
			{ c = _SLLDB_NextChar( pIter, ppCurrentLine, piPosition ); };
		if ( c==SLL_EOF_CHAR )
			{ return SLL_EOF; };

again:	/* reuse current character in new state */

		switch( eState ) {
			 case BEGIN:
				if ( iswhite(c) )
					{ break; };
				if ( c=='(' )
					{ return SLL_OPENPAREN; };
				if ( c==')' )
					{ return SLL_CLOSEPAREN; };
				if ( c=='"' )
					{ eState=IN_STRING; break; };
				if ( isalpha( c ) || c=='_' )
					{ eState=IN_FUNCTION; goto again; };
				if ( isdigit( c ) )
					{ eState=IN_NUMBER; goto again; };
				{
				PIOStrStream ostr;
				ostr << "Unexpected character '" << c << "'" << ends;
				Error( this, ostr.str() );	
				};
				return SLL_ERROR;

			case IN_FUNCTION:
				if ( !isalnum( c ) && c!='_' )
					{
					void *pV = Lookup( PIDBTYPE_OPAQUE, sString,
						PIDBFLAG_PROPAGATEUP );
					if ( !pV )
						{
						PIOStrStream ostr;
						ostr << "Unknown function '" << sString << "'" << ends;
						Error( this, ostr.str() );	
						return SLL_ERROR;
						};
					if ( ppElem )
						{ *ppElem = PI_NEW( SLLFunction( pV ) ); };
					cLookAhead = c;
					return SLL_FUNCTION;
					};
				sString.Concatenate( c );
				break;

			case IN_STRING:
				if ( c=='"' )
					{
					if ( ppElem )
						{ *ppElem = PI_NEW( SLLString( sString ) ); };
					return SLL_STRING;
					};
				sString.Concatenate( c );
				break;

			case IN_NUMBER:
				if ( !isdigit( c ) )
					{
					if ( ppElem )
						{ *ppElem = PI_NEW( SLLNumber( atoi( sString ) ) ); };
					cLookAhead = c;
					return SLL_NUMBER;
					};
				sString.Concatenate( c );
				break;
		
			default:
				assert( 0 );
				Error( this, "Internal error" );	
				return SLL_ERROR;

			};	/* switch on state */
		}; /* infinite loop */
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
SLLList *SLLDB::RecursiveReadList(
	_PIDBIterator *pIter,
	const char **ppCurrentLine,
	int *piPosition,
	unsigned char &cLookAhead )
{
	SLLList *pList = 0;
	AutoDelete<SLLList> tA( pList = PI_NEW( SLLList() ) );
	for(;;)
		{
		SLLBase *pBase = 0;
		int iRet = ReadElem( pIter, ppCurrentLine, piPosition,
			cLookAhead, &pBase );
		AutoDelete<SLLBase> tE( pBase );

		switch( iRet )
			{
			case SLL_OPENPAREN:
				{
				SLLList *pSubList = RecursiveReadList( pIter, 
					ppCurrentLine, piPosition, cLookAhead );
				if ( !pSubList ) 
					{ return 0; };		/* error */
				pList->Add( pSubList );
				break;
				};

			case SLL_CLOSEPAREN:
				{
				tA.Detach();
				return pList;
				};

			case SLL_ERROR:
				return 0;		/* error message has already been given */

			case SLL_EOF:
				Error( this, "No terminating ')' in list" );	
				return 0;		/* unexpected termination */

			case SLL_FUNCTION:
			case SLL_STRING:
			case SLL_NUMBER:
				pList->Add( pBase );
				tE.Detach();
				break;

			default:
				assert( 0 );
				Error( this, "Internal error" );	
				return 0;
				
			};	/* switch on type of element */
		};	/* infinite loop */
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool SLLDB::ParseSLL()
{
	_PIDBIterator *pIter;
	AutoDelete<_PIDBIterator> tA( pIter = GetIterator( PIDBTYPE_USER,
		PIDBKEY_SLL_LINE, 0 ) );
	(void)tA;
	assert( pIter );
	_PIDB *pTheParent = GetParent();
	assert( pTheParent );
	if ( !pTheParent )
		{ Error( this, "Internal error" );
		return false; };

#if 0
	if ( !pIter || !pIter->AtValidIndex() )
		{
		Error( this, "No SLL source lines" );	
		return false;
		};
#endif
	const char *pLine = 0;
	int iPos = 0;
	if ( !_SLLDB_NextString( pIter, &pLine, &iPos ) )
		{
		Error( this, "No SLL source lines" );
		return false;
		};

	/* ---
	Loop reading function keys (strings) and thier associated lists
	--- */
	for(;;)
		{
		unsigned char c = SLL_EOF_CHAR;
		SLLBase *pBase = 0;
		int iRet = ReadElem( pIter, &pLine, &iPos, c, &pBase );
		AutoDelete<SLLBase> tA( pBase );
		(void)tA;

		/* --- done ? --- */
		if ( iRet==SLL_EOF_CHAR )
			{ return true; };
			
		/* --- element should be a string --- */
		if ( iRet!=SLL_STRING )
			{ Error( this, "Function Key (string) expected" );
			return false; };

		/* --- subsequent element should be an open paren --- */
		iRet = ReadElem( pIter, &pLine, &iPos, c, 0 );
		if ( iRet!=SLL_OPENPAREN )
			{ Error( this, "'(' expected" );
			return false; };

		/* --- read the list --- */
		SLLList *pRoot = RecursiveReadList(
			pIter,
			&pLine,
			&iPos,
			c );

		if ( !pRoot )
			{ return false; };

		/* --- add in the list with the key of the string --- */
		assert( pBase && pBase->GetType()==SLLBase::STRING );
		const char *pString = ((SLLString *)pBase)->GetString();
		pTheParent->Add( PIDBTYPE_USER, pString,
			PI_NEW( UserDBType( tRootNodeTypeFrame, pRoot ) ), 0 );
		};

#if 0
	PIConfigDBString *pS = (PIConfigDBString *)pIter->Current( 0 );
	if ( !pS ) 
		{
		assert( pS );
		Error( this, "Internal error" );	
		return false;
		};
	const char *pLine = pS->GetString();
	int iPos = 0;

	/* --- the sequence must begin with ')' --- */
	c = _SLLDB_NextRealChar( pIter, &pLine, &iPos );
	if ( c!='(' )
		{
		Error( this, "SLL must start with a '('" );	
		return false;
		};

	/* --- read in the list --- */
	unsigned char cLookAhead = SLL_EOF_CHAR;
	SLLList *pRoot = RecursiveReadList( pIter, &pLine, &iPos, cLookAhead );
	if ( !pRoot )
		{ return false; }; 

	/* --- add the root node in the parent of the SLL configuration 
	tree --- */
	assert( GetParent() );
	if ( !GetParent() )
		{ return false; };
	GetParent()->Add( PIDBTYPE_USER, "johntemp.",
		PI_NEW( UserDBType( tRootNodeTypeFrame, pRoot ) ) );

	/* --- the last matching ')' must be the last character --- */
	c = _SLLDB_NextRealChar( pIter, &pLine, &iPos );
	if ( c!=SLL_EOF_CHAR )
		{
		Error( this, "Unexpected characters after final ')'" );	
		return false;
		};
#endif
	
	return true;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
bool SLLDB::Load( const char *pFileName )
{
	/* --- do the lexer part --- */
	bool bRet( true );
	bRet = PIConfigDB::Load( pFileName );
	if ( !bRet ) { return false; };
	bRet = ParseSLL();
	if ( !bRet ) { return false; };
	return bRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
ConfigDBParser *SLLDB::CreateParser( const char *pFileName )
{
	assert( pFileName );
	return PI_NEW( SLLLex( pFileName, this ) );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI int PIProgram_loadSLLFile( _PIDB *pDB, const char *pFileName  )
{
	if ( !pDB || !pFileName ) { return PIAPI_EINVAL; };
	PIConfigDB *pConfDB = PI_NEW( SLLDB( pDB ) );
	pConfDB->Load( pFileName );
	return PIAPI_COMPLETED;
}

⌨️ 快捷键说明

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