cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
Term::Private::FileHandler Class Reference

#include <cpp-terminal/private/file.hpp>

Inheritance diagram for Term::Private::FileHandler:
Term::Private::InputFileHandler Term::Private::OutputFileHandler

Public Types

using Handle = void*
 

Public Member Functions

 FileHandler (std::recursive_mutex &mutex, const std::string &file, const std::string &mode) noexcept
 
 FileHandler (const FileHandler &)=delete
 
 FileHandler (FileHandler &&)=delete
 
FileHandleroperator= (const FileHandler &)=delete
 
FileHandleroperator= (FileHandler &&)=delete
 
virtual ~FileHandler () noexcept
 
Handle handle () const noexcept
 
bool null () const noexcept
 
std::FILE * file () const noexcept
 
std::int32_t fd () const noexcept
 
void lockIO ()
 
void unlockIO ()
 
void flush ()
 

Private Attributes

std::recursive_mutex & m_mutex
 
bool m_null {false}
 
Handle m_handle {nullptr}
 
FILE * m_file {nullptr}
 
std::int32_t m_fd {-1}
 

Detailed Description

Definition at line 25 of file file.hpp.

Member Typedef Documentation

◆ Handle

Definition at line 29 of file file.hpp.

Constructor & Destructor Documentation

◆ FileHandler() [1/3]

Term::Private::FileHandler::FileHandler ( std::recursive_mutex & mutex,
const std::string & file,
const std::string & mode )
noexcept

Definition at line 48 of file file.cpp.

49 : m_mutex(mutex)
50{
51#if defined(_WIN32)
52 m_handle = {CreateFile(file.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)};
53 if(m_handle == INVALID_HANDLE_VALUE)
54 {
55 Term::Private::WindowsError().check_if((m_handle = CreateFile("NUL", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)) == INVALID_HANDLE_VALUE).throw_exception("Problem opening NUL");
56 m_null = true;
57 }
58 Term::Private::Errno().check_if((m_fd = _open_osfhandle(reinterpret_cast<intptr_t>(m_handle), _O_RDWR)) == -1).throw_exception("_open_osfhandle(reinterpret_cast<intptr_t>(m_handle), _O_RDWR)");
59 Term::Private::Errno().check_if(nullptr == (m_file = _fdopen(m_fd, mode.c_str()))).throw_exception("_fdopen(m_fd, mode.c_str())");
60#else
61 std::size_t flag{O_ASYNC | O_DSYNC | O_NOCTTY | O_SYNC | O_NDELAY};
62 flag &= ~static_cast<std::size_t>(O_NONBLOCK);
63 if(mode.find('r') != std::string::npos) { flag |= O_RDONLY; } //NOLINT(abseil-string-find-str-contains)
64 else if(mode.find('w') != std::string::npos) { flag |= O_WRONLY; } //NOLINT(abseil-string-find-str-contains)
65 else { flag |= O_RDWR; }
66 m_fd = {::open(file.c_str(), static_cast<int>(flag))}; //NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
67 if(m_fd == -1)
68 {
69 Term::Private::Errno().check_if((m_fd = ::open("/dev/null", static_cast<int>(flag))) == -1).throw_exception(R"(::open("/dev/null", flag))"); //NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
70 m_null = true;
71 }
72 Term::Private::Errno().check_if(nullptr == (m_file = ::fdopen(m_fd, mode.c_str()))).throw_exception("::fdopen(m_fd, mode.c_str())");
74#endif
75 Term::Private::Errno().check_if(std::setvbuf(m_file, nullptr, _IONBF, 0) != 0).throw_exception("std::setvbuf(m_file, nullptr, _IONBF, 0)");
76}
77catch(...)
78{
80}
void throw_exception(const std::string &str={}) const
Errno & check_if(const bool &ret) noexcept
std::recursive_mutex & m_mutex
Definition file.hpp:49
std::FILE * file() const noexcept
Definition file.cpp:95
WindowsError & check_if(const bool &ret) noexcept
Definition exception.cpp:75
void throw_exception(const std::string &str=std::string()) const
Definition exception.cpp:82
void ExceptionHandler(const ExceptionDestination &destination=ExceptionDestination::StdErr) noexcept

◆ FileHandler() [2/3]

Term::Private::FileHandler::FileHandler ( const FileHandler & )
delete

◆ FileHandler() [3/3]

Term::Private::FileHandler::FileHandler ( FileHandler && )
delete

◆ ~FileHandler()

Term::Private::FileHandler::~FileHandler ( )
virtualnoexcept

Definition at line 82 of file file.cpp.

84{
85 flush();
86 Term::Private::Errno().check_if(0 != std::fclose(m_file)).throw_exception("std::fclose(m_file)"); //NOLINT(cppcoreguidelines-owning-memory)
87}
88catch(...)
89{
91}

Member Function Documentation

◆ fd()

std::int32_t Term::Private::FileHandler::fd ( ) const
noexcept

Definition at line 97 of file file.cpp.

97{ return m_fd; }

◆ file()

std::FILE * Term::Private::FileHandler::file ( ) const
noexcept

Definition at line 95 of file file.cpp.

95{ return m_file; }

◆ flush()

void Term::Private::FileHandler::flush ( )

Definition at line 157 of file file.cpp.

157{ Term::Private::Errno().check_if(0 != std::fflush(m_file)).throw_exception("std::fflush(m_file)"); }

◆ handle()

Term::Private::FileHandler::Handle Term::Private::FileHandler::handle ( ) const
noexcept

Definition at line 99 of file file.cpp.

99{ return m_handle; }

◆ lockIO()

void Term::Private::FileHandler::lockIO ( )

Definition at line 159 of file file.cpp.

159{ m_mutex.lock(); }

◆ null()

bool Term::Private::FileHandler::null ( ) const
noexcept

Definition at line 93 of file file.cpp.

93{ return m_null; }

◆ operator=() [1/2]

FileHandler & Term::Private::FileHandler::operator= ( const FileHandler & )
delete

◆ operator=() [2/2]

FileHandler & Term::Private::FileHandler::operator= ( FileHandler && )
delete

◆ unlockIO()

void Term::Private::FileHandler::unlockIO ( )

Definition at line 160 of file file.cpp.

160{ m_mutex.unlock(); }

Member Data Documentation

◆ m_fd

std::int32_t Term::Private::FileHandler::m_fd {-1}
private

Definition at line 53 of file file.hpp.

53{-1};

◆ m_file

FILE* Term::Private::FileHandler::m_file {nullptr}
private

Definition at line 52 of file file.hpp.

52{nullptr};

◆ m_handle

Handle Term::Private::FileHandler::m_handle {nullptr}
private

Definition at line 51 of file file.hpp.

51{nullptr};

◆ m_mutex

std::recursive_mutex& Term::Private::FileHandler::m_mutex
private

Definition at line 49 of file file.hpp.

◆ m_null

bool Term::Private::FileHandler::m_null {false}
private

Definition at line 50 of file file.hpp.

50{false};

The documentation for this class was generated from the following files: