Skip to content

Commit

Permalink
fmt::by_value, fmt::Format removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotekina committed Aug 24, 2015
1 parent 15057ff commit ce494f8
Show file tree
Hide file tree
Showing 35 changed files with 338 additions and 370 deletions.
2 changes: 1 addition & 1 deletion Utilities/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ void log_message(Log::LogType type, Log::Severity sev, std::string text);

template<typename... Args> never_inline void log_message(Log::LogType type, Log::Severity sev, const char* fmt, Args... args)
{
log_message(type, sev, fmt::Format(fmt, fmt::do_unveil(args)...));
log_message(type, sev, fmt::format(fmt, fmt::do_unveil(args)...));
}
6 changes: 3 additions & 3 deletions Utilities/StrFmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ std::string v128::to_hex() const

std::string v128::to_xyzw() const
{
return fmt::Format("x: %g y: %g z: %g w: %g", _f[3], _f[2], _f[1], _f[0]);
return fmt::format("x: %g y: %g z: %g w: %g", _f[3], _f[2], _f[1], _f[0]);
}

std::string fmt::to_hex(u64 value, size_t count)
Expand Down Expand Up @@ -74,7 +74,7 @@ std::string fmt::to_sdec(s64 svalue)
return std::string(&res[first], sizeof(res) - first);
}

extern const std::string fmt::placeholder = "???";
//extern const std::string fmt::placeholder = "???";

std::string fmt::replace_first(const std::string& src, const std::string& from, const std::string& to)
{
Expand Down Expand Up @@ -240,7 +240,7 @@ std::string fmt::escape(std::string source)

for (char c = 0; c < 32; c++)
{
if (c != '\n') source = fmt::replace_all(source, std::string(1, c), fmt::Format("\\x%02X", c));
if (c != '\n') source = fmt::replace_all(source, std::string(1, c), fmt::format("\\x%02X", c));
}

return source;
Expand Down
84 changes: 35 additions & 49 deletions Utilities/StrFmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

class wxString;

#if defined(_MSC_VER)
#if defined(_MSC_VER) && _MSC_VER <= 1800
#define snprintf _snprintf
#endif

namespace fmt
{
struct empty_t{};
//struct empty_t{};

extern const std::string placeholder;
//extern const std::string placeholder;

template <typename T>
std::string AfterLast(const std::string& source, T searchstr)
Expand Down Expand Up @@ -90,37 +90,6 @@ namespace fmt
// return result;
//}

//small wrapper used to deal with bitfields
template<typename T>
T by_value(T x) { return x; }

//wrapper to deal with advance sprintf formating options with automatic length finding
template<typename... Args> std::string Format(const char* fmt, Args... parameters)
{
size_t length = 256;
std::string str;

for (;;)
{
std::vector<char> buffptr(length);
#if !defined(_MSC_VER)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
size_t printlen = snprintf(buffptr.data(), length, fmt, std::forward<Args>(parameters)...);
#pragma GCC diagnostic pop
#else
size_t printlen = _snprintf_s(buffptr.data(), length, length - 1, fmt, std::forward<Args>(parameters)...);
#endif
if (printlen < length)
{
str = std::string(buffptr.data(), printlen);
break;
}
length *= 2;
}
return str;
}

std::string replace_first(const std::string& src, const std::string& from, const std::string& to);
std::string replace_all(const std::string &src, const std::string& from, const std::string& to);

Expand Down Expand Up @@ -259,22 +228,39 @@ namespace fmt
return unveil<T>::get_value(arg);
}

/*
fmt::format(const char* fmt, args...)
// Formatting function with special functionality:
//
// std::string is forced to .c_str()
// be_t<> is forced to .value() (fmt::do_unveil reverts byte order automatically)
//
// External specializations for fmt::do_unveil (can be found in another headers):
// vm::ptr, vm::bptr, ... (fmt::do_unveil) (vm_ptr.h) (with appropriate address type, using .addr() can be avoided)
// vm::ref, vm::bref, ... (fmt::do_unveil) (vm_ref.h)
//
template<typename... Args> safe_buffers std::string format(const char* fmt, Args... args)
{
// fixed stack buffer for the first attempt
std::array<char, 4096> fixed_buf;

Formatting function with special functionality:
// possibly dynamically allocated buffer for additional attempts
std::unique_ptr<char[]> buf;

std::string forced to .c_str()
be_t<> forced to .value() (fmt::unveil reverts byte order automatically)
// pointer to the current buffer
char* buf_addr = fixed_buf.data();

External specializations for fmt::unveil (can be found in another headers):
vm::ptr, vm::bptr, ... (fmt::unveil) (vm_ptr.h) (with appropriate address type, using .addr() can be avoided)
vm::ref, vm::bref, ... (fmt::unveil) (vm_ref.h)
*/
template<typename... Args> force_inline safe_buffers std::string format(const char* fmt, Args... args)
{
return Format(fmt, do_unveil(args)...);
for (std::size_t buf_size = fixed_buf.size();; buf_size *= 2, buf.reset(buf_addr = new char[buf_size]))
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
const std::size_t len = snprintf(buf_addr, buf_size, fmt, do_unveil(args)...);

#pragma GCC diagnostic pop

if (len <= buf_size)
{
return{ buf_addr, len };
}
}
}

struct exception
Expand All @@ -285,7 +271,7 @@ namespace fmt
{
const std::string data = format(text, args...) + format("\n(in file %s:%d, in function %s)", file, line, func);

message = std::make_unique<char[]>(data.size() + 1);
message.reset(new char[data.size() + 1]);

std::memcpy(message.get(), data.c_str(), data.size() + 1);
}
Expand All @@ -294,7 +280,7 @@ namespace fmt
{
const std::size_t size = std::strlen(other);

message = std::make_unique<char[]>(size + 1);
message.reset(new char[size + 1]);

std::memcpy(message.get(), other, size + 1);
}
Expand Down
22 changes: 11 additions & 11 deletions rpcs3/Crypto/unself.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ void ControlInfo::Show()
{
std::string digest_str;
for (int i = 0; i < 20; i++)
digest_str += fmt::Format("%02x", file_digest_30.digest[i]);
digest_str += fmt::format("%02x", file_digest_30.digest[i]);

LOG_NOTICE(LOADER, "Digest: %s", digest_str.c_str());
LOG_NOTICE(LOADER, "Unknown: 0x%llx", file_digest_30.unknown);
Expand All @@ -364,8 +364,8 @@ void ControlInfo::Show()
std::string digest_str2;
for (int i = 0; i < 20; i++)
{
digest_str1 += fmt::Format("%02x", file_digest_40.digest1[i]);
digest_str2 += fmt::Format("%02x", file_digest_40.digest2[i]);
digest_str1 += fmt::format("%02x", file_digest_40.digest1[i]);
digest_str2 += fmt::format("%02x", file_digest_40.digest2[i]);
}

LOG_NOTICE(LOADER, "Digest1: %s", digest_str1.c_str());
Expand All @@ -380,12 +380,12 @@ void ControlInfo::Show()
std::string invdigest_str;
std::string xordigest_str;
for (int i = 0; i < 48; i++)
contentid_str += fmt::Format("%02x", npdrm.content_id[i]);
contentid_str += fmt::format("%02x", npdrm.content_id[i]);
for (int i = 0; i < 16; i++)
{
digest_str += fmt::Format("%02x", npdrm.digest[i]);
invdigest_str += fmt::Format("%02x", npdrm.invdigest[i]);
xordigest_str += fmt::Format("%02x", npdrm.xordigest[i]);
digest_str += fmt::format("%02x", npdrm.digest[i]);
invdigest_str += fmt::format("%02x", npdrm.invdigest[i]);
xordigest_str += fmt::format("%02x", npdrm.xordigest[i]);
}

LOG_NOTICE(LOADER, "Magic: 0x%08x", npdrm.magic);
Expand Down Expand Up @@ -417,10 +417,10 @@ void MetadataInfo::Show()
std::string iv_pad_str;
for (int i = 0; i < 0x10; i++)
{
key_str += fmt::Format("%02x", key[i]);
key_pad_str += fmt::Format("%02x", key_pad[i]);
iv_str += fmt::Format("%02x", iv[i]);
iv_pad_str += fmt::Format("%02x", iv_pad[i]);
key_str += fmt::format("%02x", key[i]);
key_pad_str += fmt::format("%02x", key_pad[i]);
iv_str += fmt::format("%02x", iv[i]);
iv_pad_str += fmt::format("%02x", iv_pad[i]);
}

LOG_NOTICE(LOADER, "Key: %s", key_str.c_str());
Expand Down
16 changes: 8 additions & 8 deletions rpcs3/Emu/ARMv7/ARMv7DisAsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ void ARMv7DisAsm::B(const u32 data, const ARMv7_encoding type)
Write(__FUNCTION__);
//if ((cond & 0xe) == 0xe)
//{
// Write(fmt::Format("b 0x%x", DisAsmBranchTarget(imm) + intstr_size));
// Write(fmt::format("b 0x%x", DisAsmBranchTarget(imm) + intstr_size));
//}
//else
//{
// Write(fmt::Format("b[%s] 0x%x", g_arm_cond_name[cond], DisAsmBranchTarget(imm) + intstr_size));
// Write(fmt::format("b[%s] 0x%x", g_arm_cond_name[cond], DisAsmBranchTarget(imm) + intstr_size));
//}
}

Expand Down Expand Up @@ -142,13 +142,13 @@ void ARMv7DisAsm::BKPT(const u32 data, const ARMv7_encoding type)
void ARMv7DisAsm::BL(const u32 data, const ARMv7_encoding type)
{
Write(__FUNCTION__);
//Write(fmt::Format("bl 0x%x", DisAsmBranchTarget(imm) + intstr_size));
//Write(fmt::format("bl 0x%x", DisAsmBranchTarget(imm) + intstr_size));
}

void ARMv7DisAsm::BLX(const u32 data, const ARMv7_encoding type)
{
Write(__FUNCTION__);
//Write(fmt::Format("bl 0x%x", DisAsmBranchTarget(imm) + intstr_size));
//Write(fmt::format("bl 0x%x", DisAsmBranchTarget(imm) + intstr_size));
}

void ARMv7DisAsm::BX(const u32 data, const ARMv7_encoding type)
Expand All @@ -160,8 +160,8 @@ void ARMv7DisAsm::BX(const u32 data, const ARMv7_encoding type)
void ARMv7DisAsm::CB_Z(const u32 data, const ARMv7_encoding type)
{
Write(__FUNCTION__);
//Write(fmt::Format("cbz 0x%x,%s", DisAsmBranchTarget(imm) + intstr_size, g_arm_reg_name[rn]));
//Write(fmt::Format("cbnz 0x%x,%s", DisAsmBranchTarget(imm) + intstr_size, g_arm_reg_name[rn]));
//Write(fmt::format("cbz 0x%x,%s", DisAsmBranchTarget(imm) + intstr_size, g_arm_reg_name[rn]));
//Write(fmt::format("cbnz 0x%x,%s", DisAsmBranchTarget(imm) + intstr_size, g_arm_reg_name[rn]));
}


Expand Down Expand Up @@ -471,13 +471,13 @@ void ARMv7DisAsm::PKH(const u32 data, const ARMv7_encoding type)
void ARMv7DisAsm::POP(const u32 data, const ARMv7_encoding type)
{
Write(__FUNCTION__);
//Write(fmt::Format("pop {%s}", GetRegsListString(regs_list).c_str()));
//Write(fmt::format("pop {%s}", GetRegsListString(regs_list).c_str()));
}

void ARMv7DisAsm::PUSH(const u32 data, const ARMv7_encoding type)
{
Write(__FUNCTION__);
//Write(fmt::Format("push {%s}", GetRegsListString(regs_list).c_str()));
//Write(fmt::format("push {%s}", GetRegsListString(regs_list).c_str()));
}


Expand Down
14 changes: 7 additions & 7 deletions rpcs3/Emu/ARMv7/ARMv7Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ std::string ARMv7Thread::RegsToString() const
std::string result = "Registers:\n=========\n";
for(int i=0; i<15; ++i)
{
result += fmt::Format("%s\t= 0x%08x\n", g_arm_reg_name[i], GPR[i]);
result += fmt::format("%s\t= 0x%08x\n", g_arm_reg_name[i], GPR[i]);
}

result += fmt::Format("APSR\t= 0x%08x [N: %d, Z: %d, C: %d, V: %d, Q: %d]\n",
result += fmt::format("APSR\t= 0x%08x [N: %d, Z: %d, C: %d, V: %d, Q: %d]\n",
APSR.APSR,
fmt::by_value(APSR.N),
fmt::by_value(APSR.Z),
fmt::by_value(APSR.C),
fmt::by_value(APSR.V),
fmt::by_value(APSR.Q));
u32{ APSR.N },
u32{ APSR.Z },
u32{ APSR.C },
u32{ APSR.V },
u32{ APSR.Q });

return result;
}
Expand Down
Loading

0 comments on commit ce494f8

Please sign in to comment.