Belle II Software development
ConfigFile Class Reference

Public Types

typedef std::map< std::string, std::string > ValueList
 

Public Member Functions

 ConfigFile (const std::string &file1)
 
 ConfigFile (const std::string &file1, const std::string &file2)
 
 ConfigFile (const std::string &file1, const std::string &file2, const std::string &file3)
 
 ConfigFile (std::istream &is)
 
void clear ()
 
void read (const std::string &filename, bool overload=true)
 
void read (std::istream &is, bool overload=true)
 
bool hasKey (const std::string &label)
 
const std::string get (const std::string &label)
 
int getInt (const std::string &label)
 
bool getBool (const std::string &label)
 
double getFloat (const std::string &label)
 
ValueList & getValues ()
 
std::vector< std::string > & getLabels ()
 
void add (const std::string &label, const std::string &value, bool overload)
 
void write (const std::string &path)
 
void print ()
 

Static Private Member Functions

static const std::string getFilePath (const std::string &filename)
 

Private Attributes

ValueList m_value_m
 
std::vector< std::string > m_label_v
 
std::string m_dir
 
std::map< std::string, int > m_count
 

Detailed Description

Definition at line 22 of file ConfigFile.h.

Member Typedef Documentation

◆ ValueList

typedef std::map<std::string, std::string> ValueList

Definition at line 24 of file ConfigFile.h.

Constructor & Destructor Documentation

◆ ConfigFile() [1/5]

ConfigFile ( )
inline

Definition at line 27 of file ConfigFile.h.

27{}

◆ ConfigFile() [2/5]

ConfigFile ( const std::string & file1)
inlineexplicit

Definition at line 28 of file ConfigFile.h.

29 {
30 read(file1, true);
31 }

◆ ConfigFile() [3/5]

ConfigFile ( const std::string & file1,
const std::string & file2 )
inline

Definition at line 32 of file ConfigFile.h.

33 {
34 read(file1, true);
35 read(file2, true);
36 }

◆ ConfigFile() [4/5]

ConfigFile ( const std::string & file1,
const std::string & file2,
const std::string & file3 )
inline

Definition at line 37 of file ConfigFile.h.

40 {
41 read(file1, true);
42 read(file2, true);
43 read(file3, true);
44 }

◆ ConfigFile() [5/5]

ConfigFile ( std::istream & is)
inlineexplicit

Definition at line 45 of file ConfigFile.h.

46 {
47 read(is);
48 }

◆ ~ConfigFile()

~ConfigFile ( )
inline

Definition at line 49 of file ConfigFile.h.

49{}

Member Function Documentation

◆ add()

void add ( const std::string & label,
const std::string & value,
bool overload )

Definition at line 165 of file ConfigFile.cc.

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}

◆ clear()

void clear ( )

Definition at line 131 of file ConfigFile.cc.

132{
133 m_value_m.clear();
134}

◆ get()

const std::string get ( const std::string & label)

Definition at line 136 of file ConfigFile.cc.

137{
138 if (m_value_m.find(label) != m_value_m.end()) {
139 return m_value_m[label];
140 }
141 return "";
142}

◆ getBool()

bool getBool ( const std::string & label)

Definition at line 151 of file ConfigFile.cc.

152{
153 std::string value = get(label);
154 if (value.size() > 0) return StringUtil::tolower(value) == "true";
155 else return false;
156}

◆ getFilePath()

const std::string getFilePath ( const std::string & filename)
staticprivate

Definition at line 20 of file ConfigFile.cc.

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}

◆ getFloat()

double getFloat ( const std::string & label)

Definition at line 158 of file ConfigFile.cc.

159{
160 std::string value = get(label);
161 if (value.size() > 0) return atof(value.c_str());
162 else return 0;
163}

◆ getInt()

int getInt ( const std::string & label)

Definition at line 144 of file ConfigFile.cc.

145{
146 std::string value = get(label);
147 if (value.size() > 0) return atoi(value.c_str());
148 else return 0;
149}

◆ getLabels()

std::vector< std::string > & getLabels ( )
inline

Definition at line 61 of file ConfigFile.h.

61{ return m_label_v; }

◆ getValues()

ValueList & getValues ( )
inline

Definition at line 60 of file ConfigFile.h.

60{ return m_value_m; }

◆ print()

void print ( )

Definition at line 197 of file ConfigFile.cc.

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}

◆ read() [1/2]

void read ( const std::string & filename,
bool overload = true )

Definition at line 63 of file ConfigFile.cc.

64{
65 if (filename.size() == 0) return;
66 std::ifstream fin(getFilePath(filename).c_str());
67 read(fin, overload);
68 fin.close();
69}

◆ read() [2/2]

void read ( std::istream & is,
bool overload = true )

Definition at line 71 of file ConfigFile.cc.

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}

◆ write()

void write ( const std::string & path)

Definition at line 176 of file ConfigFile.cc.

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}

Member Data Documentation

◆ m_count

std::map<std::string, int> m_count
private

Definition at line 74 of file ConfigFile.h.

◆ m_dir

std::string m_dir
private

Definition at line 73 of file ConfigFile.h.

◆ m_label_v

std::vector<std::string> m_label_v
private

Definition at line 72 of file ConfigFile.h.

◆ m_value_m

ValueList m_value_m
private

Definition at line 71 of file ConfigFile.h.


The documentation for this class was generated from the following files: