Code Monkey home page Code Monkey logo

raw_config_file_handling's Introduction

raw_config_file_handling

This small single header library allows handling of ini-style config-files. In addition to the standard ini-format, this library allows values with more than one parameter (separation with commas). Comments in the config-file are getting ignored. The standard comment-character is the semicolon (;), but also custom characters could be set. Example of this config-file format:

;Comment1
[GENERAL];Comment2
BALL_COUNT=2;Comment3

[BALL]
BALL_RGB_COLOR=0,0,255
...

Installation

  1. Include the raw_config_file_handling.hpp file in your project.
  2. Set up a struct that contains all possible configuration parameters:
struct Configuration { //program configuration
  //GENERAL-Section:
  int BallCount;

  //BALL-Section:
  vector<int> BallColorRGB;
};
  1. Inherit from the base class (RawConfigFileHandler) and implement setConfig/getConfig Methods and optional features (for a more advanced use, see the example folder):
class ConfigFileHandler : public RawConfigFileHandler {
public:
  Configuration ProgramConfiguration;

  //empty constructor:
  ConfigFileHandler() {}

  //create a method that assigns the RawConfigData (from base class) to the Configuration-struct:
  void getConfig() {
    if (readFile()) //reading raw file and check if successful
    {
      //looping through the RawConfigData vector array:
      for (size_t i = 0; i < RawConfigData.size(); i++) 
      {
        if (RawConfigData[i].Section == "GENERAL")
        {
          if (RawConfigData[i].Key == "BALL_COUNT")
          {
            //assigning the value to the program configuration parameter:
            ProgramConfiguration.BallCount = stoi(RawConfigData[i].Value[0]); 
          }
        }
        else if (RawConfigData[i].Section == "BALL")
        {
          if (RawConfigData[i].Key == "BALL_RGB_COLOR")
          {
            for (size_t j = 0; j < RawConfigData[i].Value.size(); j++)
            {
              ProgramConfiguration.BallColorRGB.push_back(stoi(RawConfigData[i].Value[j]));
            }
          }
        }
      }
    }
  }

  //create a method that assigns the Configuration-struct to the RawConfigData (in base class):
  void setConfig() {
    int i = 0;
    RawConfigData.clear();

    RawConfigData.push_back(ConfigDataStruct());
    RawConfigData[i].Section = "GENERAL";
    RawConfigData[i].Key = "BALL_COUNT";
    RawConfigData[i].Value.push_back(std::to_string((ProgramConfiguration.BallCount)));
    i++; //increment the vector array iterator of RawConfigData 

    RawConfigData.push_back(ConfigDataStruct());
    RawConfigData[i].Section = "BALL";
    RawConfigData[i].Key = "COLOR_RGB";
    for (size_t j = 0; j < ProgramConfiguration.BallColorRGB.size(); j++)
    {
      RawConfigData[i].Value.push_back(std::to_string(ProgramConfiguration.BallColorRGB[j]));
    }

    writeFile();
  }
};

Built With

Author

Daniel Duller - dadul96

License

This project/library is licensed under the MIT License - see the LICENSE file for details

raw_config_file_handling's People

Contributors

dadul96 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.