charon-core  0.3.1
ParameterFile.hxx
Go to the documentation of this file.
1 /* This file is part of Charon.
2 
3  Charon is free software: you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Charon is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public License
14  along with Charon. If not, see <http://www.gnu.org/licenses/>.
15  */
27 #ifndef _ParameterFile_HXX
28 #define _ParameterFile_HXX
29 
30 #include "ParameterFile.h"
31 #include "SplitStream.h"
32 #include "StringTool.h"
33 #include "FileTool.h"
34 
35 #include <fstream>
36 #include <sstream>
37 #include <algorithm>
38 #include <cassert>
39 
40 inline bool ParameterFile::isSet(std::string parameter) const {
41  _toLower(parameter);
42  return (_params.find(parameter) != _params.end());
43 }
44 
45 template<class T>
46 inline void ParameterFile::set(std::string parameter, const T& value) {
47  std::ostringstream str;
48  str << value;
49  _set(parameter, str.str());
50 }
51 
52 template<class T>
53 inline void ParameterFile::set(std::string parameter,
54  const std::vector<T>& value) {
55  if (value.size()) {
56  std::ostringstream str;
57  str << value[0];
58  for (unsigned int i = 1; i < value.size(); i++) {
59  str << ";" << value[i];
60  }
61  _set(parameter, str.str());
62  }
63 }
64 
65 template<class T>
66 inline T ParameterFile::get(std::string parameter) const {
67  _toLower(parameter);
68  std::map<std::string, std::string>::const_iterator found;
69  found = _params.find(parameter);
70  if (found == _params.end())
71  throw ParameterFile::Unset("Parameter " + parameter + " not set");
72 
73  std::istringstream strm(found->second);
74  T ret;
75  strm >> ret;
76  return ret;
77 }
78 
79 template<class T>
80 inline T ParameterFile::get(std::string parameter, T defaultValue) {
81  if (isSet(parameter))
82  return get<T> (parameter);
83  else {
84  if (_noFoundWarnings)
85  sout << "(WW) setting unkown parameter '" << parameter << "' to '"
86  << defaultValue << "'" << std::endl;
87  set<T> (parameter, defaultValue);
88  return defaultValue;
89  }
90 }
91 
92 template<class T>
93 inline std::vector<T> ParameterFile::getList(std::string parameter) const {
94  _toLower(parameter);
95  std::vector<T> result;
96  std::map<std::string, std::string>::const_iterator found;
97  found = _params.find(parameter);
98  if (found == _params.end())
99  throw ParameterFile::Unset("Parameter " + parameter + " not set");
100 
101  std::string value = found->second;
102  if (value != "" && value != "none") {
103  std::vector<std::string> tmp;
104  StringTool::explode(value, _delimiter, tmp);
105 
106  result.resize(tmp.size());
107  for (unsigned int i = 0; i < tmp.size(); ++i) {
108  std::istringstream strm(tmp[i]);
109  T v;
110  strm >> v;
111  result[i] = v;
112  }
113  }
114  return result;
115 }
116 
117 template<class T>
118 inline std::vector<T> ParameterFile::getList(std::string parameter,
119  std::string defaultValue) {
120  if (isSet(parameter))
121  return getList<T> (parameter);
122  else {
123  if (_noFoundWarnings)
124  sout << "(WW) setting unkown parameter '"
125  << parameter << "' to '"
126  << defaultValue << "'" << std::endl;
127  set(parameter, defaultValue);
128  return getList<T> (parameter);
129  }
130 }
131 
132 inline std::string ParameterFile::parName(std::string name) {
133  std::string::size_type pos = name.find(".");
134  if (pos != std::string::npos)
135  name = name.substr(pos + 1);
136  return name;
137 }
138 
139 inline std::string ParameterFile::objName(std::string name) {
140  std::string::size_type pos = name.find(".");
141  if (pos != std::string::npos)
142  name = name.substr(0, pos);
143  return name;
144 }
145 
146 inline void ParameterFile::_toLower(std::string& input) const {
147  std::transform(
148  input.begin(), input.end(), input.begin(),
149  (int(*)(int)) tolower);
150 }
151 
152 // specialized versions
153 
155 template<>
156 inline std::string ParameterFile::get(std::string parameter) const {
157  _toLower(parameter);
158  std::map<std::string, std::string>::const_iterator found;
159  found = _params.find(parameter);
160  if (found == _params.end())
161  throw ParameterFile::Unset("Parameter " + parameter + " not set");
162 
163  // ensure that full string is returned
164  return found->second;
165 }
166 
168 template<>
169 inline bool ParameterFile::get(std::string parameter) const {
170  _toLower(parameter);
171  std::map<std::string, std::string>::const_iterator found;
172  found = _params.find(parameter);
173  if (found == _params.end())
174  throw ParameterFile::Unset("Parameter " + parameter + " not set");
175 
176  std::string val = found->second;
177  _toLower(val);
178  if (val == "true")
179  return true;
180  if (val == "false")
181  return false;
182 
183  // check for numbers
184  std::istringstream strm(val);
185  bool v;
186  strm >> v;
187  return v;
188 }
189 
191 template<>
192 inline std::vector<std::string> ParameterFile::getList(
193  std::string parameter) const {
194  _toLower(parameter);
195  std::vector<std::string> result;
196  std::map<std::string, std::string>::const_iterator found;
197  found = _params.find(parameter);
198  if (found == _params.end())
199  throw ParameterFile::Unset("Parameter " + parameter + " not set");
200  std::string value = found->second;
201  if (value != "" && value != "none") {
202  StringTool::explode(value, _delimiter, result);
203 
204  if (_convertSlashes) {
205  for (unsigned int i = 0; i < result.size(); ++i)
206  FileTool::slashConvert(result[i]);
207  }
208  }
209  return result;
210 }
211 
212 #endif // _ParameterFile_HXX
std::map< std::string, std::string > _params
maps parameters to their respective values
Definition: ParameterFile.h:92
static charon_DEPRECATED std::string parName(std::string fullName)
get parameter component of a full name
SplitStream sout
Dummy instance for usage in other files (for interface too).
void _toLower(std::string &input) const
Get the lowercase version of a string.
Declaration of FileTool methods.
static charon_DEPRECATED std::string objName(std::string fullName)
get object name component of a full name
Declaration of class ParameterFile.
Exception thrown when trying to access unset parameters.
std::vector< T > getList(std::string parameter) const
If multiple values are set, return a list containing these values.
bool _convertSlashes
convert linux to windows slashes and vice versa (depends on define CHARON_LINUX/CHARON_WINDOWS in Str...
void charon_core_DLL_PUBLIC explode(std::string str, char delimiter, std::vector< std::string > &result)
Use delimiter to split the string in a list of substrings.
Definition: StringTool.cpp:46
char _delimiter
delimiter for lists of values (default is ';')
void set(std::string parameter, const T &value=T())
Set a parameter to the given single value.
Declaration of StringTool methods.
T get(std::string parameter) const
Get the value of a specified parameter.
bool isSet(std::string parameter) const
Check if a givem parameter has already been set.
bool _noFoundWarnings
warn if parameter was not found
Declaration of class SplitStream.
void _set(std::string parameter, std::string value)
Store a string value to the parameter list.
void charon_core_DLL_PUBLIC slashConvert(std::string &src)
Convert unix to windows paths and reversed.
Definition: FileTool.cpp:145