📄 _pilibdb.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/_PILibDB.cpp,v $
* $Date: 2003/05/13 18:42:09 $
*
Description:
Iterates over all loaded configuration data loading libraries as specified.
\*____________________________________________________________________________*/
//$SourceTop:$
#include "PiAPI.h"
#include "PIStrStr.h"
#include "_PIBase.h"
#include "_PIDBKey.h"
#include "PIConfDB.h"
#include "AutoDel.h"
#include "MiscUtil.h"
#include "Pi2API.h"
#include "_PILib.h"
#include "_PIDBUtl.h"
#include "StrToken.h"
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static void RenderDLL( ostream &os, const void *pV, int )
{
os << (void *)pV;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static void DestructDLL( void *pV )
{
PIDLL_delete( (PIDLL *)pV );
}
/*____________________________________________________________________________*\
*
Description:
Type frames for some user defined types
\*____________________________________________________________________________*/
TypeFrame tDLLTypeFrame =
{
"DLL",
"Dynamically loaded library",
0,
RenderDLL,
DestructDLL
};
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static bool LoadLibrary( _PIDB *pConf, _PIBaseDB *pLibs, _PIBaseDB *pFuncs,
_PIDB *pLib, const char *pLibraryName )
{
assert( pConf && pLibs && pFuncs && pLib );
/* ---
Has this library previously been loaded?
--- */
PIDLL *pDLL = 0;
UserDBType *pUser = (UserDBType *)pLibs->Lookup( PIDBTYPE_USER,
pLibraryName, 0 );
if ( pUser )
{ pDLL = (PIDLL *)pUser->GetValue(); };
/* ---
Loop while the library has not been loaded
--- */
if ( !pDLL )
{
const char *pLookupKey =
#if (POSIX)
PIDBKEY_CONF_POSIXPATH;
#elif (WIN32)
PIDBKEY_CONF_WIN32PATH;
#elif (WIN16)
PIDBKEY_CONF_WIN32PATH;
#else
# error Unsupported operating System
#endif
/* --- get path --- */
const char *pLibPath = PIConfigDB::LookupValue( (PIConfigDB *)pLib,
pLookupKey, 0, 0 );
if ( !pLibPath )
{
pLibs->Error( pLib, "No library path specified" );
return false;
};
PIString sLibPath = pLibPath;
/* ---
lookup absolute path.
NOTE: This may be antiquated
--- */
const char *pAbsPath = PIConfigDB::LookupValue( (PIConfigDB *)pConf,
PIDBKEY_CONF_PWD, 0, 0 );
PIString sErrorMessage = PIString::Empty();
PIString sPath = sLibPath;
PIString sTmpPath = sPath;
/* ---
Convert the library path into an absolute path
--- */
if ( pAbsPath )
{
if ( !RelativeToAbsolutePath(
pAbsPath,
sTmpPath,
sPath ) )
/* we tried, but failed */
{ sPath = sTmpPath; };
};
pDLL = PIDLL_new( sPath );
/* ---
Add it into the list of libraries ?
--- */
if ( pDLL )
{
if ( PIDLL_isLoaded( pDLL ) )
{
/* --- newly loaded library --- */
pLibs->Add( PIDBTYPE_USER, pLibraryName, PI_NEW(
UserDBType( tDLLTypeFrame, pDLL ) ), 0 );
}
else
{
sErrorMessage = PIDLL_getErrorDescription( pDLL );
PIDLL_delete( pDLL );
pDLL = 0;
};
};
/* --- attempt to load the library --- */
if ( !pDLL || !PIDLL_isLoaded( pDLL ) )
{
PIOStrStream os;
os << "Unable to load library '" << pLibraryName
<< "'" << " with path '" << sLibPath << "'";
if ( sErrorMessage!=PIString::Empty() )
{
os << ", error description is <"
<< sErrorMessage << ">";
};
os << "." << ends;
pLibs->Error( pLib, os.str() );
return false;
};
};
/* ---
Loop over function definitions and load up the functions
--- */
_PIDBIterator *pIter = 0;
AutoDelete<_PIDBIterator> tA( pIter = pLib->GetIterator(
PIDBTYPE_USER, PIDBKEY_CONF_FUNCTION, 0 ) );
if ( pIter && pIter->AtValidElement() )
{
for( ; pIter->AtValidElement(); pIter->Next() )
{
UserDBType *pUser = (UserDBType *)pIter->Current( 0 );
assert( pUser ); if ( !pUser ) { break; };
PIConfigDBString *pS = (PIConfigDBString *)pUser->GetValue();
assert( pS ); if ( !pS ) { break; };
const char *pSymbol = pS->GetString();
void *pSymbolAddress = PIDLL_getAddress( pDLL, pSymbol );
if ( !pSymbolAddress )
{
PIOStrStream os;
const char *pError = PIDLL_getErrorDescription( pDLL );
os << "Error loading function '" << pSymbol
<< "', error description is <"
<< (pError?pError:"") << ">." << ends;
pLibs->Error( pLib, os.str() );
return false;
};
pFuncs->Add( PIDBTYPE_OPAQUE, pSymbol, pSymbolAddress, 0 );
};
};
return true;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
See if the library node exists here, if not, then create it
\*____________________________________________________________________________*/
_PIBaseDB *_PILibrary::GetLibDB( _PIDB *pDB )
{
assert( pDB );
/* --- get to root node --- */
_PIDB *pRoot = pDB->GetRoot();
_PIBaseDB *pLibs = (_PIBaseDB *)pRoot->Lookup( PIDBTYPE_TREE,
PIDBKEY_LIBRARIES, 0 );
if ( !pLibs )
{ pLibs = PI_NEW( _PIBaseDB( pRoot, PIDBKEY_LIBRARIES ) ); };
assert( pLibs );
return pLibs;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
See if the library node exists here, if not, then create it
\*____________________________________________________________________________*/
_PIBaseDB *Internal_LookupLibs( _PIDB *pDB )
{
return _PILibrary::GetLibDB( pDB );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
See if the function node exists here, if not, then create it
\*____________________________________________________________________________*/
_PIBaseDB *Internal_LookupFuncs( _PIDB *pDB )
{
assert( pDB );
/* --- get to root node --- */
/* _PIDB *pRoot = pDB->GetRoot(); */
/* ---
--- */
_PIBaseDB *pLib = Internal_LookupLibs( pDB );
assert( pLib );
_PIBaseDB *pFuncs = (_PIBaseDB *)pLib->Lookup( PIDBTYPE_TREE,
PIDBKEY_FUNCTIONS, 0 );
if ( !pFuncs )
{ pFuncs = PI_NEW( _PIBaseDB( pLib, PIDBKEY_FUNCTIONS ) ); };
assert( pFuncs );
return pFuncs;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PIDLL *_PILibrary::Load( _PIDB *pDB, PIConfigDB *pConfigDB,
const char *pLibraryName )
{
assert( pDB && pLibraryName );
if ( !pConfigDB )
{ pConfigDB = PIConfigDB::FindConfiguration( pDB ); };
if ( !pConfigDB )
{ return 0; };
PIConfigDB *pConf = 0;
_PIDBIterator *pIter = 0;
AutoDelete<_PIDBIterator> tA( pIter = pConfigDB->GetIterator(
PIDBTYPE_TREE, PIDBKEY_CONF_LIBRARY, 0 ) );
for( ; pIter && pIter->AtValidElement(); pIter->Next() )
{
const char *pName = PIConfigDB::LookupValue( (PIConfigDB *)
pIter->Current( 0 ), PIDBKEY_CONF_NAME, 0, 0 );
if ( pName && !strcmp( pName, pLibraryName ) )
{
pConf = (PIConfigDB *)pIter->Current( 0 );
break;
};
};
if ( !pConf )
{
PIOStrStream os;
os << "Cannot find configuration definition for library '"
<< pLibraryName << "'" << ends;
_PIDBUtil::AddLogMessage( pDB, pConfigDB, PILOG_ERROR, os.str() );
return 0;
};
_PIBaseDB *pLibs = Internal_LookupLibs( pDB );
_PIBaseDB *pFuncs = Internal_LookupFuncs( pDB );
assert( pLibs && pFuncs );
if( !pLibs || !pFuncs )
{
_PIDBUtil::AddLogMessage( pDB, pConfigDB, PILOG_ERROR, "\
Cannot find library or function DB for library" );
return 0;
};
if ( !LoadLibrary( pConfigDB, pLibs, pFuncs, pConf, pLibraryName ) )
{ return 0; };
return _PILibrary::Lookup( pDB, pLibraryName );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PIDLL *_PILibrary::Lookup( _PIDB *pDB, const char *pLibraryName )
{
assert( pDB && pLibraryName );
_PIDB *pRoot = pDB->GetRoot();
_PIDB *pLibDB = (_PIDB *)pRoot->Lookup( PIDBTYPE_TREE,
PIDBKEY_LIBRARIES, 0 );
if ( !pLibDB )
{ return 0; };
UserDBType *pUser = (UserDBType *)pLibDB->Lookup(
PIDBTYPE_USER, pLibraryName, 0 );
return pUser ? (PIDLL *)pUser->GetValue() : 0;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PIDLL *_PILibrary::LookupOrLoad( _PIDB *pDB,
PIConfigDB *pConfigDB, const char *pLibraryName )
{
assert( pDB && pLibraryName );
PIDLL *pDLL = _PILibrary::Lookup( pDB, pLibraryName );
if ( !pDLL )
{
pDLL = _PILibrary::Load( pDB, pConfigDB, pLibraryName );
};
return pDLL;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI const PIDLL *PILibrary_load( _PIDB *pDB, PIConfigDB *pConfigDB,
const char *pLibraryName )
{
if ( !pDB || !pLibraryName )
{ return 0; };
return _PILibrary::Load( pDB, pConfigDB, pLibraryName );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
PUBLIC_PIAPI const PIDLL *PILibrary_lookup( _PIDB *pDB,
const char *pLibraryName )
{
if ( !pDB || !pLibraryName )
{ return 0; };
return _PILibrary::Lookup( pDB, pLibraryName );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
const PIDLL *PILibrary_lookupOrLoad( _PIDB *pDB, PIConfigDB *pConfigDB,
const char *pLibraryName )
{
if ( !pDB || !pLibraryName )
{ return 0; };
return _PILibrary::LookupOrLoad( pDB, pConfigDB, pLibraryName );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -