From bef2236283c333f17613b2ea4904878228fedb6e Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Fri, 31 Jan 2014 23:46:06 +0000 Subject: [PATCH] Introduce llvm::sys::path::home_directory. This will be used by the line editor library to derive a default path to the history file. Differential Revision: http://llvm-reviews.chandlerc.com/D2199 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200594 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Path.h | 6 ++++++ lib/Support/Unix/Path.inc | 15 +++++++++++++++ lib/Support/Windows/Path.inc | 17 +++++++++++++++++ unittests/Support/Path.cpp | 13 +++++++++++++ 4 files changed, 51 insertions(+) diff --git a/include/llvm/Support/Path.h b/include/llvm/Support/Path.h index b2afe1b8e8a..ba18529b48a 100644 --- a/include/llvm/Support/Path.h +++ b/include/llvm/Support/Path.h @@ -306,6 +306,12 @@ bool is_separator(char value); /// @param result Holds the resulting path name. void system_temp_directory(bool erasedOnReboot, SmallVectorImpl &result); +/// @brief Get the user's home directory. +/// +/// @param result Holds the resulting path name. +/// @result True if a home directory is set, false otherwise. +bool home_directory(SmallVectorImpl &result); + /// @brief Has root name? /// /// root_name != "" diff --git a/lib/Support/Unix/Path.inc b/lib/Support/Unix/Path.inc index d5260424616..d0b63b5ba5c 100644 --- a/lib/Support/Unix/Path.inc +++ b/lib/Support/Unix/Path.inc @@ -797,5 +797,20 @@ error_code openFileForWrite(const Twine &Name, int &ResultFD, } } // end namespace fs + +namespace path { + +bool home_directory(SmallVectorImpl &result) { + if (char *RequestedDir = getenv("HOME")) { + result.clear(); + result.append(RequestedDir, RequestedDir + strlen(RequestedDir)); + return true; + } + + return false; +} + +} // end namespace path + } // end namespace sys } // end namespace llvm diff --git a/lib/Support/Windows/Path.inc b/lib/Support/Windows/Path.inc index 4ef7285801a..e218fa2e726 100644 --- a/lib/Support/Windows/Path.inc +++ b/lib/Support/Windows/Path.inc @@ -19,6 +19,7 @@ #include "llvm/ADT/STLExtras.h" #include #include +#include #include #include @@ -1063,6 +1064,22 @@ error_code openFileForWrite(const Twine &Name, int &ResultFD, } } // end namespace fs +namespace path { + +bool home_directory(SmallVectorImpl &result) { + wchar_t Path[MAX_PATH]; + if (::SHGetFolderPathW(0, CSIDL_APPDATA | CSIDL_FLAG_CREATE, 0, + SHGFP_TYPE_CURRENT, Path) != S_OK) + return false; + + if (UTF16ToUTF8(Path, ::wcslen(Path), result)) + return false; + + return true; +} + +} // end namespace path + namespace windows { llvm::error_code UTF8ToUTF16(llvm::StringRef utf8, llvm::SmallVectorImpl &utf16) { diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index f71c7c56408..197e1c2f86b 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -210,6 +210,19 @@ TEST(Support, AbsolutePathIteratorWin32) { } #endif // LLVM_ON_WIN32 +TEST(Support, HomeDirectory) { +#ifdef LLVM_ON_UNIX + // This test only makes sense on Unix if $HOME is set. + if (::getenv("HOME")) { +#endif + SmallString<128> HomeDir; + EXPECT_TRUE(path::home_directory(HomeDir)); + EXPECT_FALSE(HomeDir.empty()); +#ifdef LLVM_ON_UNIX + } +#endif +} + class FileSystemTest : public testing::Test { protected: /// Unique temporary directory in which all created filesystem entities must -- 2.34.1