ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 AT&T Intellectual Property |
| 3 | * Copyright 2019 Nokia |
| 4 | * |
| 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 |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | // |
| 19 | // Created by adi ENZEL on 11/19/19. |
| 20 | // |
| 21 | |
| 22 | #ifndef E2_READCONFIGFILE_H |
| 23 | #define E2_READCONFIGFILE_H |
| 24 | |
| 25 | #include <string> |
| 26 | #include <unordered_map> |
| 27 | #include <iostream> |
| 28 | #include <fstream> |
| 29 | #include <sstream> |
| 30 | #include <cstdlib> |
| 31 | |
| 32 | #include <boost/algorithm/string.hpp> |
| 33 | #include <mdclog/mdclog.h> |
| 34 | |
| 35 | using namespace std; |
| 36 | |
| 37 | class ReadConfigFile { |
| 38 | public: |
| 39 | |
| 40 | explicit ReadConfigFile() = default; |
| 41 | |
| 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()); |
| 46 | return -1; |
| 47 | } |
| 48 | std::string line; |
| 49 | std::string section; |
| 50 | |
aa7133@att.com | cfb02d4 | 2020-04-02 14:31:15 +0300 | [diff] [blame] | 51 | while (std::getline(file,line, '\n')) { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 52 | if (!line.length() || line[0] == '#' || line[0] == ';' || line[0] == '{') { |
aa7133@att.com | cfb02d4 | 2020-04-02 14:31:15 +0300 | [diff] [blame] | 53 | line.clear(); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 54 | continue; |
| 55 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 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()); |
| 60 | section.clear(); |
| 61 | return -1; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 62 | } |
| 63 | section = line.substr(1, sectionEnd - 1) + "."; |
aa7133@att.com | cfb02d4 | 2020-04-02 14:31:15 +0300 | [diff] [blame] | 64 | section = trim(section); |
| 65 | line.clear(); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 66 | continue; |
| 67 | } |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 68 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 69 | mdclog_write(MDCLOG_INFO, "line = %s ", line.c_str()); |
| 70 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 71 | |
| 72 | auto leftHand = line.find('='); |
| 73 | if (leftHand == std::string::npos) { |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 74 | mdclog_write(MDCLOG_ERR, "problematic entry: %s no equal sign", line.c_str()); |
aa7133@att.com | cfb02d4 | 2020-04-02 14:31:15 +0300 | [diff] [blame] | 75 | line.clear(); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 76 | continue; |
| 77 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 78 | auto name = section + trim(line.substr(0, leftHand)); |
| 79 | |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 80 | auto value = line.substr(leftHand + 1); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 81 | if (value.length() == 0) { |
| 82 | mdclog_write(MDCLOG_ERR, "problematic entry: %s no value ", line.c_str()); |
aa7133@att.com | cfb02d4 | 2020-04-02 14:31:15 +0300 | [diff] [blame] | 83 | line.clear(); |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 84 | continue; |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 85 | } |
aa7133@att.com | cfb02d4 | 2020-04-02 14:31:15 +0300 | [diff] [blame] | 86 | line.clear(); |
| 87 | |
| 88 | value = trim(value); |
aa7133@att.com | 84bd334 | 2020-03-16 18:04:57 +0200 | [diff] [blame] | 89 | if (mdclog_level_get() >= MDCLOG_INFO) { |
| 90 | mdclog_write(MDCLOG_INFO, "entry = %s value = %s", name.c_str(), value.c_str()); |
| 91 | } |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 92 | //cout << "entry = " << name << " value = " << value << endl; |
| 93 | entries[name] = value; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param key the key we are looking |
| 100 | * @return string value of the entry and "" if not exists |
| 101 | */ |
| 102 | string getStringValue(std::string const& key) const { |
| 103 | auto entry = entries.find(key); |
| 104 | if (entry == entries.end()) { |
| 105 | return ""; |
| 106 | } |
| 107 | return entry->second; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param key the key we are looking |
| 112 | * @return int value of the entry and -1 if not exists |
| 113 | */ |
| 114 | int getIntValue(std::string const& key) const { |
| 115 | auto entry = entries.find(key); |
| 116 | if (entry == entries.end()) { |
| 117 | return -1; |
| 118 | } |
| 119 | char *dummy; |
| 120 | int ret = (int)std::strtol(entry->second.c_str(), &dummy, 10); |
| 121 | //cout << "entry = " << key << " value = " << entry->second << " int value = " << ret << endl; |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param key the key we are looking |
| 127 | * @return double value of the entry and -1.0 if not exists |
| 128 | */ |
| 129 | double getDoubleValue(std::string const& key) const { |
| 130 | auto entry = entries.find(key); |
| 131 | if (entry == entries.end()) { |
| 132 | return -1.0; |
| 133 | } |
| 134 | char *dummy; |
| 135 | return std::strtod(entry->second.c_str(), &dummy); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | std::unordered_map<string, string> entries; |
| 140 | |
| 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)); |
| 143 | return str; |
| 144 | } |
| 145 | |
| 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); |
| 148 | return str; |
| 149 | } |
| 150 | |
dhirajverma | b55bc17 | 2021-11-09 02:15:08 -0500 | [diff] [blame] | 151 | inline static std::string& trim(std::string str, const std::string& chars = "\t\n\v\f\r ") { |
ss412g | 3bac2da | 2020-01-05 11:52:19 +0200 | [diff] [blame] | 152 | return ltrim(rtrim(str, chars), chars); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | #endif //E2_READCONFIGFILE_H |