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 53 of file file.cpp.

54 : m_mutex(mutex)
55{
56#if defined(_WIN32)
57 m_handle = {CreateFile(file.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)};
58 if(m_handle == INVALID_HANDLE_VALUE)
59 {
60 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");
61 m_null = true;
62 }
63 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)");
64 Term::Private::Errno().check_if(nullptr == (m_file = _fdopen(m_fd, mode.c_str()))).throw_exception("_fdopen(m_fd, mode.c_str())");
65#else
66 std::size_t flag{O_ASYNC | O_DSYNC | O_NOCTTY | O_SYNC | O_NDELAY};
67 flag &= ~static_cast<std::size_t>(O_NONBLOCK);
68 if(mode.find('r') != std::string::npos) { flag |= O_RDONLY; } //NOLINT(abseil-string-find-str-contains)
69 else if(mode.find('w') != std::string::npos) { flag |= O_WRONLY; } //NOLINT(abseil-string-find-str-contains)
70 else { flag |= O_RDWR; }
71 m_fd = {::open(file.c_str(), static_cast<int>(flag))}; //NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
72 if(m_fd == -1)
73 {
74 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)
75 m_null = true;
76 }
77 Term::Private::Errno().check_if(nullptr == (m_file = ::fdopen(m_fd, mode.c_str()))).throw_exception("::fdopen(m_fd, mode.c_str())");
79#endif
80 Term::Private::Errno().check_if(std::setvbuf(m_file, nullptr, _IONBF, 0) != 0).throw_exception("std::setvbuf(m_file, nullptr, _IONBF, 0)");
81}
82catch(...)
83{
85}
std::recursive_mutex & m_mutex
Definition file.hpp:49
std::FILE * file() const noexcept
Definition file.cpp:124
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 87 of file file.cpp.

89{
90 flush();
91 Term::Private::Errno().check_if(0 != std::fclose(m_file)).throw_exception("std::fclose(m_file)"); //NOLINT(cppcoreguidelines-owning-memory)
92}
93catch(...)
94{
96}

Member Function Documentation

◆ fd()

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

Definition at line 126 of file file.cpp.

126{ return m_fd; }

◆ file()

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

Definition at line 124 of file file.cpp.

124{ return m_file; }

◆ flush()

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

Definition at line 186 of file file.cpp.

186{ 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 128 of file file.cpp.

128{ return m_handle; }

◆ lockIO()

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

Definition at line 188 of file file.cpp.

188{ m_mutex.lock(); }

◆ null()

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

Definition at line 122 of file file.cpp.

122{ 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 189 of file file.cpp.

189{ 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: