File: CommonFuncs.cpp

package info (click to toggle)
dolphin-emu 2512%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 76,328 kB
  • sloc: cpp: 499,023; ansic: 119,674; python: 6,547; sh: 2,338; makefile: 1,093; asm: 726; pascal: 257; javascript: 183; perl: 97; objc: 75; xml: 30
file content (101 lines) | stat: -rw-r--r-- 2,963 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2009 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Common/CommonFuncs.h"

#include <cstddef>
#include <cstring>
#include <errno.h>
#include <type_traits>

#ifdef _WIN32
#include <windows.h>
#include <SetupAPI.h>

#define strerror_r(err, buf, len) strerror_s(buf, len, err)

#include "Common/StringUtil.h"
#endif

namespace Common
{
constexpr size_t BUFFER_SIZE = 256;

// 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
}

std::string StrerrorString(int error)
{
  char error_message[BUFFER_SIZE];

  return StrErrorWrapper(error, error_message, BUFFER_SIZE);
}

// Wrapper function to get last strerror(errno) string.
// This function might change the error code.
std::string LastStrerrorString()
{
  return StrerrorString(errno);
}

#ifdef _WIN32
// Wrapper function to get GetLastError() string.
// This function might change the error code.
std::string GetLastErrorString()
{
  return GetWin32ErrorString(GetLastError());
}

// Like GetLastErrorString() but if you have already queried the error code.
std::string GetWin32ErrorString(DWORD error_code)
{
  wchar_t error_message[BUFFER_SIZE];

  FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error_code,
                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_message, BUFFER_SIZE, nullptr);
  return WStringToUTF8(error_message);
}

// 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;
}
#endif
}  // namespace Common