#include <NuFCConfig.h>
Public Member Functions | |
| ConfigFile (std::string filename) | |
| string | GetString (string key, string section="", string defaultval="") |
| int | GetInt (string key, string section="", int defaultval=0) |
| double | GetFloat (string key, string section="", double defaultval=0.0) |
| bool | GetBool (string key, string section="", bool defaultval=false) |
Private Member Functions | |
| std::string | Decomment (const std::string &source) |
| Strips the comments from a line. | |
| std::string | Trim (const std::string &source) |
| Trims the whitespace off of the beginning and end of a string. | |
| void | ReadFile (std::ifstream &cfgfile) |
| Controls the file reading. | |
| bool | IsSectionChange (const string &line) |
| Tests for a section change in a line. | |
| string | Section (const string &line) |
| Reads the section string out of a line. | |
| bool | ReadKeyPair (const string §ion, const string &line) |
| Reads a key-pair out of a line. | |
Private Attributes | |
| std::map< std::string, std::map< std::string, std::string > > | fStore |
| The storage for the group/key-value from the file. | |
|
|
Try opening the file Otherwise, we are blank! Nothing more needs to be done.... Definition at line 105 of file NuFCConfig.cxx. References MSG, and ReadFile(). 00106 {
00108 ifstream cfg(filename.c_str());
00109 if(cfg.is_open())
00110 {
00111 // All is well, read the file in!
00112 ReadFile(cfg);
00113
00114 } else {
00115 // Print an error message if we were trying to open a file
00116 if (filename != "") {
00117 MSG("ConfigFile",Msg::kFatal) << "Could not read configuration file " << filename << endl;
00118 throw runtime_error("Could not read configuration file.");
00119 }
00120 }
00121
00123 }
|
|
|
Strips the comments from a line.
Definition at line 125 of file NuFCConfig.cxx. Referenced by ReadFile(). 00126 {
00127 // Strips comments from a line
00128
00129 // Find any instances of '#', and then remove them
00130 int comment = source.find_first_of("#%");
00131 if (comment == -1) return source;
00132 return source.substr(0, comment);
00133 }
|
|
||||||||||||||||
|
Definition at line 277 of file NuFCConfig.cxx. References fStore, and MAXMSG. Referenced by NuFCConfig::LoadConfig(). 00278 {
00279 string value = fStore[section][key];
00280 if (value.size() == 0) return defaultval;
00281
00282 // Now try to work out what it is
00283 if (value == "True" || value == "true") return true;
00284 if (value == "False" || value == "false") return false;
00285
00286 // Try to convert to an integer, and see if it is non-zero
00287 istringstream iss (value);
00288 int ival = 0;
00289 iss >> ival;
00290
00291 // It worked - just return the value
00292 if (!iss.fail())
00293 {
00294 return ival;
00295 }
00296
00297 // It failed..... give up here for now
00298 MAXMSG("ConfigFile",5,Msg::kWarning) << "Failed to read [" << section << "]." << key << " = " << value << " as boolean" << endl;
00299 return defaultval;
00300 }
|
|
||||||||||||||||
|
Definition at line 258 of file NuFCConfig.cxx. References fStore, and MAXMSG. Referenced by NuFCConfig::LoadConfig(). 00259 {
00260 string value = fStore[section][key];
00261 if (value.size() == 0) return defaultval;
00262
00263 // Attempt to read an integer out of this stream
00264 istringstream iss (value);
00265 double ival = 0.0;
00266 iss >> ival;
00267
00268 if (!iss.fail())
00269 {
00270 return ival;
00271 } else {
00272 MAXMSG("ConfigFile",5,Msg::kWarning) << "Failed to read [" << section << "]." << key << " = " << value << " as integer" << endl;
00273 return defaultval;
00274 }
00275 }
|
|
||||||||||||||||
|
Definition at line 239 of file NuFCConfig.cxx. References fStore, and MAXMSG. 00240 {
00241 string value = fStore[section][key];
00242 if (value.size() == 0) return defaultval;
00243
00244 // Attempt to read an integer out of this stream
00245 istringstream iss (value);
00246 int ival = 0;
00247 iss >> ival;
00248
00249 if (!iss.fail())
00250 {
00251 return ival;
00252 } else {
00253 MAXMSG("ConfigFile",5,Msg::kWarning) << "Failed to read [" << section << "]." << key << " = " << value << " as integer" << endl;
00254 return defaultval;
00255 }
00256 }
|
|
||||||||||||||||
|
Definition at line 229 of file NuFCConfig.cxx. References fStore. Referenced by NuFCConfig::LoadConfig(). 00230 {
00231 string value = fStore[section][key];
00232
00233 if (value.size() == 0)
00234 return defaultval;
00235 else
00236 return value;
00237 }
|
|
|
Tests for a section change in a line.
Definition at line 177 of file NuFCConfig.cxx. References MSG. Referenced by ReadFile(). 00178 {
00179 // Work out if this is a change of section by checking the first char
00180 if (line[0] == '[') {
00181 // Check the last char too
00182 if (line[line.size()-1] != ']')
00183 {
00184 // cout << "Warning Parsing file " << lineNo << ": Invalid section change!" << endl;
00185 MSG("ConfigFile",Msg::kWarning) << "Invalid section change: " << line << endl;
00186 } else {
00187 // Both start and ends are valid... is a section change line
00188 // Make sure it is valid
00189 string newsec = line.substr(1, line.size()-2);
00190 if (newsec.size() == 0) {
00191 MSG("ConfigFile", Msg::kWarning) << "Invalid Section Change: Section cannot be blank" << endl;
00192 } else {
00193 return true;
00194 }
00195 }
00196 }
00197
00198 return false;
00199 }
|
|
|
Controls the file reading.
Definition at line 143 of file NuFCConfig.cxx. References Decomment(), IsSectionChange(), MSG, ReadKeyPair(), Section(), and Trim(). Referenced by ConfigFile(). 00144 {
00145 int lineNo = 0;
00146 string line, section = "";
00147
00148 // Loop over all lines in the file
00149 while(std::getline(file, line))
00150 {
00151 ++lineNo;
00152
00153 // Clean up the line
00154 line = Decomment(line);
00155 line = Trim(line);
00156
00157 // Ignore blank lines
00158 if (line.size() == 0) continue;
00159
00160 // Several paths here: Section header, Key-Value pair or invalid.
00161 // First test for section header
00162 if (IsSectionChange(line))
00163 {
00164 section = Section(line);
00165 continue;
00166 }
00167
00168 // Must be a key pair, or invalid
00169 if (!ReadKeyPair(section, line))
00170 {
00171 MSG("ConfigFile",Msg::kWarning) << "Invalid line " << lineNo << ": \"" << line << "\"" << endl;
00172 }
00173
00174 } // Go to next line
00175 }
|
|
||||||||||||
|
Reads a key-pair out of a line.
Definition at line 209 of file NuFCConfig.cxx. References fStore, and Trim(). Referenced by ReadFile(). 00210 {
00211 // Check to see if this is a key pair
00212 int split = line.find_first_of("=:");
00213
00214 // what if we don't find a split?
00215 if (split == -1)
00216 {
00217 // MSG("ConfigFile",Msg::kWarning) << "Invalid key-value pair: \"" << line << "\"" << endl;
00218 return false;
00219 }
00220
00221 // Read out the values
00222 string key = Trim(line.substr(0, split));
00223 string keyval = Trim(line.substr(split+1, line.size()-split+2));
00224 fStore[section][key] = keyval;
00225
00226 return true;
00227 }
|
|
|
Reads the section string out of a line.
Definition at line 201 of file NuFCConfig.cxx. Referenced by ReadFile(). 00202 {
00203 // Extracts the section from a line
00204 string newsec = line.substr(1, line.size()-2);
00205
00206 return newsec;
00207 }
|
|
|
Trims the whitespace off of the beginning and end of a string.
Definition at line 134 of file NuFCConfig.cxx. Referenced by ReadFile(), and ReadKeyPair(). 00135 {
00136 // Find the first non-whitespace
00137 int first = source.find_first_not_of("\n\r\t ");
00138 int last = source.find_last_not_of("\n\r\t ");
00139 if (last == -1) return "";
00140 return source.substr(first, last - first + 1);
00141 }
|
|
|
The storage for the group/key-value from the file.
Definition at line 87 of file NuFCConfig.h. Referenced by GetBool(), GetFloat(), GetInt(), GetString(), and ReadKeyPair(). |
1.3.9.1