Бранимир Караџић 6 years ago
parent
commit
5a14ea6c36
8 changed files with 108 additions and 53 deletions
  1. 6 2
      include/bx/filepath.h
  2. 37 0
      include/bx/platform.h
  3. 9 0
      include/compat/msvc/dirent.h
  4. 16 14
      src/file.cpp
  5. 35 31
      src/filepath.cpp
  6. 2 2
      src/os.cpp
  7. 2 2
      src/process.cpp
  8. 1 2
      tests/filepath_test.cpp

+ 6 - 2
include/bx/filepath.h

@@ -79,9 +79,13 @@ namespace bx
 		///
 		void join(const StringView& _str);
 
-		/// Returns C string to file path.
+		/// Implicitly converts FilePath to StringView.
 		///
-		const char* get() const;
+		operator StringView() const;
+
+		/// Returns zero-terminated C string pointer to file path.
+		///
+		const char* getCPtr() const;
 
 		/// If path is `/abv/gd/555/333/pod.mac` returns `/abv/gd/555/333/`.
 		///

+ 37 - 0
include/bx/platform.h

@@ -252,6 +252,7 @@
 #	endif // BX_CRT_*
 #endif // !BX_CRT_NONE
 
+///
 #define BX_PLATFORM_POSIX (0   \
 	||  BX_PLATFORM_ANDROID    \
 	||  BX_PLATFORM_BSD        \
@@ -266,6 +267,7 @@
 	||  BX_PLATFORM_STEAMLINK  \
 	)
 
+///
 #define BX_PLATFORM_NONE !(0   \
 	||  BX_PLATFORM_ANDROID    \
 	||  BX_PLATFORM_BSD        \
@@ -283,6 +285,41 @@
 	||  BX_PLATFORM_XBOXONE    \
 	)
 
+///
+#define BX_PLATFORM_OS_CONSOLE  (0 \
+	||  BX_PLATFORM_NX             \
+	||  BX_PLATFORM_PS4            \
+	||  BX_PLATFORM_WINRT          \
+	||  BX_PLATFORM_XBOXONE        \
+	)
+
+///
+#define BX_PLATFORM_OS_DESKTOP  (0 \
+	||  BX_PLATFORM_BSD            \
+	||  BX_PLATFORM_HURD           \
+	||  BX_PLATFORM_LINUX          \
+	||  BX_PLATFORM_OSX            \
+	||  BX_PLATFORM_WINDOWS        \
+	)
+
+///
+#define BX_PLATFORM_OS_EMBEDDED (0 \
+	||  BX_PLATFORM_RPI            \
+	||  BX_PLATFORM_STEAMLINK      \
+	)
+
+///
+#define BX_PLATFORM_OS_MOBILE   (0 \
+	||  BX_PLATFORM_ANDROID        \
+	||  BX_PLATFORM_IOS            \
+	)
+
+///
+#define BX_PLATFORM_OS_WEB      (0 \
+	||  BX_PLATFORM_EMSCRIPTEN     \
+	)
+
+///
 #if BX_COMPILER_GCC
 #	define BX_COMPILER_NAME "GCC "       \
 		BX_STRINGIZE(__GNUC__) "."       \

+ 9 - 0
include/compat/msvc/dirent.h

@@ -1043,6 +1043,7 @@ dirent_mbstowcs_s(
     DWORD flags;
 
     /* Determine code page for multi-byte string */
+#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
     if (AreFileApisANSI ()) {
         /* Default ANSI code page */
         cp = GetACP ();
@@ -1050,6 +1051,9 @@ dirent_mbstowcs_s(
         /* Default OEM code page */
         cp = GetOEMCP ();
     }
+#else
+    cp = CP_ACP;
+#endif // WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
 
     /*
      * Determine flags based on the character set.  For more information,
@@ -1134,6 +1138,7 @@ dirent_wcstombs_s(
     LPBOOL pflag;
 
     /* Determine code page for multi-byte string */
+#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
     if (AreFileApisANSI ()) {
         /* Default ANSI code page */
         cp = GetACP ();
@@ -1141,6 +1146,10 @@ dirent_wcstombs_s(
         /* Default OEM code page */
         cp = GetOEMCP ();
     }
+#else
+    cp = CP_ACP;
+#endif // WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
+
 
     /* Compute the length of input string without zero-terminator */
     len = 0;

+ 16 - 14
src/file.cpp

@@ -6,22 +6,24 @@
 #include "bx_p.h"
 #include <bx/file.h>
 
-#if BX_CRT_NONE
-#	include "crt0.h"
-#else
-#	include <dirent.h>
-#	include <stdio.h>
-#	include <sys/stat.h>
-#endif // !BX_CRT_NONE
-
 #ifndef BX_CONFIG_CRT_FILE_READER_WRITER
 #	define BX_CONFIG_CRT_FILE_READER_WRITER !BX_CRT_NONE
 #endif // BX_CONFIG_CRT_FILE_READER_WRITER
 
 #ifndef BX_CONFIG_CRT_DIRECTORY_READER
-#	define BX_CONFIG_CRT_DIRECTORY_READER !BX_CRT_NONE
+#	define BX_CONFIG_CRT_DIRECTORY_READER (BX_PLATFORM_OS_DESKTOP && !BX_CRT_NONE)
 #endif // BX_CONFIG_CRT_DIRECTORY_READER
 
+#if BX_CRT_NONE
+#	include "crt0.h"
+#else
+#	if BX_CONFIG_CRT_DIRECTORY_READER
+#		include <dirent.h>
+#	endif // BX_CONFIG_CRT_DIRECTORY_READER
+#	include <stdio.h>
+#	include <sys/stat.h>
+#endif // !BX_CRT_NONE
+
 namespace bx
 {
 	class NoopWriterImpl : public FileWriterI
@@ -100,7 +102,7 @@ namespace bx
 				return false;
 			}
 
-			m_file = fopen(_filePath.get(), "rb");
+			m_file = fopen(_filePath.getCPtr(), "rb");
 			if (NULL == m_file)
 			{
 				BX_ERROR_SET(_err, BX_ERROR_READERWRITER_OPEN, "FileReader: Failed to open file.");
@@ -180,7 +182,7 @@ namespace bx
 				return false;
 			}
 
-			m_file = fopen(_filePath.get(), _append ? "ab" : "wb");
+			m_file = fopen(_filePath.getCPtr(), _append ? "ab" : "wb");
 
 			if (NULL == m_file)
 			{
@@ -576,7 +578,7 @@ namespace bx
 		{
 			BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors.");
 
-			m_dir = opendir(_filePath.get() );
+			m_dir = opendir(_filePath.getCPtr() );
 
 			if (NULL == m_dir)
 			{
@@ -742,7 +744,7 @@ namespace bx
 
 #	if BX_COMPILER_MSVC
 		struct ::_stat64 st;
-		int32_t result = ::_stat64(_filePath.get(), &st);
+		int32_t result = ::_stat64(_filePath.getCPtr(), &st);
 
 		if (0 != result)
 		{
@@ -759,7 +761,7 @@ namespace bx
 		}
 #	else
 		struct ::stat st;
-		int32_t result = ::stat(_filePath.get(), &st);
+		int32_t result = ::stat(_filePath.getCPtr(), &st);
 		if (0 != result)
 		{
 			return false;

+ 35 - 31
src/filepath.cpp

@@ -10,7 +10,6 @@
 
 #if !BX_CRT_NONE
 #	include <stdio.h>  // remove
-#	include <dirent.h> // opendir
 
 #	if BX_CRT_MSVC
 #		include <direct.h>   // _getcwd
@@ -336,7 +335,12 @@ namespace bx
 		set(tmp);
 	}
 
-	const char* FilePath::get() const
+	FilePath::operator StringView() const
+	{
+		return StringView(m_filePath, strLen(m_filePath) );
+	}
+
+	const char* FilePath::getCPtr() const
 	{
 		return m_filePath;
 	}
@@ -360,7 +364,7 @@ namespace bx
 			return StringView(fileName.getPtr()+1);
 		}
 
-		return get();
+		return getCPtr();
 	}
 
 	StringView FilePath::getBaseName() const
@@ -414,14 +418,14 @@ namespace bx
 		}
 
 #if BX_CRT_MSVC
-		int32_t result = ::_mkdir(_filePath.get() );
+		int32_t result = ::_mkdir(_filePath.getCPtr() );
 #elif BX_CRT_MINGW
-		int32_t result = ::mkdir(_filePath.get());
+		int32_t result = ::mkdir(_filePath.getCPtr());
 #elif BX_CRT_NONE
 		BX_UNUSED(_filePath);
 		int32_t result = -1;
 #else
-		int32_t result = ::mkdir(_filePath.get(), 0700);
+		int32_t result = ::mkdir(_filePath.getCPtr(), 0700);
 #endif // BX_CRT_MSVC
 
 		if (0 != result)
@@ -455,7 +459,7 @@ namespace bx
 			return false;
 		}
 
-		const StringView dir   = strRTrim(_filePath.get(), "/");
+		const StringView dir   = strRTrim(_filePath, "/");
 		const StringView slash = strRFind(dir, '/');
 
 		if (!slash.isEmpty()
@@ -487,18 +491,18 @@ namespace bx
 		{
 			if (FileType::Dir == fi.type)
 			{
-				result = ::_rmdir(_filePath.get() );
+				result = ::_rmdir(_filePath.getCPtr() );
 			}
 			else
 			{
-				result = ::remove(_filePath.get() );
+				result = ::remove(_filePath.getCPtr() );
 			}
 		}
 #elif BX_CRT_NONE
 		BX_UNUSED(_filePath);
 		int32_t result = -1;
 #else
-		int32_t result = ::remove(_filePath.get() );
+		int32_t result = ::remove(_filePath.getCPtr() );
 #endif // BX_CRT_MSVC
 
 		if (0 != result)
@@ -535,38 +539,38 @@ namespace bx
 			return false;
 		}
 
-#if BX_CRT_NONE
-		BX_UNUSED(_filePath);
-		return false;
-#elif  BX_PLATFORM_WINDOWS \
-	|| BX_PLATFORM_LINUX   \
-	|| BX_PLATFORM_OSX
-		DIR* dir = opendir(_filePath.get() );
-		if (NULL == dir)
+		Error err;
+		DirectoryReader dr;
+
+		if (!bx::open(&dr, _filePath) )
 		{
 			BX_ERROR_SET(_err, BX_ERROR_NOT_DIRECTORY, "File already exist, and is not directory.");
 			return false;
 		}
 
-		for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
+		while (err.isOk() )
 		{
-			if (0 == strCmp(item->d_name, ".")
-			||  0 == strCmp(item->d_name, "..") )
-			{
-				continue;
-			}
+			bx::read(&dr, fi, &err);
 
-			FilePath path(_filePath);
-			path.join(item->d_name);
-			if (!removeAll(path, _err) )
+			if (err.isOk() )
 			{
-				_err->reset();
-				break;
+				if (0 == strCmp(fi.filePath, ".")
+				||  0 == strCmp(fi.filePath, "..") )
+				{
+					continue;
+				}
+
+				FilePath path(_filePath);
+				path.join(fi.filePath);
+				if (!removeAll(path, _err) )
+				{
+					_err->reset();
+					break;
+				}
 			}
 		}
 
-		closedir(dir);
-#endif // !BX_CRT_NONE
+		bx::close(&dr);
 
 		return remove(_filePath, _err);
 	}

+ 2 - 2
src/os.cpp

@@ -174,7 +174,7 @@ namespace bx
 	void* dlopen(const FilePath& _filePath)
 	{
 #if BX_PLATFORM_WINDOWS
-		return (void*)::LoadLibraryA(_filePath.get() );
+		return (void*)::LoadLibraryA(_filePath.getCPtr() );
 #elif  BX_PLATFORM_EMSCRIPTEN \
 	|| BX_PLATFORM_PS4        \
 	|| BX_PLATFORM_XBOXONE    \
@@ -183,7 +183,7 @@ namespace bx
 		BX_UNUSED(_filePath);
 		return NULL;
 #else
-		return ::dlopen(_filePath.get(), RTLD_LOCAL|RTLD_LAZY);
+		return ::dlopen(_filePath.getCPtr(), RTLD_LOCAL|RTLD_LAZY);
 #endif // BX_PLATFORM_
 	}
 

+ 2 - 2
src/process.cpp

@@ -48,7 +48,7 @@ namespace bx
 		}
 
 		char tmp[kMaxFilePath*2] = "\"";
-		strCat(tmp, BX_COUNTOF(tmp), _filePath.get() );
+		strCat(tmp, BX_COUNTOF(tmp), _filePath);
 		strCat(tmp, BX_COUNTOF(tmp), "\" ");
 		strCat(tmp, BX_COUNTOF(tmp), _args);
 
@@ -119,7 +119,7 @@ namespace bx
 		}
 
 		char tmp[kMaxFilePath*2] = "\"";
-		strCat(tmp, BX_COUNTOF(tmp), _filePath.get() );
+		strCat(tmp, BX_COUNTOF(tmp), _filePath);
 		strCat(tmp, BX_COUNTOF(tmp), "\" ");
 		strCat(tmp, BX_COUNTOF(tmp), _args);
 

+ 1 - 2
tests/filepath_test.cpp

@@ -96,9 +96,8 @@ TEST_CASE("FilePath", "")
 		const FilePathTest& test = s_filePathTest[ii];
 
 		fp.set(test.filePath);
-		const bx::StringView result = fp.get();
 
-		REQUIRE(0 == bx::strCmp(test.expected, result) );
+		REQUIRE(0 == bx::strCmp(test.expected, fp) );
 	}
 
 	for (uint32_t ii = 0; ii < BX_COUNTOF(s_filePathSplit); ++ii)