MassBuilderSaveTool/src/UESaveFile/BinaryReader.cpp

128 lines
3.5 KiB
C++

// MassBuilderSaveTool
// Copyright (C) 2021-2022 Guillaume Jacquemin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cstring>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/String.h>
#include "../Logger/Logger.h"
#include "BinaryReader.h"
BinaryReader::BinaryReader(Containers::StringView filename) {
_file = std::fopen(filename.data(), "rb");
if(!_file) {
LOG_ERROR_FORMAT("Couldn't open {} for reading: {}", filename, std::strerror(errno));
}
}
BinaryReader::~BinaryReader() {
closeFile();
}
auto BinaryReader::open() -> bool {
return _file;
}
auto BinaryReader::eof() -> bool {
return std::feof(_file) != 0;
}
auto BinaryReader::position() -> Long {
return _ftelli64(_file);
}
auto BinaryReader::seek(Long position) -> bool {
return _fseeki64(_file, position, SEEK_SET) == 0;
}
void BinaryReader::closeFile() {
std::fclose(_file);
_file = nullptr;
}
auto BinaryReader::readChar(char& value) -> bool {
return std::fread(&value, sizeof(char), 1, _file) == 1;
}
auto BinaryReader::readByte(Byte& value) -> bool {
return std::fread(&value, sizeof(Byte), 1, _file) == 1;
}
auto BinaryReader::readUnsignedByte(UnsignedByte& value) -> bool {
return std::fread(&value, sizeof(UnsignedByte), 1, _file) == 1;
}
auto BinaryReader::readShort(Short& value) -> bool {
return std::fread(&value, sizeof(Short), 1, _file) == 1;
}
auto BinaryReader::readUnsignedShort(UnsignedShort& value) -> bool {
return std::fread(&value, sizeof(UnsignedShort), 1, _file) == 1;
}
auto BinaryReader::readInt(Int& value) -> bool {
return std::fread(&value, sizeof(Int), 1, _file) == 1;
}
auto BinaryReader::readUnsignedInt(UnsignedInt& value) -> bool {
return std::fread(&value, sizeof(UnsignedInt), 1, _file) == 1;
}
auto BinaryReader::readLong(Long& value) -> bool {
return std::fread(&value, sizeof(Long), 1, _file) == 1;
}
auto BinaryReader::readUnsignedLong(UnsignedLong& value) -> bool {
return std::fread(&value, sizeof(UnsignedLong), 1, _file) == 1;
}
auto BinaryReader::readFloat(Float& value) -> bool {
return std::fread(&value, sizeof(Float), 1, _file) == 1;
}
auto BinaryReader::readDouble(Double& value) -> bool {
return std::fread(&value, sizeof(Double), 1, _file) == 1;
}
auto BinaryReader::readArray(Containers::Array<char>& array, std::size_t count) -> bool {
if(array.size() < count) {
array = Containers::Array<char>{ValueInit, count};
}
return std::fread(array.data(), sizeof(char), count, _file) == count;
}
auto BinaryReader::readUEString(Containers::String& str) -> bool {
UnsignedInt length = 0;
if(!readUnsignedInt(length) || length == 0) {
return false;
}
str = Containers::String{ValueInit, length - 1};
return std::fread(str.data(), sizeof(char), length, _file) == length;
}
auto BinaryReader::peekChar() -> Int {
Int c;
c = std::fgetc(_file);
std::ungetc(c, _file);
return c;
}