A configuration file consists of sections containing key - value pairs or arbitrary text:
Sections
The begin of section is identified by a single line in the format [section-name], without leading/trailing white-space.
A section ends with:
Keys and values
A key-value pair consists of a single line and has the format key=value, without leading white-space and optional white-space around `='. A pair of double quotes (") enclosing value is removed.
Comments
Comments start with '#' and apply till the end of the line.
link (man 2 link)
or another appropriate method to keep a copy of the original file.
Definition at line 86 of file osbcfgfile.h.
Behavior flags for nextLine(), getValue() and getSection().
Definition at line 89 of file osbcfgfile.h.
OSB_LIB::CfgFile::CfgFile | ( | ) |
Empty default constructor.
Clears flags_, i.e., sets them to 0.
OSB_LIB::CfgFile::CfgFile | ( | const std::string & | path | ) | [explicit] |
Constructor with path.
Opens the configuration file pointed to by path.
path | Path of the configuration file. |
OSB_LIB::CfgFile::~CfgFile | ( | ) |
Destructor: closes the input stream.
OSB_LIB::CfgFile::CfgFile | ( | const CfgFile & | ) | [private] |
bool OSB_LIB::CfgFile::addKey | ( | const std::string & | section, | |
const std::string & | key, | |||
const std::string & | value | |||
) |
Add or change a key/value pair.
section | Name of section (without []). | |
key | Key to add. | |
value | Value for key, it may contain a comment. |
key = value
to section:
void OSB_LIB::CfgFile::close | ( | ) |
Close the input stream.
bool OSB_LIB::CfgFile::comments | ( | ) | const |
Include comments?
bool OSB_LIB::CfgFile::copy | ( | std::ofstream & | dest, | |
size_t | first, | |||
size_t | last = (size_t)-1 | |||
) | [private] |
const std::string& OSB_LIB::CfgFile::curLine | ( | ) | const |
Get the current line.
size_t OSB_LIB::CfgFile::curLineNo | ( | ) | const |
Get the current line number.
void OSB_LIB::CfgFile::delComment | ( | std::string & | line | ) | const [private] |
Strip comment from one line.
bool OSB_LIB::CfgFile::delKey | ( | const std::string & | section, | |
const std::string & | key | |||
) |
Delete a key.
section | Name of section (without []). | |
key | Key to delete. |
bool OSB_LIB::CfgFile::emptyLines | ( | ) | const |
Include empty lines?
bool OSB_LIB::CfgFile::eoSection | ( | ) | const |
Check if the end of a section has been reached.
int OSB_LIB::CfgFile::flags | ( | ) | const |
Get the flags.
bool OSB_LIB::CfgFile::getSection | ( | std::string & | dest, | |
const std::string & | section, | |||
size_t * | lineNo = 0 | |||
) |
Get the contents of a whole section.
dest | Gets the section's content. | |
section | Name of section to retrieve (without []). | |
lineNo | If not 0: gets the line number of the section's content. |
bool OSB_LIB::CfgFile::getValue | ( | std::string & | dest, | |
const std::string & | section, | |||
const std::string & | key | |||
) |
Get a configuration value.
dest | Gets the key's value. | |
section | Name of section with the key (without []). | |
key | Key to search. |
bool OSB_LIB::CfgFile::gotoLine | ( | size_t | lineNo | ) |
Goto a line number.
bool OSB_LIB::CfgFile::isOpen | ( | ) |
Input stream open?
bool OSB_LIB::CfgFile::nextLine | ( | bool | applyFlags = true |
) |
bool OSB_LIB::CfgFile::open | ( | const std::string & | p | ) |
Open the input stream.
p | Path to the configuration file. |
const char* OSB_LIB::CfgFile::path | ( | ) | const |
Path of configuration file.
bool OSB_LIB::CfgFile::replace | ( | const std::string & | replacePath | ) | [private] |
Replace configuration file.
replacePath | Full path name of the replacing file. |
bool OSB_LIB::CfgFile::rewind | ( | ) |
Rewind to the beginning of the file.
static const std::string& OSB_LIB::CfgFile::sectionEnd | ( | ) | [static] |
End of section marker: "[end]".
bool OSB_LIB::CfgFile::seekKey | ( | const std::string & | section, | |
const std::string & | key | |||
) |
Go to the line with a key.
section | Name of section (without []). | |
key | Key to search. |
bool OSB_LIB::CfgFile::seekSection | ( | const std::string & | section | ) |
Go to the begin of section data.
section | Name of section (without []). |
std::vector<std::string> content; CfgGile cfg("cfg.txt"); if (!cfg.seekSection("domains")) { // error message, return } // copy all lines of the section into `content' while (cfg.nextLine() && !cfg.eoSection()) { content.push_back(cfg.curLine()); }
void OSB_LIB::CfgFile::setFlags | ( | int | flags | ) |
Set the flags.
const std::ifstream& OSB_LIB::CfgFile::stream | ( | ) | const |
Read access to input stream.
void OSB_LIB::CfgFile::trimValue | ( | std::string & | value | ) |
Remove white-space and enclosing quotes from value.
value | Value to trim. |
bool OSB_LIB::CfgFile::wsOnly | ( | const std::string & | line | ) | const [private] |
Check if a line consists of white-space only.
std::string OSB_LIB::CfgFile::curLine_ [private] |
size_t OSB_LIB::CfgFile::curLineNo_ [private] |
Flags OSB_LIB::CfgFile::flags_ [private] |
std::ifstream OSB_LIB::CfgFile::is_ [private] |
std::string OSB_LIB::CfgFile::path_ [private] |