开发者

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

开发者 https://www.devze.com 2023-01-31 14:53 出处:网络
I would like to store my application\'s settings in a configuration file开发者_StackOverflow.Under Linux (and Mac?) this (might) be /home/user/.config/app.conf while under Windows it (might) be \"C:\\

I would like to store my application's settings in a configuration file开发者_StackOverflow. 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?


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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消