2 * Copyright 2019 AT&T Intellectual Property
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 // Created by adi ENZEL on 11/19/19.
22 #ifndef E2_READCONFIGFILE_H
23 #define E2_READCONFIGFILE_H
26 #include <unordered_map>
32 #include <boost/algorithm/string.hpp>
33 #include <mdclog/mdclog.h>
37 class ReadConfigFile {
40 explicit ReadConfigFile() = default;
42 int openConfigFile(std::string const& configFile) {
43 std::ifstream file(configFile.c_str());
44 if (!file) { // file not found
45 mdclog_write(MDCLOG_ERR, "File: %s, failed to open", configFile.c_str());
51 while (std::getline(file,line)) {
52 if (!line.length() || line[0] == '#' || line[0] == ';' || line[0] == '{') {
55 // else if (line[0] == '#') {
57 // } else if (line[0] == ';') {
62 if (line[0] =='[') { //section
63 auto sectionEnd = line.find(']');
64 if (sectionEnd == std::string::npos) {
65 mdclog_write(MDCLOG_ERR, "Error section definition: %s ", line.c_str());
70 section = line.substr(1, sectionEnd - 1) + ".";
73 if (mdclog_level_get() >= MDCLOG_INFO) {
74 mdclog_write(MDCLOG_INFO, "line = %s ", line.c_str());
77 auto leftHand = line.find('=');
78 if (leftHand == std::string::npos) {
79 mdclog_write(MDCLOG_ERR, "problematic entry: %s no equal sign", line.c_str());
82 // auto name = line.substr(0,leftHand);
84 auto name = section + trim(line.substr(0, leftHand));
86 auto value = line.substr(leftHand + 1);
87 if (value.length() == 0) {
88 mdclog_write(MDCLOG_ERR, "problematic entry: %s no value ", line.c_str());
93 if (mdclog_level_get() >= MDCLOG_INFO) {
94 mdclog_write(MDCLOG_INFO, "entry = %s value = %s", name.c_str(), value.c_str());
96 //cout << "entry = " << name << " value = " << value << endl;
97 entries[name] = value;
103 * @param key the key we are looking
104 * @return string value of the entry and "" if not exists
106 string getStringValue(std::string const& key) const {
107 auto entry = entries.find(key);
108 if (entry == entries.end()) {
111 return entry->second;
115 * @param key the key we are looking
116 * @return int value of the entry and -1 if not exists
118 int getIntValue(std::string const& key) const {
119 auto entry = entries.find(key);
120 if (entry == entries.end()) {
124 int ret = (int)std::strtol(entry->second.c_str(), &dummy, 10);
125 //cout << "entry = " << key << " value = " << entry->second << " int value = " << ret << endl;
130 * @param key the key we are looking
131 * @return double value of the entry and -1.0 if not exists
133 double getDoubleValue(std::string const& key) const {
134 auto entry = entries.find(key);
135 if (entry == entries.end()) {
139 return std::strtod(entry->second.c_str(), &dummy);
143 std::unordered_map<string, string> entries;
145 inline static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") {
146 str.erase(0, str.find_first_not_of(chars));
150 inline static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") {
151 str.erase(str.find_last_not_of(chars) + 1);
155 inline static std::string& trim(basic_string<char> str, const std::string& chars = "\t\n\v\f\r ") {
156 return ltrim(rtrim(str, chars), chars);
161 #endif //E2_READCONFIGFILE_H