cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
file.hpp
Go to the documentation of this file.
1/*
2* cpp-terminal
3* C++ library for writing multi-platform terminal applications.
4*
5* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
6*
7* SPDX-License-Identifier: MIT
8*/
9
10#pragma once
11
13
14#include <cstddef>
15#include <cstdint>
16#include <mutex>
17#include <string>
18
19namespace Term
20{
21
22namespace Private
23{
24
26{
27public:
28#if defined(_WIN32)
29 using Handle = void*;
30#else
31 using Handle = std::FILE*;
32#endif
33 FileHandler(std::recursive_mutex& mutex, const std::string& file, const std::string& mode) noexcept;
34 FileHandler(const FileHandler&) = delete;
38 virtual ~FileHandler() noexcept;
39 Handle handle() const noexcept;
40 bool null() const noexcept;
41 std::FILE* file() const noexcept;
42 std::int32_t fd() const noexcept;
43 void lockIO();
44 void unlockIO();
45 void flush();
46
47private:
48 // should be static but MacOS don't want it (crash at runtime)
49 std::recursive_mutex& m_mutex; //NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
50 bool m_null{false};
51 Handle m_handle{nullptr};
52 FILE* m_file{nullptr};
53 std::int32_t m_fd{-1};
54};
55
57{
58public:
59 explicit OutputFileHandler(std::recursive_mutex& io_mutex) noexcept;
60 OutputFileHandler(const OutputFileHandler& other) = delete;
64 ~OutputFileHandler() override = default;
65
66 std::size_t write(const std::string& str) const;
67 std::size_t write(const char& character) const;
68#if defined(_WIN32)
69 static const constexpr char* m_file{"CONOUT$"};
70#else
71 static const constexpr char* m_file{"/dev/tty"};
72#endif
73};
74
76{
77public:
78 explicit InputFileHandler(std::recursive_mutex& io_mutex) noexcept;
83 ~InputFileHandler() override = default;
84
85 std::string read() const;
86#if defined(_WIN32)
87 static const constexpr char* m_file{"CONIN$"};
88#else
89 static const constexpr char* m_file{"/dev/tty"};
90#endif
91};
92
93extern InputFileHandler& in;
94extern OutputFileHandler& out;
95static const FileInitializer file_initializer;
96
97std::string ask(const std::string& str);
98
99} // namespace Private
100
101} // namespace Term
FileHandler(std::recursive_mutex &mutex, const std::string &file, const std::string &mode) noexcept
Definition file.cpp:48
Handle handle() const noexcept
Definition file.cpp:99
virtual ~FileHandler() noexcept
Definition file.cpp:82
std::int32_t fd() const noexcept
Definition file.cpp:97
FileHandler & operator=(const FileHandler &)=delete
FileHandler(const FileHandler &)=delete
FileHandler & operator=(FileHandler &&)=delete
FileHandler(FileHandler &&)=delete
std::recursive_mutex & m_mutex
Definition file.hpp:49
bool null() const noexcept
Definition file.cpp:93
std::FILE * file() const noexcept
Definition file.cpp:95
InputFileHandler(const InputFileHandler &)=delete
std::string read() const
Definition file.cpp:127
InputFileHandler(InputFileHandler &&)=delete
InputFileHandler & operator=(InputFileHandler &&)=delete
~InputFileHandler() override=default
static const constexpr char * m_file
Definition file.hpp:87
InputFileHandler(std::recursive_mutex &io_mutex) noexcept
Definition file.cpp:172
InputFileHandler & operator=(const InputFileHandler &)=delete
OutputFileHandler(std::recursive_mutex &io_mutex) noexcept
Definition file.cpp:162
OutputFileHandler & operator=(OutputFileHandler &&rhs)=delete
~OutputFileHandler() override=default
OutputFileHandler & operator=(const OutputFileHandler &rhs)=delete
OutputFileHandler(OutputFileHandler &&other)=delete
static const constexpr char * m_file
Definition file.hpp:69
OutputFileHandler(const OutputFileHandler &other)=delete
std::size_t write(const std::string &str) const
Definition file.cpp:101
InputFileHandler & in
Definition file.cpp:43
std::string ask(const std::string &str)
Definition file.cpp:182
OutputFileHandler & out
Definition file.cpp:44
Definition args.cpp:13