Commit cdc51ec1 authored by Dipl.-Ing. Jonas Stienen's avatar Dipl.-Ing. Jonas Stienen
Browse files

Refactoring and adding feature to store a struct to INI file (e.g. VACore.ini

parent 200f97d2
...@@ -34,13 +34,20 @@ namespace VACore ...@@ -34,13 +34,20 @@ namespace VACore
* @return Pointer to the core instance (VA interface API from VABase) * @return Pointer to the core instance (VA interface API from VABase)
*/ */
VACORE_API IVAInterface* CreateCoreInstance( const CVAStruct& oArgs ); VACORE_API IVAInterface* CreateCoreInstance( const CVAStruct& oArgs );
//! Parses input INI configuration file and converts it into a VA core config struct //! Parses input INI configuration file and converts it into a VA core config struct
/** /**
* @param[in] sConfigFilePath File path to core config (INI) file * @param[in] sConfigFilePath File path to core config (INI) file
* @return Core configuration * @return Core configuration
*/ */
VACORE_API CVAStruct GetCoreConfigFromFile( const std::string& sConfigFilePath ); VACORE_API CVAStruct LoadCoreConfigFromFile( const std::string& sConfigFilePath );
//! Store configuration to INI file from VA core config struct
/**
* @param[in] oCoreConfig Core configuration struct
* @param[in] sConfigFilePath File path to core config (INI) file
*/
VACORE_API void StoreCoreConfigToFile( const CVAStruct& oCoreConfig, const std::string& sConfigFilePath );
//! Factore method - create a VACore instance with configuration file //! Factore method - create a VACore instance with configuration file
/** /**
...@@ -51,7 +58,7 @@ namespace VACore ...@@ -51,7 +58,7 @@ namespace VACore
*/ */
inline IVAInterface* CreateCoreInstance( const std::string& sConfigFile = VACORE_DEFAULT_CONFIGFILE ) inline IVAInterface* CreateCoreInstance( const std::string& sConfigFile = VACORE_DEFAULT_CONFIGFILE )
{ {
return CreateCoreInstance( VACore::GetCoreConfigFromFile( sConfigFile ) ); return CreateCoreInstance( VACore::LoadCoreConfigFromFile( sConfigFile ) );
}; };
//! Returns the filesystem path of the VACore shared lib (e.g. VACore.dll) //! Returns the filesystem path of the VACore shared lib (e.g. VACore.dll)
......
...@@ -140,18 +140,18 @@ CVAStructValue interpretStructKey( const std::string& s ) ...@@ -140,18 +140,18 @@ CVAStructValue interpretStructKey( const std::string& s )
return CVAStructValue( s ); return CVAStructValue( s );
} }
void readINIFileAsStruct( const std::string& sFilename, CVAStruct& oData ) void LoadStructFromINIFIle( const std::string& sFilePath, CVAStruct& oData )
{ {
if( !doesFileExist( sFilename ) ) if( !doesFileExist( sFilePath ) )
VA_EXCEPT2( FILE_NOT_FOUND, std::string( "INI file \"" ) + sFilename + std::string( "\" not found" ) ); VA_EXCEPT2( FILE_NOT_FOUND, std::string( "INI file \"" ) + sFilePath + std::string( "\" not found" ) );
oData.Clear(); oData.Clear();
std::vector<std::string> vsSections = INIFileGetSections( sFilename ); std::vector<std::string> vsSections = INIFileGetSections( sFilePath );
for( std::vector<std::string>::iterator it = vsSections.begin(); it != vsSections.end(); ++it ) for( std::vector<std::string>::iterator it = vsSections.begin(); it != vsSections.end(); ++it )
{ {
std::string& sSection( *it ); std::string& sSection( *it );
std::vector<std::string> vsKeys = INIFileGetKeys( sFilename, sSection ); std::vector<std::string> vsKeys = INIFileGetKeys( sFilePath, sSection );
// Spezialfall: Section [] nach Root abbilden // Spezialfall: Section [] nach Root abbilden
if( it->empty() ) if( it->empty() )
...@@ -160,8 +160,8 @@ void readINIFileAsStruct( const std::string& sFilename, CVAStruct& oData ) ...@@ -160,8 +160,8 @@ void readINIFileAsStruct( const std::string& sFilename, CVAStruct& oData )
{ {
std::string& sKey( *jt ); std::string& sKey( *jt );
if( oData.HasKey( sKey ) ) if( oData.HasKey( sKey ) )
VA_EXCEPT2( INVALID_PARAMETER, "Uniqueness violation in " + sFilename + ": multiple detection of key '" + sKey + "' in section '" + sSection + "'" ); VA_EXCEPT2( INVALID_PARAMETER, "Uniqueness violation in " + sFilePath + ": multiple detection of key '" + sKey + "' in section '" + sSection + "'" );
oData[ sKey ] = interpretStructKey( INIFileReadString( sFilename, sSection, sKey ) ); oData[ sKey ] = interpretStructKey( INIFileReadString( sFilePath, sSection, sKey ) );
} }
} }
else else
...@@ -171,14 +171,93 @@ void readINIFileAsStruct( const std::string& sFilename, CVAStruct& oData ) ...@@ -171,14 +171,93 @@ void readINIFileAsStruct( const std::string& sFilename, CVAStruct& oData )
{ {
std::string& sKey( *jt ); std::string& sKey( *jt );
if( oData.HasKey( sKey ) ) if( oData.HasKey( sKey ) )
VA_EXCEPT2( INVALID_PARAMETER, "Uniqueness violation in " + sFilename + ": multiple detection of key '" + sKey + "' in section '" + sSection + "'" ); VA_EXCEPT2( INVALID_PARAMETER, "Uniqueness violation in " + sFilePath + ": multiple detection of key '" + sKey + "' in section '" + sSection + "'" );
oSubData[ *jt ] = interpretStructKey( INIFileReadString( sFilename, sSection, sKey ) ); oSubData[ *jt ] = interpretStructKey( INIFileReadString( sFilePath, sSection, sKey ) );
} }
oData[ sSection ] = oSubData; oData[ sSection ] = oSubData;
} }
} }
} }
void StoreStructToINIFile( const std::string& sFilePath, const CVAStruct& oData )
{
CVAStruct::const_iterator cit = oData.Begin();
while( cit != oData.End() )
{
const std::string& sKey( cit->first );
const CVAStructValue& oValue( cit->second );
cit++;
// Root-level plain key-value pairs, a bit unusual though
switch( oValue.GetDatatype() )
{
case CVAStructValue::BOOL:
{
INIFileWriteBool( sKey, bool( oValue ) );
break;
}
case CVAStructValue::INT:
{
INIFileWriteInt( sKey, int( oValue ) );
break;
}
case CVAStructValue::DOUBLE:
{
INIFileWriteDouble( sKey, double( oValue ) );
break;
}
case CVAStructValue::STRING:
{
INIFileWriteString( sKey, std::string( oValue ) );
break;
}
case CVAStructValue::STRUCT:
{
const std::string& sSection( sKey );
const CVAStruct& oSection( oValue );
CVAStruct::const_iterator cit_section = oSection.Begin();
while( cit_section != oSection.End() )
{
const std::string& sKeyInSection( cit_section->first );
const CVAStructValue& oSectionValue( cit_section->second );
cit_section++;
// Section-level key-value pairs
switch( oSectionValue.GetDatatype() )
{
case CVAStructValue::BOOL:
{
INIFileWriteBool( sFilePath, sSection, sKeyInSection, bool( oSectionValue ) );
break;
}
case CVAStructValue::INT:
{
INIFileWriteInt( sFilePath, sSection, sKeyInSection, int( oSectionValue ) );
break;
}
case CVAStructValue::DOUBLE:
{
INIFileWriteDouble( sFilePath, sSection, sKeyInSection, double( oSectionValue ) );
break;
}
case CVAStructValue::STRING:
{
INIFileWriteString( sFilePath, sSection, sKeyInSection, std::string( oSectionValue ) );
break;
}
}
}
break;
}
}
}
}
double GetAzimuthOnTarget_DEG( const VAVec3& vOriginPos, const VAVec3& vView, const VAVec3& vUp, const VAVec3& vTargetPos ) double GetAzimuthOnTarget_DEG( const VAVec3& vOriginPos, const VAVec3& vView, const VAVec3& vUp, const VAVec3& vTargetPos )
{ {
VAVec3 vDir = vTargetPos - vOriginPos; VAVec3 vDir = vTargetPos - vOriginPos;
......
...@@ -59,8 +59,15 @@ double GetElevationOnTarget_DEG( const VAVec3& vOriginPos, const VAVec3& vView, ...@@ -59,8 +59,15 @@ double GetElevationOnTarget_DEG( const VAVec3& vOriginPos, const VAVec3& vView,
//! Ersetzt Backslashes (bzw. PATH_SEPARATOR) durch doppelte Backslashes (bzw. PATH_SEPARATOR) //! Ersetzt Backslashes (bzw. PATH_SEPARATOR) durch doppelte Backslashes (bzw. PATH_SEPARATOR)
std::string correctPathForLUA( const std::string& sPath ); std::string correctPathForLUA( const std::string& sPath );
//! Lies eine INI-Datei ein und gibt sie als VAStruct zurck //! Loads an INI file and converts it to a VA struct
void readINIFileAsStruct( const std::string& sFilename, CVAStruct& oData ); void LoadStructFromINIFIle( const std::string& sFilePath, CVAStruct& oData );
//! Will only write top level values and first level structs as sections with simple types
/**
* @param[in] sFilePath File path
* @param[in] oData Struct data
*/
void StoreStructToINIFile( const std::string& sFilePath, const CVAStruct& oData );
//! Sets all motion related parameters in a core event based on a motion event //! Sets all motion related parameters in a core event based on a motion event
void SetCoreEventParams( CVAEvent& oEvent, const CVAMotionState* pMotionState ); void SetCoreEventParams( CVAEvent& oEvent, const CVAMotionState* pMotionState );
......
...@@ -185,7 +185,12 @@ IVAInterface* VACore::CreateCoreInstance( const CVAStruct& oArgs ) ...@@ -185,7 +185,12 @@ IVAInterface* VACore::CreateCoreInstance( const CVAStruct& oArgs )
return new CVACoreImpl( oArgs ); return new CVACoreImpl( oArgs );
} }
CVAStruct VACore::GetCoreConfigFromFile( const std::string& sConfigFilePath ) void VACore::StoreCoreConfigToFile( const CVAStruct& oConfig, const std::string& sConfigFilePath )
{
StoreStructToINIFile( sConfigFilePath, oConfig );
}
CVAStruct VACore::LoadCoreConfigFromFile( const std::string& sConfigFilePath )
{ {
CVAStruct oFinalCoreConfigStruct, oCurrentConfig; CVAStruct oFinalCoreConfigStruct, oCurrentConfig;
std::list< VistaFileSystemFile > voConfigFiles; std::list< VistaFileSystemFile > voConfigFiles;
...@@ -220,7 +225,7 @@ CVAStruct VACore::GetCoreConfigFromFile( const std::string& sConfigFilePath ) ...@@ -220,7 +225,7 @@ CVAStruct VACore::GetCoreConfigFromFile( const std::string& sConfigFilePath )
} }
VA_VERBOSE( "Config", std::string( "Reading INI file '" ) + oCurrentConfigFile.GetLocalName() + "'" ); VA_VERBOSE( "Config", std::string( "Reading INI file '" ) + oCurrentConfigFile.GetLocalName() + "'" );
readINIFileAsStruct( oCurrentConfigFile.GetName(), oCurrentConfig ); LoadStructFromINIFIle( oCurrentConfigFile.GetName(), oCurrentConfig );
if( oCurrentConfig.HasKey( "paths" ) ) if( oCurrentConfig.HasKey( "paths" ) )
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment