Browse Source

Added home dir lookup.

Branimir Karadžić 8 years ago
parent
commit
c04c499aee
5 changed files with 69 additions and 13 deletions
  1. 14 3
      include/bx/filepath.h
  2. 52 7
      src/filepath.cpp
  3. 1 1
      src/os.cpp
  4. 1 1
      tests/filepath_test.cpp
  5. 1 1
      tests/settings_test.cpp

+ 14 - 3
include/bx/filepath.h

@@ -11,7 +11,18 @@
 namespace bx
 namespace bx
 {
 {
 	const int32_t kMaxFilePath = 1024;
 	const int32_t kMaxFilePath = 1024;
-	struct TempDir { enum Enum { Tag }; };
+
+	///
+	struct Dir
+	{
+		enum Enum ///
+		{
+			Temp,
+			Home,
+
+			Count
+		};
+	};
 
 
 	/// FilePath parser and helper.
 	/// FilePath parser and helper.
 	///
 	///
@@ -29,7 +40,7 @@ namespace bx
 		FilePath();
 		FilePath();
 
 
 		///
 		///
-		FilePath(TempDir::Enum);
+		FilePath(Dir::Enum _dir);
 
 
 		///
 		///
 		FilePath(const char* _str);
 		FilePath(const char* _str);
@@ -41,7 +52,7 @@ namespace bx
 		FilePath& operator=(const StringView& _rhs);
 		FilePath& operator=(const StringView& _rhs);
 
 
 		///
 		///
-		void set(TempDir::Enum);
+		void set(Dir::Enum _dir);
 
 
 		///
 		///
 		void set(const StringView& _str);
 		void set(const StringView& _str);

+ 52 - 7
src/filepath.cpp

@@ -134,6 +134,35 @@ namespace bx
 		return size;
 		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)
 	static bool getTempPath(char* _out, uint32_t* _inOutSize)
 	{
 	{
 #if BX_PLATFORM_WINDOWS
 #if BX_PLATFORM_WINDOWS
@@ -156,14 +185,14 @@ namespace bx
 		{
 		{
 			uint32_t len = *_inOutSize;
 			uint32_t len = *_inOutSize;
 			*_out = '\0';
 			*_out = '\0';
-			bool result = getenv(*tmp, _out, &len);
+			bool ok = getEnv(*tmp, FileInfo::Directory, _out, &len);
 
 
-			if (result
+			if (ok
 			&&  len != 0
 			&&  len != 0
 			&&  len < *_inOutSize)
 			&&  len < *_inOutSize)
 			{
 			{
 				*_inOutSize = len;
 				*_inOutSize = len;
-				return result;
+				return ok;
 			}
 			}
 		}
 		}
 
 
@@ -185,9 +214,9 @@ namespace bx
 		set("");
 		set("");
 	}
 	}
 
 
-	FilePath::FilePath(TempDir::Enum)
+	FilePath::FilePath(Dir::Enum _dir)
 	{
 	{
-		set(TempDir::Tag);
+		set(_dir);
 	}
 	}
 
 
 	FilePath::FilePath(const char* _rhs)
 	FilePath::FilePath(const char* _rhs)
@@ -206,11 +235,27 @@ namespace bx
 		return *this;
 		return *this;
 	}
 	}
 
 
-	void FilePath::set(TempDir::Enum)
+	void FilePath::set(Dir::Enum _dir)
 	{
 	{
 		char tmp[kMaxFilePath];
 		char tmp[kMaxFilePath];
 		uint32_t len = BX_COUNTOF(tmp);
 		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) );
 		set(StringView(tmp, len) );
 	}
 	}
 
 

+ 1 - 1
src/os.cpp

@@ -236,7 +236,7 @@ namespace bx
 			result = len != 0 && len < *_inOutSize;
 			result = len != 0 && len < *_inOutSize;
 			if (len < *_inOutSize)
 			if (len < *_inOutSize)
 			{
 			{
-				strCopy(_out, len, ptr);
+				strCopy(_out, *_inOutSize, ptr);
 			}
 			}
 		}
 		}
 
 

+ 1 - 1
tests/filepath_test.cpp

@@ -119,6 +119,6 @@ TEST_CASE("FilePath", "")
 
 
 TEST_CASE("FilePath temp", "")
 TEST_CASE("FilePath temp", "")
 {
 {
-	bx::FilePath tmp(bx::TempDir::Tag);
+	bx::FilePath tmp(bx::Dir::Temp);
 	REQUIRE(0 != bx::strCmp(".", tmp.getPath().getPtr() ) );
 	REQUIRE(0 != bx::strCmp(".", tmp.getPath().getPtr() ) );
 }
 }

+ 1 - 1
tests/settings_test.cpp

@@ -10,7 +10,7 @@
 TEST_CASE("Settings", "")
 TEST_CASE("Settings", "")
 {
 {
 	bx::FilePath filePath;
 	bx::FilePath filePath;
-	filePath.set(bx::TempDir::Tag);
+	filePath.set(bx::Dir::Temp);
 	filePath.join("settings.ini");
 	filePath.join("settings.ini");
 
 
 	bx::DefaultAllocator allocator;
 	bx::DefaultAllocator allocator;