misc.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #ifdef _WIN32
  6. #include <windows.h>
  7. #else
  8. #include <cerrno>
  9. #include <cstring>
  10. #endif
  11. #include "common/common_funcs.h"
  12. std::string NativeErrorToString(int e) {
  13. #ifdef _WIN32
  14. LPSTR err_str;
  15. DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER |
  16. FORMAT_MESSAGE_IGNORE_INSERTS,
  17. nullptr, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  18. reinterpret_cast<LPSTR>(&err_str), 1, nullptr);
  19. if (!res) {
  20. return "(FormatMessageA failed to format error)";
  21. }
  22. std::string ret(err_str);
  23. LocalFree(err_str);
  24. return ret;
  25. #else
  26. char err_str[255];
  27. #if defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600))
  28. // Thread safe (GNU-specific)
  29. const char* str = strerror_r(e, err_str, sizeof(err_str));
  30. return std::string(str);
  31. #else
  32. // Thread safe (XSI-compliant)
  33. int second_err = strerror_r(e, err_str, sizeof(err_str));
  34. if (second_err != 0) {
  35. return "(strerror_r failed to format error)";
  36. }
  37. return std::string(err_str);
  38. #endif // GLIBC etc.
  39. #endif // _WIN32
  40. }
  41. std::string GetLastErrorMsg() {
  42. #ifdef _WIN32
  43. return NativeErrorToString(GetLastError());
  44. #else
  45. return NativeErrorToString(errno);
  46. #endif
  47. }