Giter Site home page Giter Site logo

cppcfg's Introduction

cppcfg ⚙️

Simple C++ library for parsing custom .cfg files.

About 📘

Hey! I always had a need to be able to add config files to my programs. I just like to have an ability to change some values in file and see the program process them in many different ways. But this always meant that I had to write myself a .cfg file parser to load those values... So I decided to make this library. This is my first C++ library released publicly so it might be kinda bad but I will try to improve it as I progress with my coding skills lmao.

✨ My website: https://febru.me/

forthebadge forthebadge

Usage 🛠️

Installation:

  • Download the cppcfg.hpp file and place it in include directory.
  • Download the cppcfg.cpp file and place it in src directory.
  • Create new ConfigurationFile object like this. Values from the file will be automatically imported.
ConfigurationFile config("config.cfg");
ConfigurationFile settings("settings.cfg", true);

NOTE: If the second parameter is true, all error messages will be suppressed.

Available methods:

  • .reload() - Reloads values from file. Returns true if file exists, false if not.
  • .getString(std::string key) - Returns string value of key.
  • .getInt(std::string key) - Returns integer value of key.
  • .getDouble(std::string key) - Returns double value of key.
  • .getFloat(std::string key) - Returns float value of key.
  • .getChar(std::string key) - Returns char value of key.
  • .getBool(std::string key) - Returns bool value of key.

Syntax of .cfg files 📝

If you want to create a config file and be able to parse it using this library, you must stick to some rules.

  • In one line there can only be one pair: <key>=<value>
  • You can only use a-z,A-Z,0-9 symbols when defining keys. No spaces allowed. You can use underscores tho.
  • Do not use spaces between key and '=' and between '=' and value.
  • Strings have to be put into "". Chars have to be put into ''.
  • When creating decimal values, you can put f at the end to make them float, not double.
  • To create boolean value, just type true or false.
  • Blank lines are allowed.
  • Lines beginning with # symbol are treated as comments and are skipped.
  • Lines containing errors and are skipped. However, you will see an error message.

If this is confusing, don't be upset. Just look at the example shown below.

Example code ⌨️

config.cfg

# Exemplary config.

hello="world"
ultimateAnswerToLifeTheUniverseAndEverything=42
someRandomNumber=21.37
someRandomNumberButFloat=326.23f
idkJustAChar=';'
isThisRepoAnyGood=true

# Some more values...
check=false
fun_number=82471.30f

# This library only cares about first and last char of your string to be ".
# This means that you don't have to escape special characters.
hey_look="you dont have to escape special chars here / | " "

main.cpp

#include <iostream>
#include "cppcfg.hpp"

int main()
{
    ConfigurationFile config("config.cfg");
    std::cout << config.getString("hello") << "\n";
    std::cout << config.getInt("ultimateAnswerToLifeTheUniverseAndEverything") << "\n";
    std::cout << config.getDouble("someRandomNumber") << "\n";
    std::cout << config.getFloat("someRandomNumberButFloat") << "\n";
    std::cout << config.getChar("idkJustAChar") << "\n";
    std::cout << config.getBool("isThisRepoAnyGood") << "\n";
}

Output:

world
42
21.37
326.23
;
1

cppcfg's People

Contributors

februu avatar

Stargazers

Fernando Mumbach avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.