Boost logo

Boost Users :

Subject: [Boost-users] filesystem : home directory
From: berenger.morel_at_[hidden]
Date: 2012-12-27 14:51:33


Hello.

I am would like to use path like "~/.config" in the filesystem. I've
searched in documentation and find nothing about it, so I ended by doing
something I do not really like, but which sounds to work (I did not
tried it on windows, but I my software is not a linux-only one and so I
hope people will use it on other OSes. My problem is that I only have an
XP partition and there are no development softwares on it. I could add
some, but it would mean I need to reboot to test a software, and on an
old windows... :/ ), at least on linux.
Here are copy/past (with todos, just ignore them) of related portions
of my code:

===========================
= utils.cpp =
===========================
#ifdef WIN32
#define HOME "HOMEPATH" ///\todo check that it should not be %APPDATA%
instead of %HOMEPATH%
#else
#define HOME "HOME"
#endif

...

std::string getUserConfigDir(void)
{
        ///\todo find a better solution to follow freeDesktop.org's
recommmandations
        std::string homepath(getenv(HOME));///\note flawfinder say I should
use getenv with care

        const fs::path usualUserConfig(homepath + "/.autorealm/config");
        if(fs::exists(usualUserConfig))
                return usualUserConfig.string();

        const fs::path xdgUserConfig(homepath + "/.config/autorealm/config");
        if(fs::exists(xdgUserConfig))
                return xdgUserConfig.string();
        return std::string();
}
===========================

Here, the thing I do not like is to need to use the getenv() function,
which is portable, but introduces an #ifdef condition.
More important, if I had used things like "~/config/" the source code
would have been easier to read and would have avoided one string
allocation and some concatenations. But maybe they would have been into
filesystem...

Another problem which will come from the usage of my system is that it
only supports WIN32 and linux, maybe Mac OS, maybe other POSIX systems.
As you can see, I can only say "maybe" for other OSes, and for non POSIX
OSes (is there is any which is not MSW?) I have just no clue.

If that feature was supported by a library, at least it would avoid
hard-coded OS's dependent stuff.

So, is there is a better way to portably use the $HOME shortcut linux
users are used ('~')? If it is any other symbol, I do not care at all,
too, I just want an unified way to do that.

PS: Of course, if you have other remarks about that code snippet or
other things, I would be grateful to hear them.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net