151 lines
3.3 KiB
C++
151 lines
3.3 KiB
C++
// MassBuilderSaveTool
|
|
// Copyright (C) 2021-2024 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 "Reader.h"
|
|
|
|
namespace BinaryIo {
|
|
|
|
Reader::Reader(Containers::StringView filename) {
|
|
_file = std::fopen(filename.data(), "rb");
|
|
|
|
if(!_file) {
|
|
LOG_ERROR_FORMAT("Couldn't open {} for reading: {}", filename, std::strerror(errno));
|
|
}
|
|
}
|
|
|
|
Reader::~Reader() {
|
|
closeFile();
|
|
}
|
|
|
|
bool
|
|
Reader::open() {
|
|
return _file;
|
|
}
|
|
|
|
bool
|
|
Reader::eof() {
|
|
return std::feof(_file) != 0;
|
|
}
|
|
|
|
std::int64_t
|
|
Reader::position() {
|
|
return _ftelli64(_file);
|
|
}
|
|
|
|
bool
|
|
Reader::seek(std::int64_t position) {
|
|
return _fseeki64(_file, position, SEEK_SET) == 0;
|
|
}
|
|
|
|
void
|
|
Reader::closeFile() {
|
|
std::fclose(_file);
|
|
_file = nullptr;
|
|
}
|
|
|
|
bool
|
|
Reader::readChar(char& value) {
|
|
return std::fread(&value, sizeof(char), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readInt8(std::int8_t& value) {
|
|
return std::fread(&value, sizeof(std::int8_t), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readUint8(std::uint8_t& value) {
|
|
return std::fread(&value, sizeof(std::uint8_t), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readInt16(std::int16_t& value) {
|
|
return std::fread(&value, sizeof(std::int16_t), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readUint16(std::uint16_t& value) {
|
|
return std::fread(&value, sizeof(std::uint16_t), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readInt32(std::int32_t& value) {
|
|
return std::fread(&value, sizeof(std::int32_t), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readUint32(std::uint32_t& value) {
|
|
return std::fread(&value, sizeof(std::uint32_t), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readInt64(std::int64_t& value) {
|
|
return std::fread(&value, sizeof(std::int64_t), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readUint64(std::uint64_t& value) {
|
|
return std::fread(&value, sizeof(std::uint64_t), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readFloat(float& value) {
|
|
return std::fread(&value, sizeof(float), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readDouble(double& value) {
|
|
return std::fread(&value, sizeof(double), 1, _file) == 1;
|
|
}
|
|
|
|
bool
|
|
Reader::readArray(Containers::Array<char>& array, std::size_t count) {
|
|
if(array.size() < count) {
|
|
array = Containers::Array<char>{ValueInit, count};
|
|
}
|
|
|
|
return std::fread(array.data(), sizeof(char), count, _file) == count;
|
|
}
|
|
|
|
bool
|
|
Reader::readUEString(Containers::String& str) {
|
|
std::uint32_t length = 0;
|
|
if(!readUint32(length) || length == 0) {
|
|
return false;
|
|
}
|
|
|
|
str = Containers::String{ValueInit, length - 1};
|
|
|
|
return std::fread(str.data(), sizeof(char), length, _file) == length;
|
|
}
|
|
|
|
std::int32_t
|
|
Reader::peekChar() {
|
|
std::int32_t c;
|
|
c = std::fgetc(_file);
|
|
std::ungetc(c, _file);
|
|
return c;
|
|
}
|
|
|
|
}
|