diff --git a/include/fc/filesystem.hpp b/include/fc/filesystem.hpp index 71c04eb..bd091b5 100644 --- a/include/fc/filesystem.hpp +++ b/include/fc/filesystem.hpp @@ -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& ); diff --git a/src/filesystem.cpp b/src/filesystem.cpp index de160f6..9448016 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -13,6 +13,10 @@ #ifdef WIN32 #include +#else + #include + #include + #include #endif namespace fc { @@ -365,4 +369,51 @@ namespace fc { { _path = fc::optional(); } + + 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; + } }