18 #define INI_LINE_BUFFER_SIZE 0x1000
23 #define CRYPTOPP_DEFAULT_NO_DLL
24 #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
27 #pragma push_macro( "new" )
30 #pragma pop_macro( "new" )
33 #pragma comment( lib, "cryptlib.lib" ) // ライブラリの指定
34 USING_NAMESPACE(CryptoPP)
35 typedef
unsigned char BYTE;
39 using namespace kome::core;
44 #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
45 #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
66 std::string sec =
NVL( section,
"" );
78 const char*
IniFile::getString(
const char* section,
const char* key,
const char* defaultValue ) {
80 std::string sec =
NVL( section,
"" );
88 return m_sectionMap[ sec ].getStringValue( key, defaultValue );
93 if ( (
char *)
NULL == pcPassword || (
char *)
NULL == pcDecrypt )
98 std::string strVal = pcPassword;
99 if ( -1 != strVal.find(
"@@@" ) )
103 byte bKey[AES::DEFAULT_KEYLENGTH];
104 byte bIv[AES::BLOCKSIZE];
108 memset( (
void *)bKey, 0x01, AES::DEFAULT_KEYLENGTH );
109 memset( (
void *)bIv, 0x01, AES::BLOCKSIZE );
114 int i = strVal.find_last_of(
"@@@" );
115 strVal = strVal.substr( i + 1, ( strVal.length( ) - i ) );
117 std::string strInputDec;
118 HexDecoder insHexDecode(
new StringSink( strInputDec ) );
119 insHexDecode.Put( (BYTE*)( strVal.c_str( ) ), strVal.size( ) );
120 insHexDecode.MessageEnd( );
123 CTR_Mode<AES>::Decryption insDecryption;
124 insDecryption.SetKeyWithIV( bKey,
sizeof( bKey ), bIv );
127 std::string strOutputDec;
128 StreamTransformationFilter insStfDec( insDecryption,
new StringSink( strOutputDec ) );
129 insStfDec.Put( (BYTE*)strInputDec.c_str( ), strInputDec.size( ) );
130 insStfDec.MessageEnd( );
132 strVal = strOutputDec;
139 memset( (
void *)pcDecrypt, 0x00, szDecrypt );
140 if ( szDecrypt <= strVal.length( ) )
144 memcpy( (
void *)pcDecrypt, (
char *)strVal.c_str( ), strVal.length( ) );
151 if ( (
char *)
NULL == pcPassword || (
char *)
NULL == pcEncrypt )
156 std::string strVal = pcPassword;
159 byte bKey[AES::DEFAULT_KEYLENGTH];
160 byte bIv[AES::BLOCKSIZE];
164 memset( (
void *)bKey, 0x01, AES::DEFAULT_KEYLENGTH );
165 memset( (
void *)bIv, 0x01, AES::BLOCKSIZE );
171 CTR_Mode<AES>::Encryption insEncryptor;
172 insEncryptor.SetKeyWithIV( bKey,
sizeof( bKey ), bIv );
175 std::string strInputEnc = strVal;
176 std::string strOutputEnc;
177 StreamTransformationFilter insStfEnc( insEncryptor,
new StringSink( strOutputEnc ) );
178 insStfEnc.Put( (BYTE*)( strInputEnc.c_str( ) ), strInputEnc.size( ) );
179 insStfEnc.MessageEnd( );
183 HexEncoder insHexEncode(
new StringSink( strHex ) );
184 insHexEncode.Put( (BYTE*)( strOutputEnc.c_str( ) ), strOutputEnc.size( ) );
185 insHexEncode.MessageEnd( );
188 strVal.append(
"@@@" );
189 strVal.append( strHex );
195 memset( (
void *)pcEncrypt, 0x00, szEncrypt );
196 if ( szEncrypt <= strVal.length( ) )
200 memcpy( (
void *)pcEncrypt, (
char *)strVal.c_str( ), strVal.length( ) );
209 std::string sec =
NVL( section,
"" );
217 m_sectionMap[ sec ].setValue( key, ( value ?
"true" :
"false" ) );
223 std::string sec =
NVL( section,
"" );
231 return m_sectionMap[ sec ].getBoolValue( key, defaultValue );
237 std::string sec =
NVL( section,
"" );
245 m_sectionMap[ sec ].setValue( key, FMT(
"%d", value ).c_str() );
251 std::string sec =
NVL( section,
"" );
259 return m_sectionMap[ sec ].getIntValue( key, defaultValue );
265 std::string sec =
NVL( section,
"" );
273 m_sectionMap[ sec ].setValue( key, FMT(
"%f", value ).c_str() );
279 std::string sec =
NVL( section,
"" );
288 if(
m_sectionMap[ sec ].getNumberOfProperties() == 0 ) {
291 for( std::vector< std::string >::iterator it =
m_sections.begin(); it !=
m_sections.end(); it++ )
293 if (!it->compare( sec ))
305 if( section ==
NULL ) {
321 std::string sec =
NVL( section,
"" );
329 return m_sectionMap[ sec ].getDoubleValue( key, defaultValue );
353 if( properties ==
NULL ) {
365 if( properties ==
NULL ) {
368 return properties->
getKey( index );
377 if( properties ==
NULL ) {
380 return properties->
getValue( index );
386 LOG_INFO( FMT(
"Loading INI file... [%s]",
NVL( path,
"" ) ) );
390 LOG_WARN( FMT(
"Illegal path [%s]",
NVL( path,
"" ) ) );
401 FILE* fp =
fileopen( p.c_str(),
"r" );
403 LOG_WARN( FMT(
"Failed to open the file. [%s]",
NVL( path,
"" ) ) );
409 char line[ INI_LINE_BUFFER_SIZE ];
413 while( fgets( line, INI_LINE_BUFFER_SIZE, fp ) !=
NULL ) {
415 line[ INI_LINE_BUFFER_SIZE - 1 ] =
'\0';
417 if( line[ 0 ] ==
'#' || line[ 0 ] ==
';' ) {
423 memcpy( line, l.c_str(), l.length() );
424 line[ l.length() ] =
'\0';
426 if( line[ 0 ] ==
'[' ) {
427 char* c = strstr( line,
"]" );
431 c = strstr( line,
"[" );
436 section = std::string( line + 1 );
441 char* value = strstr( line,
"=" );
442 if( value !=
NULL ) {
446 if( params ==
NULL ) {
459 std::string strValue = value;
460 if ( ( 0 == strKey.compare(
"password" ) ||
461 0 == strKey.compare(
"common_db_password" ) )
462 && -1 != strValue.find(
"@@@" ) )
466 if (
true ==
decryptPassword( strValue.c_str( ), cBuff,
sizeof( cBuff ) ) )
472 params->
setValue( key, strValue.c_str( ) );
491 LOG_INFO( FMT(
"Saving INI file... [%s]", p.c_str() ) );
494 FILE* fp =
fileopen( p.c_str(),
"w" );
498 "Failed to open the file. [%s]",
506 for(
unsigned int i = 0; i <
m_sections.size(); i++ ) {
510 if( !section.empty() ) {
511 fprintf( fp,
"[%s]\n", section.c_str() );
526 std::string strValue = params->
getValue( i );
527 if ( ( 0 == strKey.compare(
"password" ) || 0 == strKey.compare(
"common_db_password" ) )
528 && ( 0 != strValue.compare(
"none" ) ) )
538 fprintf( fp,
"%s=%s\n", params->
getKey( i ), strValue.c_str( ) );
564 if( iniFile ==
NULL ) {
566 if( iniFile->
load( p.c_str() ) ) {
588 if( iniFile ==
NULL ) {
622 for( std::map< std::string, IniFile* >::iterator it = m_fileMap.begin();
623 it != m_fileMap.end(); it++ ) {
641 if( m_fileMap.find( path ) != m_fileMap.end() ) {
642 delete m_fileMap[ path ];
646 m_fileMap[ path ] = file;
655 if( m_fileMap.find( p ) == m_fileMap.end() ) {
659 return m_fileMap[ p ];
665 for( std::map< std::string, IniFile* >::iterator it = m_fileMap.begin();
666 it != m_fileMap.end(); it++ ) {
Properties * getProperties(const char *section)
gets properties object specified section
bool encryptPassword(const char *pcPassword, char *pcEncrypt, const size_t szEncrypt)
decrypt password
virtual ~IniFileManager()
destructor
double getDouble(const char *section, const char *key, const double defaultValue)
gets real number value
static void saveAllIniFiles()
save all ini files
void setBool(const char *section, const char *key, const bool value)
sets boolean value
bool save(const char *path=NULL)
save the ini file
keys and values management class
IniFile object management class.
static IniFileManager & getInstance()
gets ini file manager object (This is the only object.)
void setValue(const char *key, const char *value)
sets parameter value
const char * getParameterName(const char *section, const unsigned int index)
gets parameter name
const char * getString(const char *section, const char *key, const char *defaultValue)
gets string value
static IniFile * getIniFile(const char *path)
gets ini file object. If specified file is not opened, this method calls loadIniFile method...
void setInt(const char *section, const char *key, const int value)
sets integer number value
bool getBool(const char *section, const char *key, const bool defaultValue)
gets boolean value
unsigned int getNumberOfSections()
gets the number of sections
const char * getValue(const unsigned int index)
gets the parameter value
int getInt(const char *section, const char *key, const int defaultValue)
get integer number value
std::string trimstring(const char *s)
remove white spaces from both ends of specified string
bool fileexists(const char *path)
judge whether file exists
bool checkfile(const char *path)
check file before open. (This function calls fileexists and isdirectory )
unsigned int getNumberOfProperties()
gets the number of Properties
std::map< std::string, Properties > m_sectionMap
const char * getParameterValue(const char *section, const unsigned int index)
gets parameter value
IniFileManager()
constructor
#define NVL(checkVal, replaceVal)
interfaces of IniFile class
std::string tolowercase(const char *s)
convert all of the characters in the string given to lower case
virtual ~IniFile()
destructor
FILE * fileopen(const char *path, const char *mode)
opens file
const char * getKey(const unsigned int index)
gets the name of parameter
bool load(const char *path)
load the ini file
static IniFile * loadIniFile(const char *path)
loads ini file and gets IniFile object.
unsigned int getNumberOfParameters(const char *section)
gets the number of parameters specified section
bool decryptPassword(const char *pcPassword, char *pcDecrypt, const size_t szDecrypt)
decrypt password
std::string absolutepath(const char *path)
get absolute path
void save()
save all ini files
void setDouble(const char *section, const char *key, const double value)
sets real number value
const char * getFilePath()
gets file path
void deleteValue(const char *section, const char *key)
deletes value
void setFile(IniFile *file)
sets ini file information object to map
ini file management class
std::vector< std::string > m_sections
IniFile * getFile(const char *path)
gets ini file information object
const char * getSection(const unsigned int index)
gets section
void setString(const char *section, const char *key, const char *value)
sets string value