48 if (inStr.find_first_not_of(
' ') >= inStr.find_last_not_of(
' ')+1)
50 return inStr.substr(inStr.find_first_not_of(
' '), inStr.find_last_not_of(
' ')+1);
62 str = str.substr(str.find_first_not_of(
'{'), str.find_last_not_of(
'}'));
63 while (str.find(str.front()) != string::npos) {
65 if (str.find(
',') != string::npos) {
66 val = m_trim(str.substr(str.find(str.front()), str.find_first_of(
',')));
67 str.erase(str.find(str.front()), str.find_first_of(
',')+1);
70 val = m_trim(str.substr(str.find(str.front()), string::npos));
71 str.erase(str.find(str.front()), string::npos);
73 vect.emplace_back(val);
94 ifstream fin(parameter_file.c_str());
checkIO(fin, parameter_file);
98 while (getline(fin, line)) {
101 line = line.substr(0, line.find(
"\r"));
102 line = line.substr(0, line.find(
"\n"));
103 line = line.substr(0, line.find(
"#"));
106 string::size_type eqpos = line.find(
'=');
108 if (eqpos!=string::npos) {
109 string key = m_trim(line.substr(0, eqpos));
110 string val = line.substr(eqpos+1, string::npos);
112 m_keys.emplace_back(key);
115 if (val.find(
'{') == string::npos)
117 std::string value = m_trim(val);
118 if (m_trim(val) ==
"")
119 m_parameters[key] = {};
121 m_parameters[key] = {m_trim(val)};
124 vector<string> vval = m_trim_vect(val);
125 m_parameters[key] = vval;
131 fin.clear(); fin.close();
141 ofstream fout(parameter_file.c_str());
checkIO(fout, parameter_file);
143 for (
auto &&key : m_keys)
145 vector<string> values = m_parameters.at(key);
147 fout << key <<
" = ";
149 if (values.size() == 0)
151 else if (values.size() == 1)
152 fout << values[0] << endl;
154 fout <<
"{ " << values[0];
155 for(
size_t i=1; i<values.size(); i++)
156 fout <<
", " << values[i];
157 fout <<
" }" << endl;
161 fout.clear(); fout.close();
171 if (m_parameters.find(key) == m_parameters.end())
172 m_keys.emplace_back(key);
174 return m_parameters[key];
183 if (m_parameters.find(key) == m_parameters.end())
184 ErrorCBL(
"Key "+key+
" has not been found, exiting.",
"cbl::cbl::glob::ParameterFile::operator[]",
"ReadParameters/ParameterFile.cpp");
186 vector<string> &vv = m_parameters.at(key);
189 m_parameters[key][pos] = value;
190 else if (vv.size()==pos) {
191 WarningMsgCBL(
"Found "+
conv(pos,
par::fINT)+
" values for key "+key+
" == "+
conv(pos,
par::fINT)+
". I will add one parameter.",
"cbl::cbl::glob::ParameterFile::set_key()",
"ReadParameters/ParameterFile.cpp");
192 m_parameters[key].push_back(value);
195 ErrorCBL(
"Found "+
conv(vv.size(),
par::fINT)+
" values for key "+key+
" < "+
conv(pos,
par::fINT)+
". Exiting.",
"cbl::cbl::glob::ParameterFile::set_key()",
"ReadParameters/ParameterFile.cpp");
204 if (m_parameters.find(key) == m_parameters.end())
205 ErrorCBL(
"Key "+key+
" has not been found, exiting.",
"cbl::cbl::glob::ParameterFile::operator[]",
"ReadParameters/ParameterFile.cpp");
207 m_parameters[key].erase(m_parameters[key].begin(), m_parameters[key].end());
208 m_parameters[key] = values;
217 if (m_parameters.find(key) == m_parameters.end()) {
218 WarningMsgCBL(
"Key "+key+
" has not been found, I will return default_value.",
"cbl::cbl::glob::ParameterFile::get_key",
"ReadParameters/ParameterFile.cpp");
219 return default_value;
222 const vector<string> &vv = m_parameters.at(key);
224 if (vv.size() <= pos) {
225 WarningMsgCBL(
"Found "+
conv(pos,
par::fINT)+
" values for key "+key+
" <= "+
conv(pos,
par::fINT)+
", I will return default_values.",
"cbl::cbl::glob::ParameterFile::get_key()",
"ReadParameters/ParameterFile.cpp");
226 return default_value;
238 if (m_parameters.find(key) == m_parameters.end()) {
239 WarningMsgCBL(
"Key "+key+
" has not been found, I will return default_values.",
"cbl::cbl::glob::ParameterFile::operator[]",
"ReadParameters/ParameterFile.cpp");
240 return default_values;
243 return m_parameters.at(key);
std::string m_trim(const std::string inStr)
Remove white spaces treading and leading each std::string (private function)
void write(const std::string parameter_file)
member to write a parameter file
std::vector< std::string > & operator[](const std::string key)
Method to set/get entries of the parameter file.
void read(const std::string parameter_file)
member to read a parameter file
ParameterFile()=default
Default empty constructor.
void set_key(const std::string key, std::string value, const size_t pos=0)
Method to set entries of the parameter file.
std::vector< std::string > m_trim_vect(const std::string inStr)
Stores values contained in between curly brackets in a vector of std::string (private function)
std::string get_key(const std::string key, const std::string default_value, const size_t pos=0) const
Method to get one value for a specific parameter.
static const char fINT[]
conversion symbol for: int -> std::string
The global namespace of the CosmoBolognaLib
std::string conv(const T val, const char *fact)
convert a number to a std::string
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message