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

Classes

class  BlockingQueue
 
class  Errno
 
class  ErrnoException
 
class  FileHandler
 
class  FileInitializer
 
class  Input
 
class  InputFileHandler
 
class  OutputFileHandler
 
class  Signals
 
class  Sigwinch
 
class  WindowsError
 
class  WindowsException
 

Enumerations

enum class  ExceptionDestination : std::uint8_t { MessageBox = 0 , StdErr }
 

Functions

std::uint8_t utf8_decode_step (std::uint8_t state, std::uint8_t octet, std::uint32_t *cpp)
 
std::u32string utf8_to_utf32 (const std::string &str)
 
bool is_valid_utf8_code_unit (const std::string &str)
 
std::pair< bool, std::string > getenv (const std::string &env)
 Value of an environment variables.
 
void ExceptionHandler (const ExceptionDestination &destination=ExceptionDestination::StdErr) noexcept
 
std::string ask (const std::string &str)
 
std::string to_narrow (const std::wstring &wstr)
 
std::wstring to_wide (const std::string &str)
 
std::string utf32_to_utf8 (const char32_t &codepoint, const bool &exception=false)
 Encode a codepoint using UTF-8 std::string .
 
std::string utf32_to_utf8 (const std::u32string &str, const bool &exception=false)
 Encode a std::u32string into UTF-8 std::string .
 

Variables

InputFileHandlerin = reinterpret_cast<Term::Private::InputFileHandler&>(stdin_buffer)
 
OutputFileHandlerout = reinterpret_cast<Term::Private::OutputFileHandler&>(stdout_buffer)
 
volatile std::sig_atomic_t m_signalStatus {0}
 

Enumeration Type Documentation

◆ ExceptionDestination

enum class Term::Private::ExceptionDestination : std::uint8_t
strong
Enumerator
MessageBox 
StdErr 

Definition at line 87 of file exception.hpp.

Function Documentation

◆ ask()

std::string Term::Private::ask ( const std::string & str)

Definition at line 191 of file file.cpp.

192{
193 Term::Private::out.write(str);
194 std::string ret{Term::Private::in.read()};
195 for(std::size_t i = 0; i != ret.size(); ++i) { Term::Private::out.write("\b \b"); }
196 return ret;
197}
InputFileHandler & in
Definition file.cpp:43
OutputFileHandler & out
Definition file.cpp:44

◆ ExceptionHandler()

void Term::Private::ExceptionHandler ( const ExceptionDestination & destination = ExceptionDestination::StdErr)
noexcept

Definition at line 177 of file exception.cpp.

178{
179 try
180 {
181 std::exception_ptr exception{std::current_exception()};
182 if(exception != nullptr) { std::rethrow_exception(exception); }
183 }
184 catch(const Term::Exception& exception)
185 {
186 switch(destination)
187 {
189#if defined(_WIN32)
190 MessageBoxW(nullptr, Term::Private::to_wide(exception.what()).c_str(), Term::Private::to_wide("cpp-terminal v" + Term::Version::string()).c_str(), MB_OK | MB_ICONERROR);
191 break;
192#endif
194#if defined(_WIN32)
195 (void)(fputws(Term::Private::to_wide(std::string("cpp-terminal v" + Term::Version::string() + "\n" + exception.what() + "\n")).c_str(), stderr));
196#else
197 (void)(fputs(std::string("cpp-terminal v" + Term::Version::string() + "\n" + exception.what() + "\n").c_str(), stderr));
198#endif
199 break;
200 default: break;
201 }
202 }
203 catch(const std::exception& exception)
204 {
205 switch(destination)
206 {
208#if defined(_WIN32)
209 MessageBoxW(nullptr, Term::Private::to_wide(exception.what()).c_str(), Term::Private::to_wide("cpp-terminal v" + Term::Version::string()).c_str(), MB_OK | MB_ICONERROR);
210 break;
211#endif
213#if defined(_WIN32)
214 (void)(fputws(Term::Private::to_wide(std::string("cpp-terminal v" + Term::Version::string() + "\n" + exception.what() + "\n")).c_str(), stderr));
215#else
216 (void)(fputs(std::string("cpp-terminal v" + Term::Version::string() + "\n" + exception.what() + "\n").c_str(), stderr));
217#endif
218 break;
219 default: break;
220 }
221 }
222 catch(...)
223 {
224 switch(destination)
225 {
227#if defined(_WIN32)
228 MessageBoxW(nullptr, Term::Private::to_wide("cpp-terminal v" + Term::Version::string() + "Unknown error").c_str(), Term::Private::to_wide("cpp-terminal v" + Term::Version::string()).c_str(), MB_OK | MB_ICONERROR);
229 break;
230#endif
232#if defined(_WIN32)
233 (void)(fputws(Term::Private::to_wide("cpp-terminal v" + Term::Version::string() + ": Unknown error\n").c_str(), stderr));
234#else
235 (void)(fputs(("cpp-terminal v" + Term::Version::string() + ": Unknown error\n").c_str(), stderr));
236#endif
237 default: break;
238 }
239 }
240 (void)(std::fflush(stderr));
241 std::_Exit(Term::returnCode());
242}
const char * what() const noexcept override
Definition exception.cpp:43
std::wstring to_wide(const std::string &str)
Definition unicode.cpp:40
std::string string() noexcept
String version of cpp-terminal.
std::uint16_t returnCode() noexcept

◆ getenv()

std::pair< bool, std::string > Term::Private::getenv ( const std::string & env)

Value of an environment variables.

Parameters
envThe environment variable.
Returns
std::pair<bool,std::string> with bool set to true if the environment variable is set and std::string set to the value of environment variable.
Warning
Internal use only.

Definition at line 14 of file env.cpp.

15{
16#if defined(_WIN32)
17 std::size_t size{0};
18 _wgetenv_s(&size, nullptr, 0, Term::Private::to_wide(key).c_str());
19 std::wstring ret;
20 if(size == 0 || size > ret.max_size()) return {false, std::string()};
21 ret.reserve(size);
22 _wgetenv_s(&size, &ret[0], size, Term::Private::to_wide(key).c_str());
23 return {true, Term::Private::to_narrow(ret)};
24#else
25 if(std::getenv(key.c_str()) != nullptr) { return {true, static_cast<std::string>(std::getenv(key.c_str()))}; }
26 return {false, std::string()};
27#endif
28}
std::string to_narrow(const std::wstring &wstr)
Definition unicode.cpp:26

◆ is_valid_utf8_code_unit()

bool Term::Private::is_valid_utf8_code_unit ( const std::string & str)

Definition at line 55 of file conversion.cpp.

56{
57 static const constexpr std::uint8_t b1OOOOOOO{128};
58 static const constexpr std::uint8_t b11OOOOOO{192};
59 static const constexpr std::uint8_t b111OOOOO{224};
60 static const constexpr std::uint8_t b1111OOOO{240};
61 static const constexpr std::uint8_t b11111OOO{248};
62 switch(str.size())
63 {
64 case 1: return (static_cast<std::uint8_t>(str[0]) & b1OOOOOOO) == 0;
65 case 2: return ((static_cast<std::uint8_t>(str[0]) & b111OOOOO) == b11OOOOOO) && ((static_cast<std::uint8_t>(str[1]) & b11OOOOOO) == b1OOOOOOO);
66 case 3: return ((static_cast<std::uint8_t>(str[0]) & b1111OOOO) == b111OOOOO) && ((static_cast<std::uint8_t>(str[1]) & b11OOOOOO) == b1OOOOOOO) && ((static_cast<std::uint8_t>(str[2]) & b11OOOOOO) == b1OOOOOOO);
67 case 4: return ((static_cast<std::uint8_t>(str[0]) & b11111OOO) == b1111OOOO) && ((static_cast<std::uint8_t>(str[1]) & b11OOOOOO) == b1OOOOOOO) && ((static_cast<std::uint8_t>(str[2]) & b11OOOOOO) == b1OOOOOOO) && ((static_cast<std::uint8_t>(str[3]) & b11OOOOOO) == b1OOOOOOO);
68 default: return false;
69 }
70}

◆ to_narrow()

std::string Term::Private::to_narrow ( const std::wstring & wstr)

Definition at line 26 of file unicode.cpp.

27{
28 if(in.empty()) return std::string();
29 static constexpr DWORD flag{WC_ERR_INVALID_CHARS};
30 std::size_t in_size{in.size()};
31 if(in_size > static_cast<size_t>((std::numeric_limits<int>::max)())) throw Term::Exception("String size is to big " + std::to_string(in_size) + "/" + std::to_string((std::numeric_limits<int>::max)()));
32 const int ret_size{::WideCharToMultiByte(CP_UTF8, flag, in.data(), static_cast<int>(in_size), nullptr, 0, nullptr, nullptr)};
33 if(ret_size == 0) throw Term::Private::WindowsException(::GetLastError());
34 std::string ret(static_cast<std::size_t>(ret_size), '\0');
35 int ret_error{::WideCharToMultiByte(CP_UTF8, flag, in.data(), static_cast<int>(in_size), &ret[0], ret_size, nullptr, nullptr)};
36 if(ret_error == 0) throw Term::Private::WindowsException(::GetLastError());
37 return ret;
38}

◆ to_wide()

std::wstring Term::Private::to_wide ( const std::string & str)

Definition at line 40 of file unicode.cpp.

41{
42 if(in.empty()) return std::wstring();
43 static constexpr DWORD flag{MB_ERR_INVALID_CHARS};
44 std::size_t in_size{in.size()};
45 if(in_size > static_cast<size_t>((std::numeric_limits<int>::max)())) throw Term::Exception("String size is to big " + std::to_string(in_size) + "/" + std::to_string((std::numeric_limits<int>::max)()));
46 const int ret_size{::MultiByteToWideChar(CP_UTF8, flag, in.data(), static_cast<int>(in_size), nullptr, 0)};
47 if(ret_size == 0) throw Term::Private::WindowsException(::GetLastError());
48 std::wstring ret(static_cast<std::size_t>(ret_size), '\0');
49 int ret_error{::MultiByteToWideChar(CP_UTF8, flag, in.data(), static_cast<int>(in_size), &ret[0], ret_size)};
50 if(ret_error == 0) throw Term::Private::WindowsException(::GetLastError());
51 return ret;
52}

◆ utf32_to_utf8() [1/2]

std::string Term::Private::utf32_to_utf8 ( const char32_t & codepoint,
const bool & exception = false )

Encode a codepoint using UTF-8 std::string .

Parameters
codepointThe codepoint ( char32_t ) on range [0,0x10FFFF] to convert.
exceptionIf true throw exception on error, otherwise change the out of range codepoint to "replacement character" .
Returns
std::string the UTF-8 value.
Warning
Internal use only.

Definition at line 55 of file unicode.cpp.

56{
57 static const constexpr std::array<std::uint32_t, 4> size{0x7F, 0x07FF, 0xFFFF, 0x10FFFF};
58 static const constexpr std::uint8_t mask{0x80};
59 static const constexpr std::uint8_t add{0x3F};
60 static const constexpr std::array<std::uint8_t, 3> mask_first{0x1F, 0x0F, 0x07};
61 static const constexpr std::array<std::uint8_t, 3> add_first{0xC0, 0xE0, 0xF0};
62 static const constexpr std::array<std::uint8_t, 4> shift{0, 6, 12, 18};
63 static const constexpr std::uint8_t max_size{4};
64 std::string ret;
65 ret.reserve(max_size);
66 if(codepoint <= size[0]) { ret = {static_cast<char>(codepoint)}; } // Plain ASCII
67 else if(codepoint <= size[1]) { ret = {static_cast<char>(((codepoint >> shift[1]) & mask_first[0]) | add_first[0]), static_cast<char>(((codepoint >> shift[0]) & add) | mask)}; }
68 else if(codepoint <= size[2]) { ret = {static_cast<char>(((codepoint >> shift[2]) & mask_first[1]) | add_first[1]), static_cast<char>(((codepoint >> shift[1]) & add) | mask), static_cast<char>(((codepoint >> shift[0]) & add) | mask)}; }
69 else if(codepoint <= size[3]) { ret = {static_cast<char>(((codepoint >> shift[3]) & mask_first[2]) | add_first[2]), static_cast<char>(((codepoint >> shift[2]) & add) | mask), static_cast<char>(((codepoint >> shift[1]) & add) | mask), static_cast<char>(((codepoint >> shift[0]) & add) | mask)}; }
70 else if(exception) { throw Term::Exception("Invalid UTF32 codepoint."); }
71 else { ret = "\xEF\xBF\xBD"; }
72 return ret;
73}

◆ utf32_to_utf8() [2/2]

std::string Term::Private::utf32_to_utf8 ( const std::u32string & str,
const bool & exception = false )

Encode a std::u32string into UTF-8 std::string .

Parameters
strThe std::u32string to convert.
exceptionIf true throw exception on error, otherwise change the out of range codepoint to "replacement character" .
Returns
std::string encoded in UTF-8.
Warning
Internal use only.

Definition at line 75 of file unicode.cpp.

76{
77 std::string ret;
78 for(const char32_t codepoint: str) { ret.append(utf32_to_utf8(codepoint, exception)); }
79 return ret;
80}
std::string utf32_to_utf8(const char32_t &codepoint, const bool &exception=false)
Encode a codepoint using UTF-8 std::string .
Definition unicode.cpp:55

◆ utf8_decode_step()

std::uint8_t Term::Private::utf8_decode_step ( std::uint8_t state,
std::uint8_t octet,
std::uint32_t * cpp )

Definition at line 25 of file conversion.cpp.

26{
27 static const constexpr std::array<std::uint32_t, 0x10> utf8ClassTab{0x88888888UL, 0x88888888UL, 0x99999999UL, 0x99999999UL, 0xaaaaaaaaUL, 0xaaaaaaaaUL, 0xaaaaaaaaUL, 0xaaaaaaaaUL, 0x222222ffUL, 0x22222222UL, 0x22222222UL, 0x22222222UL, 0x3333333bUL, 0x33433333UL, 0xfff5666cUL, 0xffffffffUL};
28
29 static const constexpr std::array<std::uint32_t, 0x10> utf8StateTab{0xfffffff0UL, 0xffffffffUL, 0xfffffff1UL, 0xfffffff3UL, 0xfffffff4UL, 0xfffffff7UL, 0xfffffff6UL, 0xffffffffUL, 0x33f11f0fUL, 0xf3311f0fUL, 0xf33f110fUL, 0xfffffff2UL, 0xfffffff5UL, 0xffffffffUL, 0xffffffffUL, 0xffffffffUL};
30
31 const std::uint8_t reject{static_cast<std::uint8_t>(state >> 3UL)};
32 const std::uint8_t nonAscii{static_cast<std::uint8_t>(octet >> 7UL)};
33 const std::uint8_t class_{static_cast<std::uint8_t>(!nonAscii ? 0 : (0xf & (utf8ClassTab[(octet >> 3) & 0xf] >> (4 * (octet & 7)))))};
34
35 *cpp = (state == UTF8_ACCEPT ? (octet & (0xffU >> class_)) : ((octet & 0x3fU) | (*cpp << 6)));
36
37 return (reject ? 0xf : (0xf & (utf8StateTab[class_] >> (4 * (state & 7)))));
38}

◆ utf8_to_utf32()

std::u32string Term::Private::utf8_to_utf32 ( const std::string & str)

Definition at line 40 of file conversion.cpp.

41{
42 std::uint32_t codepoint{0};
43 std::uint8_t state{UTF8_ACCEPT};
44 std::u32string ret;
45 for(char idx: str)
46 {
47 state = utf8_decode_step(state, static_cast<std::uint8_t>(idx), &codepoint);
48 if(state == UTF8_ACCEPT) { ret.push_back(codepoint); }
49 else if(state == UTF8_REJECT) { throw Term::Exception("Invalid byte in UTF8 encoded string"); }
50 }
51 if(state != UTF8_ACCEPT) { throw Term::Exception("Expected more bytes in UTF8 encoded string"); }
52 return ret;
53}
std::uint8_t utf8_decode_step(std::uint8_t state, std::uint8_t octet, std::uint32_t *cpp)

Variable Documentation

◆ in

Term::Private::InputFileHandler & Term::Private::in = reinterpret_cast<Term::Private::InputFileHandler&>(stdin_buffer)
extern

Definition at line 43 of file file.cpp.

◆ m_signalStatus

volatile std::sig_atomic_t Term::Private::m_signalStatus {0}

Definition at line 28 of file sigwinch.cpp.

28{0};

◆ out

Term::Private::OutputFileHandler & Term::Private::out = reinterpret_cast<Term::Private::OutputFileHandler&>(stdout_buffer)
extern

Definition at line 44 of file file.cpp.