diff --git a/deps/common/conf/ini.cpp b/deps/common/conf/ini.cpp index 3a7ffd89068e0f10ffad02af70dd75686ee9b58c..896490828885c34b6250f36e502198ea793e82ad 100644 --- a/deps/common/conf/ini.cpp +++ b/deps/common/conf/ini.cpp @@ -27,22 +27,23 @@ namespace common { const std::string Ini::DEFAULT_SECTION = std::string(""); const std::map Ini::empty_map_; -Ini::Ini() {} +Ini::Ini() +{} -Ini::~Ini() {} +Ini::~Ini() +{} -void Ini::insert_session(const std::string &session_name) { +void Ini::insert_session(const std::string &session_name) +{ std::map session_map; - std::pair < std::string, std::map < std::string, std::string >> entry = - std::pair < std::string, std::map < std::string, std::string - >> (session_name, - session_map); + std::pair> entry = + std::pair>(session_name, session_map); sections_.insert(entry); } -std::map *Ini::switch_session( - const std::string &session_name) { +std::map *Ini::switch_session(const std::string &session_name) +{ SessionsMap::iterator it = sections_.find(session_name); if (it != sections_.end()) { return &it->second; @@ -59,8 +60,8 @@ std::map *Ini::switch_session( return nullptr; } -const std::map &Ini::get( - const std::string §ion) { +const std::map &Ini::get(const std::string §ion) +{ SessionsMap::iterator it = sections_.find(section); if (it == sections_.end()) { return empty_map_; @@ -69,8 +70,8 @@ const std::map &Ini::get( return it->second; } -std::string Ini::get(const std::string &key, const std::string &defaultValue, - const std::string §ion) { +std::string Ini::get(const std::string &key, const std::string &defaultValue, const std::string §ion) +{ std::map section_map = get(section); std::map::iterator it = section_map.find(key); @@ -81,8 +82,8 @@ std::string Ini::get(const std::string &key, const std::string &defaultValue, return it->second; } -int Ini::put(const std::string &key, const std::string &value, - const std::string §ion) { +int Ini::put(const std::string &key, const std::string &value, const std::string §ion) +{ std::map *section_map = switch_session(section); section_map->insert(std::pair(key, value)); @@ -90,17 +91,15 @@ int Ini::put(const std::string &key, const std::string &value, return 0; } -int Ini::insert_entry(std::map *session_map, - const std::string &line) { +int Ini::insert_entry(std::map *session_map, const std::string &line) +{ if (session_map == nullptr) { - std::cerr << __FILE__ << __FUNCTION__ << " session map is null" - << std::endl; + std::cerr << __FILE__ << __FUNCTION__ << " session map is null" << std::endl; return -1; } size_t equal_pos = line.find_first_of('='); if (equal_pos == std::string::npos) { - std::cerr << __FILE__ << __FUNCTION__ << "Invalid configuration line " - << line << std::endl; + std::cerr << __FILE__ << __FUNCTION__ << "Invalid configuration line " << line << std::endl; return -1; } @@ -115,15 +114,15 @@ int Ini::insert_entry(std::map *session_map, return 0; } -int Ini::load(const std::string &file_name) { +int Ini::load(const std::string &file_name) +{ std::ifstream ifs; try { bool continue_last_line = false; - std::map *current_session = - switch_session(DEFAULT_SECTION); + std::map *current_session = switch_session(DEFAULT_SECTION); char line[MAX_CFG_LINE_LEN]; @@ -148,8 +147,7 @@ int Ini::load(const std::string &file_name) { continue; } - if (read_buf[0] == CFG_SESSION_START_TAG && - read_buf[strlen(read_buf) - 1] == CFG_SESSION_END_TAG) { + if (read_buf[0] == CFG_SESSION_START_TAG && read_buf[strlen(read_buf) - 1] == CFG_SESSION_END_TAG) { read_buf[strlen(read_buf) - 1] = '\0'; std::string session_name = std::string(read_buf + 1); @@ -171,7 +169,7 @@ int Ini::load(const std::string &file_name) { continue_last_line = true; // remove the last character - line_entry = line_entry.substr(0, line_entry.size() -1); + line_entry = line_entry.substr(0, line_entry.size() - 1); continue; } else { continue_last_line = false; @@ -186,21 +184,20 @@ int Ini::load(const std::string &file_name) { if (ifs.is_open()) { ifs.close(); } - std::cerr << "Failed to load " << file_name << SYS_OUTPUT_ERROR - << std::endl; + std::cerr << "Failed to load " << file_name << SYS_OUTPUT_ERROR << std::endl; return -1; } return 0; } -void Ini::to_string(std::string &output_str) { +void Ini::to_string(std::string &output_str) +{ output_str.clear(); output_str += "Begin dump configuration\n"; - for (SessionsMap::iterator it = sections_.begin(); it != sections_.end(); - it++) { + for (SessionsMap::iterator it = sections_.begin(); it != sections_.end(); it++) { output_str += CFG_SESSION_START_TAG; output_str += it->first; output_str += CFG_SESSION_END_TAG; @@ -208,9 +205,8 @@ void Ini::to_string(std::string &output_str) { std::map §ion_map = it->second; - for (std::map::iterator sub_it = - section_map.begin(); - sub_it != section_map.end(); sub_it++) { + for (std::map::iterator sub_it = section_map.begin(); sub_it != section_map.end(); + sub_it++) { output_str += sub_it->first; output_str += "="; output_str += sub_it->second; @@ -225,9 +221,10 @@ void Ini::to_string(std::string &output_str) { } //! Accessor function which wraps global properties object -Ini *&get_properties() { +Ini *&get_properties() +{ static Ini *properties = new Ini(); return properties; } -} //namespace common +} // namespace common