Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some common way to write and read config files?

I need my program to create and edit a config file, which would contain information about set of objects, and than read it at every execution. Is there some sort of guideline for config style that i can use?

I'm using C++ on windows.

like image 538
Blin Avatar asked Apr 16 '10 18:04

Blin


2 Answers

I recommend checking out boost::property_tree.

The Property Tree library provides a data structure that stores an arbitrarily deeply nested tree of values, indexed at each level by some key. Each node of the tree stores its own value, plus an ordered list of its subnodes and their keys. The tree allows easy access to any of its nodes by means of a path, which is a concatenation of multiple keys.

Additionally, it contains parsers and generators for XML, INI, and JSON, so you can save/load in the format of your choice.

like image 90
thekidder Avatar answered Nov 01 '22 20:11

thekidder


It largely depends on the language, platform and the scope of your config files. There's for example the properties files in Java world for configuration, and others already mentioned here such as YAML.

XML is generally frowned upon for configuration, since it's very verbose. You still find it in many applications, Web frameworks, etc.

I think the best practice would be to choose the right configuration format for the job at hand. You can evaluate and try them for yourself, while considering these pointers:

  1. What's the standard? (e.g. ini files in Windows, properties files in Java)
  2. Is there native support in my language, or do I have to roll my own implementation?
  3. Can my configuration format easily describe what I want to store as configuration?

I'm sure you could think of other considerations. If you update your question to clarify the scope, you'll get more useful answers.

like image 39
Cesar Avatar answered Nov 01 '22 22:11

Cesar