MassBuilderSaveTool/src/Gvas/BinaryWriter.cpp

164 lines
3.9 KiB
C++

// MassBuilderSaveTool
// Copyright (C) 2021-2023 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 <https://www.gnu.org/licenses/>.
#include <cstring>
#include "../Logger/Logger.h"
#include "BinaryWriter.h"
using namespace Containers::Literals;
namespace Gvas {
BinaryWriter::BinaryWriter(Containers::StringView filename) {
_file = std::fopen(filename.data(), "wb");
if(!_file) {
LOG_ERROR_FORMAT("Couldn't open {} for reading: {}", filename, std::strerror(errno));
}
}
BinaryWriter::~BinaryWriter() {
closeFile();
}
bool
BinaryWriter::open() {
return _file;
}
void
BinaryWriter::closeFile() {
std::fflush(_file);
std::fclose(_file);
_file = nullptr;
}
std::int64_t
BinaryWriter::position() {
return _ftelli64(_file);
}
Containers::ArrayView<const char>
BinaryWriter::array() const {
return _data;
}
std::size_t
BinaryWriter::arrayPosition() const {
return _index;
}
bool
BinaryWriter::flushToFile() {
bool ret = writeArray(_data);
std::fflush(_file);
_data = Containers::Array<char>{};
_index = 0;
return ret;
}
bool
BinaryWriter::writeChar(char value) {
return std::fwrite(&value, sizeof(char), 1, _file) == 1;
}
bool
BinaryWriter::writeInt8(std::int8_t value) {
return std::fwrite(&value, sizeof(std::int8_t), 1, _file) == 1;
}
bool
BinaryWriter::writeUint8(std::uint8_t value) {
return std::fwrite(&value, sizeof(std::uint8_t), 1, _file) == 1;
}
bool
BinaryWriter::writeInt16(std::int16_t value) {
return std::fwrite(&value, sizeof(std::int16_t), 1, _file) == 1;
}
bool
BinaryWriter::writeUint16(std::uint16_t value) {
return std::fwrite(&value, sizeof(std::uint16_t), 1, _file) == 1;
}
bool
BinaryWriter::writeInt32(std::int32_t value) {
return std::fwrite(&value, sizeof(std::int32_t), 1, _file) == 1;
}
bool
BinaryWriter::writeUint32(std::uint32_t value) {
return std::fwrite(&value, sizeof(std::uint32_t), 1, _file) == 1;
}
bool
BinaryWriter::writeInt64(std::int64_t value) {
return std::fwrite(&value, sizeof(std::int64_t), 1, _file) == 1;
}
bool
BinaryWriter::writeUint64(std::uint64_t value) {
return std::fwrite(&value, sizeof(std::uint64_t), 1, _file) == 1;
}
bool
BinaryWriter::writeFloat(float value) {
return std::fwrite(&value, sizeof(float), 1, _file) == 1;
}
bool
BinaryWriter::writeDouble(double value) {
return std::fwrite(&value, sizeof(double), 1, _file) == 1;
}
bool
BinaryWriter::writeArray(Containers::ArrayView<const char> array) {
if(array.isEmpty()) {
return false;
}
return std::fwrite(array.data(), sizeof(char), array.size(), _file) == array.size();
}
bool
BinaryWriter::writeUEString(Containers::StringView str) {
if(str.size() > UINT32_MAX) {
LOG_ERROR_FORMAT("String is too big. Expected size() < UINT32_MAX, got {} instead.", str.size());
return false;
}
writeUint32(static_cast<std::uint32_t>(str.size()) + 1);
if(str.size() > 0) {
std::size_t count = std::fwrite(str.data(), sizeof(char), str.size(), _file);
if(count != str.size()) {
return false;
}
}
return writeChar('\0');
}
std::size_t
BinaryWriter::writeUEStringToArray(Containers::StringView value) {
return writeValueToArray<std::uint32_t>(std::uint32_t(value.size()) + 1u) +
writeDataToArray(Containers::ArrayView<const char>{value}) +
writeValueToArray<char>('\0');
}
}