mirror of
https://github.com/AxioDL/LibCommon.git
synced 2026-03-30 11:47:23 -07:00
baf3acc098
We can make all of the IOUtils helpers obsolete.
118 lines
2.1 KiB
C++
118 lines
2.1 KiB
C++
#include "CFileInStream.h"
|
|
|
|
CFileInStream::CFileInStream() = default;
|
|
|
|
CFileInStream::CFileInStream(const TString& rkFile)
|
|
{
|
|
Open(rkFile, std::endian::big);
|
|
}
|
|
|
|
CFileInStream::CFileInStream(const TString& rkFile, std::endian FileEndianness)
|
|
{
|
|
Open(rkFile, FileEndianness);
|
|
}
|
|
|
|
CFileInStream::CFileInStream(const CFileInStream& rkSrc)
|
|
{
|
|
Open(rkSrc.mName, rkSrc.mDataEndianness);
|
|
|
|
if (rkSrc.IsValid())
|
|
Seek(rkSrc.Tell(), SEEK_SET);
|
|
}
|
|
|
|
CFileInStream::~CFileInStream()
|
|
{
|
|
if (IsValid())
|
|
Close();
|
|
}
|
|
|
|
void CFileInStream::Open(const TString& rkFile, std::endian FileEndianness)
|
|
{
|
|
if (IsValid())
|
|
Close();
|
|
|
|
#ifdef _WIN32
|
|
_wfopen_s(&mpFStream, ToWChar(rkFile), L"rb");
|
|
#else
|
|
mpFStream = fopen(*rkFile, "rb");
|
|
#endif
|
|
mName = rkFile;
|
|
mDataEndianness = FileEndianness;
|
|
|
|
if (IsValid())
|
|
{
|
|
Seek(0x0, SEEK_END);
|
|
mFileSize = Tell();
|
|
Seek(0x0, SEEK_SET);
|
|
}
|
|
else
|
|
mFileSize = 0;
|
|
|
|
SetSourceString(rkFile.GetFileName());
|
|
}
|
|
|
|
void CFileInStream::Close()
|
|
{
|
|
if (IsValid())
|
|
fclose(mpFStream);
|
|
mpFStream = nullptr;
|
|
}
|
|
|
|
void CFileInStream::ReadBytes(void *pDst, uint32 Count)
|
|
{
|
|
if (!IsValid()) return;
|
|
fread(pDst, 1, Count, mpFStream);
|
|
}
|
|
|
|
bool CFileInStream::Seek(int32 Offset, uint32 Origin)
|
|
{
|
|
if (!IsValid()) return false;
|
|
return (fseek(mpFStream, Offset, Origin) != 0);
|
|
}
|
|
|
|
bool CFileInStream::Seek64(int64 Offset, uint32 Origin)
|
|
{
|
|
if (!IsValid()) return false;
|
|
#ifdef _WIN32
|
|
return (_fseeki64(mpFStream, Offset, Origin) != 0);
|
|
#else
|
|
return (fseeko(mpFStream, Offset, Origin) != 0);
|
|
#endif
|
|
}
|
|
|
|
uint32 CFileInStream::Tell() const
|
|
{
|
|
if (!IsValid()) return 0;
|
|
return ftell(mpFStream);
|
|
}
|
|
|
|
uint64 CFileInStream::Tell64() const
|
|
{
|
|
if (!IsValid()) return 0;
|
|
#ifdef _WIN32
|
|
return _ftelli64(mpFStream);
|
|
#else
|
|
return ftello(mpFStream);
|
|
#endif
|
|
}
|
|
|
|
bool CFileInStream::EoF() const
|
|
{
|
|
return (Tell() >= mFileSize);
|
|
}
|
|
|
|
bool CFileInStream::IsValid() const
|
|
{
|
|
return (mpFStream != 0);
|
|
}
|
|
|
|
uint32 CFileInStream::Size() const
|
|
{
|
|
return mFileSize;
|
|
}
|
|
|
|
TString CFileInStream::FileName() const
|
|
{
|
|
return mName;
|
|
}
|