Belle II Software development
ConfigFile.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include "daq/slc/base/ConfigFile.h"
9
10#include "daq/slc/base/StringUtil.h"
11
12#include <cstdlib>
13#include <cstring>
14#include <fstream>
15#include <iostream>
16#include <sstream>
17
18using namespace Belle2;
19
20const std::string ConfigFile::getFilePath(const std::string& filename_in)
21{
22 std::string filename = filename_in;
23 if (filename == "slowcontrol") {
24 /*
25 char* hostname = getenv("HOSTNAME");
26 if (hostname != NULL && std::string(hostname).find("hlt") != std::string::npos) {
27 filename = "slowcontrol.hlt";
28 }
29 */
30 char* slcfile = getenv("BELLE2_SLC_FILE");
31 if (slcfile != NULL && strlen(slcfile) > 0) {
32 filename = slcfile;
33 }
34 }
35 std::string file_path;
36 if (filename.size() > std::string(".conf").size() + 1 &&
37 StringUtil::find(filename, ".conf")) {
38 return filename;
39 }
40 if (filename.at(0) != '/') {
41 char* path = getenv("BELLE2_DAQ_SLC");
42 if (path == NULL) {
43 path = getenv("BELLE2_LOCAL_DIR");
44 if (path == NULL) {
45 exit(1);
46 }
47 file_path = path;
48 file_path += "/daq/slc/";
49 } else {
50 file_path = path;
51 }
52 if (filename.find("/") == std::string::npos) {
53 file_path += "/data/config/" + filename + ".conf";
54 } else {
55 file_path += "/data/" + filename + ".conf";
56 }
57 } else {
58 file_path = filename;
59 }
60 return file_path;
61}
62
63void ConfigFile::read(const std::string& filename, bool overload)
64{
65 if (filename.size() == 0) return;
66 std::ifstream fin(getFilePath(filename).c_str());
67 read(fin, overload);
68 fin.close();
69}
70
71void ConfigFile::read(std::istream& is, bool overload)
72{
73 std::string s;
74 while (is && getline(is, s)) {
75 if (s.size() == 0 || s.at(0) == '#') continue;
76 std::vector<std::string> str_v = StringUtil::split(s, ':');
77 if (str_v.size() >= 2) {
78 std::string label = StringUtil::replace(StringUtil::replace(str_v[0],
79 " ", ""), "\t", "");
80 if (label.find("[]") != std::string::npos) {
81 if (m_count.find(label) == m_count.end()) {
82 m_count.insert(std::pair<std::string, int>(label, 0));
83 }
84 std::string l = label;
85 label = StringUtil::replace(label, "[]", StringUtil::form("[%d]", m_count[l]));
86 m_count[l] = m_count[l] + 1;
87 }
88 if (str_v.size() > 2) {
89 for (size_t i = 2; i < str_v.size(); i++) {
90 str_v[1].append(":");
91 str_v[1].append(str_v[i]);
92 }
93 }
94 size_t i = 0;
95 std::stringstream ss;
96 for (; i < str_v[1].size(); i++) {
97 if (str_v[1].at(i) == '#' || str_v[1].at(i) == '\n') break;
98 if (str_v[1].at(i) == ' ' || str_v[1].at(i) == '\t') continue;
99 if (str_v[1].at(i) == '"') {
100 for (i++ ; i < str_v[1].size(); i++) {
101 if (str_v[1].at(i) == '"') break;
102 ss << str_v[1].at(i);
103 }
104 break;
105 }
106 if (str_v[1].at(i) == '$') {
107 i++;
108 if (str_v[1].at(i) == '{') {
109 for (i++ ; i < str_v[1].size(); i++) {
110 if (str_v[1].at(i) == '}') break;
111 ss << str_v[1].at(i);
112 }
113 }
114 std::string tmp = ss.str();
115 const char* env = getenv(tmp.c_str());
116 ss.str("");
117 if (env != NULL) {
118 ss << env;
119 } else if (m_value_m.find(tmp) != m_value_m.end()) {
120 ss << m_value_m[tmp];
121 }
122 continue;
123 }
124 ss << str_v[1].at(i);
125 }
126 add(label, ss.str(), overload);
127 }
128 }
129}
130
131void ConfigFile::clear()
132{
133 m_value_m.clear();
134}
135
136const std::string ConfigFile::get(const std::string& label)
137{
138 if (m_value_m.find(label) != m_value_m.end()) {
139 return m_value_m[label];
140 }
141 return "";
142}
143
144int ConfigFile::getInt(const std::string& label)
145{
146 std::string value = get(label);
147 if (value.size() > 0) return atoi(value.c_str());
148 else return 0;
149}
150
151bool ConfigFile::getBool(const std::string& label)
152{
153 std::string value = get(label);
154 if (value.size() > 0) return StringUtil::tolower(value) == "true";
155 else return false;
156}
157
158double ConfigFile::getFloat(const std::string& label)
159{
160 std::string value = get(label);
161 if (value.size() > 0) return atof(value.c_str());
162 else return 0;
163}
164
165void ConfigFile::add(const std::string& label,
166 const std::string& value, bool overload)
167{
168 if (m_value_m.find(label) == m_value_m.end()) {
169 m_value_m.insert(ValueList::value_type(label, value));
170 m_label_v.push_back(label);
171 } else if (overload) {
172 m_value_m[label] = value;
173 }
174}
175
176void ConfigFile::write(const std::string& path)
177{
178 std::stringstream ss;
179 ss << "#" << std::endl
180 << "#" << std::endl
181 << "#" << std::endl
182 << "" << std::endl;
183 for (std::vector<std::string>::iterator it = m_label_v.begin();
184 it != m_label_v.end(); ++it) {
185 const std::string& label(*it);
186 const std::string& value(m_value_m[label]);
187 ss << label << " : " << value << std::endl;
188 }
189 ss << "" << std::endl
190 << "#" << std::endl
191 << "#" << std::endl
192 << "#" << std::endl;
193 std::ofstream fout(getFilePath(path).c_str());
194 fout << ss.str();
195}
196
197void ConfigFile::print()
198{
199 std::cout << "#" << std::endl
200 << "#" << std::endl
201 << "#" << std::endl
202 << "" << std::endl;
203 for (std::vector<std::string>::iterator it = m_label_v.begin();
204 it != m_label_v.end(); ++it) {
205 const std::string& label(*it);
206 const std::string& value(m_value_m[label]);
207 std::cout << label << " : " << value << std::endl;
208 }
209 std::cout << "" << std::endl
210 << "#" << std::endl
211 << "#" << std::endl
212 << "#" << std::endl;
213}
Abstract base class for different kinds of events.