Skip to content
Snippets Groups Projects
Commit 468b7e4b authored by Jorrit Fahlke's avatar Jorrit Fahlke
Browse files

[ConfigParser] Allow to retain older entries when parsing from a file/stream

Often the configuration file is given on the command line.  However,
configuration entries can also be given on the command line, and should
usually take precedence over those from a file.  With these changes it is
possible to read entries from the command line, possibly setting one
configuration entry to the name of a config file in the process.  The
configuration file can then be parsed, setting entries, which have not been
set on the command line.  Entries which already have been set on the command
line will retain their command-line values.

[[Imported from SVN: r6007]]
parent 96dc1aa8
No related branches found
No related tags found
No related merge requests found
......@@ -18,19 +18,25 @@ using namespace std;
ConfigParser::ConfigParser()
{}
void ConfigParser::parseFile(std::string file)
void ConfigParser::parseFile(std::string file, bool overwrite)
{
ifstream in(file.c_str());
if (!in)
DUNE_THROW(IOError, "Could not open configuration file " << file);
parseStream(in, "file '" + file + "'");
parseStream(in, "file '" + file + "'", overwrite);
}
void ConfigParser::parseStream(std::istream& in,
const std::string srcname)
bool overwrite)
{
parseStream(in, "stream", overwrite);
}
void ConfigParser::parseStream(std::istream& in,
const std::string srcname,
bool overwrite)
{
string prefix;
set<string> keysInFile;
......@@ -88,7 +94,8 @@ void ConfigParser::parseStream(std::istream& in,
"' appears twice in " << srcname << " !");
else
{
(*this)[key] = value;
if(overwrite || ! hasKey(key))
(*this)[key] = value;
keysInFile.insert(key);
}
}
......
......@@ -78,6 +78,23 @@ namespace Dune {
ConfigParser();
/** \brief parse C++ stream
*
* Parses C++ stream and build hierarchical config structure.
*
* \param in The stream to parse
* \param overwrite Whether to overwrite already existing values.
* If false, values in the stream will be ignored
* if the key is already present.
*
* \note This method is identical to parseStream(std::istream&,
* const std::string&, bool) with the exception that that
* method allows to give a custom name for the stream.
*/
void parseStream(std::istream& in,
bool overwrite);
/** \brief parse C++ stream
*
* Parses C++ stream and build hierarchical config structure.
......@@ -85,9 +102,13 @@ namespace Dune {
* \param in The stream to parse
* \param srcname Name of the configuration source for error
* messages, "stdin" or a filename.
* \param overwrite Whether to overwrite already existing values.
* If false, values in the stream will be ignored
* if the key is already present.
*/
void parseStream(std::istream& in,
const std::string srcname = "stream");
const std::string srcname = "stream",
bool overwrite = true);
/** \brief parse file
......@@ -95,8 +116,11 @@ namespace Dune {
* Parses file with given name and build hierarchical config structure.
*
* \param file filename
* \param overwrite Whether to overwrite already existing values.
* If false, values in the stream will be ignored
* if the key is already present.
*/
void parseFile(std::string file);
void parseFile(std::string file, bool overwrite = true);
/** \brief parse command line
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment