From 5a14ea6c36d21c827b499c83d6a9f6313ef4f8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Tue, 19 Feb 2019 17:52:00 -0800 Subject: [PATCH] Cleanup. --- include/bx/filepath.h | 8 +++-- include/bx/platform.h | 37 ++++++++++++++++++++ include/compat/msvc/dirent.h | 9 +++++ src/file.cpp | 30 ++++++++-------- src/filepath.cpp | 66 +++++++++++++++++++----------------- src/os.cpp | 4 +-- src/process.cpp | 4 +-- tests/filepath_test.cpp | 3 +- 8 files changed, 108 insertions(+), 53 deletions(-) diff --git a/include/bx/filepath.h b/include/bx/filepath.h index 7df63e0..a2e4dfe 100644 --- a/include/bx/filepath.h +++ b/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/`. /// diff --git a/include/bx/platform.h b/include/bx/platform.h index c8bf110..bf41b86 100644 --- a/include/bx/platform.h +++ b/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__) "." \ diff --git a/include/compat/msvc/dirent.h b/include/compat/msvc/dirent.h index 0116a88..3177d33 100644 --- a/include/compat/msvc/dirent.h +++ b/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; diff --git a/src/file.cpp b/src/file.cpp index 342d10b..56311fa 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -6,22 +6,24 @@ #include "bx_p.h" #include -#if BX_CRT_NONE -# include "crt0.h" -#else -# include -# include -# include -#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 +# endif // BX_CONFIG_CRT_DIRECTORY_READER +# include +# include +#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; diff --git a/src/filepath.cpp b/src/filepath.cpp index 7d0d597..1622fec 100644 --- a/src/filepath.cpp +++ b/src/filepath.cpp @@ -10,7 +10,6 @@ #if !BX_CRT_NONE # include // remove -# include // opendir # if BX_CRT_MSVC # include // _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); } diff --git a/src/os.cpp b/src/os.cpp index 8bd3da2..e774628 100644 --- a/src/os.cpp +++ b/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_ } diff --git a/src/process.cpp b/src/process.cpp index d499dee..b079bbc 100644 --- a/src/process.cpp +++ b/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); diff --git a/tests/filepath_test.cpp b/tests/filepath_test.cpp index 7f60b6b..1ad64e7 100644 --- a/tests/filepath_test.cpp +++ b/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)