Files
ExPkg/expkg/src/BinaryOPT/BinaryWriter.cpp
2025-08-06 18:06:30 +08:00

51 lines
1.2 KiB
C++

//
// Created by sfd on 25-8-4.
//
#include "BinaryWriter.h"
#include <filesystem>
#include <iostream>
namespace PKG
{
BinaryWriter::BinaryWriter(const std::filesystem::path& fileName, const std::ios_base::openmode optMode)
{
m_FilePath = fileName.string();
const std::filesystem::path path(fileName.parent_path());
m_File.open(m_FilePath, optMode);
if (!m_File.is_open() && !std::filesystem::exists(path))
{
std::filesystem::create_directories(path);
m_File.open(m_FilePath, optMode);
if (!m_File.is_open())
std::cerr << "cound not create file: " << m_FilePath << std::endl;
}
}
BinaryWriter::~BinaryWriter()
{
if (!m_File.is_open())
close();
}
void BinaryWriter::WriteBytes(const std::string& data, const uint32_t size)
{
if (size == 0)
m_File.write(data.data(), data.size());
else
m_File.write(data.data(), size);
}
void BinaryWriter::WriteString(const std::string& str)
{
m_File.write(str.c_str(), str.size());
}
void BinaryWriter::close()
{
m_File.close();
}
}