CARMA C++
carma::util::KeyValueConfigFile Class Reference

Common functions for manipulating key-value config files This class contains no state. More...

#include <carma/util/KeyValueConfigFile.h>

Public Member Functions

 KeyValueConfigFile ()
 Default constructor. More...
 
virtual ~KeyValueConfigFile ()
 Destructor. More...
 

Static Public Member Functions

static std::map< std::string,
std::string > 
load (const std::string &filename, const std::string &delimiter="=", const std::string &commentStr="#", const std::string &macroStartStr="${", const std::string &macroEndStr="}", const bool &useEnvironment=true)
 read a config file and return its values as a key-value map Lines which begin with commentStr are considered comments and are not processed. More...
 

Detailed Description

Common functions for manipulating key-value config files This class contains no state.

Definition at line 29 of file KeyValueConfigFile.h.

Constructor & Destructor Documentation

carma::util::KeyValueConfigFile::KeyValueConfigFile ( )

Default constructor.

virtual carma::util::KeyValueConfigFile::~KeyValueConfigFile ( )
virtual

Destructor.

Member Function Documentation

static std::map<std::string,std::string> carma::util::KeyValueConfigFile::load ( const std::string &  filename,
const std::string &  delimiter = "=",
const std::string &  commentStr = "#",
const std::string &  macroStartStr = "${",
const std::string &  macroEndStr = "}",
const bool &  useEnvironment = true 
)
static

read a config file and return its values as a key-value map Lines which begin with commentStr are considered comments and are not processed.

Lines with more than one delimiter are not processed. Tabs and blank spaces are removed from the beginning and end (but not the middle) of both keys and values. Macro expansion (a'la Makefiles) is used using macroStartStr and macroEndStr. For example, in a file with the following lines

foo = bar
baz = ${foo}.raz

This method will expand baz to bar.raz (assuming the default values of macroStartStr and macroEndStr are used). If a macro has not been defined previously in the config file, but is an environment variable, the value of the environment variable will be substituted unless the useEnvironment parameter is false config files may be included in config files by using the

include <filename>

directive. The path of the included file must be relative to the file which includes it. include directives may occur at any point in a config file; the result is just if the code in the included file had been inserted in place of the include directive (e.g., any variables defined prior to the include directive will be overwritten if they are defined again in the include file, any variables defined in the include file will be overwritten if they are redefined in the original file after the include directive

Parameters
filenamethe file to read
delimiterthe key-value delimiter to use
commentStrlines starting with this string (after stripping leading spaces will be treated as comments
macroStartStrstring indicating the start of a macro
macroEndStrstring indicating the end of a macro
useEnvironmentif true, search for an environment variable matching the macro and use its value if it exists
Returns
the key-value map
Exceptions
NotFoundExceptionif file cannot be read

The documentation for this class was generated from the following file: