// 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 "BinaryWriter.h" BinaryWriter::BinaryWriter(const std::string& filename) { _file = std::fopen(filename.c_str(), "wb"); if(!_file) { Utility::Error{} << "Couldn't open" << filename.c_str() << "for reading:\n" << std::strerror(errno); } } BinaryWriter::~BinaryWriter() { closeFile(); } auto BinaryWriter::open() -> bool { return _file; } void BinaryWriter::closeFile() { std::fflush(_file); std::fclose(_file); _file = nullptr; } auto BinaryWriter::position() -> Long { return _ftelli64(_file); } auto BinaryWriter::arrayPosition() const -> UnsignedLong { return _index; } auto BinaryWriter::flushToFile() -> bool { bool ret = writeArray(_data); std::fflush(_file); _data = Containers::Array{}; _index = 0; return ret; } auto BinaryWriter::writeChar(char value) -> bool { return std::fwrite(&value, sizeof(char), 1, _file) == 1; } auto BinaryWriter::writeByte(Byte value) -> bool { return std::fwrite(&value, sizeof(Byte), 1, _file) == 1; } auto BinaryWriter::writeUnsignedByte(UnsignedByte value) -> bool { return std::fwrite(&value, sizeof(UnsignedByte), 1, _file) == 1; } auto BinaryWriter::writeShort(Short value) -> bool { return std::fwrite(&value, sizeof(Short), 1, _file) == 1; } auto BinaryWriter::writeUnsignedShort(UnsignedShort value) -> bool { return std::fwrite(&value, sizeof(UnsignedShort), 1, _file) == 1; } auto BinaryWriter::writeInt(Int value) -> bool { return std::fwrite(&value, sizeof(Int), 1, _file) == 1; } auto BinaryWriter::writeUnsignedInt(UnsignedInt value) -> bool { return std::fwrite(&value, sizeof(UnsignedInt), 1, _file) == 1; } auto BinaryWriter::writeLong(Long value) -> bool { return std::fwrite(&value, sizeof(Long), 1, _file) == 1; } auto BinaryWriter::writeUnsignedLong(UnsignedLong value) -> bool { return std::fwrite(&value, sizeof(UnsignedLong), 1, _file) == 1; } auto BinaryWriter::writeFloat(Float value) -> bool { return std::fwrite(&value, sizeof(Float), 1, _file) == 1; } auto BinaryWriter::writeDouble(Double value) -> bool { return std::fwrite(&value, sizeof(Double), 1, _file) == 1; } auto BinaryWriter::writeArray(Containers::ArrayView array) -> bool { if(array.size() == 0) { return false; } return std::fwrite(array.data(), sizeof(char), array.size(), _file) == array.size(); } auto BinaryWriter::writeUEString(const std::string& str) -> bool { if(str.length() > UINT32_MAX) { Utility::Error{} << "BinaryWriter::writeUEString(): string is too big."; return false; } writeUnsignedInt(static_cast(str.length()) + 1); if(str.length() > 0) { std::size_t count = std::fwrite(&str[0], sizeof(char), str.length(), _file); if(count != str.length()) { return false; } } return writeChar('\0'); } auto BinaryWriter::writeUEStringToArray(const std::string& value) -> UnsignedLong { Containers::ArrayView view{value.c_str(), value.length()}; return writeValueToArray(UnsignedInt(value.length()) + 1u) + writeDataToArray(view) + writeValueToArray('\0'); }