INI file parsing and serialization utilities.
- INI file loading/saving
- Section-based configuration
- Key-value pair management
- Comment support (; prefix)
- Automatic whitespace trimming
- Section/key enumeration
- Simple read/write interface
INIParser(const std::string& filename_a)- Constructor with filenamebool Load()- Loads INI filebool Save() const- Saves changes to INI filestd::string GetValue(const std::string& section_a, const std::string& key_a) const- Gets value by section and keyvoid SetValue(const std::string& section_a, const std::string& key_a, const std::string& value_a)- Sets valuestd::vector<std::string> GetSections() const- Gets all section namesstd::map<std::string, std::string> GetKeys(const std::string& section_a) const- Gets all key-value pairs in section
#include <CUtils/INIParser.hpp>
int main()
{
// Create parser and load file
CUtils::INIParser parser_("config.ini");
if (!parser_.Load())
{
std::cerr << "Failed to load config.ini" << std::endl;
return 1;
}
// Read values
std::string title_ = parser_.GetValue("App", "Title");
int width_ = std::stoi(parser_.GetValue("Window", "Width"));
// Modify values
parser_.SetValue("Window", "Fullscreen", "true");
// Add new section and key
parser_.SetValue("Audio", "Volume", "80");
// List sections
auto sections_ = parser_.GetSections();
for (const auto& section_ : sections_)
{
std::cout << "Section: " << section_ << std::endl;
}
// Save changes
if (!parser_.Save())
{
std::cerr << "Failed to save config" << std::endl;
}
return 0;
}Утилиты для чтения и записи INI-файлов.
- Загрузка/сохранение INI-файлов
- Работа с секциями конфигурации
- Управление ключами-значениями
- Поддержка комментариев (префикс ;)
- Автоматическое удаление пробелов
- Перечисление секций и ключей
- Простой интерфейс чтения/записи
INIParser(const std::string& filename_a)- Конструктор с именем файлаbool Load()- Загружает INI-файлbool Save() const- Сохраняет изменения в файлstd::string GetValue(const std::string& section_a, const std::string& key_a) const- Получает значение по секции и ключуvoid SetValue(const std::string& section_a, const std::string& key_a, const std::string& value_a)- Устанавливает значениеstd::vector<std::string> GetSections() const- Возвращает все имена секцийstd::map<std::string, std::string> GetKeys(const std::string& section_a) const- Возвращает все пары ключ-значение в секции
#include <CUtils/INIParser.hpp>
int main()
{
// Создайть парсер и загрузить файл
CUtils::INIParser parser_("config.ini");
if (!parser_.Load())
{
std::cerr << "Failed to load config.ini" << std::endl;
return 1;
}
// Прочитать значения
std::string title_ = parser_.GetValue("App", "Title");
int width_ = std::stoi(parser_.GetValue("Window", "Width"));
// Модифицировать значения
parser_.SetValue("Window", "Fullscreen", "true");
// Добавить новую секцию и ключ
parser_.SetValue("Audio", "Volume", "80");
// Список секций
auto sections_ = parser_.GetSections();
for (const auto& section_ : sections_)
{
std::cout << "Секция: " << section_ << std::endl;
}
// Сохранить изменения
if (!parser_.Save())
{
std::cerr << "Не удалось сохранить конфигурацию" << std::endl;
}
return 0;
}