views:

320

answers:

9

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.

A: 

What platform + language?

Is this a per-user type config file holding permanent settings, or a data file for each run of the program?

Martin Beckett
Not to mention the requirements of the configuration settings themselves. Storing something like a username and password would have different requirements to storing the window state.
dash-tom-bang
+2  A: 

The most common format is the INI File format. Most mdoern languages should have something built in to handle these, otherwise I'm sure you'd be able to find a third-party library.

Failing this, you could use XML, and if your language supports it, serializing to and deserializing from these XML files. If you don't want to/can't use XML, most languages will have a default format they use, such as a pure binary dump of the data in an object.

Slokun
+1  A: 

YAML is a very popular solution for creating configuration files. It is used prominently by Ruby on Rails and Google AppEngine, for instance. YAML libraries are available for many if not most languages.

Adam Crossland
A: 

In the Java world, a properties file is pretty easy to use: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

As the others have said, you would need to describe your environment to get the best recommendation.

Thomas Laresch
+1  A: 

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.

Cesar
+3  A: 

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.

thekidder
+1  A: 

If you're working with Qt (based on your other questions),

look at the QSettings class. I've been using these to set a lot of persistent info, such as last visited directory, and which options were last used.

Will
A: 

boost::program_options has some capacity to read ini files.

What is nice is that is allow to unify reading ini file and command line options. As Windows program goes, that may not be seen as such an interresting future, but for my unix app it was a killer feature.

However, boost::po does not allow editing or writing of ini files, hence you'll probably be better off with boost::property_tree.

kriss
A: 

There is a free, cross-platform library for configuration file management, called libconfig. It supports very nice Yaml-based grammar, and supports user-defined grammar too. It also has many other computing language bindings. See it's homepage: http://www.hyperrealm.com/libconfig/ .

mbaitoff