MassBuilderSaveTool/src/BinaryIo/Writer.h

113 lines
3.7 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/ArrayView.h>
#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Containers/StaticArray.h>
#include <Corrade/Containers/StringView.h>
2021-09-22 17:37:50 +02:00
using namespace Corrade;
namespace BinaryIo {
class Writer {
2021-09-22 17:37:50 +02:00
public:
explicit Writer(Containers::StringView filename);
~Writer();
2021-09-22 17:37:50 +02:00
Writer(const Writer& other) = delete;
Writer& operator=(const Writer& other) = delete;
2022-02-11 19:44:16 +01:00
Writer(Writer&& other) = default;
Writer& operator=(Writer&& other) = default;
2022-02-11 19:44:16 +01:00
2022-12-03 16:49:39 +01:00
bool open();
2021-09-22 17:37:50 +02:00
void closeFile();
auto position() -> std::int64_t;
2022-12-03 16:49:39 +01:00
auto array() const -> Containers::ArrayView<const char>;
auto arrayPosition() const -> std::size_t;
2022-12-03 16:49:39 +01:00
bool flushToFile();
bool writeChar(char value);
bool writeInt8(std::int8_t value);
bool writeUint8(std::uint8_t value);
bool writeInt16(std::int16_t value);
bool writeUint16(std::uint16_t value);
bool writeInt32(std::int32_t value);
bool writeUint32(std::uint32_t value);
bool writeInt64(std::int64_t value);
bool writeUint64(std::uint64_t value);
bool writeFloat(float value);
bool writeDouble(double value);
2022-12-03 16:49:39 +01:00
bool writeArray(Containers::ArrayView<const char> array);
template<std::size_t size>
2022-12-03 16:49:39 +01:00
bool writeString(const char(&str)[size]) {
return writeArray({str, size - 1});
}
2021-09-22 17:37:50 +02:00
template<std::size_t S>
2022-12-03 16:49:39 +01:00
bool writeStaticArray(Containers::StaticArrayView<S, const char> array) {
2021-09-22 17:37:50 +02:00
return std::fwrite(array.data(), sizeof(char), S, _file) == S;
}
2022-12-03 16:49:39 +01:00
bool writeUEString(Containers::StringView str);
2021-09-22 17:37:50 +02:00
template<typename T, typename U = std::conditional_t<std::is_trivially_copyable<T>::value, T, T&>>
auto writeValueToArray(U value) -> std::size_t {
Containers::ArrayView<T> view{&value, 1};
2021-09-22 17:37:50 +02:00
return writeDataToArray(view);
}
auto writeUEStringToArray(Containers::StringView value) -> std::size_t;
2021-09-22 17:37:50 +02:00
template<typename T>
void writeValueToArrayAt(T& value, std::size_t position) {
2021-09-22 17:37:50 +02:00
Containers::ArrayView<T> view{&value, 1};
writeDataToArrayAt(view, position);
}
template<typename T>
auto writeDataToArray(Containers::ArrayView<T> view) -> std::size_t {
2021-09-22 17:37:50 +02:00
arrayAppend(_data, Containers::arrayCast<const char>(view));
_index += sizeof(T) * view.size();
return sizeof(T) * view.size();
}
template<typename T>
void writeDataToArrayAt(Containers::ArrayView<T> view, std::size_t position) {
2021-09-22 17:37:50 +02:00
auto casted_view = Containers::arrayCast<const char>(view);
for(std::size_t i = 0; i < casted_view.size(); i++) {
2021-09-22 17:37:50 +02:00
_data[position + i] = casted_view[i];
}
}
private:
FILE* _file = nullptr;
Containers::Array<char> _data;
std::size_t _index = 0;
2021-09-22 17:37:50 +02:00
};
}