CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
ParameterFile.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 by Federico Marulli and Alfonso Veropalumbo *
3  * federico.marulli3@unibo.it, alfonso.veropalumbo@uniroma3.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
35 #include "ParameterFile.h"
36 
37 using namespace std;
38 
39 using namespace cbl;
40 using namespace glob;
41 
42 
43 // ============================================================================
44 
45 
46 std::string cbl::glob::ParameterFile::m_trim (const string inStr)
47 {
48  if (inStr.find_first_not_of(' ') >= inStr.find_last_not_of(' ')+1)
49  return ""; //CHECK
50  return inStr.substr(inStr.find_first_not_of(' '), inStr.find_last_not_of(' ')+1);
51 }
52 
53 
54 // ============================================================================
55 
56 
57 vector<string> cbl::glob::ParameterFile::m_trim_vect (const string inStr)
58 {
59  string str = inStr;
60  str = m_trim(str);
61  vector<string> vect;
62  str = str.substr(str.find_first_not_of('{'), str.find_last_not_of('}'));
63  while (str.find(str.front()) != string::npos) {
64  string val;
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);
68  }
69  else {
70  val = m_trim(str.substr(str.find(str.front()), string::npos));
71  str.erase(str.find(str.front()), string::npos);
72  }
73  vect.emplace_back(val);
74  }
75  return vect;
76 }
77 
78 
79 // ============================================================================
80 
81 
82 cbl::glob::ParameterFile::ParameterFile (const std::string parameter_file)
83 {
84  read(parameter_file);
85 }
86 
87 
88 // ============================================================================
89 
90 
91 void cbl::glob::ParameterFile::read (const std::string parameter_file)
92 {
93  // open the input parameter file
94  ifstream fin(parameter_file.c_str()); checkIO(fin, parameter_file);
95 
96  // read all lines and skip comments (#)
97  string line;
98  while (getline(fin, line)) {
99 
100  // remove potential CR or NL or comments at end of line #
101  line = line.substr(0, line.find("\r"));
102  line = line.substr(0, line.find("\n"));
103  line = line.substr(0, line.find("#"));
104 
105  if (line.size()>0) {
106  string::size_type eqpos = line.find('=');
107 
108  if (eqpos!=string::npos) {
109  string key = m_trim(line.substr(0, eqpos));
110  string val = line.substr(eqpos+1, string::npos);
111 
112  m_keys.emplace_back(key);
113 
114  // trim and store
115  if (val.find('{') == string::npos)
116  {
117  std::string value = m_trim(val);
118  if (m_trim(val) == "")
119  m_parameters[key] = {};
120  else
121  m_parameters[key] = {m_trim(val)};
122  }
123  else {
124  vector<string> vval = m_trim_vect(val);
125  m_parameters[key] = vval;
126  }
127 
128  }
129  }
130  }
131  fin.clear(); fin.close();
132 }
133 
134 
135 // ============================================================================
136 
137 
138 void cbl::glob::ParameterFile::write (const std::string parameter_file)
139 {
140  // open the input parameter file
141  ofstream fout(parameter_file.c_str()); checkIO(fout, parameter_file);
142 
143  for (auto &&key : m_keys)
144  {
145  vector<string> values = m_parameters.at(key);
146 
147  fout << key << " = ";
148 
149  if (values.size() == 0)
150  fout << endl;
151  else if (values.size() == 1)
152  fout << values[0] << endl;
153  else {
154  fout << "{ " << values[0];
155  for(size_t i=1; i<values.size(); i++)
156  fout << ", " << values[i];
157  fout << " }" << endl;
158  }
159  }
160 
161  fout.clear(); fout.close();
162 }
163 
164 
165 // ============================================================================
166 
167 
168 std::vector<std::string> & cbl::glob::ParameterFile::operator[] (const std::string key)
169 {
170  // Add a new entry
171  if (m_parameters.find(key) == m_parameters.end())
172  m_keys.emplace_back(key);
173 
174  return m_parameters[key];
175 }
176 
177 
178 // ============================================================================
179 
180 
181 void cbl::glob::ParameterFile::set_key (const std::string key, std::string value, const size_t pos)
182 {
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");
185 
186  vector<string> &vv = m_parameters.at(key);
187 
188  if (vv.size()>pos)
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);
193  }
194  else
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");
196 }
197 
198 
199 // ============================================================================
200 
201 
202 void cbl::glob::ParameterFile::set_key (const std::string key, const std::vector<std::string> values)
203 {
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");
206 
207  m_parameters[key].erase(m_parameters[key].begin(), m_parameters[key].end());
208  m_parameters[key] = values;
209 }
210 
211 
212 // ============================================================================
213 
214 
215 std::string cbl::glob::ParameterFile::get_key (const std::string key, const std::string default_value, const size_t pos) const
216 {
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;
220  }
221 
222  const vector<string> &vv = m_parameters.at(key);
223 
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;
227  }
228 
229  return vv[pos];
230 }
231 
232 
233 // ============================================================================
234 
235 
236 std::vector<std::string> cbl::glob::ParameterFile::get_key (const std::string key, const std::vector<std::string> default_values) const
237 {
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;
241  }
242 
243  return m_parameters.at(key);
244 }
The class ParameterFile.
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
Definition: Constants.h:121
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
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
Definition: Kernel.h:780
void WarningMsgCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL)
internal CBL warning message
Definition: Kernel.h:747