blob: 90bf64b64d67f0a53b1756e196ff4de4ce91cc3b [file] [log] [blame]
#include "FileHandle.hpp"
#include <fcntl.h>
#include <unistd.h>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <string>
FileHandle::FileHandle(const std::filesystem::path& name,
std::ios_base::openmode mode) :
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
fd(open(name.c_str(), mode))
{
if (fd < 0)
{
throw std::out_of_range(name.string() + " failed to open");
}
}
FileHandle::FileHandle(int fdIn) : fd(fdIn) {};
FileHandle::FileHandle(FileHandle&& in) noexcept : fd(in.fd)
{
in.fd = -1;
}
FileHandle& FileHandle::operator=(FileHandle&& in) noexcept
{
fd = in.fd;
in.fd = -1;
return *this;
}
FileHandle::~FileHandle()
{
if (fd >= 0)
{
int r = close(fd);
if (r < 0)
{
std::cerr << "Failed to close fd " << std::to_string(fd);
}
}
}
int FileHandle::handle() const
{
return fd;
}