forked from cyrus/high-dimensional-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigFile.h
166 lines (127 loc) · 5.29 KB
/
ConfigFile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Copyright (C) 2004,2005,2006,2007,2008,2009,2010,2011,2012,2013 Cyrus Shaoul and Geoff Hollis
This file is part of HiDEx.
HiDEx is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
HiDEx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HiDEx in the COPYING.txt file.
If not, see <http://www.gnu.org/licenses/>.
*/
/** @file ConfigFile.h
Class ConfigFile.
*/
#ifndef CONFIGFILE_H
#define CONFIGFILE_H
#include <iosfwd>
#include <map>
#include <string>
#include <vector>
/** Parser for config files.
The format is similar to Windows or KDE INI-files.
- Comment lines begin with \#
- Empty lines are ignored
- Keys are case-sensitive
Key value pairs are defined with lines like:
@verbatim key = value @endverbatim
The value can contain spaces; leading and trailing spaces are stripped.
*/
class ConfigFile
{
public:
/** Create a configuration from a file.
@throws Exception If file contains syntax errors or not allowed keys.
*/
ConfigFile(const std::string& filename,
const std::vector<std::string>& allowedKeys, std::string& multi);
/** Create a configuration from a stream.
@throws Exception If file contains syntax errors or not allowed keys.
*/
ConfigFile(std::istream& in, const std::vector<std::string>& allowedKeys);
/** Check if file contains a key. */
bool contains(const std::string& key) const;
/** Get value.
@throws Exception If file does not contain the key.
*/
std::string get(const std::string& key) const;
/** Get value or return default.
@return The value from the file or the default value, if the file
does not contain the key.
*/
std::string get(const std::string& key,
const std::string& defaultValue) const;
/** Get boolean value.
@throws Exception If file does not contain the key or value is not a
boolean (0 or 1).
*/
bool getBool(const std::string& key) const;
/** Get boolean value or return default.
@return The value from the file or the default value, if the file
does not contain the key.
@throws Exception If value is not a boolean (0 or 1).
*/
bool getBool(const std::string& key, bool defaultValue) const;
/** Get double value.
@throws Exception If file does not contain the key or value is not a
double.
*/
double getDouble(const std::string& key) const;
/** Get double value or return default.
@return The value from the file or the default value, if the file
does not contain the key.
@throws Exception If value is not a double.
*/
double getDouble(const std::string& key, double defaultValue) const;
/** Get integer value.
@throws Exception If file does not contain the key or value is not an
integer.
*/
int getInt(const std::string& key) const;
/** Get integer value or return default.
@return The value from the file or the default value, if the file
does not contain the key.
@throws Exception If value is not an integer.
*/
int getInt(const std::string& key, int defaultValue) const;
/** Get positive integer value or return default.
@return The value from the file or the default value, if the file
does not contain the key.
@throws Exception If value is not a positive integer or 0.
*/
int getPositiveInt(const std::string& key, int defaultValue) const;
size_t getSizet(const std::string& key) const;
/** Get size_t value or return default.
@return The value from the file or the default value, if the file
does not contain the key.
@throws Exception If value is not an integer.
*/
size_t getSizet(const std::string& key, size_t defaultValue) const;
/** Get size_t value or return default.
@return The value from the file or the default value, if the file
does not contain the key.
@throws Exception If value is not a positive integer or 0.
*/
unsigned long long int getLong(const std::string& key) const;
/** Get size_t value or return default.
@return The value from the file or the default value, if the file
does not contain the key.
@throws Exception If value is not an integer.
*/
unsigned long long int getLong(const std::string& key, unsigned long long int defaultValue) const;
private:
const std::string _filename;
const std::vector<std::string> _allowedKeys;
std::map<std::string,std::string> _map;
void handleLine(const std::string& line, int lineNumber);
std::string getItem(std::string& inputstring);
bool isAllowedKey(const std::string& key) const;
void read(std::istream& in);
void readstring(std::string& inputstring);
void throwError(const std::string& message, int lineNumber) const;
};
#endif // CONFIGFILE_H