Skip to content

Commit

Permalink
file: fix windows error messages (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablohoch authored Oct 22, 2024
1 parent 0469892 commit 56e9191
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions include/cista/targets/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <io.h>
#include <windows.h>
#include <string>
#include <system_error>
#endif

#include <cinttypes>
Expand All @@ -27,27 +28,7 @@
namespace cista {

inline std::string last_error_str() {
auto const err = ::GetLastError();
if (err == 0) {
return "no error";
}

struct buf {
~buf() {
if (b_ != nullptr) {
LocalFree(b_);
b_ = nullptr;
}
}
LPSTR b_ = nullptr;
} b;
auto const size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), b.b_, 0,
nullptr);

return size == 0 ? std::to_string(err) : std::string{b.b_, size};
return std::system_category().message(static_cast<int>(::GetLastError()));
}

inline HANDLE open_file(char const* path, char const* mode) {
Expand Down

0 comments on commit 56e9191

Please sign in to comment.