BAN: Error can now be constructed from c_string or format string
If the resulting string would overflow, we just truncate it to fit the error message buffer (128) bytes
This commit is contained in:
@@ -21,20 +21,32 @@ namespace BAN
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
static Error from_string(const char* message)
|
||||
static Error from_c_string(const char* message)
|
||||
{
|
||||
static_assert(sizeof(message) < 128);
|
||||
Error result;
|
||||
strncpy(result.m_message, message, sizeof(m_message));
|
||||
result.m_message[sizeof(result.m_message) - 1] = '\0';
|
||||
strncpy(result.m_message, message, sizeof(Error::m_message));
|
||||
result.m_message[sizeof(Error::m_message) - 1] = '\0';
|
||||
result.m_error_code = 0xFF;
|
||||
return result;
|
||||
}
|
||||
template<typename... Args>
|
||||
static Error from_format(const char* format, Args&&... args)
|
||||
{
|
||||
char buffer[sizeof(Error::m_message)] {};
|
||||
size_t index = 0;
|
||||
auto putc = [&](char ch)
|
||||
{
|
||||
if (index < sizeof(buffer) - 1)
|
||||
buffer[index++] = ch;
|
||||
};
|
||||
Formatter::print(putc, format, forward<Args>(args)...);
|
||||
return from_c_string(buffer);
|
||||
}
|
||||
static Error from_errno(int error)
|
||||
{
|
||||
Error result;
|
||||
strncpy(result.m_message, strerror(error), sizeof(m_message));
|
||||
result.m_message[sizeof(result.m_message) - 1] = '\0';
|
||||
strncpy(result.m_message, strerror(error), sizeof(Error::m_message));
|
||||
result.m_message[sizeof(Error::m_message) - 1] = '\0';
|
||||
result.m_error_code = error;
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user