Skip to content
Permalink
Browse files

Common: Use fmt where applicable

Begins the transition to using fmt for string formatting where
applicable. Given fmt supports formatting std::string instances out of
the box, we can remove now-unnecessary calls to .c_str() and .data().

Note that this change does not touch the actual logging subsystem aside
from converting the final StringFromFormat call in the process over to
fmt::format. Given our logging system is heavily used throughout the
entire codebase, and converting that over will be quite a large change
by itself, this will be tackled near the end of the conversion process.
  • Loading branch information...
lioncash committed Jun 14, 2019
1 parent 925afca commit 5b92d5076a0be89945fa0fc722749dd5fbfcdf4c
@@ -10,10 +10,11 @@
#include <sys/auxv.h>
#include <unistd.h>

#include <fmt/format.h>

#include "Common/CPUDetect.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/StringUtil.h"

const char procfile[] = "/proc/cpuinfo";

@@ -77,9 +78,9 @@ std::string CPUInfo::Summarize()
{
std::string sum;
if (num_cores == 1)
sum = StringFromFormat("%s, %i core", cpu_string, num_cores);
sum = fmt::format("{}, 1 core", cpu_string);
else
sum = StringFromFormat("%s, %i cores", cpu_string, num_cores);
sum = fmt::format("{}, {} cores", cpu_string, num_cores);

if (bAES)
sum += ", AES";
@@ -9,6 +9,8 @@
#include <string>
#include <vector>

#include <fmt/format.h>

#include "Common/CDUtils.h"
#include "Common/Common.h"
#include "Common/CommonTypes.h"
@@ -134,20 +136,23 @@ std::vector<std::string> GetCDDevices()
}
#else
// checklist: /dev/cdrom, /dev/dvd /dev/hd?, /dev/scd? /dev/sr?
static struct
struct CheckListEntry
{
const char* format;
unsigned int num_min;
unsigned int num_max;
} checklist[] = {
};

constexpr CheckListEntry checklist[] = {
#ifdef __linux__
{"/dev/cdrom", 0, 0}, {"/dev/dvd", 0, 0}, {"/dev/hd%c", 'a', 'z'},
{"/dev/scd%d", 0, 27}, {"/dev/sr%d", 0, 27},
{"/dev/cdrom", 0, 0}, {"/dev/dvd", 0, 0}, {"/dev/hd{}", 'a', 'z'},
{"/dev/scd{}", 0, 27}, {"/dev/sr{}", 0, 27},
#else
{"/dev/acd%d", 0, 27},
{"/dev/cd%d", 0, 27},
{"/dev/acd{}", 0, 27},
{"/dev/cd{}", 0, 27},
#endif
{nullptr, 0, 0}};
{nullptr, 0, 0},
};

// Returns true if a device is a block or char device and not a symbolic link
static bool IsDevice(const std::string& source_name)
@@ -189,7 +194,7 @@ std::vector<std::string> GetCDDevices()
{
for (unsigned int j = checklist[i].num_min; j <= checklist[i].num_max; ++j)
{
std::string drive = StringFromFormat(checklist[i].format, j);
std::string drive = fmt::format(checklist[i].format, j);
if (IsCDROM(drive))
{
drives.push_back(std::move(drive));
@@ -8,6 +8,8 @@
#include <vector>
#include <winternl.h>

#include <fmt/format.h>

#include "Common/CommonTypes.h"
#include "Common/LdrWatcher.h"
#include "Common/StringUtil.h"
@@ -213,41 +215,41 @@ void CompatPatchesInstall(LdrWatcher* watcher)
auto patcher = ImportPatcher(event.base_address);
patcher.PatchIAT("kernel32.dll", "HeapCreate", HeapCreateLow4GB);
}});
watcher->Install({{L"ucrtbase.dll"}, [](const LdrDllLoadEvent& event) {
// ucrtbase implements caching between fseek/fread, old versions have a bug
// such that some reads return incorrect data. This causes noticable bugs
// in dolphin since we use these APIs for reading game images.
Version version;
if (!GetModuleVersion(event.name.c_str(), &version))
return;
const u16 fixed_build = 10548;
if (version.build >= fixed_build)
return;
const UcrtPatchInfo patches[] = {
// 10.0.10240.16384 (th1.150709-1700)
{0xF61ED, 0x6AE7B, 5},
// 10.0.10240.16390 (th1_st1.150714-1601)
{0xF5ED9, 0x6AE7B, 5},
// 10.0.10137.0 (th1.150602-2238)
{0xF8B5E, 0x63ED6, 2},
};
for (const auto& patch : patches)
{
if (ApplyUcrtPatch(event.name.c_str(), patch))
return;
}
// If we reach here, the version is buggy (afaik) and patching failed
auto msg = StringFromFormat(
"You are running %S version %d.%d.%d.%d.\n"
"An important fix affecting Dolphin was introduced in build %d.\n"
"You can use Dolphin, but there will be known bugs.\n"
"Please update this file by installing the latest Universal C Runtime.\n",
event.name.c_str(), version.major, version.minor, version.build,
version.qfe, fixed_build);
// Use MessageBox for maximal user annoyance
MessageBoxA(nullptr, msg.c_str(), "WARNING: BUGGY UCRT VERSION",
MB_ICONEXCLAMATION);
}});
watcher->Install(
{{L"ucrtbase.dll"}, [](const LdrDllLoadEvent& event) {
// ucrtbase implements caching between fseek/fread, old versions have a bug
// such that some reads return incorrect data. This causes noticable bugs
// in dolphin since we use these APIs for reading game images.
Version version;
if (!GetModuleVersion(event.name.c_str(), &version))
return;
const u16 fixed_build = 10548;
if (version.build >= fixed_build)
return;
const UcrtPatchInfo patches[] = {
// 10.0.10240.16384 (th1.150709-1700)
{0xF61ED, 0x6AE7B, 5},
// 10.0.10240.16390 (th1_st1.150714-1601)
{0xF5ED9, 0x6AE7B, 5},
// 10.0.10137.0 (th1.150602-2238)
{0xF8B5E, 0x63ED6, 2},
};
for (const auto& patch : patches)
{
if (ApplyUcrtPatch(event.name.c_str(), patch))
return;
}
// If we reach here, the version is buggy (afaik) and patching failed
const auto msg =
fmt::format("You are running {} version {}.{}.{}.{}.\n"
"An important fix affecting Dolphin was introduced in build {}.\n"
"You can use Dolphin, but there will be known bugs.\n"
"Please update this file by installing the latest Universal C Runtime.\n",
UTF16ToUTF8(event.name), version.major, version.minor, version.build,
version.qfe, fixed_build);
// Use MessageBox for maximal user annoyance
MessageBoxA(nullptr, msg.c_str(), "WARNING: BUGGY UCRT VERSION", MB_ICONEXCLAMATION);
}});
}

int __cdecl EnableCompatPatches()
@@ -3,9 +3,12 @@
// Refer to the license.txt file included.

#include "Common/DynamicLibrary.h"

#include <cstring>

#include <fmt/format.h>

#include "Common/Assert.h"
#include "Common/StringUtil.h"

#ifdef _WIN32
#include <Windows.h>
@@ -42,27 +45,27 @@ std::string DynamicLibrary::GetVersionedFilename(const char* libname, int major,
{
#if defined(_WIN32)
if (major >= 0 && minor >= 0)
return StringFromFormat("%s-%d-%d.dll", libname, major, minor);
return fmt::format("{}-{}-{}.dll", libname, major, minor);
else if (major >= 0)
return StringFromFormat("%s-%d.dll", libname, major);
return fmt::format("{}-{}.dll", libname, major);
else
return StringFromFormat("%s.dll", libname);
return fmt::format("{}.dll", libname);
#elif defined(__APPLE__)
const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
if (major >= 0 && minor >= 0)
return StringFromFormat("%s%s.%d.%d.dylib", prefix, libname, major, minor);
return fmt::format("{}{}.{}.{}.dylib", prefix, libname, major, minor);
else if (major >= 0)
return StringFromFormat("%s%s.%d.dylib", prefix, libname, major);
return fmt::format("{}{}.{}.dylib", prefix, libname, major);
else
return StringFromFormat("%s%s.dylib", prefix, libname);
return fmt::format("{}{}.dylib", prefix, libname);
#else
const char* prefix = std::strncmp(libname, "lib", 3) ? "lib" : "";
if (major >= 0 && minor >= 0)
return StringFromFormat("%s%s.so.%d.%d", prefix, libname, major, minor);
return fmt::format("{}{}.so.{}.{}", prefix, libname, major, minor);
else if (major >= 0)
return StringFromFormat("%s%s.so.%d", prefix, libname, major);
return fmt::format("{}{}.so.{}", prefix, libname, major);
else
return StringFromFormat("%s%s.so", prefix, libname);
return fmt::format("{}{}.so", prefix, libname);
#endif
}

0 comments on commit 5b92d50

Please sign in to comment.
You can’t perform that action at this time.