1 |
// ConfigFile.h
|
2 |
// Class for reading named values from configuration files
|
3 |
// Richard J. Wagner v2.1 24 May 2004 wagnerr@umich.edu
|
4 |
|
5 |
// Copyright (c) 2004 Richard J. Wagner
|
6 |
//
|
7 |
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 |
// of this software and associated documentation files (the "Software"), to
|
9 |
// deal in the Software without restriction, including without limitation the
|
10 |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
11 |
// sell copies of the Software, and to permit persons to whom the Software is
|
12 |
// furnished to do so, subject to the following conditions:
|
13 |
//
|
14 |
// The above copyright notice and this permission notice shall be included in
|
15 |
// all copies or substantial portions of the Software.
|
16 |
//
|
17 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
23 |
// IN THE SOFTWARE.
|
24 |
|
25 |
// Typical usage
|
26 |
// -------------
|
27 |
//
|
28 |
// Given a configuration file "settings.inp":
|
29 |
// atoms = 25
|
30 |
// length = 8.0 # nanometers
|
31 |
// name = Reece Surcher
|
32 |
//
|
33 |
// Named values are read in various ways, with or without default values:
|
34 |
// ConfigFile config( "settings.inp" );
|
35 |
// int atoms = config.read<int>( "atoms" );
|
36 |
// double length = config.read( "length", 10.0 );
|
37 |
// string author, title;
|
38 |
// config.readInto( author, "name" );
|
39 |
// config.readInto( title, "title", string("Untitled") );
|
40 |
//
|
41 |
// See file example.cpp for more examples.
|
42 |
|
43 |
#ifndef CONFIGFILE_H
|
44 |
#define CONFIGFILE_H
|
45 |
|
46 |
#include <string>
|
47 |
#include <map>
|
48 |
#include <iostream>
|
49 |
#include <fstream>
|
50 |
#include <sstream>
|
51 |
|
52 |
using std::string;
|
53 |
|
54 |
class ConfigFile {
|
55 |
// Data
|
56 |
protected:
|
57 |
string myDelimiter; // separator between key and value
|
58 |
string myComment; // separator between value and comments
|
59 |
string mySentry; // optional string to signal end of file
|
60 |
std::map<string,string> myContents; // extracted keys and values
|
61 |
|
62 |
typedef std::map<string,string>::iterator mapi;
|
63 |
typedef std::map<string,string>::const_iterator mapci;
|
64 |
|
65 |
string _filename;
|
66 |
|
67 |
|
68 |
// Methods
|
69 |
public:
|
70 |
ConfigFile( string filename,
|
71 |
string delimiter = "=",
|
72 |
string comment = "#",
|
73 |
string sentry = "EndConfigFile" );
|
74 |
ConfigFile();
|
75 |
|
76 |
// Search for key and read value or optional default value
|
77 |
template<class T> T read( const string& key ) const; // call as read<T>
|
78 |
template<class T> T read( const string& key, const T& value ) const;
|
79 |
template<class T> bool readInto( T& var, const string& key ) const;
|
80 |
template<class T>
|
81 |
bool readInto( T& var, const string& key, const T& value ) const;
|
82 |
|
83 |
// Modify keys and values
|
84 |
template<class T> void add( string key, const T& value );
|
85 |
void remove( const string& key );
|
86 |
|
87 |
// Check whether key exists in configuration
|
88 |
bool keyExists( const string& key ) const;
|
89 |
|
90 |
// Check or change configuration syntax
|
91 |
string getDelimiter() const { return myDelimiter; }
|
92 |
string getComment() const { return myComment; }
|
93 |
string getSentry() const { return mySentry; }
|
94 |
string setDelimiter( const string& s )
|
95 |
{ string old = myDelimiter; myDelimiter = s; return old; }
|
96 |
string setComment( const string& s )
|
97 |
{ string old = myComment; myComment = s; return old; }
|
98 |
|
99 |
// Write or read configuration
|
100 |
friend std::ostream& operator<<( std::ostream& os, const ConfigFile& cf );
|
101 |
friend std::istream& operator>>( std::istream& is, ConfigFile& cf );
|
102 |
|
103 |
//std::ifstream getFStream();
|
104 |
protected:
|
105 |
template<class T> static string T_as_string( const T& t );
|
106 |
template<class T> static T string_as_T( const string& s );
|
107 |
static void trim( string& s );
|
108 |
|
109 |
|
110 |
// Exception types
|
111 |
public:
|
112 |
struct file_not_found {
|
113 |
string filename;
|
114 |
file_not_found( const string& filename_ = string() )
|
115 |
: filename(filename_) {} };
|
116 |
struct key_not_found { // thrown only by T read(key) variant of read()
|
117 |
string key;
|
118 |
key_not_found( const string& key_ = string() )
|
119 |
: key(key_) {} };
|
120 |
};
|
121 |
|
122 |
|
123 |
/* static */
|
124 |
template<class T>
|
125 |
string ConfigFile::T_as_string( const T& t )
|
126 |
{
|
127 |
// Convert from a T to a string
|
128 |
// Type T must support << operator
|
129 |
std::ostringstream ost;
|
130 |
ost << t;
|
131 |
return ost.str();
|
132 |
}
|
133 |
|
134 |
|
135 |
/* static */
|
136 |
template<class T>
|
137 |
T ConfigFile::string_as_T( const string& s )
|
138 |
{
|
139 |
// Convert from a string to a T
|
140 |
// Type T must support >> operator
|
141 |
T t;
|
142 |
std::istringstream ist(s);
|
143 |
ist >> t;
|
144 |
return t;
|
145 |
}
|
146 |
|
147 |
|
148 |
/* static */
|
149 |
template<>
|
150 |
inline string ConfigFile::string_as_T<string>( const string& s )
|
151 |
{
|
152 |
// Convert from a string to a string
|
153 |
// In other words, do nothing
|
154 |
return s;
|
155 |
}
|
156 |
|
157 |
|
158 |
/* static */
|
159 |
template<>
|
160 |
inline bool ConfigFile::string_as_T<bool>( const string& s )
|
161 |
{
|
162 |
// Convert from a string to a bool
|
163 |
// Interpret "false", "F", "no", "n", "0" as false
|
164 |
// Interpret "true", "T", "yes", "y", "1", "-1", or anything else as true
|
165 |
bool b = true;
|
166 |
string sup = s;
|
167 |
for( string::iterator p = sup.begin(); p != sup.end(); ++p )
|
168 |
*p = toupper(*p); // make string all caps
|
169 |
if( sup==string("FALSE") || sup==string("F") ||
|
170 |
sup==string("NO") || sup==string("N") ||
|
171 |
sup==string("0") || sup==string("NONE") )
|
172 |
b = false;
|
173 |
return b;
|
174 |
}
|
175 |
|
176 |
|
177 |
template<class T>
|
178 |
T ConfigFile::read( const string& key ) const
|
179 |
{
|
180 |
// Read the value corresponding to key
|
181 |
mapci p = myContents.find(key);
|
182 |
if( p == myContents.end() ) throw key_not_found(key);
|
183 |
return string_as_T<T>( p->second );
|
184 |
}
|
185 |
|
186 |
|
187 |
template<class T>
|
188 |
T ConfigFile::read( const string& key, const T& value ) const
|
189 |
{
|
190 |
// Return the value corresponding to key or given default value
|
191 |
// if key is not found
|
192 |
mapci p = myContents.find(key);
|
193 |
if( p == myContents.end() ) return value;
|
194 |
return string_as_T<T>( p->second );
|
195 |
}
|
196 |
|
197 |
|
198 |
template<class T>
|
199 |
bool ConfigFile::readInto( T& var, const string& key ) const
|
200 |
{
|
201 |
// Get the value corresponding to key and store in var
|
202 |
// Return true if key is found
|
203 |
// Otherwise leave var untouched
|
204 |
mapci p = myContents.find(key);
|
205 |
bool found = ( p != myContents.end() );
|
206 |
if( found ) var = string_as_T<T>( p->second );
|
207 |
if( !found ) { std::cout<<"Error: no key '"<<key.c_str()<<"' found in config file"<<std::endl; throw key_not_found(key);}
|
208 |
return found;
|
209 |
}
|
210 |
|
211 |
|
212 |
template<class T>
|
213 |
bool ConfigFile::readInto( T& var, const string& key, const T& value ) const
|
214 |
{
|
215 |
// Get the value corresponding to key and store in var
|
216 |
// Return true if key is found
|
217 |
// Otherwise set var to given default
|
218 |
mapci p = myContents.find(key);
|
219 |
bool found = ( p != myContents.end() );
|
220 |
if( found )
|
221 |
var = string_as_T<T>( p->second );
|
222 |
else
|
223 |
var = value;
|
224 |
return found;
|
225 |
}
|
226 |
|
227 |
|
228 |
template<class T>
|
229 |
void ConfigFile::add( string key, const T& value )
|
230 |
{
|
231 |
// Add a key with given value
|
232 |
string v = T_as_string( value );
|
233 |
trim(key);
|
234 |
trim(v);
|
235 |
myContents[key] = v;
|
236 |
return;
|
237 |
}
|
238 |
|
239 |
#endif // CONFIGFILE_H
|
240 |
|
241 |
// Release notes:
|
242 |
// v1.0 21 May 1999
|
243 |
// + First release
|
244 |
// + Template read() access only through non-member readConfigFile()
|
245 |
// + ConfigurationFileBool is only built-in helper class
|
246 |
//
|
247 |
// v2.0 3 May 2002
|
248 |
// + Shortened name from ConfigurationFile to ConfigFile
|
249 |
// + Implemented template member functions
|
250 |
// + Changed default comment separator from % to #
|
251 |
// + Enabled reading of multiple-line values
|
252 |
//
|
253 |
// v2.1 24 May 2004
|
254 |
// + Made template specializations inline to avoid compiler-dependent linkage
|
255 |
// + Allowed comments within multiple-line values
|
256 |
// + Enabled blank line termination for multiple-line values
|
257 |
// + Added optional sentry to detect end of configuration file
|
258 |
// + Rewrote messy trimWhitespace() function as elegant trim()
|