#include "Config.h" #include #include #include #include #include // use boost Config::Config() : m_keyDelimiter( CONFIG_DEFAULT_KEY_DELIMITER ) , m_valueDelimiter( CONFIG_DEFAULT_VALUE_DELIMITER ) { } /// @brief destructor Config::~Config() { Clear(); } /// @brief ÁöÁ¤µÈ Config ÆÄÀÏÀÇ ¸ðµç Á¤º¸¸¦ ÀÐ¾î ³»ºÎ º¯¼ö¿¡ ÀúÀåó¸® /// @param path [in] Config file ÀÇ Àüü °æ·ÎÁ¤º¸( C ¹è¿­ Áö¿øÀ» À§ÇØ & »ç¿ë¾ÈÇÔ) /// @return On success return true, otherwise return false bool Config::Open( const string path ) { ifstream file; // Config file open file.open( path.c_str()); if( file.is_open() == false ) { cerr << "[error] " << __FILE__<< ":" << __func__ << ": config file open failed.[" << path << "][" << strerror( errno ) << "]" << endl; return false; } // ±âÁ¸ µ¥ÀÌÅͰ¡ Á¸ÀçÇÏ¸é »èÁ¦ ó¸®. if( m_configData.empty() == false ) m_configData.clear(); // config file parsing string line; string section; vector< string > configs; // ·çÇÁ¸¦ µ¹¸é¼­ Config ÆÄÀÏÀ» line ´ÜÀ§·Î ÀоîµéÀδç.... while( std::getline( file, line ) ) { // ¾ÕµÚ °ø¹é Á¦°Å ó¸® boost::trim( line ); // °ø¹é or ÁÖ¼®Ã³¸® ¶óÀÎ °Ë»ç if( line.empty() == true || boost::starts_with( line, string("#") ) == true || line == "\r" ) { continue; } // Section ¿©ºÎ °Ë»ç if( boost::starts_with( line, string("[") ) == true && boost::ends_with( line, string("]") ) == true ) { // [] ·Î Á¤ÀÇµÈ section ÀÇ ¹®ÀÚ¿­ °ª ÃßÃâ line.erase( line.begin() ); line.erase( line.end() -1 ); boost::trim( line ); if( section.empty() == false && section != line ) { // ÀÌÀü ¼¼¼Ç°ú ´Ù¸¥ ½Å±Ô ¼¼¼ÇÀÎ °æ¿ì // ±âÁ¸±îÁö ÀúÀåÇß´ø µ¥ÀÌÅ͸¦ ¸â¹ö º¯¼ö¿¡ ÀԷ ó¸® ÈÄ ³»ºÎº¯¼ö ÃʱâÈ­ ó¸®. m_configData.insert( make_pair( section, configs ) ); section.clear(); configs.clear(); } // ½Å±Ô section Á¤º¸ ÀúÀåó¸®. section = line; line.clear(); } else { // Section Á¤º¸°¡ ¾Æ´Ñ °æ¿ì => ½ÇÁ¦ ¼³Á¤°ª .. ^^ configs.push_back( line ); } } // ¸¶Áö¸· ¼¼¼Ç ó¸®µÈ Á¤º¸°¡ Á¸ÀçÇÏ´Â °æ¿ì ¸â¹ö º¯¼ö¿¡ ÀúÀå ó¸®. if( section.empty() == false ) { m_configData.insert( make_pair( section, configs ) ); } // Á¾·á ó¸®. file.close(); return true; } /// @brief Config ¸â¹ö º¯¼ö¿¡ ÀúÀåµÈ µ¥ÀÌÅÍ »èÁ¦Ã³¸®. /// @return void void Config::Clear() { m_configData.clear(); } /// @brief ÇØ´ç Section, Key ¿¡ ÇØ´çÇÏ´Â value °ªÀ» ã¾Æ ¹Ýȯ /// @param section [in] ã°íÀÚ ÇÏ´Â Section °ª /// @param key [in] ã°íÀÚ ÇÏ´Â key °ª /// @param value [out] ÇØ´ç Section, Key ¿¡ ÇØ´çÇÏ´Â value °ªÀ» ÀúÀåÇϱâ À§ÇÑ º¯¼ö /// @return On success return true, otherwise return false bool Config::GetConfig( const string& section, const string& key, string& value ) { // Section ¿¡ ´ëÇÑ Àӽà µ¥ÀÌÅÍ ÀúÀ尴ü »ý¼º vector< string > configs; // ÇØ´ç Section ÀÌ Á¸ÀçÇÏÁö ¾Ê´Â °æ¿ì if( Find( section, configs ) == false ) return false; string line; string result; vector< string >::const_iterator it; // ÇØ´ç Key ÀÌ Á¸ÀçÇÏ´ÂÁö °Ë»ç for( it = configs.begin(); it != configs.end(); it++) { line = *it; // Line »óÀÇ ÁÖ¼® Á¦°Å string::size_type pos = line.find( '#' ); if( pos != string::npos) { line = line.substr(0, pos); boost::trim( line ); } // key = value ¿¡¼­ key ºÎºÐ ÃßÃâ pos = line.find( m_keyDelimiter ); if( pos == string::npos ) { continue; } else { result = line.substr(0, pos); boost::trim( result ); } if( key == result ) { // Key °ªÀÌ µ¿ÀÏÇÑ °æ¿ì value = line.substr( pos+1 ); boost::trim( value ); return true; } } return false; } /// @brief ÇØ´ç Section, Key ¿¡ ÇØ´çÇÏ´Â value Array °ªÀ» ã¾Æ ¹Ýȯ /// @param section [in] ã°íÀÚ ÇÏ´Â Section °ª /// @param key [in] ã°íÀÚ ÇÏ´Â key °ª /// @param value [out] ÇØ´ç Section, Key ¿¡ ÇØ´çÇÏ´Â value °ªÀ» ÀúÀåÇϱâ À§ÇÑ ¹è¿­ /// @return On success return true, otherwise return false bool Config::GetConfig( const string& section, const string& key, vector< string >& value ) { string result; vector< string > vec; if( GetConfig( section, key, result ) == false ) return false; // Àü´Þ¹ÞÀº result °ªÀ» value delimiter ¸¦ ÀÌ¿ëÇÏ¿© parsing ó¸® boost::split( vec, result, boost::is_any_of( m_valueDelimiter )); if( vec.empty() == true ) return false; // ·çÇÁ¸¦ µ¹¸é¼­ trim ó¸® ÈÄ °á°ú°ª ÀúÀåó¸®. vector< string >::const_iterator it; for( it = vec.begin(); it != vec.end(); it++) { result = boost::trim_copy( *it ); if( result.empty() == false ) value.push_back( result ); } if( value.empty() == true ) return false; else return true; } /// @brief Config ¸â¹ö º¯¼ö¿¡ ÀúÀåµÈ µ¥ÀÌÅÍ Áß ÇØ´ç Section ¿¡ ÇØ´çÇÏ´Â µ¥ÀÌÅÍ ¹Ýȯ. /// @param section [in] section ¸í /// @param configData [out] ÇØ´ç Section ¿¡¼­ ÀÐÀº Cofig Á¤º¸¸¦ ÀúÀåÇÒ string ¹è¿­ /// @return On success return true, otherwise return false bool Config::Find( const string& section, vector& configData ) { map< string, vector< string > >::iterator it = m_configData.find( section ); if( it != m_configData.end() ) { // ÇØ´ç section À» ãÀº °æ¿ì configData = it->second; return true; } // ÇØ´ç section À» ãÁö ¸øÇÑ °æ¿ì return false; }