Config is a very minimalisc config class, which allows to parse simple Key-Value pairs from a text file. This values can then be accessed by their particular key via the subscript operator: [<key>]
.
After cloning, execute git submodule update --init --recursive --remote
in the Repo to initialize and download all dependencies.
dependencies
contains a git-submodule to the Exception-Baseclass.src
contains the source code (Config
class andConfigExceptions
classes).test
contains a test application and a test config-file. The latter can be edited to test the correct functionality.
C++14 is required for compilation.
In order to use Config, <path>/Config/src/Config.hpp
needs to be included.
In order to compile Config, following files need to be compiled and linked:
<path>/Config/dependencies/Exception/src/Exception.cpp
<path>/Config/src/ConfigExceptions.cpp
<path>/Config/src/Config.cpp
- A config file is read line by line; each line representing one key-value pair.
- The key starts at the beginning of the line, and lasts until the first occurence of our delimiter.
- The standard delimiter is
=
.
- The standard delimiter is
- The value starts after the delimiter and lasts until the end of the line, which is marked by any EOL character (
\n
or\r
).- A value can also be empty.
- All keys and values are trimmed off their leading and trailing spaces. Spaces in between are being preserved.
- A valid commment gets introduced whenever the first non-space character (after trimming) equals the comment delimiter.
- The standard comment delimiter is
#
. - The line can either be a key-value pair, or a comment; comments after a value are not possible.
- The standard comment delimiter is
Example of a valid config file:
# This is a comment
string=test string # This is no comment and just gets appended to the rest of the string
int=2147483647
uint=4294967295
long=9223372036854775807
ulong=18446744073709551615
longlong=9223372036854775807
ulonglong=18446744073709551615
float=1.337
double=1.337
ldouble=-1.337
bool=0
- Instantiate a new
Config
-Object with the path to the config file as parameter:Config config( "./config.cfg" );
.- The default delimiter is
=
and the default comment-delimiter is#
.
This can optionally be changed in the constructor:Config config( "<configPath>", '<delimiter>', '<commentDelimiter>' );
.
- The default delimiter is
- Access a value with one of the following functions (for example the subscript operator:
std::string value1 = config["value1"];
):std::string getString( std::string key )
orstd::string operator[]( std::string key )
int getInteger( std::string key )
unsigned int getUnsignedInteger( std::string key )
long getLong( std::string key )
unsigned long getUnsignedLong( std::string key )
long long getLongLong( std::string key )
unsigned long long getUnsignedLongLong( std::string key )
float getFloat( std::string key )
double getDouble( std::string key )
long double getLongDouble( std::string key )
bool getBool( std::string key ) // This tries to convert an integer to a bool (0 = false, everything else = true)
- Catch the exceptions to handle possible errors while using Config:
ConfigMalformedException
ConfigMissingException
ConfigKeyNotFoundException
NumericConfigValueMalformedException
All values are contained in a std::map<std::string, std::string>
. The container can be accessed via the public variable data
.
Example for iterating over all contained key-value pairs:
Config config( "./config.cfg" );
for ( std::map<std::string, std::string>::iterator it = config.data.begin(); it != config.data.end(); ++it )
{
std::cout << it->first << " = " << it->second << std::endl;
}
Copyright 2019 Jan-Eric Schober
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.