MassBuilderSaveTool/src/BinaryIo/Reader.h

81 lines
2.4 KiB
C
Raw Normal View History

2021-09-22 17:37:50 +02:00
#pragma once
// MassBuilderSaveTool
// Copyright (C) 2021-2024 Guillaume Jacquemin
2021-09-22 17:37:50 +02:00
//
// 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>
2021-09-22 17:37:50 +02:00
#include <cstdio>
#include <Corrade/Containers/Containers.h>
2021-09-22 17:37:50 +02:00
#include <Corrade/Containers/StaticArray.h>
#include <Corrade/Containers/StringView.h>
2021-09-22 17:37:50 +02:00
using namespace Corrade;
namespace BinaryIo {
class Reader {
2021-09-22 17:37:50 +02:00
public:
explicit Reader(Containers::StringView filename);
~Reader();
Reader(const Reader& other) = delete;
Reader& operator=(const Reader& other) = delete;
Reader(Reader&& other) = default;
Reader& operator=(Reader&& other) = default;
2021-09-22 17:37:50 +02:00
2022-12-03 16:49:39 +01:00
bool open();
bool eof();
auto position() -> std::int64_t;
2021-09-22 17:37:50 +02:00
bool seek(std::int64_t position);
2021-09-22 17:37:50 +02:00
void closeFile();
2022-12-03 16:49:39 +01:00
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);
2022-12-03 16:49:39 +01:00
bool readArray(Containers::Array<char>& array, std::size_t count);
2021-09-22 17:37:50 +02:00
template<typename T>
2022-12-03 16:49:39 +01:00
bool readValue(T& value) {
return fread(&value, sizeof(T), 1, _file) == 1;
}
2021-09-22 17:37:50 +02:00
template<std::size_t S>
2022-12-03 16:49:39 +01:00
bool readStaticArray(Containers::StaticArray<S, char>& array) {
2021-09-22 17:37:50 +02:00
return std::fread(array.data(), sizeof(char), S, _file) == S;
}
2022-12-03 16:49:39 +01:00
bool readUEString(Containers::String& str);
2021-09-22 17:37:50 +02:00
auto peekChar() -> std::int32_t;
2021-09-22 17:37:50 +02:00
private:
std::FILE* _file = nullptr;
};
}