8#include "daq/slc/base/ConfigFile.h"
10#include "daq/slc/base/StringUtil.h"
20const std::string ConfigFile::getFilePath(
const std::string& filename_in)
22 std::string filename = filename_in;
23 if (filename ==
"slowcontrol") {
30 char* slcfile = getenv(
"BELLE2_SLC_FILE");
31 if (slcfile != NULL && strlen(slcfile) > 0) {
35 std::string file_path;
36 if (filename.size() > std::string(
".conf").size() + 1 &&
37 StringUtil::find(filename,
".conf")) {
40 if (filename.at(0) !=
'/') {
41 char* path = getenv(
"BELLE2_DAQ_SLC");
43 path = getenv(
"BELLE2_LOCAL_DIR");
48 file_path +=
"/daq/slc/";
52 if (filename.find(
"/") == std::string::npos) {
53 file_path +=
"/data/config/" + filename +
".conf";
55 file_path +=
"/data/" + filename +
".conf";
63void ConfigFile::read(
const std::string& filename,
bool overload)
65 if (filename.size() == 0)
return;
66 std::ifstream fin(getFilePath(filename).c_str());
71void ConfigFile::read(std::istream& is,
bool overload)
75 while (is && getline(is, s)) {
76 if (s.size() == 0 || s.at(0) ==
'#')
continue;
77 std::vector<std::string> str_v = StringUtil::split(s,
':');
78 if (str_v.size() >= 2) {
79 std::string label = StringUtil::replace(StringUtil::replace(str_v[0],
81 if (label.find(
"[]") != std::string::npos) {
82 if (m_count.find(label) == m_count.end()) {
83 m_count.insert(std::pair<std::string, int>(label, 0));
85 std::string l = label;
86 label = StringUtil::replace(label,
"[]", StringUtil::form(
"[%d]", m_count[l]));
87 m_count[l] = m_count[l] + 1;
89 if (str_v.size() > 2) {
90 for (
size_t i = 2; i < str_v.size(); i++) {
92 str_v[1].append(str_v[i]);
95 std::string value =
"";
98 for (; i < str_v[1].size(); i++) {
99 if (str_v[1].at(i) ==
'#' || str_v[1].at(i) ==
'\n')
break;
100 if (str_v[1].at(i) ==
' ' || str_v[1].at(i) ==
'\t')
continue;
101 if (str_v[1].at(i) ==
'"') {
102 for (i++ ; i < str_v[1].size(); i++) {
103 if (str_v[1].at(i) ==
'"')
break;
104 ss << str_v[1].at(i);
108 if (str_v[1].at(i) ==
'$') {
110 if (str_v[1].at(i) ==
'{') {
111 for (i++ ; i < str_v[1].size(); i++) {
112 if (str_v[1].at(i) ==
'}')
break;
113 ss << str_v[1].at(i);
116 std::string tmp = ss.str();
117 const char* env = getenv(tmp.c_str());
121 }
else if (m_value_m.find(tmp) != m_value_m.end()) {
122 ss << m_value_m[tmp];
126 ss << str_v[1].at(i);
128 add(label, ss.str(), overload);
133void ConfigFile::clear()
138const std::string ConfigFile::get(
const std::string& label)
140 if (m_value_m.find(label) != m_value_m.end()) {
141 return m_value_m[label];
146int ConfigFile::getInt(
const std::string& label)
148 std::string value = get(label);
149 if (value.size() > 0)
return atoi(value.c_str());
153bool ConfigFile::getBool(
const std::string& label)
155 std::string value = get(label);
156 if (value.size() > 0)
return StringUtil::tolower(value) ==
"true";
160double ConfigFile::getFloat(
const std::string& label)
162 std::string value = get(label);
163 if (value.size() > 0)
return atof(value.c_str());
167void ConfigFile::add(
const std::string& label,
168 const std::string& value,
bool overload)
170 if (m_value_m.find(label) == m_value_m.end()) {
171 m_value_m.insert(ValueList::value_type(label, value));
172 m_label_v.push_back(label);
173 }
else if (overload) {
174 m_value_m[label] = value;
178void ConfigFile::write(
const std::string& path)
180 std::stringstream ss;
181 ss <<
"#" << std::endl
185 for (std::vector<std::string>::iterator it = m_label_v.begin();
186 it != m_label_v.end(); ++it) {
187 std::string& label(*it);
188 std::string& value(m_value_m[label]);
189 ss << label <<
" : " << value << std::endl;
191 ss <<
"" << std::endl
195 std::ofstream fout(getFilePath(path).c_str());
199void ConfigFile::print()
201 std::cout <<
"#" << std::endl
205 for (std::vector<std::string>::iterator it = m_label_v.begin();
206 it != m_label_v.end(); ++it) {
207 std::string& label(*it);
208 std::string& value(m_value_m[label]);
209 std::cout << label <<
" : " << value << std::endl;
211 std::cout <<
"" << std::endl
Abstract base class for different kinds of events.