📄 vminifileloader.cpp
字号:
/*****************************************************************************/
/* SOURCE FILE */
/*****************************************************************************/
/*
$Archive: $
$Revision: $
Last Checkin:
$Date: $
By:
$Author: $
Last Modification:
$ModTime: $
Description: Implementation of a class that creates a structured data
store from the contents of an ini file
TOOL And XML FORMS License
==========================
Except where otherwise noted, all of the documentation
and software included in the TOOL package is
copyrighted by Michael Swartzendruber.
Copyright (C) 2005 Michael John Swartzendruber.
All rights reserved.
Access to this code, whether intentional or accidental,
does NOT IMPLY any transfer of rights.
This software is provided "as-is," without any express
or implied warranty. In no event shall the author be held
liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for
any purpose, including commercial applications, and to
alter and redistribute it, provided that the following
conditions are met:
1. All redistributions of source code files must retain
all copyright notices that are currently in place,
and this list of conditions without modification.
2. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
3. If you use this software in another product, an acknowledgment
in the product documentation would be appreciated but is
not required.
4. Modified versions in source or binary form must be plainly
marked as such, and must not be misrepresented as being
the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $JustDate: $";
/*****************************************************************************/
#include <fstream> // 2b|!2b==?
#include <iostream>#include "VMIniFileLoader.h"
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::VMIniFileLoader
DESCRIPTION: ctor.
INPUT: poFileName - name of the file to load
OUTPUT: none
RETURNS: none
*/
VMIniFileLoader::VMIniFileLoader( void ): m_oCurrentSection( cchGlobal ), m_bCanLoad( false ){ m_oCmdlineSection = std::string( cchCmdline ); m_oProfileSection = std::string( "" );}/* End of function "VMIniFileLoader::VMIniFileLoader"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::~VMIniFileLoader
DESCRIPTION: dtor. cleans up storage
INPUT: void
OUTPUT: none
RETURNS: none
*/
VMIniFileLoader::~VMIniFileLoader( void ){ SECTION_MAP_ITER oIter; for ( oIter = m_oEntries.begin(); oIter != m_oEntries.end(); oIter++ ) { STRING_MAP* poKeyValues = (*oIter).second; poKeyValues->clear(); delete poKeyValues; } m_oEntries.clear();}/* End of function "VMIniFileLoader::~VMIniFileLoader"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::IsGood
DESCRIPTION: quality check
INPUT: void
OUTPUT: none
RETURNS: true if good, false if not
*/
bool VMIniFileLoader::IsGood( void ){ return( m_bCanLoad );}/* End of function "VMIniFileLoader::IsGood"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::ContainsEntry
DESCRIPTION: query to determine if there is a data value stored at the
given section/key pair location
INPUT: oSection - ini file section
oKey - ini file key
OUTPUT:
RETURNS: true if entry found, false if not
*/
bool VMIniFileLoader::ContainsEntry( std::string oSection, std::string oKey ){ SECTION_MAP_ITER oSectionsIter = m_oEntries.find( oSection );
if ( oSectionsIter != m_oEntries.end() ) { STRING_MAP* poKeyValues = (*oSectionsIter).second; STRING_MAP_ITER oEntry = poKeyValues->find( oKey );
if ( oEntry != poKeyValues->end() ) { return( true ); } else { return( false ); } } else { return( false ); }}/* End of function "VMIniFileLoader::ContainsEntry"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::ContainsEntry
DESCRIPTION: query to determine if there is any entry in the 'global'
section
INPUT: oKey - the key value to look for
OUTPUT: none
RETURNS: true if entry exists, false if not
*/
bool VMIniFileLoader::ContainsEntry( std::string oKey ){ return( ContainsEntry( std::string( cchGlobal ), oKey ) );}/* End of function "VMIniFileLoader::ContainsEntry"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::TrimRight
DESCRIPTION: trims trailing white space from the given string
INPUT: pchToTrim - pointer to the string to trim
OUTPUT: none
RETURNS: pointer to the trimmed string
*/
char* VMIniFileLoader::TrimRight( char* pchToTrim ){ if ( NULL != pchToTrim ) { int iLength = strlen( pchToTrim ); while ( iLength && isspace( pchToTrim[ iLength - 1 ] ) ) { pchToTrim[ --iLength ] = '\0'; } } return( pchToTrim );}/* End of function "VMIniFileLoader::TrimRight"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::TrimLeft
DESCRIPTION: trims leading white space from the given string
INPUT: pchToTrim - pointer to the string to trim
OUTPUT: none
RETURNS: pointer to the trimmed string
*/
char* VMIniFileLoader::TrimLeft( char* pchToTrim ){ if ( NULL != pchToTrim ) { int iCursor = 0; while ( pchToTrim[ iCursor ] && isspace( pchToTrim[ iCursor ] ) ) { iCursor++; } if ( iCursor ) { strcpy( pchToTrim, &pchToTrim[ iCursor ] ); } } return( pchToTrim );}/* End of function "VMIniFileLoader::TrimLeft"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::TrimBoth
DESCRIPTION: function to from leading/trailing white space from the
given string
INPUT: pchToTrim - pointer to the string to trim
OUTPUT: none
RETURNS: pointer to the trimmed string
*/
char* VMIniFileLoader::TrimBoth( char* pchToTrim ){ TrimLeft( pchToTrim ); return( TrimRight( pchToTrim ) );}/* End of function "VMIniFileLoader::TrimBoth"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::LoadConfigFromFile
DESCRIPTION: loads the config file
INPUT: void
OUTPUT: none
RETURNS: true if worked, false if not
*/
bool VMIniFileLoader::LoadConfigFromFile( const std::string oFileName ){ m_oFileName = oFileName;
char achFormat[ 20 ]; sprintf( achFormat, "%%%d[^=]=%%%dc", ciParamNameLength, ciMaxLineLength );
if ( !m_bCanLoad )
{ std::ifstream oFile; oFile.open( m_oFileName.c_str() ); oFile.seekg( 0, std::ios::end ); int iLeftToRead = oFile.tellg(); oFile.seekg( 0, std::ios::beg ); char achBuffer[ ciMaxLineLength + 1 ]; char achName[ ciParamNameLength + 1 ]; char achValue[ ciMaxLineLength + 1 ]; while ( iLeftToRead > 0 ) { memset( achBuffer, 0, ciMaxLineLength ); oFile.getline( achBuffer, ciMaxLineLength ); iLeftToRead -= oFile.gcount(); TrimBoth( achBuffer ); if ( *achBuffer == '#' || *achBuffer == ';' ) { // its a comment, so..... // continue; } TrimBoth( achBuffer ); if ( ( *achBuffer == '[' ) && ( achBuffer[ strlen( achBuffer ) - 1 ] == ']' ) ) { // its a section marker, so store that value // m_oCurrentSection = achBuffer; continue; } memset( achName, 0, ciParamNameLength ); memset( achValue, 0, ciMaxLineLength ); if ( sscanf( achBuffer, achFormat, achName, achValue ) > 0 ) { TrimBoth( achName ); TrimBoth( achValue ); if ( strlen( achName ) && strlen( achValue ) ) { // store the values found // SECTION_MAP_ITER oSectionsIter = m_oEntries.find( m_oCurrentSection ); if ( oSectionsIter == m_oEntries.end() ) { STRING_MAP* poNewMap = new STRING_MAP; poNewMap->insert( STRING_MAP::value_type( std::string( achName ), std::string( achValue ) ) ); m_oEntries.insert( SECTION_MAP::value_type( m_oCurrentSection, poNewMap ) ); } else { STRING_MAP* poSection = (*oSectionsIter).second; poSection->insert( STRING_MAP::value_type( std::string( achName ), std::string( achValue ) ) ); } } } } if ( oFile.is_open() )
{ oFile.close(); }
}
m_bCanLoad = true;
return( true );}/* End of function "VMIniFileLoader::LoadConfigFromFile"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::GetValueAsString
DESCRIPTION: returns the data value associated with the current section
key name pair. or the default value if not found. will also
optionally store the default value into the data store
INPUT: oKey - the key to use
oDefaultValue - the default value if not found
bStoreDefault - flag to store the default value provided
OUTPUT:
RETURNS: the value stored under the given key
*/
std::string VMIniFileLoader::GetValueAsString( std::string oKey, std::string oDefaultValue, bool bStoreDefault ){ if ( ( m_oCmdlineSection != "" )
&& ContainsEntry( m_oCmdlineSection, oKey ) ) { return( GetSectionValueAsString( m_oCmdlineSection, oKey ) ); } else
if ( ( m_oProfileSection != "" )
&& ContainsEntry( m_oProfileSection, oKey ) ) { return( GetSectionValueAsString( m_oProfileSection, oKey ) ); } else { return( GetValueAsString( std::string( cchGlobal ), oKey, oDefaultValue, bStoreDefault ) ); }}/* End of function "VMIniFileLoader::GetValueAsString"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::GetValueAsInteger
DESCRIPTION: returns the data value associated with the current section
key name pair. or the default value if not found. will also
optionally store the default value into the data store
INPUT: oKey - the key to use
iDefaultValue - the default value if not found
bStoreDefault - flag to store the default value provided
OUTPUT:
RETURNS: the value stored under the given key as an integer
*/
int VMIniFileLoader::GetValueAsInteger( std::string oKey, int iDefaultValue, bool bStoreDefault ){ char achValue[ 32 ];
sprintf( achValue, "%d", iDefaultValue ); std::string oValue = GetValueAsString( oKey, std::string( achValue ), bStoreDefault );
return( atoi( oValue.c_str() ) );}/* End of function "VMIniFileLoader::GetValueAsInteger"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::GetValueAsBool
DESCRIPTION: returns the data value associated with the current section
key name pair. or the default value if not found. will also
optionally store the default value into the data store
INPUT: oKey - the key to use
bDefaultValue - the default value if not found
bStoreDefault - flag to store the default value provided
OUTPUT:
RETURNS: the value stored under the given key as a boolean
*/
bool VMIniFileLoader::GetValueAsBool( std::string oKey, bool bDefaultValue, bool bStoreDefault ){ char achValue[ 32 ]; sprintf( achValue, "%s", bDefaultValue ? "true" : "false" ); std::string oValue; oValue = GetValueAsString( oKey, std::string( achValue ), bStoreDefault ); return( oValue == "true" );}/* End of function "VMIniFileLoader::GetValueAsBool"
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: VMIniFileLoader::GetSectionValueAsString
DESCRIPTION: returns the value stored under the section/key key pair
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -