608 lines
11 KiB
C++
608 lines
11 KiB
C++
#include "ehs/io/BaseFile.h"
|
|
|
|
namespace ehs
|
|
{
|
|
BaseFile::BaseFile()
|
|
: mode(Mode::READ_WRITE), disposition(Disposition::OPEN_PERSISTENT)
|
|
{
|
|
}
|
|
|
|
BaseFile::BaseFile(const Str_8& filePath, const Mode mode, const Disposition disposition)
|
|
: path(filePath), fullName(ParseFullName_8(filePath)),
|
|
name(ParseName_8(fullName)), extension(ParseExt_8(fullName)),
|
|
mode(mode), disposition(disposition)
|
|
{
|
|
}
|
|
|
|
BaseFile::BaseFile(BaseFile&& file) noexcept
|
|
: path(std::move(file.path)), fullName(std::move(file.fullName)), name(std::move(file.name)),
|
|
extension(std::move(file.extension)), mode(file.mode), disposition(file.disposition)
|
|
{
|
|
file.mode = Mode::READ_WRITE;
|
|
file.disposition = Disposition::OPEN_PERSISTENT;
|
|
}
|
|
|
|
BaseFile& BaseFile::operator=(BaseFile&& file) noexcept
|
|
{
|
|
if (this == &file)
|
|
return *this;
|
|
|
|
path = std::move(file.path);
|
|
fullName = std::move(file.fullName);
|
|
name = std::move(file.name);
|
|
extension = std::move(file.extension);
|
|
mode = file.mode;
|
|
disposition = file.disposition;
|
|
|
|
file.mode = Mode::READ_WRITE;
|
|
file.disposition = Disposition::OPEN_PERSISTENT;
|
|
|
|
return *this;
|
|
}
|
|
|
|
void BaseFile::WriteStr_32(const Char_32* const str, const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&((Byte*)str)[total], size - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(size) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < size);
|
|
}
|
|
|
|
void BaseFile::WriteStr_32(const Str_32& str)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&str.ToBytes()[total], str.Size(true) - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(str.Size()) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < str.Size(true));
|
|
}
|
|
|
|
void BaseFile::WriteStr_16(const Char_16* const str, const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&((Byte*)str)[total], size - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(size) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < size);
|
|
}
|
|
|
|
void BaseFile::WriteStr_16(const Str_16& str)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&str.ToBytes()[total], str.Size(true) - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(str.Size()) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < str.Size(true));
|
|
}
|
|
|
|
void BaseFile::WriteStr_8(const Char_8* const str, const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&((Byte*)str)[total], size - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(size) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < size);
|
|
}
|
|
|
|
void BaseFile::WriteStr_8(const Str_8& str)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&str.ToBytes()[total], str.Size(true) - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(str.Size()) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < str.Size(true));
|
|
}
|
|
|
|
void BaseFile::WriteVector(const Vector<Byte, UInt_64>& vec)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&vec[total], vec.Size() - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(vec.Size()) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < vec.Size());
|
|
}
|
|
|
|
void BaseFile::WriteArray(const Array<Byte, UInt_64>& arr)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&arr[total], arr.Size() - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(arr.Size()) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < arr.Size());
|
|
}
|
|
|
|
void BaseFile::WriteSerializer_64(const Serializer<UInt_64>& ser)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&ser[total], ser.Size() - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(ser.Size()) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < ser.Size());
|
|
}
|
|
|
|
void BaseFile::WriteSerializer_32(const Serializer<UInt_32>& ser)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 written = Write(&ser[total], ser.Size() - total);
|
|
if (!written)
|
|
{
|
|
EHS_LOG_INT(LogType::ERR, 0, "Failed to write all data (" + Str_8::FromNum(total) + "/" +
|
|
Str_8::FromNum(ser.Size()) + ").");
|
|
break;
|
|
}
|
|
|
|
total += written;
|
|
}
|
|
while (total < ser.Size());
|
|
}
|
|
|
|
void BaseFile::ReadStr_32(Char_32* const buffer, UInt_64& size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read(&((Byte*)buffer)[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
size = total;
|
|
}
|
|
|
|
Str_32 BaseFile::ReadStr_32(const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return {};
|
|
|
|
Str_32 result(size / 4);
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read(&result.ToBytes()[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
result.Resize(total);
|
|
return result;
|
|
}
|
|
|
|
void BaseFile::ReadStr_16(Char_16* const buffer, UInt_64& size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read(&((Byte*)buffer)[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
size = total;
|
|
}
|
|
|
|
Str_16 BaseFile::ReadStr_16(const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return {};
|
|
|
|
Str_16 result(size / 2);
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read(&result.ToBytes()[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
result.Resize(total);
|
|
return result;
|
|
}
|
|
|
|
void BaseFile::ReadStr_8(Char_8* const buffer, UInt_64& size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return;
|
|
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read((Byte*)&buffer[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
size = total;
|
|
}
|
|
|
|
Str_8 BaseFile::ReadStr_8(const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return {};
|
|
|
|
Str_8 result(size);
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read((Byte*)&result[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
result.Resize(total);
|
|
return result;
|
|
}
|
|
|
|
Vector<Byte, UInt_64> BaseFile::ReadVector(const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return {};
|
|
|
|
Vector<Byte, UInt_64> result(size, 0);
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read(&result[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
result.Resize(total);
|
|
return result;
|
|
}
|
|
|
|
Array<Byte, UInt_64> BaseFile::ReadArray(const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return {};
|
|
|
|
Array<Byte, UInt_64> result(size);
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read(&result[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
result.Resize(total);
|
|
return result;
|
|
}
|
|
|
|
Serializer<UInt_64> BaseFile::ReadSerializer_64(const Endianness end, const UInt_64 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return {};
|
|
|
|
Serializer<UInt_64> result(end, size);
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read(&result[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
result.Resize(total);
|
|
return result;
|
|
}
|
|
|
|
Serializer<UInt_32> BaseFile::ReadSerializer_32(const Endianness end, const UInt_32 size)
|
|
{
|
|
if (!IsValid() || IsMapped())
|
|
return {};
|
|
|
|
Serializer<UInt_32> result(end, size);
|
|
UInt_64 total = 0;
|
|
|
|
do
|
|
{
|
|
UInt_64 read = Read(&result[total], size - total);
|
|
if (!read)
|
|
break;
|
|
|
|
total += read;
|
|
}
|
|
while (total < size);
|
|
|
|
result.Resize(total);
|
|
return result;
|
|
}
|
|
|
|
Str_8 BaseFile::GetPath() const
|
|
{
|
|
return path;
|
|
}
|
|
|
|
Str_8 BaseFile::GetFullName() const
|
|
{
|
|
return fullName;
|
|
}
|
|
|
|
Str_8 BaseFile::GetName() const
|
|
{
|
|
return name;
|
|
}
|
|
|
|
Str_8 BaseFile::GetExtension() const
|
|
{
|
|
return extension;
|
|
}
|
|
|
|
void BaseFile::Rename_32(const Str_32& filePath, const Str_32& newName)
|
|
{
|
|
}
|
|
|
|
void BaseFile::Rename_16(const Str_16& filePath, const Str_16& newName)
|
|
{
|
|
}
|
|
|
|
void BaseFile::Rename_8(const Str_8& filePath, const Str_8& newName)
|
|
{
|
|
}
|
|
|
|
Str_32 BaseFile::ParseFullName_32(const Str_32& filePath)
|
|
{
|
|
UInt_64 index = 0;
|
|
|
|
if (!filePath.Find(U"/", &index, SearchPattern::RIGHT_LEFT) && !filePath.Find(U"\\", &index, SearchPattern::RIGHT_LEFT))
|
|
return filePath;
|
|
|
|
return filePath.Sub(index);
|
|
}
|
|
|
|
Str_16 BaseFile::ParseFullName_16(const Str_16& filePath)
|
|
{
|
|
UInt_64 index = 0;
|
|
|
|
if (!filePath.Find(L"/", &index, SearchPattern::RIGHT_LEFT) && !filePath.Find(L"\\", &index, SearchPattern::RIGHT_LEFT))
|
|
return filePath;
|
|
|
|
return filePath.Sub(index);
|
|
}
|
|
|
|
Str_8 BaseFile::ParseFullName_8(const Str_8& filePath)
|
|
{
|
|
UInt_64 index = 0;
|
|
|
|
if (!filePath.Find("/", &index, SearchPattern::RIGHT_LEFT) && !filePath.Find("\\", &index, SearchPattern::RIGHT_LEFT))
|
|
return filePath;
|
|
|
|
return filePath.Sub(index);
|
|
}
|
|
|
|
Str_32 BaseFile::ParseName_32(const Str_32& filePath)
|
|
{
|
|
UInt_64 index;
|
|
Str_32 file = filePath;
|
|
|
|
if (file.Find(U"/", &index, SearchPattern::RIGHT_LEFT) || file.Find(U"\\", &index, SearchPattern::RIGHT_LEFT))
|
|
file = file.Sub(index);
|
|
|
|
if (!file.Find(U".", &index, SearchPattern::RIGHT_LEFT))
|
|
return file;
|
|
|
|
return file.Sub(0, index - 1);
|
|
}
|
|
|
|
Str_16 BaseFile::ParseName_16(const Str_16& filePath)
|
|
{
|
|
UInt_64 index;
|
|
Str_16 file = filePath;
|
|
|
|
if (file.Find(L"/", &index, SearchPattern::RIGHT_LEFT) || file.Find(L"\\", &index, SearchPattern::RIGHT_LEFT))
|
|
file = file.Sub(index);
|
|
|
|
if (!file.Find(L".", &index, SearchPattern::RIGHT_LEFT))
|
|
return file;
|
|
|
|
return file.Sub(0, index - 1);
|
|
}
|
|
|
|
Str_8 BaseFile::ParseName_8(const Str_8& filePath)
|
|
{
|
|
UInt_64 index;
|
|
Str_8 file = filePath;
|
|
|
|
if (file.Find("/", &index, SearchPattern::RIGHT_LEFT) || file.Find("\\", &index, SearchPattern::RIGHT_LEFT))
|
|
file = file.Sub(index);
|
|
|
|
if (!file.Find(".", &index, SearchPattern::RIGHT_LEFT))
|
|
return file;
|
|
|
|
return file.Sub(0, index - 1);
|
|
}
|
|
|
|
Str_32 BaseFile::ParseExt_32(const Str_32& filePath)
|
|
{
|
|
UInt_64 index = 0;
|
|
|
|
filePath.Find(U".", &index, SearchPattern::RIGHT_LEFT);
|
|
|
|
return filePath.Sub(index);
|
|
}
|
|
|
|
Str_16 BaseFile::ParseExt_16(const Str_16& filePath)
|
|
{
|
|
UInt_64 index = 0;
|
|
|
|
filePath.Find(L".", &index, SearchPattern::RIGHT_LEFT);
|
|
|
|
return filePath.Sub(index);
|
|
}
|
|
|
|
Str_8 BaseFile::ParseExt_8(const Str_8& filePath)
|
|
{
|
|
UInt_64 index = 0;
|
|
|
|
filePath.Find(".", &index, SearchPattern::RIGHT_LEFT);
|
|
|
|
return filePath.Sub(index);
|
|
}
|
|
}
|