📄 properties.cpp
字号:
//--------------------------------------------------------------------------------//// Filename : Properties.cpp// Written By : Reiot// Description : ////--------------------------------------------------------------------------------// include files#include "Properties.h"#include <stdlib.h> // atoi()//--------------------------------------------------------------------------------//--------------------------------------------------------------------------------const char Properties::Comment = '#';const char Properties::Separator = ':';const char * Properties::WhiteSpaces = " \t";//--------------------------------------------------------------------------------// constructor//--------------------------------------------------------------------------------Properties::Properties () throw (){ __BEGIN_TRY __END_CATCH}Properties::Properties ( const string & filename ) throw (): m_Filename(filename){ __BEGIN_TRY __END_CATCH} //--------------------------------------------------------------------------------// destructor//--------------------------------------------------------------------------------Properties::~Properties () throw (){ __BEGIN_TRY // 葛电 pair 甫 昏力茄促. m_Properties.clear(); __END_CATCH}//--------------------------------------------------------------------------------// load from file//--------------------------------------------------------------------------------void Properties::load () throw ( IOException , Error ){ __BEGIN_TRY if ( m_Filename.empty() ) throw Error("filename not specified"); ifstream ifile( m_Filename.c_str() , ios::in ); if ( ! ifile ) throw FileNotExistException( m_Filename.c_str() ); while ( true ) { string line; getline( ifile , line ); if ( ifile.eof() ) break; // 内膏飘 扼牢捞芭唱 后 扼牢捞骨肺 skip 茄促. if ( line.size() == 0 || line[0] == Comment ) continue; // key 狼 矫累巩磊(white space啊 酒囱 巩磊)甫 茫绰促. uint key_begin = line.find_first_not_of( WhiteSpaces ); // key_begin捞 npos 扼绰 舵篮 弊繁 巩磊甫 茫瘤 给沁促绰 舵捞促. // 溜, 柯烹 white space 肺父 登绢 乐绰 扼牢捞骨肺 skip 茄促. if ( key_begin == string::npos ) continue; // key 客 value 甫 备盒窿绰 separator 甫 茫绰促. // key_end 焊促 sep 甫 刚历 茫绰 捞蜡绰 find_last_not_of()甫 结辑 // sep 俊辑何磐 开栏肺 key_end 甫 茫扁 困秦辑捞促. ^^; uint sep = line.find( Separator , key_begin ); // Separator 甫 惯斑窍瘤 给沁阑 版快绰 颇教 俊矾肺 埃林茄促. if ( sep == string::npos ) throw IOException("missing separator"); // sep 俊辑何磐 开栏肺 key_end 甫 茫酒唱埃促. uint key_end = line.find_last_not_of( WhiteSpaces , sep - 1 ); // sep 俊辑何磐 value_begin 阑 茫绰促. uint value_begin = line.find_first_not_of( WhiteSpaces , sep + 1 ); // key 绰 乐瘤父 value 啊 绝绰 惑怕捞促. if ( value_begin == string::npos ) throw IOException("missing value"); // 盖 场俊辑何磐 开栏肺 value_end 甫 茫绰促. // ( value_begin 捞 乐栏搁 value_end 绰 公炼扒 粮犁茄促.) uint value_end = line.find_last_not_of( WhiteSpaces ); // key_begin,key_end 客 value_begin,value_end 甫 荤侩秦辑 // line 狼 substring 牢 key 客 value 甫 积己茄促. string key = line.substr( key_begin , key_end - key_begin + 1 ); string value = line.substr( value_begin , value_end - value_begin + 1 ); // property 肺 殿废茄促. setProperty( key , value ); } ifile.close(); __END_CATCH}//--------------------------------------------------------------------------------// save to file//--------------------------------------------------------------------------------void Properties::save () throw ( IOException ){ __BEGIN_TRY if ( m_Filename.empty() ) throw Error("filename not specified"); ofstream ofile( m_Filename.c_str() , ios::out | ios::trunc ); for ( map< string , string , StringCompare >::iterator itr = m_Properties.begin() ; itr != m_Properties.end() ; itr ++ ) ofile << itr->first << ' ' << Separator << ' ' << itr->second << endl; ofile.close(); __END_CATCH}//--------------------------------------------------------------------------------// get property//--------------------------------------------------------------------------------string Properties::getProperty ( string key ) const throw ( NoSuchElementException ){ __BEGIN_TRY string value; map< string , string , StringCompare >::const_iterator itr = m_Properties.find( key ); if ( itr != m_Properties.end() ) value = itr->second; else throw NoSuchElementException(key); return value; __END_CATCH}//--------------------------------------------------------------------------------// get property as int//--------------------------------------------------------------------------------int Properties::getPropertyInt ( string key ) const throw ( NoSuchElementException ){ __BEGIN_TRY return atoi( getProperty(key).c_str() ); __END_CATCH}//--------------------------------------------------------------------------------// set property//--------------------------------------------------------------------------------void Properties::setProperty ( string key , string value ) throw (){ __BEGIN_TRY // 捞固 虐啊 粮犁且 版快, value 甫 丹绢敬促. m_Properties[ key ] = value; __END_CATCH}//--------------------------------------------------------------------------------// get debug string//--------------------------------------------------------------------------------string Properties::toString () const throw (){ __BEGIN_TRY StringStream msg; for ( map< string , string , StringCompare >::const_iterator itr = m_Properties.begin() ; itr != m_Properties.end() ; itr ++ ) { msg << itr->first << " : " << itr->second << "\n"; } if ( msg.isEmpty() ) msg << "empty properties"; return msg.toString(); __END_CATCH}//--------------------------------------------------------------------------------// global variable definition//--------------------------------------------------------------------------------Properties * g_pConfig = NULL;Properties * g_pTestConfig = NULL; // by sigi. 2002.12.26
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -