[llvm] r200594 - Introduce llvm::sys::path::home_directory.

Peter Collingbourne peter at pcc.me.uk
Fri Jan 31 15:46:06 PST 2014


Author: pcc
Date: Fri Jan 31 17:46:06 2014
New Revision: 200594

URL: http://llvm.org/viewvc/llvm-project?rev=200594&view=rev
Log:
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

Modified:
    llvm/trunk/include/llvm/Support/Path.h
    llvm/trunk/lib/Support/Unix/Path.inc
    llvm/trunk/lib/Support/Windows/Path.inc
    llvm/trunk/unittests/Support/Path.cpp

Modified: llvm/trunk/include/llvm/Support/Path.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Path.h?rev=200594&r1=200593&r2=200594&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Path.h (original)
+++ llvm/trunk/include/llvm/Support/Path.h Fri Jan 31 17:46:06 2014
@@ -306,6 +306,12 @@ bool is_separator(char value);
 /// @param result Holds the resulting path name.
 void system_temp_directory(bool erasedOnReboot, SmallVectorImpl<char> &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<char> &result);
+
 /// @brief Has root name?
 ///
 /// root_name != ""

Modified: llvm/trunk/lib/Support/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Path.inc?rev=200594&r1=200593&r2=200594&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc (original)
+++ llvm/trunk/lib/Support/Unix/Path.inc Fri Jan 31 17:46:06 2014
@@ -797,5 +797,20 @@ error_code openFileForWrite(const Twine
 }
 
 } // end namespace fs
+
+namespace path {
+
+bool home_directory(SmallVectorImpl<char> &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

Modified: llvm/trunk/lib/Support/Windows/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Path.inc?rev=200594&r1=200593&r2=200594&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/Path.inc (original)
+++ llvm/trunk/lib/Support/Windows/Path.inc Fri Jan 31 17:46:06 2014
@@ -19,6 +19,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include <fcntl.h>
 #include <io.h>
+#include <shlobj.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -1063,6 +1064,22 @@ error_code openFileForWrite(const Twine
 }
 } // end namespace fs
 
+namespace path {
+
+bool home_directory(SmallVectorImpl<char> &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<wchar_t> &utf16) {

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=200594&r1=200593&r2=200594&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Fri Jan 31 17:46:06 2014
@@ -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





More information about the llvm-commits mailing list