CfgManipulator Build Status

CfgManipulator is a fast and powerful tool for working with configuration files for the C++ language.
It can read, create strings and sections, change the value of a string and much more.

Before starting, you should have installed

  • GCC 4.8 or higher

It can

  • Read the value of the strings and change it.
  • Create sections.
  • Format the file in its own style.
  • Detect errors.
  • Create strings and sections.
  • Read a string value and convert it to various data types.

How to use

  • Clone this repo.
  • Copy the files CfgManipulator.cpp and CfgManipulaor.hpp to your project folder.
  • Include the CfgManipulator.hpp file in your project.
  • Compile CfgManipulator.cpp along with other source files.

Demonstration

Demonstration

Code example

//main.cpp

#include "CfgManipulator.hpp"

#include <iostream>

using namespace std;

CfgManipulator::CfgFile settings;

int main() {
    settings.open("settings.cfg");

    /* You can also create a configuration file class and open it in one line:
     * CfgManipulator::CfgFile settings("settings.cfg");
    */

   cout << settings.read_int32("int32") << endl
        << settings.read_int64("int64") << endl
        << settings.read_bool("bool") << endl
        << settings.read_double("double") << endl;

    settings.create_string("line", "value");

    settings.create_section("section");

    settings.create_string("section", "line", "value");

    cout << settings.read("line") << endl;

    cout << settings.read("section", "line");

    settings.change_value("line", "another_value");

    settings.change_value("section", "line", "another_value");

    /* Let's say you have changed the value of a string and want to add changes to a file, 
     * then you will need to call "overwrite" function.
    */
    settings.overwrite();

    settings.close();

    return EXIT_SUCCESS;
}

# settings.cfg

int32 = "2147483647"
int64 = "9223372036854775807"
bool = "true"
double = "0.5"
line = "value" # comment

[section]
    line = "value" # comment

Compiling

  • g++ -std=c++11 -Wall -Werror main.cpp CfgManipulator.cpp

Explanations of functions

Function Explanation
void CfgFile::open(const std::string &path) Opens the configuration file.
void CfgFile::close() Closes the configuration file.
bool CfgFile::is_open() Returns true if the file is open, false if closed.
std::string CfgFile::read(const string &key) Reads the value of a string.
std::string CfgFile::read(const std::string &section, const std::string &key) Reads the value of a string from a specific section.
std::string CfgFile::get_path() Returns the path to the file.
void CfgFile::change_value(const string &key, const string &value) Changes the value of a string.
void CfgFile::change_value(const string &section, const string &key, const string &value) Changes the value of a string from a specific section.
void CfgFile::create_string(const std::string &key, const std::string &value) Creates a new string in the file.
void CfgFile::create_string(const std::string &section, const std::string &key, const std::string &value) Creates a new string in a specific section.
void CfgFile::create_section(const std::string &section) Creates a new section.
void CfgFile::overwrite() Overwrites the file.
std::int32_t CfgFile::read_int32(const string &key) Reads a string and converts its value to int32.
std::int64_t CfgFile::read_int64(const string &key) Reads a string and converts its value to int64.
bool CfgFile::read_bool(string str) Reads a string and converts its value to boolean.
double CfgFile::read_double(const string &key) Reads a string and converts its value to double.

Features that will be added in the future

  • Arrays

28.01.2022

  • Added the ability to create sections and strings.
  • Added the ability to convert string value into various data types
  • Fixed bugs.

License: MIT

If you like this project, give it a star ?⭐

GitHub

View Github