Skip to content

odeviceblack/Config-Library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Config Library (C++)

Lightweight, header-based configuration library for C++17, providing INI-style file parsing with type-safe value access and section/key management.


📌 Overview

This library provides:

  • Type-safe configuration values (bool, int, float, std::string)
  • INI file format with section support
  • Automatic type parsing and serialization
  • Comment preservation (full round-trip support)
  • Order-preserved sections and keys
  • Simple bind system for easy variable management
  • Header-only core (implementation optionally separated)

🧱 Structure


config.hpp     → Main library header (ConfigEntry, Config)
config.cpp     → Implementation (optional, can be header-only)


⚙️ Requirements

  • C++17 compiler
  • Standard Library (string, variant, unordered_map, vector, fstream)

No external dependencies.


🚀 Usage

Basic example

#include "config.hpp"

Config cfg("settings.ini");
cfg.Load();

// Bind or create entries
ConfigEntry& volume = cfg.Bind("volume", 0.75f, "Audio");
ConfigEntry& fullscreen = cfg.Bind("fullscreen", true);

// Read values
float vol = volume.Get<float>();        // 0.75
bool fs = fullscreen.Get<bool>();       // true

// Modify values
volume.Set(0.8f);
fullscreen.Set(false);

// Save back to file
cfg.Save();

Working with sections

Config cfg("game.ini");
cfg.Load();

// Bind with section
ConfigEntry& username = cfg.Bind("name", std::string("player"), "Account");
ConfigEntry& score = cfg.Bind("highscore", 0, "Stats");

// Check existence
if (cfg.HasSection("Account")) {
    // section exists
}

if (cfg.HasKey("name", "Account")) {
    std::string name = cfg.Bind("name", "", "Account").Get<std::string>();
}

INI file format

# Global section (no brackets)
fullscreen = true
language = english

[Audio]
volume = 0.8
muted = false

[Graphics]
resolution = 1920x1080
vsync = true

Type safety

ConfigEntry& entry = cfg.Bind("answer", 42);

int value = entry.Get<int>();        // OK: 42
float f = entry.Get<float>();        // Returns fallback 0.0f
bool b = entry.Get<bool>();          // Returns fallback false

// Check type before accessing
if (entry.IsInt()) {
    int x = entry.Get<int>();
}

Comment preservation (full round-trip)

// Original file content:
# This is a comment
fullscreen = true

[Audio]  # section comment
volume = 0.8  # inline comment

cfg.Load();
cfg.Bind("volume", 0.9f, "Audio").Set(0.9f);
cfg.Save();

// Output preserves comments:
# This is a comment
fullscreen = true

[Audio]  # section comment
volume = 0.9  # inline comment

🔧 API Reference

ConfigEntry

Method Description Set(const T& value) Sets the entry value (T must be bool, int, float, or std::string) Get(const T& fallback = {}) Returns value if type matches, otherwise fallback GetString(const char* fallback = "") Returns const char* (works for both const char* and std::string) GetSection() / GetKey() Returns section/key names IsBool() / IsInt() / IsFloat() / IsString() Type checking

Config

Method Description Config(const char* configName) Constructor with filename Load() Loads and parses INI file Save() Saves current values preserving comments Bind(key, defaultValue, section = "") Gets existing entry or creates new one HasSection(section) Checks if section exists HasKey(key, section = "") Checks if key exists CanInteract() Checks if file is readable/writable


🧠 Technical Characteristics

· Type-safe variant-based storage · No dynamic allocations for value storage (std::variant) · Comment preservation via line-based round-trip · Order-preserved sections and keys · Single-header or split implementation · File format: INI-compatible (section headers with [section], key-value pairs with =)


⚠️ Notes

· std::string is required (no STL-less version available) · Strings with spaces are preserved as-is (no quotes needed) · Boolean values: true/false (case-sensitive) · Comments: lines starting with # are preserved · Empty lines are preserved in round-trip mode


📄 License

MIT License


👤 Credits

· DeviceBlack — full implementation, design, and documentation

About

Lightweight, header-based configuration library for C++17, providing INI-style file parsing with type-safe value access and section/key management.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors