Added home dir lookup.

This commit is contained in:
Branimir Karadžić
2017-11-15 20:19:57 -08:00
parent a2dd7097fb
commit c04c499aee
5 changed files with 69 additions and 13 deletions

View File

@@ -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);

View File

@@ -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) );
}

View File

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

View File

@@ -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() ) );
}

View File

@@ -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;