NAGASH 0.9.8
Next Generation Analysis System
Loading...
Searching...
No Matches
ConfigTool.cxx
Go to the documentation of this file.
1//***************************************************************************************
4//***************************************************************************************
5
6#include "NAGASH/ConfigTool.h"
7
8using namespace NAGASH;
9using namespace std;
10
11// new version
12// use std::regex to match token
13// also enable using # as a start of comment
14
47ConfigTool::ConfigTool(const char *inputfilename)
48{
49 msg = std::make_shared<MSGTool>();
50
51 msg->StartTitle("ConfigTool::ReadConfigFromFile");
52 ReadConfigFromFile(inputfilename);
53 msg->EndTitle();
54
55 // Config MSGTool
56 bool doexistmsg = false;
57 bool doexistfocus = false;
58 bool doexistlog = false;
59 msg->OutputLevel = MSGLevel::SILENT;
60 auto msglevel = GetPar<TString>("MSGLevel", &doexistmsg);
61 auto FocusName = GetPar<TString>("FocusRegionName", &doexistfocus);
62 auto LogName = GetPar<TString>("LogFileName", &doexistlog);
63 bool docreatelog = GetPar<bool>("CreateLog");
64 bool doopenlog = GetPar<bool>("OpenLog");
65 msg->OutputLevel = MSGLevel::INFO;
67 if (doexistmsg)
68 {
69 if (msglevel == "SILENT")
70 level = MSGLevel::SILENT;
71 if (msglevel == "FATAL")
72 level = MSGLevel::FATAL;
73 if (msglevel == "ERROR")
74 level = MSGLevel::ERROR;
75 if (msglevel == "WARNING")
76 level = MSGLevel::WARNING;
77 if (msglevel == "INFO")
78 level = MSGLevel::INFO;
79 if (msglevel == "DEBUG")
80 level = MSGLevel::DEBUG;
81 if (msglevel == "FOCUS")
82 level = MSGLevel::FOCUS;
83
84 if (doexistfocus)
85 msg.reset(new MSGTool(level, FocusName));
86 else
87 msg.reset(new MSGTool(level));
88 }
89
90 if (doexistlog)
91 {
92 if (docreatelog)
93 msg->CreateLog(LogName);
94 if (doopenlog)
95 msg->OpenLog(LogName);
96 }
97 else if (docreatelog || doopenlog)
98 {
99 MSGUser()->StartTitle("ConfigTool::SetMSG");
100 MSGUser()->MSG(MSGLevel::WARNING, "LogFileName not set, will print message to console");
101 MSGUser()->EndTitle();
102 }
103}
104
107void ConfigTool::ReadConfigFromFile(const TString &filename)
108{
109 std::ifstream infile;
110 infile.open(filename, std::ios::in);
111 if (!infile.is_open())
112 {
113 msg->MSG_ERROR("config file ", filename, " does not exist");
114 return;
115 }
116
117 // md5 check sum, in order to prevent same file from being included twice
118 {
119 auto md5sum = Exec(TString("md5sum " + filename).Data());
120 std::regex md5_regex("([0-9a-f]{32})");
121 auto match = std::sregex_iterator(md5sum.begin(), md5sum.end(), md5_regex);
122
123 if (std::find(InputMD5Sum.begin(), InputMD5Sum.end(), match->str()) != InputMD5Sum.end())
124 {
125 msg->MSG_ERROR("input file ", filename, " has already been read");
126 return;
127 }
128 else
129 InputMD5Sum.emplace_back(match->str());
130 }
131
132 std::string line;
133 while (std::getline(infile, line))
134 {
135 // remove comment
136 auto commentstart = line.find("#");
137 if (commentstart != std::string::npos)
138 {
139 line.erase(commentstart, line.size());
140 }
141
142 // use regex to match token
143 // std::regex word_regex("([\\+-/A-Za-z0-9_\\.]+)");
144 std::regex word_regex("([^\\s]+)");
145 auto words_begin = std::sregex_iterator(line.begin(), line.end(), word_regex);
146 auto words_end = std::sregex_iterator();
147 std::vector<std::string> matched_words;
148 for (auto i = words_begin; i != words_end; ++i)
149 {
150 matched_words.push_back((*i).str());
151 }
152
153 if (matched_words.size() >= 2)
154 {
155 if (matched_words[0] == "include")
156 {
157 for (uint32_t i = 1; i < matched_words.size(); i++)
158 ReadConfigFromFile(matched_words[i]);
159
160 continue;
161 }
162 }
163
164 if (matched_words.size() >= 3)
165 {
166 std::string type = matched_words[0];
167 std::string name = matched_words[1];
168
169 if (type == "int")
170 {
171 std::istringstream record(matched_words[2]);
172 int temp_int;
173 record >> temp_int;
174 BookParFile<int>(name, temp_int);
175 }
176 else if (type == "intvec")
177 {
178 std::vector<int> temp_intvec;
179 for (uint32_t i = 2; i < matched_words.size(); i++)
180 {
181 std::istringstream record(matched_words[i]);
182 int temp_int;
183 record >> temp_int;
184 temp_intvec.push_back(temp_int);
185 }
186 BookParFile<std::vector<int>>(name, temp_intvec);
187 }
188 else if (type == "double")
189 {
190 std::istringstream record(matched_words[2]);
191 double temp_double;
192 record >> temp_double;
193 BookParFile<double>(name, temp_double);
194 }
195 else if (type == "doublevec")
196 {
197 std::vector<double> temp_doublevec;
198 for (uint64_t i = 2; i < matched_words.size(); i++)
199 {
200 std::istringstream record(matched_words[i]);
201 double temp_double;
202 record >> temp_double;
203 temp_doublevec.push_back(temp_double);
204 }
205 BookParFile<std::vector<double>>(name, temp_doublevec);
206 }
207 else if (type == "long")
208 {
209 std::istringstream record(matched_words[2]);
210 long temp_long;
211 record >> temp_long;
212 BookParFile<long>(name, temp_long);
213 }
214 else if (type == "longvec")
215 {
216 std::vector<long> temp_longvec;
217 for (uint64_t i = 2; i < matched_words.size(); i++)
218 {
219 std::istringstream record(matched_words[i]);
220 long temp_long;
221 record >> temp_long;
222 temp_longvec.push_back(temp_long);
223 }
224 BookParFile<std::vector<long>>(name, temp_longvec);
225 }
226 else if (type == "bool")
227 {
228 std::istringstream record(matched_words[2]);
229 bool temp_bool;
230 record >> temp_bool;
231 BookParFile<bool>(name, temp_bool);
232 }
233 else if (type == "boolvec")
234 {
235 std::vector<bool> temp_boolvec;
236 for (uint64_t i = 2; i < matched_words.size(); i++)
237 {
238 std::istringstream record(matched_words[i]);
239 bool temp_bool;
240 record >> temp_bool;
241 temp_boolvec.push_back(temp_bool);
242 }
243 BookParFile<std::vector<bool>>(name, temp_boolvec);
244 }
245 else if (type == "string")
246 {
247 std::istringstream record(matched_words[2]);
248 TString temp_string;
249 record >> temp_string;
250 BookParFile<TString>(name, temp_string);
251 }
252 else if (type == "stringvec")
253 {
254 std::vector<TString> temp_stringvec;
255 for (uint64_t i = 2; i < matched_words.size(); i++)
256 {
257 std::istringstream record(matched_words[i]);
258 TString temp_string;
259 record >> temp_string;
260 temp_stringvec.push_back(temp_string);
261 }
262 BookParFile<std::vector<TString>>(name, temp_stringvec);
263 }
264 else
265 {
266 MSGUser()->MSG_WARNING("parameter ", name, " type ", type, " is not allowed");
267 }
268 }
269 }
270
271 infile.close();
272}
273
274std::string ConfigTool::Exec(std::string command)
275{
276 char buffer[128];
277 std::string result = "";
278
279 // Open pipe to file
280 FILE *pipe = popen(command.c_str(), "r");
281 if (!pipe)
282 return "popen failed!";
283
284 // read till end of process:
285 while (!feof(pipe))
286 {
287 // use buffer to read and add to result
288 if (fgets(buffer, 128, pipe) != NULL)
289 result += buffer;
290 }
291
292 pclose(pipe);
293 return result;
294}
std::string line
std::string Exec(std::string command)
std::shared_ptr< MSGTool > msg
Definition ConfigTool.h:35
void ReadConfigFromFile(const TString &filename)
Read config info from given file.
std::shared_ptr< MSGTool > MSGUser() const
Get the MSGTool pointer.
Definition ConfigTool.h:44
std::vector< TString > InputMD5Sum
Definition ConfigTool.h:34
Maniplulate all messages of NAGASH.
Definition MSGTool.h:28
MSGLevel
class to define different message level
Definition MSGTool.h:14