blob: 87e511d00e7f4f6c2b96f6825ee99bbab3894f57 [file] [log] [blame]
ss412g3bac2da2020-01-05 11:52:19 +02001/*
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
35using namespace std;
36
37class ReadConfigFile {
38public:
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.comcfb02d42020-04-02 14:31:15 +030051 while (std::getline(file,line, '\n')) {
ss412g3bac2da2020-01-05 11:52:19 +020052 if (!line.length() || line[0] == '#' || line[0] == ';' || line[0] == '{') {
aa7133@att.comcfb02d42020-04-02 14:31:15 +030053 line.clear();
ss412g3bac2da2020-01-05 11:52:19 +020054 continue;
55 }
ss412g3bac2da2020-01-05 11:52:19 +020056 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;
ss412g3bac2da2020-01-05 11:52:19 +020062 }
63 section = line.substr(1, sectionEnd - 1) + ".";
aa7133@att.comcfb02d42020-04-02 14:31:15 +030064 section = trim(section);
65 line.clear();
ss412g3bac2da2020-01-05 11:52:19 +020066 continue;
67 }
aa7133@att.com84bd3342020-03-16 18:04:57 +020068 if (mdclog_level_get() >= MDCLOG_INFO) {
69 mdclog_write(MDCLOG_INFO, "line = %s ", line.c_str());
70 }
ss412g3bac2da2020-01-05 11:52:19 +020071
72 auto leftHand = line.find('=');
73 if (leftHand == std::string::npos) {
aa7133@att.com84bd3342020-03-16 18:04:57 +020074 mdclog_write(MDCLOG_ERR, "problematic entry: %s no equal sign", line.c_str());
aa7133@att.comcfb02d42020-04-02 14:31:15 +030075 line.clear();
ss412g3bac2da2020-01-05 11:52:19 +020076 continue;
77 }
ss412g3bac2da2020-01-05 11:52:19 +020078 auto name = section + trim(line.substr(0, leftHand));
79
aa7133@att.com84bd3342020-03-16 18:04:57 +020080 auto value = line.substr(leftHand + 1);
ss412g3bac2da2020-01-05 11:52:19 +020081 if (value.length() == 0) {
82 mdclog_write(MDCLOG_ERR, "problematic entry: %s no value ", line.c_str());
aa7133@att.comcfb02d42020-04-02 14:31:15 +030083 line.clear();
ss412g3bac2da2020-01-05 11:52:19 +020084 continue;
ss412g3bac2da2020-01-05 11:52:19 +020085 }
aa7133@att.comcfb02d42020-04-02 14:31:15 +030086 line.clear();
87
88 value = trim(value);
aa7133@att.com84bd3342020-03-16 18:04:57 +020089 if (mdclog_level_get() >= MDCLOG_INFO) {
90 mdclog_write(MDCLOG_INFO, "entry = %s value = %s", name.c_str(), value.c_str());
91 }
ss412g3bac2da2020-01-05 11:52:19 +020092 //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
138private:
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
dhirajvermab55bc172021-11-09 02:15:08 -0500151 inline static std::string& trim(std::string str, const std::string& chars = "\t\n\v\f\r ") {
ss412g3bac2da2020-01-05 11:52:19 +0200152 return ltrim(rtrim(str, chars), chars);
153 }
154};
155
156
157#endif //E2_READCONFIGFILE_H