Debian Codegraph @

src/ConfigReader.cpp

raw
1 // Implements the ConfigReader class
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8
9 #ifdef HAVE_CONFIG_H
10 #include "e2config.h"
11 #endif
12 #include "ConfigReader.hpp"
13
14 #include <fstream>
15 #include <map>
16
17 #include "Logger.hpp"
18 #include "String.hpp"
19
20 // IMPLEMENTATION
21 const char* char_equals = "="; // delimites option and value e.g option = value
22 const char* delimiter = ":"; // delimits multiple values e.g. value1 : value2 : value3
23
24 class ConfigReaderImpl
25 {
26 public:
27 void addOption(String line);
28 std::deque<String> * getOption(std::string optionName);
29
30 private:
31 std::map<std::string, std::deque<String> > options;
32 std::deque<String> empty;
33
34 };
35
36
37
38 void ConfigReaderImpl::addOption(String line)
39 {
40 String name = line.before(char_equals);
41 String value = line.after(char_equals);
42
43 while (name.endsWith(" ")) { // get rid of tailing spaces before =
44 name.chop();
45 }
46 if ( name.empty() ) return;
47
48 while (value.startsWith(" ")) { // get rid of heading spaces
49 value.lop();
50 }
51 if (value.startsWith("'")) { // inverted commas
52 value.lop();
53 }
54 while (value.endsWith(" ")) { // get rid of tailing spaces
55 value.chop();
56 }
57 if (value.endsWith("'")) { // inverted commas
58 value.chop();
59 }
60
61 std::deque<String> entry = options[name.c_str()];
62 entry.push_back(value.c_str());
63 options[name.c_str()] = entry;
64
65 }
66
67 std::deque<String> * ConfigReaderImpl::getOption(std::string optionName)
68 {
69 if (options.count(optionName) > 0)
70 {
71 return & options[optionName];
72 }
73 else
74 {
75 return & empty;
76 }
77
78 }
79
80 // PUBLIC
81
82 // constructor
83 ConfigReader::ConfigReader() : pImpl( new ConfigReaderImpl )
84 {
85 }
86 ConfigReader::~ConfigReader() {
87 delete pImpl;
88 }
89
90 // construct & read in the given config file
91 ConfigReader::ConfigReader(const Path &filename, const Path &list_pwd)
92 {
93 readConfig(filename, list_pwd );
94 }
95
96 // method readConfig
97 bool ConfigReader::readConfig(const Path &filename, const Path &list_pwd) {
98
99 Path base_dir = filename.baseDir();
100 std::string now_pwd = list_pwd.fullPath();
101 std::string linebuffer;
102
103 std::ifstream conffile(filename.fullPath(), std::ios::in); // e2guardian.conf
104 if (!conffile.good()) {
105 E2LOGGER_error("Error reading ", filename.fullPath());
106 return false;
107 }
108
109 while (!conffile.eof()) {
110 getline(conffile, linebuffer);
111 if (!conffile.fail() && linebuffer.length() != 0) {
112 String temp = (char *) linebuffer.c_str();
113 if (linebuffer[0] == '#') {
114 continue; // skip comment line
115 }
116 else if (linebuffer[0] == '.') {
117 String filename = temp.after(".Include<").before(">");
118 if (filename.length() > 0) {
119 if (!readConfig( base_dir.combine(filename), now_pwd)) {
120 conffile.close();
121 return false;
122 }
123 }
124 temp = temp.after(".Define LISTDIR <").before(">");
125 if (temp.length() > 0) {
126 now_pwd = temp;
127 if(now_pwd.back() != '/')
128 now_pwd += "/";
129 }
130 }
131 else
132 {
133 if (temp.contains("#")) {
134 temp = temp.before("#"); // remove trailing comment
135 }
136 temp.removeWhiteSpace(); // get rid of spaces at end of line
137 if (temp.contains("__LISTDIR__")) {
138 temp.replaceall("__LISTDIR__", now_pwd.c_str());
139 }
140 // append ,listdir=now_pwd if line contains a file path - so that now_pwd can be passed
141 // to list file handler so that it can honour __LISTDIR__ in Included listfiles
142 if (temp.contains("path=") && !temp.contains("listdir=")) {
143 temp += ",listdir=";
144 temp += now_pwd;
145 }
146
147 }
148
149 DEBUG_config("read:", temp);
150 pImpl->addOption(temp); // store option
151 }
152 }
153 conffile.close();
154 return true;
155 }
156
157 // findoptionM returns all instances of an option
158 std::deque<String>* ConfigReader::findoptionM(const char *option)
159 {
160 return pImpl->getOption(option);
161 }
162
163 // findoptionMD returns all instances of an option & allows multiple entries on a line separated by delim
164 std::deque<String> ConfigReader::findoptionMD(const char *option, const char *delimiter) {
165 std::deque<String>* values = findoptionM(option);
166 std::deque<String> results;
167 String temp;
168
169 for (std::deque<String>::iterator i = values->begin(); i != values->end(); i++)
170 {
171 temp = (*i).c_str();
172 if (delimiter != nullptr) {
173 while (temp.contains(delimiter)) {
174 String t = temp.before(delimiter);
175 results.push_back(t);
176 temp = temp.after(delimiter);
177 }
178 }
179 results.push_back(temp);
180 }
181 return results;
182 }
183
184
185 std::string ConfigReader::findoptionS(const char *option)
186 {
187 std::deque<String>* values = findoptionM(option);
188 if ( values && values->size() > 0 )
189 return values->back();
190 else
191 return "";
192 }
193
194 bool ConfigReader::findoptionB(const char *option)
195 {
196 std::string ok = findoptionS(option);
197 if (ok=="on" || ok == "yes") return true;
198 return false;
199 }
200
201 long int ConfigReader::findoptionI(const char *option)
202 {
203 std::string number = findoptionS(option);
204 if ( number != "")
205 return std::stol(number);
206 else
207 return 0;
208 }
209
210 // findoptionIWithDefault gets an option value, checks for minl and maxl bounds and defaults to defaultl if no value was found
211 long int ConfigReader::findoptionIWithDefault(const char * option, long int minl, long int maxl, long int defaultl)
212 {
213 std::string s = findoptionS(option);
214 if ( s == "" ) return defaultl;
215 long int value = std::stol(s);
216
217 if ((value < minl) || ((maxl > 0) && (value > maxl))) {
218 E2LOGGER_error("Config problem; check allowed values for ", option, "( ", value , " should be >= ", minl, " <=", maxl, ")",
219 "we are using default value:", defaultl);
220 return defaultl;
221 }
222 return value;
223 }