From c04c499aee7d43187fd61818521cfaccf9481dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 15 Nov 2017 20:19:57 -0800 Subject: [PATCH] Added home dir lookup. --- include/bx/filepath.h | 17 +++++++++--- src/filepath.cpp | 59 ++++++++++++++++++++++++++++++++++++----- src/os.cpp | 2 +- tests/filepath_test.cpp | 2 +- tests/settings_test.cpp | 2 +- 5 files changed, 69 insertions(+), 13 deletions(-) diff --git a/include/bx/filepath.h b/include/bx/filepath.h index 5029d9c..a46a258 100644 --- a/include/bx/filepath.h +++ b/include/bx/filepath.h @@ -11,7 +11,18 @@ namespace bx { const int32_t kMaxFilePath = 1024; - struct TempDir { enum Enum { Tag }; }; + + /// + struct Dir + { + enum Enum /// + { + Temp, + Home, + + Count + }; + }; /// FilePath parser and helper. /// @@ -29,7 +40,7 @@ namespace bx FilePath(); /// - FilePath(TempDir::Enum); + FilePath(Dir::Enum _dir); /// FilePath(const char* _str); @@ -41,7 +52,7 @@ namespace bx FilePath& operator=(const StringView& _rhs); /// - void set(TempDir::Enum); + void set(Dir::Enum _dir); /// void set(const StringView& _str); diff --git a/src/filepath.cpp b/src/filepath.cpp index 3a49118..dd17a45 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -134,6 +134,35 @@ namespace bx return size; } + static bool getEnv(const char* _name, FileInfo::Enum _type, char* _out, uint32_t* _inOutSize) + { + uint32_t len = *_inOutSize; + *_out = '\0'; + + if (getenv(_name, _out, &len) ) + { + FileInfo fi; + if (stat(_out, fi) + && _type == fi.m_type) + { + *_inOutSize = len; + return true; + } + } + + return false; + } + + static bool getHomePath(char* _out, uint32_t* _inOutSize) + { + return false +#if BX_PLATFORM_WINDOWS + || getEnv("USERPROFILE", FileInfo::Directory, _out, _inOutSize) +#endif // BX_PLATFORM_WINDOWS + || getEnv("HOME", FileInfo::Directory, _out, _inOutSize) + ; + } + static bool getTempPath(char* _out, uint32_t* _inOutSize) { #if BX_PLATFORM_WINDOWS @@ -156,14 +185,14 @@ namespace bx { uint32_t len = *_inOutSize; *_out = '\0'; - bool result = getenv(*tmp, _out, &len); + bool ok = getEnv(*tmp, FileInfo::Directory, _out, &len); - if (result + if (ok && len != 0 && len < *_inOutSize) { *_inOutSize = len; - return result; + return ok; } } @@ -185,9 +214,9 @@ namespace bx set(""); } - FilePath::FilePath(TempDir::Enum) + FilePath::FilePath(Dir::Enum _dir) { - set(TempDir::Tag); + set(_dir); } FilePath::FilePath(const char* _rhs) @@ -206,11 +235,27 @@ namespace bx return *this; } - void FilePath::set(TempDir::Enum) + void FilePath::set(Dir::Enum _dir) { char tmp[kMaxFilePath]; uint32_t len = BX_COUNTOF(tmp); - getTempPath(tmp, &len); + + switch (_dir) + { + case Dir::Temp: + getTempPath(tmp, &len); + break; + + case Dir::Home: + getHomePath(tmp, &len); +bx::debugPrintf("%s", tmp); + break; + + default: + len = 0; + break; + } + set(StringView(tmp, len) ); } diff --git a/src/os.cpp b/src/os.cpp index e416afa..a233e47 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -236,7 +236,7 @@ namespace bx result = len != 0 && len < *_inOutSize; if (len < *_inOutSize) { - strCopy(_out, len, ptr); + strCopy(_out, *_inOutSize, ptr); } } diff --git a/tests/filepath_test.cpp b/tests/filepath_test.cpp index e8af194..61e9b2a 100644 --- a/tests/filepath_test.cpp +++ b/tests/filepath_test.cpp @@ -119,6 +119,6 @@ TEST_CASE("FilePath", "") TEST_CASE("FilePath temp", "") { - bx::FilePath tmp(bx::TempDir::Tag); + bx::FilePath tmp(bx::Dir::Temp); REQUIRE(0 != bx::strCmp(".", tmp.getPath().getPtr() ) ); } diff --git a/tests/settings_test.cpp b/tests/settings_test.cpp index b173916..f400644 100644 --- a/tests/settings_test.cpp +++ b/tests/settings_test.cpp @@ -10,7 +10,7 @@ TEST_CASE("Settings", "") { bx::FilePath filePath; - filePath.set(bx::TempDir::Tag); + filePath.set(bx::Dir::Temp); filePath.join("settings.ini"); bx::DefaultAllocator allocator;