1

I'm looking to store some "preferences" for my C++ application.

Under windows I know I have to use the "AppData" folder, but I need the equivalent for Linux and OsX.

Is there some library or portable way to get such information in C++ ?

Here is the code I use currently:

#ifdef VD_OS_WINDOWS
    LPWSTR wszPath = NULL;
    HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE, NULL, &wszPath);

    _bstr_t bstrPath(wszPath);
    std::string strPath((char*)bstrPath);
    CoTaskMemFree(wszPath);

    return strPath;
#else
    char* path = getenv("XDG_CONFIG_HOME");
    if (!path)
        getenv("HOME") + ".local/share";

    return string(path);
#endif

Thanks

ClubberLang
  • 1,624
  • 3
  • 21
  • 45

2 Answers2

2

If you happen to write a Qt application, there is the QSettings Class. The documentation says the following about this class:

The QSettings class provides persistent platform-independent application settings.

Users normally expect an application to remember its settings (window sizes and positions, options, etc.) across sessions. This information is often stored in the system registry on Windows, and in XML preferences files on Mac OS X. On Unix systems, in the absence of a standard, many applications (including the KDE applications) use INI text files.

This delivers IMHO the best "out-of-the-box" experience. And it's really platform independent.

An alternative would be boost::program_options or boost::property_tree. But the aim of these libraries is the data handling, not so much the storage. This means you would still need to detect the platform, and store the data in the correct location.

Community
  • 1
  • 1
user23573
  • 2,479
  • 1
  • 17
  • 36
  • Thanks... what I need is the "folder" where I can save the datas ! But without QT please ! Boost is fine ! – ClubberLang Nov 30 '15 at 21:02
  • @ChristopheDemez boost will not do that for you unfortunately. I don't know of any other library besides Qt that would. – user23573 Nov 30 '15 at 21:08
0

Historically on Linux the program stores its configuration data in a hidden file or folder (one beginning with a dot .) in the $HOME directory.

So something like:

$HOME/.my_prog_data.conf

or

$HOME/.my_prog_data/config.conf

In a more recent effort to clean up the $HOME directory nowadays programs tend to either use $HOME/.config or $HOME/.local/share rather than $HOME itself.

Galik
  • 47,303
  • 4
  • 80
  • 117