diff --git a/src/filesystem/filesystem_std.cpp b/src/filesystem/filesystem_std.cpp index 4df79c53..8fad1707 100644 --- a/src/filesystem/filesystem_std.cpp +++ b/src/filesystem/filesystem_std.cpp @@ -4,13 +4,9 @@ // ---------------------------------------------------------------------------- // NOTE: use this for standalone/tools projects //=============================================================================// +#include "tier0/utility.h" #include "filesystem_std.h" -// These are used for the 'stat()' and 'access()' in CBaseFileSystem::IsDirectory(). -#include -#include -#include - ssize_t CBaseFileSystem::Read(void* pOutput, ssize_t size, FileHandle_t file) { return fread(pOutput, sizeof(uint8_t), size, (FILE*)file); @@ -305,48 +301,16 @@ ssize_t CBaseFileSystem::ReadEx(void* pOutput, ssize_t /*destSize*/, ssize_t siz } -int CBaseFileSystem::CreateDirHierarchy(const char* pFileName, const char* pPathID) +int CBaseFileSystem::CreateDirHierarchy(const char* pPath, const char* pPathID) { - char fullPath[1024]; - int results; - - //if (pPathID) - // snprintf(fullPath, sizeof(fullPath), "%s/%s", pPathID, pFileName); - //else - snprintf(fullPath, sizeof(fullPath), "%s", pFileName); - - V_FixSlashes(fullPath); - - char* pFullPath = fullPath; - while ((pFullPath = strchr(pFullPath, CORRECT_PATH_SEPARATOR)) != NULL) - { - // Temporarily turn the slash into a null - // to get the current directory. - *pFullPath = '\0'; - - results = _mkdir(fullPath); - - if (results && errno != EEXIST) - return results; - - *pFullPath++ = CORRECT_PATH_SEPARATOR; - } - - // Try to create the final directory in the path. - return _mkdir(fullPath); + NOTE_UNUSED(pPathID); + return ::CreateDirHierarchy(pPath); } -bool CBaseFileSystem::IsDirectory(const char* path, const char* pathID) +bool CBaseFileSystem::IsDirectory(const char* pPath, const char* pPathID) { - if (_access(path, 0) == 0) - { - struct stat status; - stat(path, &status); - - return (status.st_mode & S_IFDIR) != 0; - } - - return false; + NOTE_UNUSED(pPathID); + return ::IsDirectory(pPath); } char* CBaseFileSystem::ReadLine(char* maxChars, ssize_t maxOutputLength, FileHandle_t file) diff --git a/src/public/tier0/utility.h b/src/public/tier0/utility.h index 3d7bd539..44a2f2c1 100644 --- a/src/public/tier0/utility.h +++ b/src/public/tier0/utility.h @@ -4,7 +4,9 @@ // Internals BOOL IsBadReadPtrV2(void* ptr); BOOL FileExists(LPCTSTR szPath); -BOOL FileEmpty(ifstream& pFile); +int CreateDirHierarchy(const char* filePath); +bool IsDirectory(const char* path); +bool FileEmpty(ifstream& pFile); MODULEINFO GetModuleInfo(const char* szModule); ///////////////////////////////////////////////////////////////////////////// diff --git a/src/tier0/utility.cpp b/src/tier0/utility.cpp index 0814ff3a..8774d371 100644 --- a/src/tier0/utility.cpp +++ b/src/tier0/utility.cpp @@ -5,6 +5,11 @@ #include "core/logdef.h" #include "tier0/utility.h" +// These are used for the 'stat()' and 'access()' in ::IsDirectory(). +#include +#include +#include + /////////////////////////////////////////////////////////////////////////////// // For checking if a specific file exists. BOOL FileExists(LPCTSTR szPath) @@ -15,9 +20,54 @@ BOOL FileExists(LPCTSTR szPath) !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); } +/////////////////////////////////////////////////////////////////////////////// +// For creating a directory hierarchy +int CreateDirHierarchy(const char* const filePath) +{ + char fullPath[1024]; + int results; + + snprintf(fullPath, sizeof(fullPath), "%s", filePath); + + V_FixSlashes(fullPath); + + char* pFullPath = fullPath; + while ((pFullPath = strchr(pFullPath, CORRECT_PATH_SEPARATOR)) != NULL) + { + // Temporarily turn the slash into a null + // to get the current directory. + *pFullPath = '\0'; + + results = _mkdir(fullPath); + + if (results && errno != EEXIST) + return results; + + *pFullPath++ = CORRECT_PATH_SEPARATOR; + } + + // Try to create the final directory in the path. + return _mkdir(fullPath); +} + +/////////////////////////////////////////////////////////////////////////////// +// For checking if a directory exists +bool IsDirectory(const char* path) +{ + if (_access(path, 0) == 0) + { + struct stat status; + stat(path, &status); + + return (status.st_mode & S_IFDIR) != 0; + } + + return false; +} + /////////////////////////////////////////////////////////////////////////////// // For checking if a specific file is empty. -BOOL FileEmpty(ifstream& pFile) +bool FileEmpty(ifstream& pFile) { return pFile.peek() == ifstream::traits_type::eof(); } @@ -97,7 +147,8 @@ void HexDump(const char* szHeader, const char* szLogger, const void* pData, size { char szAscii[17]; static std::mutex m; - static std::shared_ptr logger = spdlog::default_logger(); + + std::shared_ptr logger; m.lock(); szAscii[16] = '\0'; @@ -113,6 +164,10 @@ void HexDump(const char* szHeader, const char* szLogger, const void* pData, size return; } } + else + { + logger = spdlog::default_logger(); + } // Add time stamp. logger->set_level(spdlog::level::trace);