1 #include "daq/slc/base/ConfigFile.h"
3 #include "daq/slc/base/StringUtil.h"
13 const std::string ConfigFile::getFilePath(
const std::string& filename_in)
16 if (filename ==
"slowcontrol") {
23 char* slcfile = getenv(
"BELLE2_SLC_FILE");
24 if (slcfile != NULL && strlen(slcfile) > 0) {
28 std::string file_path;
29 if (
filename.size() > std::string(
".conf").size() + 1 &&
30 StringUtil::find(filename,
".conf")) {
34 char* path = getenv(
"BELLE2_DAQ_SLC");
36 path = getenv(
"BELLE2_LOCAL_DIR");
41 file_path +=
"/daq/slc/";
45 if (
filename.find(
"/") == std::string::npos) {
46 file_path +=
"/data/config/" +
filename +
".conf";
48 file_path +=
"/data/" +
filename +
".conf";
56 void ConfigFile::read(
const std::string& filename,
bool overload)
59 std::ifstream fin(getFilePath(filename).c_str());
64 void ConfigFile::read(std::istream& is,
bool overload)
68 while (is && getline(is, s)) {
69 if (s.size() == 0 || s.at(0) ==
'#')
continue;
70 std::vector<std::string> str_v = StringUtil::split(s,
':');
71 if (str_v.size() >= 2) {
72 std::string label = StringUtil::replace(StringUtil::replace(str_v[0],
74 if (label.find(
"[]") != std::string::npos) {
75 if (m_count.find(label) == m_count.end()) {
76 m_count.insert(std::pair<std::string, int>(label, 0));
78 std::string l = label;
79 label = StringUtil::replace(label,
"[]", StringUtil::form(
"[%d]", m_count[l]));
80 m_count[l] = m_count[l] + 1;
82 if (str_v.size() > 2) {
83 for (
size_t i = 2; i < str_v.size(); i++) {
85 str_v[1].append(str_v[i]);
88 std::string value =
"";
91 for (; i < str_v[1].size(); i++) {
92 if (str_v[1].at(i) ==
'#' || str_v[1].at(i) ==
'\n')
break;
93 if (str_v[1].at(i) ==
' ' || str_v[1].at(i) ==
'\t')
continue;
94 if (str_v[1].at(i) ==
'"') {
95 for (i++ ; i < str_v[1].size(); i++) {
96 if (str_v[1].at(i) ==
'"')
break;
101 if (str_v[1].at(i) ==
'$') {
103 if (str_v[1].at(i) ==
'{') {
104 for (i++ ; i < str_v[1].size(); i++) {
105 if (str_v[1].at(i) ==
'}')
break;
106 ss << str_v[1].at(i);
109 std::string tmp = ss.str();
110 const char* env = getenv(tmp.c_str());
114 }
else if (m_value_m.find(tmp) != m_value_m.end()) {
115 ss << m_value_m[tmp];
119 ss << str_v[1].at(i);
121 add(label, ss.str(), overload);
126 void ConfigFile::clear()
131 const std::string ConfigFile::get(
const std::string& label)
133 if (m_value_m.find(label) != m_value_m.end()) {
134 return m_value_m[label];
139 int ConfigFile::getInt(
const std::string& label)
141 std::string value = get(label);
142 if (value.size() > 0)
return atoi(value.c_str());
146 bool ConfigFile::getBool(
const std::string& label)
148 std::string value = get(label);
149 if (value.size() > 0)
return StringUtil::tolower(value) ==
"true";
153 double ConfigFile::getFloat(
const std::string& label)
155 std::string value = get(label);
156 if (value.size() > 0)
return atof(value.c_str());
160 void ConfigFile::add(
const std::string& label,
161 const std::string& value,
bool overload)
163 if (m_value_m.find(label) == m_value_m.end()) {
164 m_value_m.insert(ValueList::value_type(label, value));
165 m_label_v.push_back(label);
166 }
else if (overload) {
167 m_value_m[label] = value;
171 void ConfigFile::write(
const std::string& path)
173 std::stringstream ss;
174 ss <<
"#" << std::endl
178 for (std::vector<std::string>::iterator it = m_label_v.begin();
179 it != m_label_v.end(); it++) {
180 std::string& label(*it);
181 std::string& value(m_value_m[label]);
182 ss << label <<
" : " << value << std::endl;
184 ss <<
"" << std::endl
188 std::ofstream fout(getFilePath(path).c_str());
192 void ConfigFile::print()
194 std::cout <<
"#" << std::endl
198 for (std::vector<std::string>::iterator it = m_label_v.begin();
199 it != m_label_v.end(); it++) {
200 std::string& label(*it);
201 std::string& value(m_value_m[label]);
202 std::cout << label <<
" : " << value << std::endl;
204 std::cout <<
"" << std::endl