Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TUBEMQ-263] Create C/C++ ini file read utils #184

Merged
merged 1 commit into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/inc/const_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
48 changes: 48 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/inc/file_ini.h
Original file line number Diff line number Diff line change
@@ -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

136 changes: 136 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/src/file_ini.cc
Original file line number Diff line number Diff line change
@@ -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;
}


}