6

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?

Malvineous
  • 25,144
  • 16
  • 116
  • 151

4 Answers4

3

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.

Malvineous
  • 25,144
  • 16
  • 116
  • 151
2

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/.

Kyle Lutz
  • 7,966
  • 2
  • 20
  • 23
  • Try `QDesktopServices::storageLocation(QDesktopServices::DataLocation)` . See http://qt-project.org/doc/qt-4.8/qdesktopservices.html – Dan Nissenbaum May 05 '13 at 16:13
0

wxWidgets has a function you can call to get this, but for Unix, it's a bit outdated as it returns the home directory instead of the more common ~/.config

See: https://docs.wxwidgets.org/3.0/classwx_standard_paths.html#a7c7cf595d94d29147360d031647476b0 https://github.com/wxWidgets/wxWidgets/issues/9300

ferdymercury
  • 698
  • 4
  • 15
-1

I think the boost filesystem libraries should help. It has a platform independent path grammar.

sashang
  • 11,704
  • 6
  • 44
  • 58
  • 2
    What grammar do I use that will appear as /home/user/.config under Linux, but automatically become C:\Documents and Settings\username\App Data under Windows? – Malvineous Dec 20 '10 at 11:58
  • 1
    dear lord, *boost* being used for only finding a home directory? Talk about overkill. I don't want to be the one who has to compile that in MinGW. – Wyatt Ward Jan 03 '16 at 18:54