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

📄 qconfig.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/Intrface/QConfig.cpp,v $
 * $Date: 2003/05/13 18:42:07 $
 *
 Description:

\*____________________________________________________________________________*/
//$SourceTop:$

#include <fstream.h>
#include <assert.h>

#include "PIString.h"
#include "DblList.h"
#include "QConfig.h"

/*
** #define D { cerr << __FILE__ << ": " << __LINE__ << endl; };
*/
#define D

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
class ValuePair
{
public:	
	PIString sVariable;
	PIString sValue;

	ValuePair( const char *pVariable, const char *pValue )
	:	sVariable( pVariable ), sValue( pValue )
	    {};
};

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
class CallBackRecord
{
public:
    int (* pFn)( const char *pSection, const char *pVariable, int iAction,
        void *pActionData, void *pData );
    void *pData;

    CallBackRecord( int (* pTheFn)( const char *pSection, const char *pVariable,
        int iAction, void *pActionData, void *pData ), void *pTheData )
    :   pFn(pTheFn),
        pData( pTheData )
        {};
};

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
class Section
{
public:	
	PIString sSectionName;
	DblList lPairs;
    DblList lCallBacks;

	Section( const char *pSectionName )
	:	sSectionName( pSectionName )
	    {};

	~Section()
	    {
	    for( DblListIterator i( lPairs ); !i.BadIndex(); i++ )
		    { PI_DELETE( (ValuePair *)i.Current() ); };
	    for( DblListIterator j( lCallBacks ); !j.BadIndex(); j++ )
		    { PI_DELETE( (CallBackRecord *)j.Current() ); };
	    };

	void NewValue( const char *pPair )
	    {
	    assert( pPair );

	    int i;
	    for( i=0; pPair[i] && pPair[i]!=':'; i++ );
	    PIString sVariable( pPair, i );
	    PIString sValue;
	    if ( pPair[i] )
		    { sValue = &(pPair[i+1]); };
	    lPairs.Append( PI_NEW( ValuePair( sVariable, sValue ) ) );
	    };
    
    void AddCallBack( int (* fnCB)( const char *pSection,
        const char *pVariable, int iAction, void *pActionData, void *pData ),
        void *pData )
        {
        lCallBacks.Append( (DblList::type) PI_NEW( CallBackRecord(
            fnCB, pData ) ) );
        };

    int DispatchCallBack( const char *pVariable, int iAction,
        void *pCallBackData )
        {
	    for( DblListIterator j( lCallBacks ); !j.BadIndex(); j++ )
		    {
            CallBackRecord *pCB = (CallBackRecord *)j.Current();
            if ( !(pCB->pFn)( sSectionName, pVariable, iAction, pCallBackData,
                pCB->pData ) )
                { /* abort */ return 0; };
            
            };
        return 1;
        };
};

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
const char *QConfig::LookupValue( const char *pSection,
    const char *pVariable )
{
    return LookupValueEx( pSection, pVariable, 0 );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
const char *QConfig::LookupValueEx( const char *pSection,
    const char *pVariable, int iIndex )
{
    assert( pSection && pVariable );

	for( DblListIterator i( *((DblList *)pConfig) ); !i.BadIndex(); i++ )
		{
		Section *pTheSection = (Section *)i.Current();
	
		if ( strcmp( pTheSection->sSectionName, pSection ) ) continue;
	
		for( DblListIterator j( pTheSection->lPairs ); !j.BadIndex(); j++ )
			{
			ValuePair *pPair = (ValuePair *)j.Current();
			if ( pPair->sVariable==pVariable && !(iIndex--) )
				{ return pPair->sValue; };
			};
		};
	
	return 0; 
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
const char *QConfig::LookupSection( int iIndex )
{
	DblList *pList = (DblList *)pConfig;
	if ( iIndex >= pList->Size() )
	    { return 0; };

	Section *pSection = (Section *)((*pList)[iIndex]);
	assert( pSection );
	return pSection->sSectionName;
}
	
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int QConfig::LookupPair( const char *pSection, int iIndex,
	const char **ppVariable, const char **ppValue )
{
    assert( pSection && ppVariable && ppValue );
	for( DblListIterator i( *((DblList *)pConfig) ); !i.BadIndex(); i++ )
		{
		Section *pTheSection = (Section *)i.Current();
	
		if ( strcmp( pTheSection->sSectionName, pSection ) ) continue;
	
	    DblList &lPairs = pTheSection->lPairs;
	    if ( iIndex >= lPairs.Size() )
	        { return 0; };

	    ValuePair *pPair = (ValuePair *)lPairs[iIndex];
	    *ppVariable = pPair->sVariable;
	    *ppValue = pPair->sValue;
	    return 1;
		};
	
    return 0; 
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int QConfig::Internal_changeValue( const char *pSection,
	const char *pVariable, const char *pNewValue, int iReplace )
{
	assert( pSection && pVariable && pNewValue );
	
	for( DblListIterator i( *((DblList *)pConfig) ); !i.BadIndex(); i++ )
		{
		Section *pTheSection = (Section *)i.Current();
	
		if ( strcmp( pTheSection->sSectionName, pSection ) ) continue;
	
		if ( iReplace )
			{
			/*
			** Search for an existing value and replace it if found
			*/
			for(DblListIterator j( pTheSection->lPairs );!j.BadIndex();j++)
				{
				ValuePair *pPair = (ValuePair *)j.Current();
				if ( pPair->sVariable==pVariable )
					{
					pPair->sValue = pNewValue;
					return 1; 
					};
				};
			};
	
		/* 
		** variable not found in section, or new value
		*/
		pTheSection->lPairs.Append( (DblList::type)PI_NEW( ValuePair(
			pVariable, pNewValue ) ) );
	
		return 1;
		};
	
	/*
	** section not found
	*/
	Section *pNewSection = PI_NEW( Section( pSection ) );
	pNewSection->lPairs.Append( (DblList::type)PI_NEW( ValuePair(
			pVariable, pNewValue ) ) );
	((DblList *)pConfig)->Append( (DblList::type)pNewSection );
	
	return 1;	/* success */
}
	
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int QConfig::AddValue( const char *pSection, const char *pVariable,
    const char *pNewValue )
{
    return Internal_changeValue( pSection, pVariable, pNewValue, 0 );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int QConfig::ReplaceValue( const char *pSection, const char *pVariable,
    const char *pNewValue )
{
    return Internal_changeValue( pSection, pVariable, pNewValue, 1 );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int QConfig::DeleteValue( const char *pSection, const char *pVariable )
{
	assert( pSection && pVariable );
	
	for( DblListIterator i( *((DblList *)pConfig) ); !i.BadIndex(); i++ )
		{
		Section *pTheSection = (Section *)i.Current();
	
		if ( strcmp( pTheSection->sSectionName, pSection ) ) continue;
	
		for(;;)
			{
			/*
			** Loop forever to restart each time a match is deleted
			*/
			int iNotFound = 1;
			for(DblListIterator j( pTheSection->lPairs );!j.BadIndex();j++)
				{
				ValuePair *pPair = (ValuePair *)j.Current();
				if ( pPair->sVariable==pVariable )
					{
					pTheSection->lPairs.Detach( j );
					PI_DELETE( pPair );
					iNotFound = 0;	/* start again */
					break;
					};
				};
			if ( iNotFound ) { break; };
			};
		};
	
	return 1; 
}
	
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void QConfig::RegisterCallBack( const char *pSection,
    int (*fnCallBack)( const char *pSection,
    const char *pVariable, int iAction, void *pActionData, void *pData ),
    void *pData )
{
    assert( fnCallBack );
	assert( pSection );

    Section *pTheSection = 0;
	
	for( DblListIterator i( *((DblList *)pConfig) ); !i.BadIndex(); i++ )
		{
		pTheSection = (Section *)i.Current();
	
		if ( !strcmp( pTheSection->sSectionName, pSection ) )
            { break; };

        pTheSection = 0;
        };

    if ( !pTheSection )
        {
        pTheSection = PI_NEW( Section( pSection ) );
        ((DblList *)pConfig)->Append( (DblList::type)pTheSection );
        };

    pTheSection->AddCallBack( fnCallBack, pData );
}
	
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int QConfig::DispatchWriteCallBacks( const char *pSection,
    int iAction, ostream &os )
{
    assert( pSection );
    assert(iAction==M_CB_BEFORESECTIONWRITE || iAction==M_CB_AFTERSECTIONWRITE);

	for( DblListIterator i( *((DblList *)pConfig) ); !i.BadIndex(); i++ )
		{
		Section *pTheSection = (Section *)i.Current();
	
		if ( strcmp( pTheSection->sSectionName, pSection ) )
            { continue; };

        SectionWriteCallBackStructure tS( os );
        return pTheSection->DispatchCallBack( 0, iAction, &tS );
        };

    return 1;
}
	
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int QConfig::Internal_destroyTConfig()
{
	for( DblListIterator i( *((DblList *)pConfig) ); !i.BadIndex(); i++ )
		{
		Section *pTheSection = (Section *)i.Current();
		PI_DELETE( pTheSection );
		};
	
	PI_DELETE( (DblList *)pConfig );
	return 1;	/* success */
}
	
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
QConfig::QConfig()
:   pConfig( PI_NEW( DblList ) )
{
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
QConfig::~QConfig()
{
    Internal_destroyTConfig();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
QConfig *FindParentQConfig( PIDB *pCurrentDB )
{
    assert( pCurrentDB );
    if ( !pCurrentDB ) { return 0; };
    void *pData = PIDB_lookup( pCurrentDB, PIDBTYPE_OPAQUE,
	QCONFIG_KEY, PIDBFLAG_PROPAGATEUP );
    return (QConfig *)pData;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
QConfig *CreateQConfig()
{
    return PI_NEW( QConfig );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void DestroyQConfig( QConfig *pConfig )
{
    PI_DELETE( pConfig );
}

⌨️ 快捷键说明

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