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, '\n')) {
52 if (!line.length() || line[0] == '#' || line[0] == ';' || line[0] == '{') {
56 if (line[0] =='[') { //section
57 auto sectionEnd = line.find(']');
58 if (sectionEnd == std::string::npos) {
59 mdclog_write(MDCLOG_ERR, "Error section definition: %s ", line.c_str());
63 section = line.substr(1, sectionEnd - 1) + ".";
64 section = trim(section);
68 if (mdclog_level_get() >= MDCLOG_INFO) {
69 mdclog_write(MDCLOG_INFO, "line = %s ", line.c_str());
72 auto leftHand = line.find('=');
73 if (leftHand == std::string::npos) {
74 mdclog_write(MDCLOG_ERR, "problematic entry: %s no equal sign", line.c_str());
78 auto name = section + trim(line.substr(0, leftHand));
80 auto value = line.substr(leftHand + 1);
81 if (value.length() == 0) {
82 mdclog_write(MDCLOG_ERR, "problematic entry: %s no value ", line.c_str());
89 if (mdclog_level_get() >= MDCLOG_INFO) {
90 mdclog_write(MDCLOG_INFO, "entry = %s value = %s", name.c_str(), value.c_str());
92 //cout << "entry = " << name << " value = " << value << endl;
93 entries[name] = value;
99 * @param key the key we are looking
100 * @return string value of the entry and "" if not exists
102 string getStringValue(std::string const& key) const {
103 auto entry = entries.find(key);
104 if (entry == entries.end()) {
107 return entry->second;
111 * @param key the key we are looking
112 * @return int value of the entry and -1 if not exists
114 int getIntValue(std::string const& key) const {
115 auto entry = entries.find(key);
116 if (entry == entries.end()) {
120 int ret = (int)std::strtol(entry->second.c_str(), &dummy, 10);
121 //cout << "entry = " << key << " value = " << entry->second << " int value = " << ret << endl;
126 * @param key the key we are looking
127 * @return double value of the entry and -1.0 if not exists
129 double getDoubleValue(std::string const& key) const {
130 auto entry = entries.find(key);
131 if (entry == entries.end()) {
135 return std::strtod(entry->second.c_str(), &dummy);
139 std::unordered_map<string, string> entries;
141 inline static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") {
142 str.erase(0, str.find_first_not_of(chars));
146 inline static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ") {
147 str.erase(str.find_last_not_of(chars) + 1);
151 inline static std::string& trim(std::string str, const std::string& chars = "\t\n\v\f\r ") {
152 return ltrim(rtrim(str, chars), chars);
157 #endif //E2_READCONFIGFILE_H