📄 _piobj.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/Pi2API/_PIObj.cpp,v $
* $Date: 2003/05/13 18:42:09 $
*
Description:
\*____________________________________________________________________________*/
//$SourceTop:$
#include <ctype.h>
#include "StrToken.h"
#include "_PIObj.h"
#include "AutoDel.h"
#include "PIStrStr.h"
#include "_PIDBUtl.h"
#include "DeQuote.h"
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
_PIObject *_PIObject::Load( PIDB *pDB, PIConfigDB *pConfig,
const char *pObjectName, int iArgc, const char *ppArgv[] )
{
assert( pDB && pObjectName );
PIConfigDB *pConf = PIConfigDB::LookupObjectWithName( pDB, pConfig,
pObjectName );
if ( !pConf )
{
PIOStrStream os;
os << "Unable to load object '" << pObjectName << "', configuration \
definition could not be found." << ends;
_PIDBUtil::AddLogMessage( pDB, pConfig, PILOG_ERROR, os.str() );
return 0;
};
/* --- get the name of the class --- */
const char *pClassName = PIConfigDB::LookupValue( pConf,
PIDBKEY_CONF_CLASS, 0, 0 );
if ( !pClassName )
{
PIOStrStream os;
os << "Unable to load object '" << pObjectName << "', configuration \
definition is missing a definition for the object's 'Class'." << ends;
_PIDBUtil::AddLogMessage( pDB, pConfig, PILOG_ERROR, os.str() );
return 0;
};
const _PIClass *pClass = _PIClass::LookupOrLoad( pDB, pConfig, pClassName );
if ( !pClass )
{
return 0;
};
/* --- correct the name the of object, wrt case --- */
pObjectName = PIConfigDB::LookupValue( pConf, PIDBKEY_CONF_NAME, 0, 0 );
/* ---
Check for object attributes
NOTE: This is antiquated
--- */
const char *pTmp = PIConfigDB::LookupValue( pConf, PIDBKEY_CONF_ATTRIBUTE,
0, 0 );
bool bHasTree = pTmp && !pi_stricmp( pTmp, PIDBVALUE_CONF_HASTREE );
#if 0
/* ---
Check for nested objects
NOTE: Removed because its antiquated
--- */
_PIDBIterator *pIter = 0;
AutoDelete<_PIDBIterator> tA( pIter = pConf->GetIterator(
PIDBTYPE_CONF_STRING, PIDBKEY_CONF_OBJECT, 0 ) );
for(; pIter && pIter->AtValidElement(); pIter->Next() )
{
PIConfigDBString *pString = (PIConfigDBString *)pIter->Current( 0 );
if ( !pString ) { continue; };
const char *pValue = pString->GetString();
};
#endif
if ( bHasTree )
{
pDB = PI_NEW( PIDBTree( pDB, pObjectName ) );
};
_PIObject *pObj = PI_NEW( _PIObject( pObjectName, pClass, pDB, pConf ) );
if ( pClass->Constructor( pObj, iArgc, ppArgv ) )
{
PIOStrStream os;
os << "Failed to initialize object '" << pObj->GetName()
<< "' of class '" << pClass->GetName() << "'." << ends;
_PIDBUtil::AddLogMessage( pDB, pConfig, PILOG_ERROR, os.str() );
Delete( pObj, 0, 0 );
pObj = 0;
if ( bHasTree )
{
pDB->Detach();
PI_DELETE( pDB );
};
};
return pObj;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
_PIObject *_PIObject::LoadFromLine( PIDB *pDB, PIConfigDB *pConfigDB,
const char *pObjectLine )
{
if ( !pObjectLine )
{
assert( 0 );
return 0;
};
if ( !pDB || !pConfigDB )
{
PILog_addMessage( pDB, pConfigDB, PILOG_ERROR, "\
Object has no DB or Configuration DB" );
return 0;
};
StringTokenizer tTokens( pObjectLine, " ", 0 );
int iNumTokens = tTokens.NumTokens();
if ( iNumTokens<1 )
{
PIOStrStream os;
os << "Object line is empty" << ends;
PILog_addMessage( pDB, pConfigDB, PILOG_ERROR, os.str() );
return 0;
};
const char *pObjectName = tTokens.GetToken( 0 );
int iArgc = iNumTokens-1;
char **ppArgv = 0;
if ( iArgc>0 )
{
ppArgv = PI_NEW( char *[iArgc] );
for( int i=0; i<iArgc; i++ )
{
ppArgv[i] = (char *)(const char *)tTokens.GetToken( i+1 );
};
};
PIObject *pNewObject = Load( pDB, 0, pObjectName, iArgc,
(const char **)ppArgv );
PI_DELETE( [] ppArgv );
if ( !pNewObject )
{
/* --- Message generated by Load() --- */
return 0;
};
return pNewObject;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
_PIObject *_PIObject::LoadEx( const _PIObject *pObject,
const char *pObjectKey )
{
if ( !pObject || !pObjectKey )
{
assert( 0 );
return 0;
};
PIDB *pDB = PIObject_getDB( pObject );
PIConfigDB *pConfigDB = PIObject_getConfigurationDB( pObject );
if ( !pDB || !pConfigDB )
{
PILog_addMessage( pDB, pConfigDB, PILOG_ERROR, "\
Object has no DB or Configuration DB" );
return 0;
};
const char *pObjectLine = PIConfig_lookupValue( pConfigDB,
pObjectKey, 0, 0 );
if ( !pObjectLine )
{
PIOStrStream os;
os << "No definition for '" << pObjectKey << "'" << ends;
PILog_addMessage( pDB, pConfigDB, PILOG_ERROR, os.str() );
return 0;
};
return _PIObject::LoadFromLine( pDB, pConfigDB, pObjectLine );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
#define CONFIG_ERR(this, msg) \
{ PILog_addMessage( PIObject_getDB(this), \
PIObject_getConfigurationDB(this), PILOG_ERROR, (msg) ); }
int _PIObject::ReadParameters(
PIObject *pObject,
int iArgc,
const char *ppArgv[],
int (* fnParameter)( void *, const char *, const char *, const char *),
void *pParam )
{
assert( pObject && fnParameter );
if ( !( pObject && fnParameter ) )
{
return 0;
};
/* --- loop over parameters passed on line --- */
if ( ppArgv )
{
for( int i=0; i<iArgc; i++ )
{
const char *pLine = ppArgv[i];
if ( !pLine )
{ continue; };
/* --- remove leading whitespace --- */
for( ; (isspace(*pLine)); pLine++ );
int j=0;
/* --- scan to first '=' --- */
for( ; pLine[j] && pLine[j]!='='; j++ );
/* --- grap variable name --- */
PIString sVariable( pLine, j );
/* --- skip the '=' --- */
if ( pLine[j] )
{ j++; };
DeQuote tUnQuoted( &(pLine[j]) );
if ( !(fnParameter)( pParam, sVariable, tUnQuoted, "" ) )
{
return 0;
};
};
};
PIDBIterator *pIter = PIDB_getIterator(
(PIDB *)PIObject_getConfigurationDB( pObject ),
PIDBTYPE_USER, 0, 0 );
if ( !pIter )
{
CONFIG_ERR( pObject, "Could not read values for object" );
PIDBIterator_delete( pIter );
return 0;
};
for(; PIDBIterator_atValidElement( pIter ); PIDBIterator_next( pIter ))
{
const char *pVariable = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -