1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// SPDX-License-Identifier: BSD-3-Clause // Copyright (c) 2026 Fernando Sahmkow #include <whiteout/models/wem/writer.h> #include "../../common/binary_writer.h" #include "../../common/streams.h" #include "../../common/unicode_path.h" #include "binary_writer_visitor.h" #include <fstream> #include <stdexcept> namespace whiteout { namespace models { namespace wem { using common::BinaryWriter; // ============================================================================ // WriterImpl // ============================================================================ class Writer::Impl { public: void write(BinaryWriter& writer, const Model& model) { BinaryWriterVisitor visitor(writer); visitor.write(model); } }; // ============================================================================ // Writer Public Interface // ============================================================================ Writer::Writer() : pImpl(std::make_unique<Impl>()) {} Writer::~Writer() = default; bool Writer::write(const std::string& filePath, const Model& model) { auto file = common::open_ofstream(filePath, std::ios::binary); if (!file.is_open()) { return false; } BinaryWriter writer(file); pImpl->write(writer, model); return true; } std::vector<u8> Writer::write(const Model& model) { std::vector<u8> buffer; buffer.reserve(256 * 1024); // 256KB initial reserve common::vector_streambuf streambuf(buffer); std::ostream out(&streambuf); BinaryWriter writer(out); pImpl->write(writer, model); buffer.shrink_to_fit(); return buffer; } } // namespace wem } // namespace models } // namespace whiteout