Belle II Software  release-05-02-19
ConfigFile.cc
1 #include "daq/slc/base/ConfigFile.h"
2 
3 #include "daq/slc/base/StringUtil.h"
4 
5 #include <cstdlib>
6 #include <cstring>
7 #include <fstream>
8 #include <iostream>
9 #include <sstream>
10 
11 using namespace Belle2;
12 
13 const std::string ConfigFile::getFilePath(const std::string& filename_in)
14 {
15  std::string filename = filename_in;
16  if (filename == "slowcontrol") {
17  /*
18  char* hostname = getenv("HOSTNAME");
19  if (hostname != NULL && std::string(hostname).find("hlt") != std::string::npos) {
20  filename = "slowcontrol.hlt";
21  }
22  */
23  char* slcfile = getenv("BELLE2_SLC_FILE");
24  if (slcfile != NULL && strlen(slcfile) > 0) {
25  filename = slcfile;
26  }
27  }
28  std::string file_path;
29  if (filename.size() > std::string(".conf").size() + 1 &&
30  StringUtil::find(filename, ".conf")) {
31  return filename;
32  }
33  if (filename.at(0) != '/') {
34  char* path = getenv("BELLE2_DAQ_SLC");
35  if (path == NULL) {
36  path = getenv("BELLE2_LOCAL_DIR");
37  if (path == NULL) {
38  exit(1);
39  }
40  file_path = path;
41  file_path += "/daq/slc/";
42  } else {
43  file_path = path;
44  }
45  if (filename.find("/") == std::string::npos) {
46  file_path += "/data/config/" + filename + ".conf";
47  } else {
48  file_path += "/data/" + filename + ".conf";
49  }
50  } else {
51  file_path = filename;
52  }
53  return file_path;
54 }
55 
56 void ConfigFile::read(const std::string& filename, bool overload)
57 {
58  if (filename.size() == 0) return;
59  std::ifstream fin(getFilePath(filename).c_str());
60  read(fin, overload);
61  fin.close();
62 }
63 
64 void ConfigFile::read(std::istream& is, bool overload)
65 {
66  std::string s;
67  std::string dir = "";
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],
73  " ", ""), "\t", "");
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));
77  }
78  std::string l = label;
79  label = StringUtil::replace(label, "[]", StringUtil::form("[%d]", m_count[l]));
80  m_count[l] = m_count[l] + 1;
81  }
82  if (str_v.size() > 2) {
83  for (size_t i = 2; i < str_v.size(); i++) {
84  str_v[1].append(":");
85  str_v[1].append(str_v[i]);
86  }
87  }
88  std::string value = "";
89  size_t i = 0;
90  std::stringstream ss;
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;
97  ss << str_v[1].at(i);
98  }
99  break;
100  }
101  if (str_v[1].at(i) == '$') {
102  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);
107  }
108  }
109  std::string tmp = ss.str();
110  const char* env = getenv(tmp.c_str());
111  ss.str("");
112  if (env != NULL) {
113  ss << env;
114  } else if (m_value_m.find(tmp) != m_value_m.end()) {
115  ss << m_value_m[tmp];
116  }
117  continue;
118  }
119  ss << str_v[1].at(i);
120  }
121  add(label, ss.str(), overload);
122  }
123  }
124 }
125 
126 void ConfigFile::clear()
127 {
128  m_value_m.clear();
129 }
130 
131 const std::string ConfigFile::get(const std::string& label)
132 {
133  if (m_value_m.find(label) != m_value_m.end()) {
134  return m_value_m[label];
135  }
136  return "";
137 }
138 
139 int ConfigFile::getInt(const std::string& label)
140 {
141  std::string value = get(label);
142  if (value.size() > 0) return atoi(value.c_str());
143  else return 0;
144 }
145 
146 bool ConfigFile::getBool(const std::string& label)
147 {
148  std::string value = get(label);
149  if (value.size() > 0) return StringUtil::tolower(value) == "true";
150  else return false;
151 }
152 
153 double ConfigFile::getFloat(const std::string& label)
154 {
155  std::string value = get(label);
156  if (value.size() > 0) return atof(value.c_str());
157  else return 0;
158 }
159 
160 void ConfigFile::add(const std::string& label,
161  const std::string& value, bool overload)
162 {
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;
168  }
169 }
170 
171 void ConfigFile::write(const std::string& path)
172 {
173  std::stringstream ss;
174  ss << "#" << std::endl
175  << "#" << std::endl
176  << "#" << std::endl
177  << "" << 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;
183  }
184  ss << "" << std::endl
185  << "#" << std::endl
186  << "#" << std::endl
187  << "#" << std::endl;
188  std::ofstream fout(getFilePath(path).c_str());
189  fout << ss.str();
190 }
191 
192 void ConfigFile::print()
193 {
194  std::cout << "#" << std::endl
195  << "#" << std::endl
196  << "#" << std::endl
197  << "" << 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;
203  }
204  std::cout << "" << std::endl
205  << "#" << std::endl
206  << "#" << std::endl
207  << "#" << std::endl;
208 }
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
alignment.constraints_generator.filename
filename
File name.
Definition: constraints_generator.py:224