Common/CommonFuncs: Move interface into Common namespace

Gets these functions out of the global namespace.
This commit is contained in:
Lioncash 2023-04-18 12:50:31 -04:00
parent 361ffd5917
commit f1ad43afaf
8 changed files with 19 additions and 12 deletions

View File

@ -13,6 +13,8 @@
#define strerror_r(err, buf, len) strerror_s(buf, len, err) #define strerror_r(err, buf, len) strerror_s(buf, len, err)
#endif #endif
namespace Common
{
constexpr size_t BUFFER_SIZE = 256; constexpr size_t BUFFER_SIZE = 256;
// Wrapper function to get last strerror(errno) string. // Wrapper function to get last strerror(errno) string.
@ -73,3 +75,4 @@ std::optional<std::wstring> GetModuleName(void* hInstance)
return name; return name;
} }
#endif #endif
} // namespace Common

View File

@ -39,6 +39,8 @@ __declspec(dllimport) void __stdcall DebugBreak(void);
} }
#endif // WIN32 ndef #endif // WIN32 ndef
namespace Common
{
// Wrapper function to get last strerror(errno) string. // Wrapper function to get last strerror(errno) string.
// This function might change the error code. // This function might change the error code.
std::string LastStrerrorString(); std::string LastStrerrorString();
@ -51,3 +53,4 @@ std::string GetLastErrorString();
// Obtains a full path to the specified module. // Obtains a full path to the specified module.
std::optional<std::wstring> GetModuleName(void* hInstance); std::optional<std::wstring> GetModuleName(void* hInstance);
#endif #endif
} // namespace Common

View File

@ -170,7 +170,7 @@ static std::optional<std::wstring> GetModulePath(const wchar_t* name)
if (module == nullptr) if (module == nullptr)
return std::nullopt; return std::nullopt;
return GetModuleName(module); return Common::GetModuleName(module);
} }
static bool GetModuleVersion(const wchar_t* name, Version* version) static bool GetModuleVersion(const wchar_t* name, Version* version)

View File

@ -358,14 +358,14 @@ u64 GetSize(FILE* f)
const u64 pos = ftello(f); const u64 pos = ftello(f);
if (fseeko(f, 0, SEEK_END) != 0) if (fseeko(f, 0, SEEK_END) != 0)
{ {
ERROR_LOG_FMT(COMMON, "GetSize: seek failed {}: {}", fmt::ptr(f), LastStrerrorString()); ERROR_LOG_FMT(COMMON, "GetSize: seek failed {}: {}", fmt::ptr(f), Common::LastStrerrorString());
return 0; return 0;
} }
const u64 size = ftello(f); const u64 size = ftello(f);
if ((size != pos) && (fseeko(f, pos, SEEK_SET) != 0)) if ((size != pos) && (fseeko(f, pos, SEEK_SET) != 0))
{ {
ERROR_LOG_FMT(COMMON, "GetSize: seek failed {}: {}", fmt::ptr(f), LastStrerrorString()); ERROR_LOG_FMT(COMMON, "GetSize: seek failed {}: {}", fmt::ptr(f), Common::LastStrerrorString());
return 0; return 0;
} }
@ -379,7 +379,7 @@ bool CreateEmptyFile(const std::string& filename)
if (!File::IOFile(filename, "wb")) if (!File::IOFile(filename, "wb"))
{ {
ERROR_LOG_FMT(COMMON, "CreateEmptyFile: failed {}: {}", filename, LastStrerrorString()); ERROR_LOG_FMT(COMMON, "CreateEmptyFile: failed {}: {}", filename, Common::LastStrerrorString());
return false; return false;
} }
@ -726,7 +726,7 @@ std::string GetBundleDirectory()
std::string GetExePath() std::string GetExePath()
{ {
#ifdef _WIN32 #ifdef _WIN32
auto exe_path = GetModuleName(nullptr); auto exe_path = Common::GetModuleName(nullptr);
if (!exe_path) if (!exe_path)
return {}; return {};
std::error_code error; std::error_code error;

View File

@ -45,7 +45,8 @@ bool CEXIETHERNET::TAPServerNetworkInterface::Activate()
if (connect(fd, reinterpret_cast<sockaddr*>(&sun), sizeof(sun)) == -1) if (connect(fd, reinterpret_cast<sockaddr*>(&sun), sizeof(sun)) == -1)
{ {
ERROR_LOG_FMT(SP1, "Couldn't connect socket ({}), unable to init BBA", LastStrerrorString()); ERROR_LOG_FMT(SP1, "Couldn't connect socket ({}), unable to init BBA",
Common::LastStrerrorString());
close(fd); close(fd);
fd = -1; fd = -1;
return false; return false;
@ -99,14 +100,14 @@ void CEXIETHERNET::TAPServerNetworkInterface::ReadThreadHandler()
u16 size; u16 size;
if (read(fd, &size, 2) != 2) if (read(fd, &size, 2) != 2)
{ {
ERROR_LOG_FMT(SP1, "Failed to read size field from BBA: {}", LastStrerrorString()); ERROR_LOG_FMT(SP1, "Failed to read size field from BBA: {}", Common::LastStrerrorString());
} }
else else
{ {
int read_bytes = read(fd, m_eth_ref->mRecvBuffer.get(), size); int read_bytes = read(fd, m_eth_ref->mRecvBuffer.get(), size);
if (read_bytes < 0) if (read_bytes < 0)
{ {
ERROR_LOG_FMT(SP1, "Failed to read packet data from BBA: {}", LastStrerrorString()); ERROR_LOG_FMT(SP1, "Failed to read packet data from BBA: {}", Common::LastStrerrorString());
} }
else if (readEnabled.IsSet()) else if (readEnabled.IsSet())
{ {

View File

@ -299,13 +299,13 @@ void AutoUpdateChecker::TriggerUpdate(const AutoUpdateChecker::NewVersionInforma
} }
else else
{ {
const std::string error = GetLastErrorString(); const std::string error = Common::GetLastErrorString();
CriticalAlertFmtT("Could not start updater process: {0}", error); CriticalAlertFmtT("Could not start updater process: {0}", error);
} }
#else #else
if (popen(command_line.c_str(), "r") == nullptr) if (popen(command_line.c_str(), "r") == nullptr)
{ {
const std::string error = LastStrerrorString(); const std::string error = Common::LastStrerrorString();
CriticalAlertFmtT("Could not start updater process: {0}", error); CriticalAlertFmtT("Could not start updater process: {0}", error);
} }
#endif #endif

View File

@ -373,7 +373,7 @@ bool UpdateFiles(const std::vector<TodoList::UpdateOp>& to_update,
const std::string& install_base_path, const std::string& temp_path) const std::string& install_base_path, const std::string& temp_path)
{ {
#ifdef _WIN32 #ifdef _WIN32
const auto self_path = std::filesystem::path(GetModuleName(nullptr).value()); const auto self_path = std::filesystem::path(Common::GetModuleName(nullptr).value());
const auto self_filename = self_path.filename(); const auto self_filename = self_path.filename();
#endif #endif

View File

@ -31,7 +31,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
// Test for write permissions // Test for write permissions
bool need_admin = false; bool need_admin = false;
auto path = GetModuleName(hInstance); auto path = Common::GetModuleName(hInstance);
if (!path) if (!path)
{ {
UI::Error("Failed to get updater filename."); UI::Error("Failed to get updater filename.");