Reputation: 30145
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:
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?
Upvotes: 7
Views: 2767
Reputation: 158364
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.
Upvotes: 7
Reputation: 82535
See also Where should cross-platform apps keep their data?
Upvotes: 3
Reputation: 4665
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.
Upvotes: 1