views:

342

answers:

3

What is a fairly standard way for storing application settings, mainly for windows but also easy to move over to other platforms.

There's basically 4 groups of settings I want to have:

  • Global settings, affects all users, and may be moved between machines
  • Global system settings, affects all users, but specific to that system (eg defaults for that system, eg graphics options)
  • User settings, user settings that are moved between systems (eg sound volume)
  • User system settings, user settings specific to that system (eg graphics options that are hardware dependent)

Each level overrides the previous level, allowing for "global settings" to be largly the applications defaults, with user settings storing the options the user chose. The first two will basically be defaults where there is no user setting (eg for a new user).

I considered implementing a set of functions, which I could then implement for the different systems (likely to be through ini files), but is this the best way?

(c++)

namespace config
{
    void Init(const std::string &AppName);
    //updates config for keys/sections that don't exist (ie don't overwrite changes by advanced users by rewriting the entire file)
    void Defaults          (std::map<std::string,std::map<std::string,std::string> > &Map);
    void SystemDefaults    (std::map<std::string,std::map<std::string,std::string> > &Map);

    void Set               (const std::string &Section, const std::string &Key, const std::string &Value);
    void SetSystem         (const std::string &Section, const std::string &Key, const std::string &Value);

    void SetUser           (const std::string &Section, const std::string &Key, const std::string &Value);
    void SetUserSystem     (const std::string &Section, const std::string &Key, const std::string &Value);

    std::string GetValue   (const std::string &Section, const std::string &Key);
}

I know windows has a set of directories for such settings, but are these the correct dirs for my needs?

EDIT: I would rather go with files (ini or xml), rather than using say the windows registery. However wheres the best places to put these config files under each OS?

Under Vista I found these, which seem to fit my groups, however what of older windows versions (I need to support win2000, XP, etc), and does mac/linux have there own simelar folders?

  • Global settings - <SYSDRIVE>\Users\Default\Appdata\Roaming
  • Global system settings - <SYSDRIVE>\Users\Default\Appdata\Local
  • User settings - <SYSDRIVE>\Users\<USER>\AppData\Roaming
  • User system settings - <SYSDRIVE>\Users\<USER>\AppData\Local
A: 

There are (at least) three reasonable choices:

Registry: This is my least favorite because of portability and relative opacity.

Environment variables: I recommend using one (just one) that points to a place where your material is kept - an "installation directory" or some such.

Files: Both a/the user-home directory (or in a subdirectory thereof) and project/product directory are suitable for storing things.

You might want to use a simple keyword=value paradigm, and basic rules so your variables - settings - can be read by more than one type of code very easily. For example, I typically use the Java paradigm for Property files and use matching behavior C code so both my codelines can easily read the settings.

Richard T
Well files are my prefered way, since the same stuff will work on all platforms with little work.The main problem is where the files should be, and how to provide a clean interface(eg microsoft doesn't seem to want apps working with files in the apps own directory...id be happy with %username%.ini).
Fire Lancer
+4  A: 

If you are a boost user, you might take a look at the program options library, it supports using config files as well as environment variables and (of course) command line options.

It is designed to be portable, so that should ease your cross-platform headaches.

ceretullis