1
0
mirror of https://github.com/Mauler125/r5sdk.git synced 2025-02-09 19:15:03 +01:00

FileSystem: make CreateDirHierarchy and IsDirectory general function

Moved to general utility
This commit is contained in:
Kawe Mazidjatari 2024-01-28 18:06:36 +01:00
parent 1c2b02b972
commit 22431e903c
3 changed files with 67 additions and 46 deletions
r5dev
filesystem
public/tier0
tier0

@ -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 <io.h>
#include <sys/types.h>
#include <sys/stat.h>
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)

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

@ -5,6 +5,11 @@
#include "core/logdef.h"
#include "tier0/utility.h"
// These are used for the 'stat()' and 'access()' in ::IsDirectory().
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
///////////////////////////////////////////////////////////////////////////////
// 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<spdlog::logger> logger = spdlog::default_logger();
std::shared_ptr<spdlog::logger> 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);