diff options
Diffstat (limited to 'compat/strerror.cpp')
-rw-r--r-- | compat/strerror.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/compat/strerror.cpp b/compat/strerror.cpp new file mode 100644 index 00000000..67b87e2f --- /dev/null +++ b/compat/strerror.cpp @@ -0,0 +1,31 @@ +#include "strerror.hpp" +#include <errno.h> +#include <string.h> + +namespace floormat { + +StringView get_error_string(ArrayView<char> buf) +{ +#ifndef _WIN32 + if constexpr(std::is_same_v<char*, std::decay_t<decltype(::strerror_r(errno, buf.data(), buf.size()))>>) + { + const char* str { ::strerror_r(errno, buf.data(), buf.size()) }; + if (str) + return str; + } + else + { + const int status { ::strerror_r(errno, buf.data(), buf.size()) }; + if (status == 0) + return buf; + } +#else + ::strerror_s(buf.data(), buf.size(), errno); + if (buf[0]) + return buf; +#endif + + return "Unknown error"_s; +}; + +} // namespace floormat |