Code Monkey home page Code Monkey logo

config's Introduction

config

Simple C++ config reader and writer.

Format

# single line comment

string = "this is a string";
number = 123;
floating_number = 1.23;

# , without a following value will create an empty string in that place
# 3 for example has the index 4
array = [1, 2, , , 3, "4", 5.5] # the ; is optional after brackets

object = {
	name = "value within object";
	nr = 1;
	
	nested = {
		name = "nested object";
	}
	
	# careful, you cannot store values directly in objects
}

Variable, array and object names can be anything, except of double quotes ("). Spaces outside of strings will be skipped, so a name like "my var" will be parsed to "myvar". For strings you can also use single quotes. Currently there is no support for arrays of objects or multi dimensional arrays.

Usage

#include "Config.hpp"

void main(){
	Config cfg;
	
	try{
		// read cfg file, if possible
		if(!cfg.read("in.cfg")){
			std::cerr<<"Could not open config file!"<<std::endl;
			return 1;
		}
	}
	catch(std::invalid_argument& e){
		// this error can occur if the cfg file cannot be completly parsed
		// the parsed content until failure will be kept
		std::cerr<<"Error reading config file!"<<std::endl;
		std::cerr<<e.what()<<std::endl;
	}
	
	// read a value (this can cause an invalid_argument exception if cast is not possible
	// if a variable does not exists, it will be created
	std::string string = cfg["string"].getString(); // will always work
	int number = cfg["number"].getInt();
	float floating_number = cfg["floating_number"].getFloat();
	
	// read from object
	int arrayIndex1 = cfg["array"].getInt(1);
	std::string arrayIndex5 = cfg["array"].getString(5);
	
	// read from object
	int nr = cfg["object"]["nr"];
	
	// write variable
	cfg["new_string"] = string;
	
	// create array
	cfg["new_array"].set("value0", 0);
	cfg["new_array"].set("value1", 1);
	cfg["new_array"].set("value2", 5); // spaces will be inserted
	
	// create object
	cfg["new_object"]["foo"] = "bar";
	
	// write cfg file (wont keep comments)
	if(!cfg.write("out.cfg")){
		std:.cerr<<"Could not write config file!"<<std::endl;
	}
	
	// clear content
	cfg.clear();
}

The read() method can throw an std::invalid_argument exception, which signals that the config file read cannot be parsed completly. Everything until the error will be kept and what() returns in which line and at which character the error occured. If you store data in an variable that also contain child variables, it will be an objects. Which means all data in the parent object won't be stored on save.

Features that will be added in the future

  • keep comments on save
  • better parser error output
  • nested arrays
  • array of objects

Build

Simply compile with C++11 compiler. No dependencies required.

License

MIT

config's People

Contributors

kugelschieber avatar

Watchers

James Cloos 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.