FileSystem: Add some overloads for FILE*

This commit is contained in:
Connor McLaughlin 2021-02-16 00:50:31 +10:00
parent cacf12c209
commit 3ab7e140ee
2 changed files with 82 additions and 8 deletions

View File

@ -483,14 +483,19 @@ std::optional<std::vector<u8>> ReadBinaryFile(const char* filename)
if (!fp) if (!fp)
return std::nullopt; return std::nullopt;
std::fseek(fp.get(), 0, SEEK_END); return ReadBinaryFile(fp.get());
long size = std::ftell(fp.get()); }
std::fseek(fp.get(), 0, SEEK_SET);
std::optional<std::vector<u8>> ReadBinaryFile(std::FILE* fp)
{
std::fseek(fp, 0, SEEK_END);
long size = std::ftell(fp);
std::fseek(fp, 0, SEEK_SET);
if (size < 0) if (size < 0)
return std::nullopt; return std::nullopt;
std::vector<u8> res(static_cast<size_t>(size)); std::vector<u8> res(static_cast<size_t>(size));
if (size > 0 && std::fread(res.data(), 1u, static_cast<size_t>(size), fp.get()) != static_cast<size_t>(size)) if (size > 0 && std::fread(res.data(), 1u, static_cast<size_t>(size), fp) != static_cast<size_t>(size))
return std::nullopt; return std::nullopt;
return res; return res;
@ -502,15 +507,20 @@ std::optional<std::string> ReadFileToString(const char* filename)
if (!fp) if (!fp)
return std::nullopt; return std::nullopt;
std::fseek(fp.get(), 0, SEEK_END); return ReadFileToString(fp.get());
long size = std::ftell(fp.get()); }
std::fseek(fp.get(), 0, SEEK_SET);
std::optional<std::string> ReadFileToString(std::FILE* fp)
{
std::fseek(fp, 0, SEEK_END);
long size = std::ftell(fp);
std::fseek(fp, 0, SEEK_SET);
if (size < 0) if (size < 0)
return std::nullopt; return std::nullopt;
std::string res; std::string res;
res.resize(static_cast<size_t>(size)); res.resize(static_cast<size_t>(size));
if (size > 0 && std::fread(res.data(), 1u, static_cast<size_t>(size), fp.get()) != static_cast<size_t>(size)) if (size > 0 && std::fread(res.data(), 1u, static_cast<size_t>(size), fp) != static_cast<size_t>(size))
return std::nullopt; return std::nullopt;
return res; return res;
@ -1030,6 +1040,33 @@ bool FileSystem::StatFile(const char* path, FILESYSTEM_STAT_DATA* pStatData)
return true; return true;
} }
bool FileSystem::StatFile(std::FILE* fp, FILESYSTEM_STAT_DATA* pStatData)
{
const int fd = _fileno(fp);
if (fd < 0)
return false;
struct _stat64 st;
if (_fstati64(fd, &st) != 0)
return false;
// parse attributes
pStatData->Attributes = 0;
if ((st.st_mode & _S_IFMT) == _S_IFDIR)
pStatData->Attributes |= FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY;
// parse times
pStatData->ModificationTime.SetUnixTimestamp((Timestamp::UnixTimestampValue)st.st_mtime);
// parse size
if ((st.st_mode & _S_IFMT) == _S_IFREG)
pStatData->Size = static_cast<u64>(st.st_size);
else
pStatData->Size = 0;
return true;
}
bool FileSystem::FileExists(const char* path) bool FileSystem::FileExists(const char* path)
{ {
// has a path // has a path
@ -1485,6 +1522,40 @@ bool StatFile(const char* Path, FILESYSTEM_STAT_DATA* pStatData)
return true; return true;
} }
bool StatFile(std::FILE* fp, FILESYSTEM_STAT_DATA* pStatData)
{
int fd = fileno(fp);
if (fd < 0)
return false;
// stat file
#if defined(__HAIKU__) || defined(__APPLE__)
struct stat sysStatData;
if (fstat(fd, &sysStatData) < 0)
#else
struct stat64 sysStatData;
if (fstat64(fd, &sysStatData) < 0)
#endif
return false;
// parse attributes
pStatData->Attributes = 0;
if (S_ISDIR(sysStatData.st_mode))
pStatData->Attributes |= FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY;
// parse times
pStatData->ModificationTime.SetUnixTimestamp((Timestamp::UnixTimestampValue)sysStatData.st_mtime);
// parse size
if (S_ISREG(sysStatData.st_mode))
pStatData->Size = static_cast<u64>(sysStatData.st_size);
else
pStatData->Size = 0;
// ok
return true;
}
bool FileExists(const char* Path) bool FileExists(const char* Path)
{ {
// has a path // has a path

View File

@ -162,6 +162,7 @@ bool FindFiles(const char* Path, const char* Pattern, u32 Flags, FindResultsArra
// stat file // stat file
bool StatFile(const char* Path, FILESYSTEM_STAT_DATA* pStatData); bool StatFile(const char* Path, FILESYSTEM_STAT_DATA* pStatData);
bool StatFile(std::FILE* fp, FILESYSTEM_STAT_DATA* pStatData);
// file exists? // file exists?
bool FileExists(const char* Path); bool FileExists(const char* Path);
@ -180,7 +181,9 @@ ManagedCFilePtr OpenManagedCFile(const char* filename, const char* mode);
std::FILE* OpenCFile(const char* filename, const char* mode); std::FILE* OpenCFile(const char* filename, const char* mode);
std::optional<std::vector<u8>> ReadBinaryFile(const char* filename); std::optional<std::vector<u8>> ReadBinaryFile(const char* filename);
std::optional<std::vector<u8>> ReadBinaryFile(std::FILE* fp);
std::optional<std::string> ReadFileToString(const char* filename); std::optional<std::string> ReadFileToString(const char* filename);
std::optional<std::string> ReadFileToString(std::FILE* fp);
bool WriteBinaryFile(const char* filename, const void* data, size_t data_length); bool WriteBinaryFile(const char* filename, const void* data, size_t data_length);
bool WriteFileToString(const char* filename, const std::string_view& sv); bool WriteFileToString(const char* filename, const std::string_view& sv);