libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::ConfigFile Class Reference

Parses and manages an INI-style configuration file. More...

#include <cfg.hpp>

Collaboration diagram for mx::ConfigFile:
[legend]

Public Member Functions

 ConfigFile ()=default
 Default constructor — no file loaded.
 ConfigFile (const std::string &filex)
 Open and parse a configuration file.
 ~ConfigFile ()
 Destructor — closes any open file resources.
 ConfigFile (const ConfigFile &)=delete
ConfigFile & operator= (const ConfigFile &)=delete
 ConfigFile (ConfigFile &&)=delete
ConfigFile & operator= (ConfigFile &&)=delete
Item itemAtKey (const std::string &section, const std::string &key)
 Retrieve a configuration item by section and key.
void setItem (const std::string &section, const std::string &key, const std::string value)
 Insert or update a configuration item.
void loadFile (const std::string &f)
 Load (or reload) a configuration file from disk.
void saveFile (const std::string &f2)
 Save the current configuration to a file.
std::vector< std::string > splitByComma (const std::string &str)
 Split a string on comma delimiters.

Private Attributes

std::unordered_map< std::string, std::unordered_map< std::string, Item > > values
std::string filename

Detailed Description

Parses and manages an INI-style configuration file.

Sections are denoted by [SectionName] headers. Items within a section are stored as key=value pairs. The file can be loaded, queried, modified in memory, and saved back to disk.

Definition at line 37 of file cfg.hpp.

Constructor & Destructor Documentation

◆ ConfigFile() [1/4]

mx::ConfigFile::ConfigFile ( )
default

Default constructor — no file loaded.

Referenced by ConfigFile(), ConfigFile(), operator=(), and operator=().

◆ ConfigFile() [2/4]

mx::ConfigFile::ConfigFile ( const std::string & filex)

Open and parse a configuration file.

Parameters
filexPath to the configuration file.

Definition at line 104 of file cfg.cpp.

References filename, and loadFile().

◆ ~ConfigFile()

mx::ConfigFile::~ConfigFile ( )

Destructor — closes any open file resources.

Definition at line 108 of file cfg.cpp.

References filename, and saveFile().

◆ ConfigFile() [3/4]

mx::ConfigFile::ConfigFile ( const ConfigFile & )
delete

References ConfigFile().

◆ ConfigFile() [4/4]

mx::ConfigFile::ConfigFile ( ConfigFile && )
delete

References ConfigFile().

Member Function Documentation

◆ itemAtKey()

Item mx::ConfigFile::itemAtKey ( const std::string & section,
const std::string & key )

Retrieve a configuration item by section and key.

Parameters
sectionSection name (text inside [] brackets).
keyKey name within that section.
Returns
The matching Item, or a default-constructed one if not found.

Definition at line 77 of file cfg.cpp.

References values.

◆ loadFile()

void mx::ConfigFile::loadFile ( const std::string & f)

Load (or reload) a configuration file from disk.

Parameters
fPath to the file.

Definition at line 12 of file cfg.cpp.

References mx::Item::key, mx::Item::value, and values.

Referenced by ConfigFile().

◆ operator=() [1/2]

ConfigFile & mx::ConfigFile::operator= ( ConfigFile && )
delete

References ConfigFile().

◆ operator=() [2/2]

ConfigFile & mx::ConfigFile::operator= ( const ConfigFile & )
delete

References ConfigFile().

◆ saveFile()

void mx::ConfigFile::saveFile ( const std::string & f2)

Save the current configuration to a file.

Parameters
f2Destination file path.

Definition at line 46 of file cfg.cpp.

References mx::keys, and values.

Referenced by ~ConfigFile().

◆ setItem()

void mx::ConfigFile::setItem ( const std::string & section,
const std::string & key,
const std::string value )

Insert or update a configuration item.

Parameters
sectionSection name.
keyKey name.
valueNew value string.

Definition at line 86 of file cfg.cpp.

References values.

◆ splitByComma()

std::vector< std::string > mx::ConfigFile::splitByComma ( const std::string & str)

Split a string on comma delimiters.

Parameters
strSource string.
Returns
Vector of substrings between commas.

Definition at line 90 of file cfg.cpp.

Member Data Documentation

◆ filename

std::string mx::ConfigFile::filename
private

Definition at line 39 of file cfg.hpp.

Referenced by ConfigFile(), and ~ConfigFile().

◆ values

std::unordered_map<std::string, std::unordered_map<std::string, Item> > mx::ConfigFile::values
private

Definition at line 38 of file cfg.hpp.

Referenced by itemAtKey(), loadFile(), saveFile(), and setItem().


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