This is an automated email from the ASF dual-hosted git repository. gosonzhang pushed a commit to branch tubemq-client-cpp in repository https://gitbox.apache.org/repos/asf/incubator-tubemq.git
The following commit(s) were added to refs/heads/tubemq-client-cpp by this push: new be0a3c1 [TUBEMQ-263] Create C/C++ ini file read utils (#184) be0a3c1 is described below commit be0a3c101ff188882033d7e3bd124e775852bb15 Author: gosonzhang <4675...@qq.com> AuthorDate: Sat Jul 4 16:16:52 2020 +0000 [TUBEMQ-263] Create C/C++ ini file read utils (#184) Co-authored-by: gosonzhang <gosonzh...@tencent.com> --- .../tubemq-client-cpp/inc/const_config.h | 8 ++ .../tubemq-client-cpp/inc/file_ini.h | 48 ++++++++ .../tubemq-client-cpp/src/file_ini.cc | 136 +++++++++++++++++++++ 3 files changed, 192 insertions(+) diff --git a/tubemq-client-twins/tubemq-client-cpp/inc/const_config.h b/tubemq-client-twins/tubemq-client-cpp/inc/const_config.h index a48873d..1cc2b60 100644 --- a/tubemq-client-twins/tubemq-client-cpp/inc/const_config.h +++ b/tubemq-client-twins/tubemq-client-cpp/inc/const_config.h @@ -82,6 +82,14 @@ namespace delimiter { static const string kDelimiterColon = ":"; static const string kDelimiterAt = "@"; static const string kDelimiterPound = "#"; + static const string kDelimiterSemicolon = ";"; + //Double slash + static const string kDelimiterDbSlash = "//"; + // left square bracket + static const string kDelimiterLftSB = "["; + // right square bracket + static const string kDelimiterRgtSB = "]"; + } // namespace delimiter diff --git a/tubemq-client-twins/tubemq-client-cpp/inc/file_ini.h b/tubemq-client-twins/tubemq-client-cpp/inc/file_ini.h new file mode 100644 index 0000000..ecc7f1f --- /dev/null +++ b/tubemq-client-twins/tubemq-client-cpp/inc/file_ini.h @@ -0,0 +1,48 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _TUBEMQ_CLIENT_FILE_INI_H_ +#define _TUBEMQ_CLIENT_FILE_INI_H_ + +#include <map> +#include <string> + +namespace tubemq { + +using namespace std; + +class Fileini { + public: + Fileini(); + ~Fileini(); + bool Loadini(string& err_info, const string& file_name); + bool GetValue(string& err_info, const string& sector, + const string& key, string& value, const string& def); + + private: + bool init_flag_; + // sector key value + map<string, map<string, string> > ini_map_; +}; + + +} + +#endif + diff --git a/tubemq-client-twins/tubemq-client-cpp/src/file_ini.cc b/tubemq-client-twins/tubemq-client-cpp/src/file_ini.cc new file mode 100644 index 0000000..84a847f --- /dev/null +++ b/tubemq-client-twins/tubemq-client-cpp/src/file_ini.cc @@ -0,0 +1,136 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <fstream> +#include <sstream> +#include "utils.h" +#include "file_ini.h" +#include "const_config.h" + +namespace tubemq { + + +Fileini::Fileini() { + this->init_flag_ = false; + this->ini_map_.clear(); +} + +Fileini::~Fileini() { + this->init_flag_ = false; + this->ini_map_.clear(); +} + +bool Fileini::Loadini(string& err_info, const string& file_name) { + // check paramter + if (file_name.empty()) { + err_info = "Ini configure file is null!"; + return false; + } + // open configure file and check + ifstream conf_file(file_name.c_str()); + if (!conf_file.is_open()) { + err_info = "Open file " + file_name + " failure!"; + return false; + } + string line_str = ""; + string sector = ""; + string key = ""; + string value = ""; + string::size_type lftsb_pos = 0; + string::size_type rgtsb_pos = 0; + string::size_type equal_pos = 0; + // read ini file and parse content + while (getline(conf_file, line_str)) { + // check if a comment + line_str = Utils::Trim(line_str); + if (line_str.empty() + || line_str.find(delimiter::kDelimiterDbSlash) == 0 + || line_str.find(delimiter::kDelimiterSemicolon) == 0) { + continue; + } + // check if a sector head + lftsb_pos = line_str.find(delimiter::kDelimiterLftSB); + rgtsb_pos = line_str.find(delimiter::kDelimiterRgtSB); + if (lftsb_pos != string::npos && rgtsb_pos != string::npos) { + sector = line_str.substr(lftsb_pos + (delimiter::kDelimiterLftSB).size(), + rgtsb_pos - (delimiter::kDelimiterRgtSB).size()); + sector = Utils::Trim(sector); + continue; + } + // check if a key=value string + equal_pos = line_str.find(delimiter::kDelimiterEqual); + if (equal_pos == string::npos) { + continue; + } + key = line_str.substr(0, equal_pos); + value = line_str.substr(equal_pos + (delimiter::kDelimiterEqual).size(), line_str.size() - 1); + key = Utils::Trim(key); + value = Utils::Trim(value); + // get data from file to memory + if (sector.empty() && key.empty() && value.empty()) { + continue; + } + map<string, map<string, string> >::iterator it_sec; + it_sec = this->ini_map_.find(sector); + if (it_sec == this->ini_map_.end()) { + map<string, string> tmp_key_val_map; + tmp_key_val_map[key] = value; + ini_map_[sector] = tmp_key_val_map; + } else { + it_sec->second[key] = value; + } + } + // close configure file and clear status + conf_file.close(); + conf_file.clear(); + // set parser status + this->init_flag_ = true; + // end + err_info = "Ok"; + return true; +} + +bool Fileini::GetValue(string& err_info, const string& sector, + const string& key, string& value, const string& def) { + if (!this->init_flag_) { + err_info = "Please load configure file first!"; + return false; + } + err_info = "Ok"; + // search key's value in sector + map<string, map<string, string> >::iterator it_sec; + map<string, string>::iterator it_keyval; + it_sec = this->ini_map_.find(sector); + if (it_sec == this->ini_map_.end()) { + value = def; + return true; + } + it_keyval = it_sec->second.find(key); + if (it_keyval == it_sec->second.end()) { + value = def; + return true; + } + value = it_keyval->second; + return true; +} + + +} + +