Zero  0.1.0
confparser.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-kits -- Benchmark implementations for Shore-MT
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 // Copyright (c) 2004 Richard J. Wagner
25 //
26 // Permission is hereby granted, free of charge, to any person obtaining a copy
27 // of this software and associated documentation files (the "Software"), to
28 // deal in the Software without restriction, including without limitation the
29 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
30 // sell copies of the Software, and to permit persons to whom the Software is
31 // furnished to do so, subject to the following conditions:
32 //
33 // The above copyright notice and this permission notice shall be included in
34 // all copies or substantial portions of the Software.
35 //
36 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
41 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
42 // IN THE SOFTWARE.
43 
44 // Typical usage
45 // -------------
46 //
47 // Given a configuration file "settings.inp":
48 // atoms = 25
49 // length = 8.0 # nanometers
50 // name = Reece Surcher
51 //
52 // Named values are read in various ways, with or without default values:
53 // ConfigFile config( "settings.inp" );
54 // int atoms = config.read<int>( "atoms" );
55 // double length = config.read( "length", 10.0 );
56 // string author, title;
57 // config.readInto( author, "name" );
58 // config.readInto( title, "title", string("Untitled") );
59 //
60 
68 #ifndef __CONFPARSER_H
69 #define __CONFPARSER_H
70 
71 #include <map>
72 #include <iostream>
73 #include <fstream>
74 #include <sstream>
75 
76 using std::string;
77 
78 struct ConfigOption {
79  string _opt_name;
80 
82 
84 
85  ConfigOption(string name, string entry, string value) {
86  _opt_name = name;
87  _opt_config_entry = entry;
88  _opt_default_value = value;
89  }
90 }; // EOF: ConfigOption
91 
92 
93 class ConfigFile {
94  // Data
95 protected:
96  string myDelimiter; // separator between key and value
97  string myComment; // separator between value and comments
98  string mySentry; // optional string to signal end of file
99 
100  std::map<string, string> myContents; // extracted keys and values
101  string _fname;
102 
103  typedef std::map<string, string>::iterator mapi;
104 
105  typedef std::map<string, string>::const_iterator mapci;
106 
107  // Methods
108 public:
109  ConfigFile(string filename,
110  string delimiter = "=",
111  string comment = "#",
112  string sentry = "EndConfigFile");
113 
114  ConfigFile();
115 
116  // Search for key and read value or optional default value
117  template<class T>
118  T read(const string& key) const; // call as read<T>
119  template<class T>
120  T read(const string& key, const T& value) const;
121 
122  template<class T>
123  bool readInto(T& var, const string& key) const;
124 
125  template<class T>
126  bool readInto(T& var, const string& key, const T& value) const;
127 
128  // Modify keys and values
129  template<class T>
130  void add(string key, const T& value);
131 
132  void remove(const string& key);
133 
134  // Check whether key exists in configuration
135  bool keyExists(const string& key) const;
136 
137  // Check or change configuration syntax
138  string getDelimiter() const {
139  return myDelimiter;
140  }
141 
142  string getComment() const {
143  return myComment;
144  }
145 
146  string getSentry() const {
147  return mySentry;
148  }
149 
150  string setDelimiter(const string& s) {
151  string old = myDelimiter;
152  myDelimiter = s;
153  return old;
154  }
155 
156  string setComment(const string& s) {
157  string old = myComment;
158  myComment = s;
159  return old;
160  }
161 
162  // save configuration
163  void saveCurrentConfig();
164 
165  // Write or read configuration
166  friend std::ostream& operator<<(std::ostream& os, const ConfigFile& cf);
167 
168  friend std::istream& operator>>(std::istream& is, ConfigFile& cf);
169 
170 protected:
171  template<class T>
172  static string T_as_string(const T& t);
173 
174  template<class T>
175  static T string_as_T(const string& s);
176 
177  static void trim(string& s);
178 
179 
180  // Exception types
181 public:
182  struct file_not_found {
183  string filename;
184 
185  file_not_found(const string& filename_ = string())
186  : filename(filename_) {}
187  };
188 
189  struct key_not_found { // thrown only by T read(key) variant of read()
190  string key;
191 
192  key_not_found(const string& key_ = string())
193  : key(key_) {}
194  };
195 }; // EOF: ConfigFile
196 
197 
198 /*static*/ template<class T>
199 string ConfigFile::T_as_string(const T& t) {
200  // Convert from a T to a string
201  // Type T must support << operator
202  std::ostringstream ost;
203  ost << t;
204  return ost.str();
205 }
206 
207 /*static*/ template<class T>
208 T ConfigFile::string_as_T(const string& s) {
209  // Convert from a string to a T
210  // Type T must support >> operator
211  T t;
212  std::istringstream ist(s);
213  ist >> t;
214  return t;
215 }
216 
217 /*static*/ template<>
218 inline string ConfigFile::string_as_T<string>(const string& s) {
219  // Convert from a string to a string
220  // In other words, do nothing
221  return s;
222 }
223 
224 /*static*/ template<>
225 inline bool ConfigFile::string_as_T<bool>(const string& s) {
226  // Convert from a string to a bool
227  // Interpret "false", "F", "no", "n", "0" as false
228  // Interpret "true", "T", "yes", "y", "1", "-1", or anything else as true
229  bool b = true;
230  string sup = s;
231  for (string::iterator p = sup.begin(); p != sup.end(); ++p) {
232  *p = toupper(*p);
233  } // make string all caps
234  if (sup == string("FALSE") || sup == string("F") ||
235  sup == string("NO") || sup == string("N") ||
236  sup == string("0") || sup == string("NONE"))
237  b = false;
238  return b;
239 }
240 
241 template<class T>
242 T ConfigFile::read(const string& key) const {
243  // Read the value corresponding to key
244  mapci p = myContents.find(key);
245  if (p == myContents.end()) {
246  throw key_not_found(key);
247  }
248  return string_as_T<T>(p->second);
249 }
250 
251 template<class T>
252 T ConfigFile::read(const string& key, const T& value) const {
253  // Return the value corresponding to key or given default value
254  // if key is not found
255  mapci p = myContents.find(key);
256  if (p == myContents.end()) {
257  return value;
258  }
259  return string_as_T<T>(p->second);
260 }
261 
262 template<class T>
263 bool ConfigFile::readInto(T& var, const string& key) const {
264  // Get the value corresponding to key and store in var
265  // Return true if key is found
266  // Otherwise leave var untouched
267  mapci p = myContents.find(key);
268  bool found = (p != myContents.end());
269  if (found) {
270  var = string_as_T<T>(p->second);
271  }
272  return found;
273 }
274 
275 template<class T>
276 bool ConfigFile::readInto(T& var, const string& key, const T& value) const {
277  // Get the value corresponding to key and store in var
278  // Return true if key is found
279  // Otherwise set var to given default
280  mapci p = myContents.find(key);
281  bool found = (p != myContents.end());
282  if (found) {
283  var = string_as_T<T>(p->second);
284  } else {
285  var = value;
286  }
287  return found;
288 }
289 
290 template<class T>
291 void ConfigFile::add(string key, const T& value) {
292  // Add a key with given value
293  string v = T_as_string(value);
294  trim(key);
295  trim(v);
296  myContents[key] = v;
297  return;
298 }
299 
300 #endif // __CONFPARSER_H
bool readInto(T &var, const string &key) const
Definition: confparser.h:263
std::map< string, string > myContents
Definition: confparser.h:100
Definition: confparser.h:93
Definition: confparser.h:189
string filename
Definition: confparser.h:183
string getDelimiter() const
Definition: confparser.h:138
Definition: confparser.h:182
std::istream & operator>>(std::istream &is, ConfigFile &cf)
Definition: confparser.cpp:94
string setComment(const string &s)
Definition: confparser.h:156
string getComment() const
Definition: confparser.h:142
string _opt_config_entry
Definition: confparser.h:81
string _opt_name
Definition: confparser.h:79
string myDelimiter
Definition: confparser.h:96
key_not_found(const string &key_=string())
Definition: confparser.h:192
string mySentry
Definition: confparser.h:98
static T string_as_T(const string &s)
Definition: confparser.h:208
file_not_found(const string &filename_=string())
Definition: confparser.h:185
string key
Definition: confparser.h:190
string getSentry() const
Definition: confparser.h:146
string myComment
Definition: confparser.h:97
std::ostream & operator<<(std::ostream &os, const ConfigFile &cf)
Definition: confparser.cpp:83
string _fname
Definition: confparser.h:101
std::map< string, string >::const_iterator mapci
Definition: confparser.h:105
Definition: confparser.h:78
string _opt_default_value
Definition: confparser.h:83
std::map< string, string >::iterator mapi
Definition: confparser.h:103
void add(string key, const T &value)
Definition: confparser.h:291
T read(const string &key) const
Definition: confparser.h:242
string setDelimiter(const string &s)
Definition: confparser.h:150
#define T
Definition: w_okvl_inl.h:45
static string T_as_string(const T &t)
Definition: confparser.h:199
ConfigOption(string name, string entry, string value)
Definition: confparser.h:85