2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2021-12-09 19:22:16 -07:00
|
|
|
#include "Common/CommonFuncs.h"
|
|
|
|
|
2017-05-13 03:22:30 -06:00
|
|
|
#include <cstddef>
|
2017-12-16 05:08:50 -07:00
|
|
|
#include <cstring>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <errno.h>
|
2017-12-16 05:08:50 -07:00
|
|
|
#include <type_traits>
|
2009-03-07 01:35:01 -07:00
|
|
|
|
2017-01-23 00:08:45 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
2017-08-17 13:12:44 -06:00
|
|
|
#define strerror_r(err, buf, len) strerror_s(buf, len, err)
|
2017-01-23 00:08:45 -07:00
|
|
|
#endif
|
|
|
|
|
2023-04-18 10:50:31 -06:00
|
|
|
namespace Common
|
|
|
|
{
|
2017-08-17 13:12:44 -06:00
|
|
|
constexpr size_t BUFFER_SIZE = 256;
|
|
|
|
|
2023-07-12 09:18:03 -06:00
|
|
|
// There are two variants of strerror_r. The XSI version stores the message to the passed-in
|
|
|
|
// buffer and returns an int (0 on success). The GNU version returns a pointer to the message,
|
|
|
|
// which might have been stored in the passed-in buffer or might be a static string.
|
|
|
|
//
|
|
|
|
// This function might change the errno value.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// https://www.gnu.org/software/gnulib/manual/html_node/strerror_005fr.html
|
|
|
|
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html
|
|
|
|
// https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/baselib-strerror-r.html
|
|
|
|
const char* StrErrorWrapper(int error, char* buffer, std::size_t length)
|
|
|
|
{
|
|
|
|
// We check defines in order to figure out which variant is in use.
|
|
|
|
#if (defined(__GLIBC__) || __ANDROID_API__ >= 23) && \
|
|
|
|
(_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600))
|
|
|
|
return strerror_r(error, buffer, length);
|
|
|
|
#else
|
|
|
|
const int error_code = strerror_r(error, buffer, length);
|
|
|
|
return error_code == 0 ? buffer : "";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-08-17 13:12:44 -06:00
|
|
|
// Wrapper function to get last strerror(errno) string.
|
2009-03-07 01:35:01 -07:00
|
|
|
// This function might change the error code.
|
2017-08-17 13:12:44 -06:00
|
|
|
std::string LastStrerrorString()
|
2009-03-07 01:35:01 -07:00
|
|
|
{
|
2017-08-17 13:12:44 -06:00
|
|
|
char error_message[BUFFER_SIZE];
|
2011-03-16 15:08:20 -06:00
|
|
|
|
2023-07-12 09:18:03 -06:00
|
|
|
return StrErrorWrapper(errno, error_message, BUFFER_SIZE);
|
2017-08-17 13:12:44 -06:00
|
|
|
}
|
2011-03-16 15:08:20 -06:00
|
|
|
|
2017-08-17 13:12:44 -06:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Wrapper function to get GetLastError() string.
|
|
|
|
// This function might change the error code.
|
|
|
|
std::string GetLastErrorString()
|
|
|
|
{
|
|
|
|
char error_message[BUFFER_SIZE];
|
|
|
|
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_message, BUFFER_SIZE, nullptr);
|
|
|
|
return std::string(error_message);
|
2009-03-07 01:35:01 -07:00
|
|
|
}
|
2019-10-06 14:17:00 -06:00
|
|
|
|
|
|
|
// Obtains a full path to the specified module.
|
|
|
|
std::optional<std::wstring> GetModuleName(void* hInstance)
|
|
|
|
{
|
|
|
|
DWORD max_size = 50; // Start with space for 50 characters and grow if needed
|
|
|
|
std::wstring name(max_size, L'\0');
|
|
|
|
|
|
|
|
DWORD size;
|
|
|
|
while ((size = GetModuleFileNameW(static_cast<HMODULE>(hInstance), name.data(), max_size)) ==
|
|
|
|
max_size &&
|
|
|
|
GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
{
|
|
|
|
max_size *= 2;
|
|
|
|
name.resize(max_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
name.resize(size);
|
|
|
|
return name;
|
|
|
|
}
|
2017-08-17 13:12:44 -06:00
|
|
|
#endif
|
2023-04-18 10:50:31 -06:00
|
|
|
} // namespace Common
|