CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
ReadParameters.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 by Federico Marulli and Tommaso Ronconi *
3  * federico.marulli3@unibo.it, tommaso.ronconi@outlook.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 "ReadParameters.h"
36 
37 using namespace std;
38 
39 using namespace cbl;
40 using namespace glob;
41 
42 
43 // ============================================================================
44 
45 
46 cbl::glob::ReadParameters::ReadParameters (const std::string parameter_file)
47 {
48  // open the input parameter file
49  ifstream fin(parameter_file.c_str()); checkIO(fin, parameter_file);
50 
51  // read all lines and skip comments (#)
52  string line;
53  while (getline(fin, line)) {
54 
55  // remove potential CR or NL or comments at end of line #
56  line = line.substr(0, line.find("\r"));
57  line = line.substr(0, line.find("\n"));
58  line = line.substr(0, line.find("#"));
59 
60  if (line.size()>0) {
61  string::size_type eqpos = line.find('=');
62 
63  if (eqpos!=string::npos) {
64  string key = line.substr(0, eqpos);
65  string val = line.substr(eqpos+1, string::npos);
66 
67  // trim and store
68  if (val.find('{') == string::npos) m_parameters[m_trim(key)] = m_trim(val);
69  else {
70  vector<string> vval = m_trim_vect(val);
71  m_vectors[m_trim(key)] = vval;
72  }
73 
74  }
75  }
76  }
77 
78  fin.clear(); fin.close();
79 }
80 
81 
82 // ============================================================================
83 
84 
85 std::string cbl::glob::ReadParameters::m_trim (const string inStr)
86 {
87  return inStr.substr(inStr.find_first_not_of(' '), inStr.find_last_not_of(' ')+1);
88 }
89 
90 
91 // ============================================================================
92 
93 vector<string> cbl::glob::ReadParameters::m_trim_vect (const string inStr)
94 {
95  string str = inStr;
96  str = m_trim(str);
97  vector<string> vect;
98  str = str.substr(str.find_first_not_of('{'), str.find_last_not_of('}'));
99  while (str.find(str.front()) != string::npos) {
100  string val;
101  if (str.find(',') != string::npos) {
102  val = m_trim(str.substr(str.find(str.front()), str.find_first_of(',')));
103  str.erase(str.find(str.front()), str.find_first_of(',')+1);
104  }
105  else {
106  val = m_trim(str.substr(str.find(str.front()), string::npos));
107  str.erase(str.find(str.front()), string::npos);
108  }
109  vect.emplace_back(val);
110  }
111  return vect;
112 }
113 
The class ReadParameters.
std::string m_trim(const std::string inStr)
Remove white spaces treading and leading each std::string (private function)
ReadParameters()=default
Default empty constructor.
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)
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160