From 47b6ebce2e2830857b933560e312280d7e88d90b Mon Sep 17 00:00:00 2001 From: flagarde Date: Mon, 6 Nov 2023 06:07:50 +0100 Subject: [PATCH] fixes --- cpp-terminal/platforms/exception.cpp | 28 +++++++++++---------- cpp-terminal/platforms/exception.hpp | 21 ++++++++-------- cpp-terminal/platforms/file_initializer.cpp | 3 +++ cpp-terminal/platforms/file_initializer.hpp | 1 + cpp-terminal/platforms/terminfo.cpp | 1 + 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/cpp-terminal/platforms/exception.cpp b/cpp-terminal/platforms/exception.cpp index 115d29d7..bfa13c29 100644 --- a/cpp-terminal/platforms/exception.cpp +++ b/cpp-terminal/platforms/exception.cpp @@ -18,6 +18,8 @@ #include #endif +#include + Term::Exception::Exception(const std::string& message) noexcept : m_message(message) {} Term::Exception::Exception(const std::int64_t& code, const std::string& message) noexcept : m_code(code), m_message(message) {} @@ -36,7 +38,7 @@ std::string Term::Exception::context() const noexcept { return m_context; } Term::Exception::Exception(const std::int64_t& code) noexcept : m_code(code) {} -void Term::Exception::build_what() const noexcept +void Term::Exception::build_what() const noexcept { if(0 == m_code) { m_what = m_message; } else { m_what = "error " + std::to_string(m_code) + ": " + m_message; } @@ -53,13 +55,13 @@ Term::Private::WindowsError::WindowsError() = default; Term::Private::WindowsError::~WindowsError() = default; -std::int32_t Term::Private::WindowsError::error() const { return m_error; } +std::int64_t Term::Private::WindowsError::error() const { return m_error; } bool Term::Private::WindowsError::check_value() const { return m_check_value; } Term::Private::WindowsError& Term::Private::WindowsError::check_if(const bool& ret) { - m_error = GetLastError(); + m_error = static_cast(GetLastError()); m_check_value = ret; return *this; } @@ -69,11 +71,11 @@ void Term::Private::WindowsError::throw_exception(const std::string& str) if(m_check_value) { throw Term::Private::WindowsException(m_error, str); } } -Term::Private::WindowsException::WindowsException(const unsigned long& error, const std::string& context) : Term::Exception(static_cast(error)) +Term::Private::WindowsException::WindowsException(const std::int64_t& error, const std::string& context) : Term::Exception(static_cast(error)) { - m_context = context; + setContext(context); wchar_t* ptr{nullptr}; - const DWORD cchMsg{FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, static_cast(m_code), 0, reinterpret_cast(&ptr), 0, nullptr)}; + const DWORD cchMsg{FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, static_cast(code()), 0, reinterpret_cast(&ptr), 0, nullptr)}; if(cchMsg > 0) { auto deleter = [](void* p) @@ -83,15 +85,15 @@ Term::Private::WindowsException::WindowsException(const unsigned long& error, co std::unique_ptr ptrBuffer(ptr, deleter); std::string ret{Term::Private::to_narrow(ptrBuffer.get())}; if(ret.size() >= 2 && ret[ret.size() - 1] == '\n' && ret[ret.size() - 2] == '\r') ret.erase(ret.size() - 2); - m_message = ret; + setMessage(ret); } else { throw Term::Exception(::GetLastError(), "Error in FormatMessageW"); } } -void Term::Private::WindowsException::build_what() +void Term::Private::WindowsException::build_what() const noexcept { - m_what = "windows error " + std::to_string(m_code) + ": " + m_message; - if(!m_context.empty()) m_what += +" [" + m_context + "]"; + std::string what{"windows error " + std::to_string(code()) + ": " + message()}; + if(!context().empty()) what += " [" + context() + "]"; } #endif @@ -122,7 +124,7 @@ Term::Private::Errno::~Errno() noexcept Term::Private::Errno& Term::Private::Errno::check_if(const bool& ret) noexcept { #if defined(_WIN32) - _get_errno(&m_errno); + _get_errno(&static_cast(m_errno)); #else m_errno = static_cast(*::__errno_location()); #endif @@ -132,11 +134,11 @@ Term::Private::Errno& Term::Private::Errno::check_if(const bool& ret) noexcept bool Term::Private::Errno::check_value() const noexcept { return m_check_value; } -std::uint32_t Term::Private::Errno::error() const noexcept { return m_errno; } +std::int64_t Term::Private::Errno::error() const noexcept { return m_errno; } Term::Private::ErrnoException::~ErrnoException() = default; -Term::Private::ErrnoException::ErrnoException(const std::uint32_t& error, const std::string& context) : Term::Exception(static_cast(error)) +Term::Private::ErrnoException::ErrnoException(const std::int64_t& error, const std::string& context) : Term::Exception(static_cast(error)) { setContext(context); #if defined(_WIN32) diff --git a/cpp-terminal/platforms/exception.hpp b/cpp-terminal/platforms/exception.hpp index fcd45d19..03aa3705 100644 --- a/cpp-terminal/platforms/exception.hpp +++ b/cpp-terminal/platforms/exception.hpp @@ -10,6 +10,7 @@ #include "cpp-terminal/exception.hpp" #include +#include namespace Term { @@ -22,25 +23,25 @@ namespace Private class WindowsException : public Term::Exception { public: - WindowsException(const unsigned long& error, const std::string& context = std::string()); + WindowsException(const std::int64_t& error, const std::string& context = std::string()); virtual ~WindowsException() = default; private: - void build_what() override; + void build_what() const noexcept; }; class WindowsError { public: WindowsError(); - ~WindowsError(); - std::int32_t error() const; + virtual ~WindowsError(); + std::int64_t error() const; bool check_value() const; WindowsError& check_if(const bool& ret); void throw_exception(const std::string& str = std::string()); private: - int m_error{0}; + std::int64_t m_error{0}; bool m_check_value{false}; }; #endif @@ -54,24 +55,24 @@ class Errno ~Errno() noexcept; Errno& operator=(Errno&&) noexcept = default; Errno& operator=(const Errno&) noexcept = default; - std::uint32_t error() const noexcept; + std::int64_t error() const noexcept; bool check_value() const noexcept; Errno& check_if(const bool& ret) noexcept; void throw_exception(const std::string& str = std::string()) const; private: - std::uint32_t m_errno{0}; + std::int64_t m_errno{0}; bool m_check_value{false}; }; class ErrnoException : public Term::Exception { public: - ErrnoException(const std::uint32_t& error, const std::string& context = std::string()); - ~ErrnoException() override; + explicit ErrnoException(const std::int64_t& error, const std::string& context = std::string()); + virtual ~ErrnoException(); private: - void build_what() const noexcept override; + void build_what() const noexcept; }; } // namespace Private diff --git a/cpp-terminal/platforms/file_initializer.cpp b/cpp-terminal/platforms/file_initializer.cpp index a8b00066..6d68b82d 100644 --- a/cpp-terminal/platforms/file_initializer.cpp +++ b/cpp-terminal/platforms/file_initializer.cpp @@ -17,6 +17,9 @@ #include #if defined(_WIN32) + #include + #include + #include "cpp-terminal/platforms/unicode.hpp" #else #include #endif diff --git a/cpp-terminal/platforms/file_initializer.hpp b/cpp-terminal/platforms/file_initializer.hpp index 8ef221f4..36866278 100644 --- a/cpp-terminal/platforms/file_initializer.hpp +++ b/cpp-terminal/platforms/file_initializer.hpp @@ -10,6 +10,7 @@ #pragma once #include +#include namespace Term { diff --git a/cpp-terminal/platforms/terminfo.cpp b/cpp-terminal/platforms/terminfo.cpp index 8e30cff0..a349b2a1 100644 --- a/cpp-terminal/platforms/terminfo.cpp +++ b/cpp-terminal/platforms/terminfo.cpp @@ -14,6 +14,7 @@ #include "cpp-terminal/cursor.hpp" #include "cpp-terminal/platforms/env.hpp" #include "cpp-terminal/platforms/file.hpp" +#include "cpp-terminal/platforms/file_initializer.hpp" #include "cpp-terminal/terminfo.hpp" #include