adding utility methods for getting home dir and app dir

This commit is contained in:
Daniel Larimer 2014-02-15 01:22:59 -05:00
parent a80164645f
commit 0ecd66fa73
2 changed files with 61 additions and 2 deletions

View file

@ -135,8 +135,16 @@ namespace fc {
void create_hard_link( const path& from, const path& to );
path unique_path();
path temp_directory_path();
path unique_path();
path temp_directory_path();
/** @return the home directory on Linux and OS X and the Profile directory on Windows */
const path& home_path();
/** @return the home_path() on Linux, home_path()/Library/Application Support/ on OS X,
* and APPDATA on windows
*/
const path& app_path();
class variant;
void to_variant( const fc::path&, fc::variant& );

View file

@ -13,6 +13,10 @@
#ifdef WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <pwd.h>
#include <uuid/uuid.h>
#endif
namespace fc {
@ -365,4 +369,51 @@ namespace fc {
{
_path = fc::optional<fc::path>();
}
const fc::path& home_path()
{
static fc::path p = []()
{
#ifdef WIN32
char* home = getenv( "USERPROFILE" );
if( nullptr == home )
{
FC_ASSERT( home != nullptr, "The USERPROFILE environment variable is not set" );
}
return fc::path( home );
#else
char* home = getenv( "HOME" );
if( nullptr == home )
{
struct passwd* pwd = getpwuid(getuid());
if( pwd )
{
return fc::path( std::string( pwd->pw_dir ) );
}
FC_ASSERT( home != nullptr, "The HOME environment variable is not set" );
}
return fc::path( std::string(home) );
#endif
}();
return p;
}
const fc::path& app_dir()
{
#ifdef __APPLE__
static fc::path appdir = [](){ return home_path() / "Library" / "Application Support"; }();
#elif defined( WIN32 )
static fc::path appdir = [](){
char* appdata = getenv( "APPDATA" );
if( nullptr == appdata )
{
FC_ASSERT( appdata != nullptr, "The APPDATA environment variable is not set" );
}
return fc::path( std::string(appdata) );
}();
#else
static fc::path appdir = home_dir();
#endif
return appdir;
}
}