Files
core_misc/tjp/core/file/writeSegmentsToFile.cpp
Timothy Prepscius fa54be052a flatten 20260225
2026-02-25 12:39:24 -05:00

54 lines
1.1 KiB
C++

// TJP COPYRIGHT HEADER
#include "writeSegmentsToFile.h"
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <tjp/core/exception/Exception.hpp>
#include <tjp/core/system/System.h>
#include <tjp/core/string/to_string.hpp>
#include "../random/Random.h"
#include <utime.h>
namespace tjp {
namespace core {
namespace file {
bool writeSegmentsToFile(const StringView &path_, const StackArray<containers::MemorySegment<u8>> &segments, const WriteOptions &options)
{
auto path = String(path_);
auto extension = "." + core::to_string(core::StandardRandom.next<u32>());
auto temporary = path + extension;
std::ofstream file(temporary, std::ios::binary);
if (!file)
return false;
for (auto &contents: segments)
file.write((char *)contents.data(), contents.size());
file.close();
remove(path.c_str());
if (rename(temporary.c_str(), path.c_str()))
return false;
if (options.lastModifiedTime)
{
setFileLastModifiedTime(path, options.lastModifiedTime);
}
return true;
}
} // namespace
} // namespace
} // namespace