#pragma once // 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 . #include #include #include #include #include #include using namespace Corrade; namespace BinaryIo { class Writer { public: explicit Writer(Containers::StringView filename); ~Writer(); Writer(const Writer& other) = delete; Writer& operator=(const Writer& other) = delete; Writer(Writer&& other) = default; Writer& operator=(Writer&& other) = default; bool open(); void closeFile(); auto position() -> std::int64_t; auto array() const -> Containers::ArrayView; auto arrayPosition() const -> std::size_t; 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); bool writeArray(Containers::ArrayView array); template bool writeString(const char(&str)[size]) { return writeArray({str, size - 1}); } template bool writeStaticArray(Containers::StaticArrayView array) { return std::fwrite(array.data(), sizeof(char), S, _file) == S; } bool writeUEString(Containers::StringView str); template::value, T, T&>> auto writeValueToArray(U value) -> std::size_t { Containers::ArrayView view{&value, 1}; return writeDataToArray(view); } auto writeUEStringToArray(Containers::StringView value) -> std::size_t; template void writeValueToArrayAt(T& value, std::size_t position) { Containers::ArrayView view{&value, 1}; writeDataToArrayAt(view, position); } template auto writeDataToArray(Containers::ArrayView view) -> std::size_t { arrayAppend(_data, Containers::arrayCast(view)); _index += sizeof(T) * view.size(); return sizeof(T) * view.size(); } template void writeDataToArrayAt(Containers::ArrayView view, std::size_t position) { auto casted_view = Containers::arrayCast(view); for(std::size_t i = 0; i < casted_view.size(); i++) { _data[position + i] = casted_view[i]; } } private: FILE* _file = nullptr; Containers::Array _data; std::size_t _index = 0; }; }