Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
flagarde committed Nov 6, 2023
1 parent eabffd5 commit 47b6ebc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
28 changes: 15 additions & 13 deletions cpp-terminal/platforms/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <cstring>
#endif

#include <string>

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) {}
Expand All @@ -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; }
Expand All @@ -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<std::int64_t>(GetLastError());
m_check_value = ret;
return *this;
}
Expand All @@ -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<std::int64_t>(error))
Term::Private::WindowsException::WindowsException(const std::int64_t& error, const std::string& context) : Term::Exception(static_cast<std::int64_t>(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<uint32_t>(m_code), 0, reinterpret_cast<wchar_t*>(&ptr), 0, nullptr)};
const DWORD cchMsg{FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, static_cast<uint32_t>(code()), 0, reinterpret_cast<wchar_t*>(&ptr), 0, nullptr)};
if(cchMsg > 0)
{
auto deleter = [](void* p)
Expand All @@ -83,15 +85,15 @@ Term::Private::WindowsException::WindowsException(const unsigned long& error, co
std::unique_ptr<wchar_t, decltype(deleter)> 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
Expand Down Expand Up @@ -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<int>(m_errno));
#else
m_errno = static_cast<std::uint32_t>(*::__errno_location());
#endif
Expand All @@ -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<std::int64_t>(error))
Term::Private::ErrnoException::ErrnoException(const std::int64_t& error, const std::string& context) : Term::Exception(static_cast<std::int64_t>(error))
{
setContext(context);
#if defined(_WIN32)
Expand Down
21 changes: 11 additions & 10 deletions cpp-terminal/platforms/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "cpp-terminal/exception.hpp"

#include <cstdint>
#include <string>

namespace Term
{
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cpp-terminal/platforms/file_initializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <ostream>

#if defined(_WIN32)
#include <windows.h>
#include <io.h>
#include "cpp-terminal/platforms/unicode.hpp"
#else
#include <sys/stat.h>
#endif
Expand Down
1 change: 1 addition & 0 deletions cpp-terminal/platforms/file_initializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <cstdint>
#include <cstddef>

namespace Term
{
Expand Down
1 change: 1 addition & 0 deletions cpp-terminal/platforms/terminfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string>
Expand Down

0 comments on commit 47b6ebc

Please sign in to comment.