MassBuilderSaveTool/src/UESaveFile/BinaryReader.h

71 lines
2.2 KiB
C++

#pragma once
// 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 <cstdint>
#include <cstdio>
#include <Corrade/Containers/Containers.h>
#include <Corrade/Containers/StaticArray.h>
#include <Corrade/Containers/StringView.h>
using namespace Corrade;
class BinaryReader {
public:
explicit BinaryReader(Containers::StringView filename);
~BinaryReader();
bool open();
bool eof();
std::int64_t position();
bool seek(std::int64_t position);
void closeFile();
bool readChar(char& value);
bool readInt8(std::int8_t& value);
bool readUint8(std::uint8_t& value);
bool readInt16(std::int16_t& value);
bool readUint16(std::uint16_t& value);
bool readInt32(std::int32_t& value);
bool readUint32(std::uint32_t& value);
bool readInt64(std::int64_t& value);
bool readUint64(std::uint64_t& value);
bool readFloat(float& value);
bool readDouble(double& value);
bool readArray(Containers::Array<char>& array, std::size_t count);
template<typename T>
bool readValue(T& value) {
return fread(&value, sizeof(T), 1, _file) == sizeof(T);
}
template<std::size_t S>
bool readStaticArray(Containers::StaticArray<S, char>& array) {
return std::fread(array.data(), sizeof(char), S, _file) == S;
}
bool readUEString(Containers::String& str);
std::int32_t peekChar();
private:
std::FILE* _file = nullptr;
};