CXI
Loading...
Searching...
No Matches
Public Member Functions | List of all members
Config Class Reference

Reads and parses configuration files and provides methods to access configuration items. More...

Public Member Functions

 Config (void)
 Creates empty configuration object.
 
 Config (std::string filename)
 
void dump (void)
 Dumps the current configuration to stdout.
 
bool exist (std::string key) const
 
void add (std::string key, const ByteArray &value)
 
void addString (std::string key, std::string value)
 
void addInt (std::string key, int value)
 
ByteArray get (std::string key, ByteArray def) const
 
std::string getString (std::string key, std::string def)
 
int getInt (std::string key, int def)
 
std::vector< std::string > getStringValues (std::string key) const
 

Detailed Description

Reads and parses configuration files and provides methods to access configuration items.

The Config class parses a given configuration file and provides the values.

Valid lines have to be structured according the scheme:

<KEY> = <VALUE>

Optionally multiple values can be assigned to a key either separated by a space or tabulator:

<KEY> = <VALUE1> <VALUE2> <VALUE3>

or spread over multiple lines each ended by a backslash:

<KEY> = <VALUE1> \
<VALUE2> \
<VALUE3>

Rules:

  • leading and trailing blanks are stripped on keys and values.
  • a key may not contain blanks.
  • a configuration value may contain blanks (e.g. 'Device = c:/program files/myapp/config').
  • backward slashes are used to indicate that a value is continued on the next line.
  • a backward slash may not follow immediately to a value, at least one blank has to separate value and backward slash.
  • backward slashes may not be used within values, even on Windows forward slashes have to be used within path names.
  • a char ('#') may be used to comment out the current line.
  • a char may only be placed in front of a key.
  • invalid lines are ignored.

A sample configuration file looks like this:

# This is a sample CXI configuration file
LogFile = d:/temp/cxi.log
LogLevel = 3
LogSize = 1MB
# dedicated device
#Device = PCI:0
# cluster
Device = PCI:0 \
192.168.4.183 \
192.168.4.184 \
192.168.4.185
ConnectTimeout = 5000
Timeout = 60000
Definition: cxi.cpp:173

The following example illustrates the usage of the Config class:

// create configuration object from config file
Config config = Config("d:/temp/cxi.cfg");
// output configuration values for debug purposes
config.dump();
// get a integer value (-1 will be returned if value is not found)
int timeout = config.getInt("Timeout", -1);
// get a multiline string value
vector<string> devices = config.getStringValues("Device");
// add a new value to configuration object
config.addInt("LogLevel", 4);
// create Cxi object with the given configuration object
cxi = new Cxi(config);
Reads and parses configuration files and provides methods to access configuration items.
Definition: sw/cxi_api_c/def/config.h:28
int getInt(std::string key, int def)
Definition: config.cpp:224
void addInt(std::string key, int value)
Definition: config.cpp:181
Config(void)
Creates empty configuration object.
Definition: config.cpp:127
void dump(void)
Dumps the current configuration to stdout.
Definition: config.cpp:294
std::vector< std::string > getStringValues(std::string key) const
Definition: config.cpp:256
Command interface to CXI firmware module. Handles all communication with the CryptoServer.
Definition: sw/cxi_api_c/def/cxi.h:60

Constructor & Destructor Documentation

◆ Config()

Config ( std::string  filename)

Parses given configuration file and creates configuration object from it.

Parameters
filenameConfiguration file (including path) to be parsed.

Member Function Documentation

◆ exist()

bool exist ( std::string  key) const

Checks whether the desired item exists.

Parameters
keyConfigItem key to be checked.

◆ add()

void add ( std::string  key,
const ByteArray value 
)

Adds a configuration item to the configuration object. If the given key already exists, the existing item will be replaced.

Parameters
keykey specifier of the item to be added to the configuration object.
valuevalue of the item to be added to the configuration object.

◆ addString()

void addString ( std::string  key,
std::string  value 
)

Adds a configuration item to the configuration object. If the given key already exists, the existing item will be replaced.

Parameters
keykey specifier of the item to be added to the configuration object.
valuevalue of the item to be added to the configuration object.

◆ addInt()

void addInt ( std::string  key,
int  value 
)

Adds a configuration item to the configuration object. If the given key already exists, the existing item will be replaced.

Parameters
keykey specifier of the item to be added to the configuration object.
valuevalue of the item to be added to the configuration object.

◆ get()

ByteArray get ( std::string  key,
ByteArray  def 
) const

Returns the value for the given key or the default value if the key doesn't exist.

Parameters
keykey to be returned.
defdefault value returned if the key doesn't exist.

◆ getString()

std::string getString ( std::string  key,
std::string  def 
)

Returns the specified item value as string or the default value if the key doesn't exist.

Parameters
keykey of item to be returned.
defdefault value returned if the key doesn't exist.

◆ getInt()

int getInt ( std::string  key,
int  def 
)

Returns the specified item value as integer.

Parameters
keykey of item to be returned.
defdefault value returned if the key doesn't exist.

◆ getStringValues()

std::vector< std::string > getStringValues ( std::string  key) const

Returns the specified multiple item value as array (vector) of strings. Use this method in case multiple values have been assigned to one key (e.g. Devices = PCI:0 192.168.4.183 192.168.4.185)

Parameters
keykey of item to be returned.