📄 piconfdb.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/DB/PIConfDB.cpp,v $
* $Date: 2003/05/13 18:41:56 $
*
Description:
\*____________________________________________________________________________*/
//$SourceTop:$
#include "PIConfDB.h"
#include "ParsConf.h"
#include "Utils.h"
#include "AutoDel.h"
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Manipulate objects of type 'string'.
\*____________________________________________________________________________*/
static void StringRender( ostream &os, const void *pV, int )
{
if ( pV )
{
((PIConfigDBString *)pV)->WriteInfo( os );
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Manipulate objects of type 'string'.
\*____________________________________________________________________________*/
static void StringDestruct( void *pV )
{
PI_DELETE( (PIConfigDBString *)pV );
};
/*____________________________________________________________________________*\
*
Global declarations:
Type frames for the new types.
\*____________________________________________________________________________*/
TypeFrame PIConfigDB::tConfigLineTypeFrame =
{
"ConfigurationLine",
"Configuration database value",
0,
StringRender,
StringDestruct
};
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PIConfigDB::PIConfigDB( _PIDB *pDB, const char *pName )
: _PIBaseDB( pDB, pName )
{
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
ConfigDBParser *PIConfigDB::CreateParser( const char *pFileName )
{
assert( pFileName );
return PI_NEW( ConfigDBParser( pFileName, this ) );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
bool PIConfigDB::LoadConfigurationFile( const char *pFileName )
{
assert( pFileName );
if ( !pFileName )
{ return false; };
ConfigDBParser *pParser = 0;
AutoDelete<ConfigDBParser> tA( pParser = CreateParser( pFileName ) );
(void)tA; /* --- stop annoying compiler warnings --- */
if ( !pParser ) { return false; };
return pParser->Parse();
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
bool PIConfigDB::Load( const char *pFileName )
{
return LoadConfigurationFile( pFileName );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public
Description:
\*____________________________________________________________________________*/
int PIConfigDB::LoadConfigurationFile( _PIDB *pDB, const char *pFileName,
PIConfigDB **ppConfigDB )
{
if ( !pDB || !pFileName ) { return PIAPI_EINVAL; };
PIConfigDB *pConfDB = PI_NEW( PIConfigDB( pDB ) );
if ( ppConfigDB )
{ *ppConfigDB = pConfDB; };
return pConfDB->Load( pFileName ) ? PIAPI_COMPLETED : PIAPI_ERROR;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PIConfigDB *PIConfigDB::FindConfiguration( PIDB *pDB )
{
if ( !pDB )
{ return 0; };
return (PIConfigDB *)pDB->Lookup( PIDBTYPE_TREE, PIDBKEY_CONFIGURATION,
PIDBFLAG_PROPAGATEUP );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void *InternalConfigLookup( PIConfigDB *pConfDB, int iType, const char *pKey )
{
if ( !pConfDB || !pKey )
{ return 0; };
/* --- get the definition --- */
return (void *)pConfDB->Lookup( iType, pKey, 0 );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
const char *PIConfigDB::LookupValue( PIConfigDB *pDB, const char *pKey,
char *pInfoBuffer, int iLen )
{
UserDBType *pUser = (UserDBType *)
InternalConfigLookup( pDB, PIDBTYPE_USER, pKey );
if ( !pUser || (&(pUser->GetTypeFrame()))!=&tConfigLineTypeFrame )
{ return 0; };
PIConfigDBString *pS = (PIConfigDBString *)pUser->GetValue();
if ( !pS )
{ return 0; };
if ( pInfoBuffer && iLen )
{
ostrstream os( pInfoBuffer, iLen );
os << pS->GetFileName() << ": " << pS->GetY() << ", " << pS->GetX()
<< ends;
pInfoBuffer[iLen-1]='\0';
};
return pS->GetString();
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PIConfigDB *PIConfigDB::LookupTree( PIConfigDB *pDB, const char *pKey,
char *pInfoBuffer, int iLen )
{
PIConfigDB *pSubDB = (PIConfigDB *)
InternalConfigLookup( pDB, PIDBTYPE_TREE, pKey );
if ( !pSubDB )
{ return 0; };
if ( pInfoBuffer && iLen )
{
ostrstream os( pInfoBuffer, iLen );
const char *pNote = (const char *)
pSubDB->Lookup( PIDBTYPE_STRING, PIDBKEY_NOTE, 0 );
if ( pNote )
{ os << pNote; }
else
{ os << "No information"; };
os << ends;
pInfoBuffer[iLen-1]='\0';
};
return pSubDB;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PIConfigDB *PIConfigDB::LookupObjectWithName( PIDB *pDB,
PIConfigDB *pConfig, const char *pObjectName )
{
assert( pDB && pObjectName );
if ( !pDB || !pObjectName )
{ return 0; };
/* --- get configuration DB --- */
if ( !pConfig )
{ pConfig = FindConfiguration( pDB ); };
if ( !pConfig )
{ return 0; };
PIConfigDB *pConf = 0;
_PIDBIterator *pIter = 0;
AutoDelete<_PIDBIterator> tA( pIter = pConfig->GetIterator(
PIDBTYPE_TREE, PIDBKEY_CONF_OBJECT, 0 ) );
if ( pIter )
{
for( ; pIter->AtValidElement(); pIter->Next() )
{
const char *pName = PIConfigDB::LookupValue( (PIConfigDB *)
pIter->Current( 0 ), PIDBKEY_CONF_NAME, 0, 0 );
if ( pName && !strcmp( pName, pObjectName ) )
{
pConf = (PIConfigDB *)pIter->Current( 0 );
break;
};
};
};
(void)tA; /* --- stop annoying compiler warnings --- */
return pConf;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -