PIP 5.6.1
Platform-Independent Primitives
Classes | Public Member Functions | List of all members
PIConfig Class Reference

Parser and writer for configuration files with tree structure supportPIConfig provides functionality to read, write and manipulate configuration files in a tree-like structure. Supports dotted paths, INI-style section prefixes, multiline values and include entries resolved during parsing. More...

#include <piconfig.h>

Classes

class  Branch
 Branch class - container for Entry objects. More...
 
class  Entry
 Node of the parsed configuration tree. More...
 

Public Member Functions

 PIConfig (const PIString &path, PIIODevice::DeviceMode mode=PIIODevice::ReadWrite)
 Opens and parses configuration file at "path".
 
 PIConfig (PIString *string, PIIODevice::DeviceMode mode=PIIODevice::ReadWrite)
 Opens and parses configuration stored in "string".
 
 PIConfig (PIIODevice *device=nullptr, PIIODevice::DeviceMode mode=PIIODevice::ReadWrite)
 Opens and parses configuration from custom device "device".
 
 ~PIConfig ()
 Destroys the parser and releases owned devices.
 
bool open (const PIString &path, PIIODevice::DeviceMode mode=PIIODevice::ReadWrite)
 Opens and parses configuration file at "path".
 
bool open (PIString *string, PIIODevice::DeviceMode mode=PIIODevice::ReadWrite)
 Opens and parses configuration stored in "string".
 
bool open (PIIODevice *device, PIIODevice::DeviceMode mode=PIIODevice::ReadWrite)
 Opens and parses configuration from custom device "device".
 
bool isOpened () const
 Returns whether a backing device is currently opened.
 
EntrygetValue (const PIString &vname, const PIString &def=PIString(), bool *exists=0)
 Resolves top-level path "vname". More...
 
Branch getValues (const PIString &vname)
 Get top-level entry with name "vname" and default value "def". More...
 
void setValue (const PIString &name, const PIString &value, const PIString &type="s", bool write=true)
 Sets or creates top-level path "name", stores "value", assigns type mark "type" and optionally writes changes immediately.
 
void setValue (const PIString &name, const PIStringList &value, bool write=true)
 Stores string list and marks type as "l".
 
void setValue (const PIString &name, const char *value, bool write=true)
 Stores C-string and marks type as "s".
 
void setValue (const PIString &name, const bool value, bool write=true)
 Stores boolean value and marks type as "b".
 
void setValue (const PIString &name, const short value, bool write=true)
 Stores numeric value and marks type as "n".
 
void setValue (const PIString &name, const int value, bool write=true)
 Stores numeric value and marks type as "n".
 
void setValue (const PIString &name, const long value, bool write=true)
 Stores numeric value and marks type as "n".
 
void setValue (const PIString &name, const uchar value, bool write=true)
 Stores numeric value and marks type as "n".
 
void setValue (const PIString &name, const ushort value, bool write=true)
 Stores numeric value and marks type as "n".
 
void setValue (const PIString &name, const uint value, bool write=true)
 Stores numeric value and marks type as "n".
 
void setValue (const PIString &name, const ulong value, bool write=true)
 Stores numeric value and marks type as "n".
 
void setValue (const PIString &name, const float value, bool write=true)
 Stores floating-point value and marks type as "f".
 
void setValue (const PIString &name, const double value, bool write=true)
 Stores floating-point value and marks type as "f".
 
EntryrootEntry ()
 Returns root entry of the parsed tree.
 
int entriesCount () const
 Returns total number of parsed entries below the root.
 
bool isEntryExists (const PIString &name) const
 Returns true if any parsed entry path contains name "name".
 
Branch allTree ()
 Returns all direct children of the root entry.
 
Branch allLeaves ()
 Returns all stored leaves and valued branch entries sorted by source order.
 
int entryIndex (const PIString &name)
 Returns index of path "name" inside allLeaves(), or -1.
 
PIString getName (uint number)
 Returns entry name by allLeaves() index.
 
PIString getValueByIndex (uint number)
 Returns entry value by allLeaves() index.
 
PIChar getType (uint number)
 Returns entry type mark by allLeaves() index.
 
PIString getComment (uint number)
 Returns entry comment by allLeaves() index.
 
void addEntry (const PIString &name, const PIString &value, const PIString &type="s", bool write=true)
 Creates new path "name" when it does not already exist and optionally writes changes immediately.
 
void setName (uint number, const PIString &name, bool write=true)
 Renames entry referenced by allLeaves() index "number".
 
void setValue (uint number, const PIString &value, bool write=true)
 Replaces stored value of entry referenced by allLeaves() index "number".
 
void setType (uint number, const PIString &type, bool write=true)
 Replaces type mark of entry referenced by allLeaves() index "number".
 
void setComment (uint number, const PIString &comment, bool write=true)
 Replaces comment of entry referenced by allLeaves() index "number".
 
void removeEntry (const PIString &name, bool write=true)
 Removes entry path "name" and its subtree when needed.
 
void removeEntry (uint number, bool write=true)
 Removes entry referenced by allLeaves() index "number".
 
void clear ()
 Removes all parsed entries and clears the backing device content.
 
void readAll ()
 Rebuilds internal tree from current device contents.
 
void writeAll ()
 Writes current tree back to the device and reparses it.
 
const PIStringdelimiter () const
 Returns current path delimiter, "." by default.
 
void setDelimiter (const PIString &d)
 Sets path delimiter for subsequent parsing and reparses the device.
 

Detailed Description

Parser and writer for configuration files with tree structure support

PIConfig provides functionality to read, write and manipulate configuration files in a tree-like structure. Supports dotted paths, INI-style section prefixes, multiline values and include entries resolved during parsing.

Configuration file.

This class provide handle access to configuration file.

Synopsis

PIConfig reads configuration file and create internal dendritic representation of all entries of this file. You can easily read some values and write new.

PIConfig supports also INI-style files with sections "[section]". In this case line with section name interpret as prefix to the next lines. For example, these configs are equal:

ser.device = /dev/ttyS0
ser.speed = 115200
debug = true
[ser]
device = /dev/ttyS0
speed = 115200
[]
debug = true

You can use multiline values ends with " \" @code value = start \ #s comment _mid \ _end \endcode In this example value = "start_mid_end"

Concepts

Each node of internal tree has type PIConfig::Entry. PIConfig has one root element rootEntry(). Any entry of configuration file is a child of this element.

Member Function Documentation

◆ getValue()

PIConfig::Entry & PIConfig::getValue ( const PIString vname,
const PIString def = PIString(),
bool *  exists = 0 
)

Resolves top-level path "vname".

If lookup fails, returns a shared default entry filled with "def" and sets exists to false when provided.

◆ getValues()

PIConfig::Branch PIConfig::getValues ( const PIString vname)

Get top-level entry with name "vname" and default value "def".

Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Get top-level entry with name "vname" and default value "def" Returns top-level entries whose names contain substring "vname".