version 4.0.7
[ric-plt/e2.git] / RIC-E2-TERMINATION / ReadConfigFile.h
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
51         while (std::getline(file,line)) {
52             if (!line.length() || line[0] == '#' || line[0] == ';' || line[0] == '{') {
53                 continue;
54             }
55 //            else if (line[0] == '#') {
56 //                continue;
57 //            } else if (line[0] == ';') {
58 //                continue;
59 //            }
60
61
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());
66                     section.clear();
67                     return -1;
68 //                    continue;
69                 }
70                 section = line.substr(1, sectionEnd - 1) + ".";
71                 continue;
72             }
73             if (mdclog_level_get() >= MDCLOG_INFO) {
74                 mdclog_write(MDCLOG_INFO, "line = %s ", line.c_str());
75             }
76
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());
80                 continue;
81             }
82 //            auto name = line.substr(0,leftHand);
83 //            trim(name);
84             auto name = section + trim(line.substr(0, leftHand));
85
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());
89                 continue;
90
91             }
92             trim(value);
93             if (mdclog_level_get() >= MDCLOG_INFO) {
94                 mdclog_write(MDCLOG_INFO, "entry = %s value = %s", name.c_str(), value.c_str());
95             }
96             //cout << "entry = " << name << " value = " << value  << endl;
97             entries[name] = value;
98         }
99         return 0;
100     }
101
102     /**
103      * @param key the key we are looking
104      * @return string value of the entry and "" if not exists
105      */
106     string getStringValue(std::string const& key) const {
107         auto entry = entries.find(key);
108         if (entry == entries.end()) {
109             return "";
110         }
111         return entry->second;
112     }
113
114     /**
115      * @param key the key we are looking
116      * @return int value of the entry and -1 if not exists
117      */
118     int getIntValue(std::string const& key) const {
119         auto entry = entries.find(key);
120         if (entry == entries.end()) {
121              return -1;
122         }
123         char *dummy;
124         int ret = (int)std::strtol(entry->second.c_str(), &dummy, 10);
125         //cout << "entry = " << key << " value = " << entry->second  << " int value = " << ret << endl;
126         return ret;
127     }
128
129     /**
130     * @param key the key we are looking
131     * @return double value of the entry and -1.0 if not exists
132     */
133     double getDoubleValue(std::string const& key) const {
134         auto entry = entries.find(key);
135         if (entry == entries.end()) {
136             return -1.0;
137         }
138         char *dummy;
139         return std::strtod(entry->second.c_str(), &dummy);
140     }
141
142 private:
143     std::unordered_map<string, string> entries;
144
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));
147         return str;
148     }
149
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);
152         return str;
153     }
154
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);
157     }
158 };
159
160
161 #endif //E2_READCONFIGFILE_H