Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining cross-platform path for config file (C/C++)

I would like to store my application's settings in a configuration file. Under Linux (and Mac?) this (might) be /home/user/.config/app.conf while under Windows it (might) be "C:\Documents and Settings\username\Application Data\app.conf". It can of course be stored elsewhere, so the only way to get the correct location is to use a platform-specific function.

Suffice it to say I don't wish to risk coding this myself and getting it wrong (because I lack access to some of these platforms for testing), so does anyone know if there are any well-tested cross-platform C/C++ libraries that can do this? A .h/.hpp file that uses a bunch of #defines would also be fine, as long as it's widely used.

I thought Boost's program options library might be able to (as it can load configuration files) but it doesn't seem able to.

Any suggestions?

like image 648
Malvineous Avatar asked Dec 20 '10 03:12

Malvineous


2 Answers

This came up again, so I decided to bite the bullet and create my own solution since the only existing ones are part of huge frameworks and impractical for small programs.

I have published the code at https://github.com/Malvineous/cfgpath

It is placed in the public domain so free to use by anyone for any purpose. It has no dependencies beyond the standard platform APIs. Just #include a single .h file and call one of the functions. The other files in the repository are just test code, you don't need these unless you want to make changes you intend to send to me (please do!)

Unfortunately as I said in my original post I don't have easy access to many platforms, so I hope I will get a few patches to add support for more platforms.

like image 95
Malvineous Avatar answered Oct 10 '22 13:10

Malvineous


Qt's QSettings class will do this for you.

On *nix the settings will be stored in $HOME/.config. On Windows the settings will be stored in the registry. On Mac the settings will be stored in $HOME/Library/Preferences/.

like image 38
Kyle Lutz Avatar answered Oct 10 '22 14:10

Kyle Lutz