#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 . #include #include #include #include #include #include using namespace Corrade; using namespace Magnum; class BinaryWriter { public: explicit BinaryWriter(Containers::StringView filename); ~BinaryWriter(); BinaryWriter(const BinaryWriter& other) = delete; BinaryWriter& operator=(const BinaryWriter& other) = delete; BinaryWriter(BinaryWriter&& other) = default; BinaryWriter& operator=(BinaryWriter&& other) = default; auto open() -> bool; void closeFile(); auto position() -> Long; auto array() const -> Containers::ArrayView; auto arrayPosition() const -> UnsignedLong; auto flushToFile() -> bool; auto writeByte(Byte value) -> bool; auto writeChar(char value) -> bool; auto writeUnsignedByte(UnsignedByte value) -> bool; auto writeShort(Short value) -> bool; auto writeUnsignedShort(UnsignedShort value) -> bool; auto writeInt(Int value) -> bool; auto writeUnsignedInt(UnsignedInt value) -> bool; auto writeLong(Long value) -> bool; auto writeUnsignedLong(UnsignedLong value) -> bool; auto writeFloat(Float value) -> bool; auto writeDouble(Double value) -> bool; auto writeArray(Containers::ArrayView array) -> bool; template auto writeString(const char(&str)[size]) -> bool { return writeArray({str, size - 1}); } template auto writeStaticArray(Containers::StaticArrayView array) -> bool { return std::fwrite(array.data(), sizeof(char), S, _file) == S; } auto writeUEString(Containers::StringView str) -> bool; template::value, T, T&>> auto writeValueToArray(U value) -> UnsignedLong { Containers::ArrayView view{&value, 1}; return writeDataToArray(view); } auto writeUEStringToArray(Containers::StringView value) -> UnsignedLong; template void writeValueToArrayAt(T& value, UnsignedLong position) { Containers::ArrayView view{&value, 1}; writeDataToArrayAt(view, position); } template auto writeDataToArray(Containers::ArrayView view) -> UnsignedLong { arrayAppend(_data, Containers::arrayCast(view)); _index += sizeof(T) * view.size(); return sizeof(T) * view.size(); } template void writeDataToArrayAt(Containers::ArrayView view, UnsignedLong position) { auto casted_view = Containers::arrayCast(view); for(UnsignedLong i = 0; i < casted_view.size(); i++) { _data[position + i] = casted_view[i]; } } private: FILE* _file = nullptr; Containers::Array _data; UnsignedLong _index = 0; };