c++appdatagetenv

AppData / Similar for all OS : C++?


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


Solution

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