Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #456 from lioncash/adios-sprintf
Kill off replaceable usages of s[n]printf.
  • Loading branch information
delroth committed Jun 18, 2014
2 parents dccd9ea + ce54c1e commit 591a27e
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 245 deletions.
19 changes: 11 additions & 8 deletions Source/Core/Core/HW/SI_Device.cpp
Expand Up @@ -2,6 +2,9 @@
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <string>

#include "Common/StringUtil.h"
#include "Core/HW/SI_Device.h"
#include "Core/HW/SI_DeviceAMBaseboard.h"
#include "Core/HW/SI_DeviceDanceMat.h"
Expand All @@ -14,24 +17,24 @@
int ISIDevice::RunBuffer(u8* _pBuffer, int _iLength)
{
#ifdef _DEBUG
DEBUG_LOG(SERIALINTERFACE, "Send Data Device(%i) - Length(%i) ", ISIDevice::m_iDeviceNumber,_iLength);
DEBUG_LOG(SERIALINTERFACE, "Send Data Device(%i) - Length(%i) ", ISIDevice::m_iDeviceNumber, _iLength);

char szTemp[256] = "";
std::string temp;
int num = 0;

while (num < _iLength)
{
char szTemp2[128] = "";
sprintf(szTemp2, "0x%02x ", _pBuffer[num^3]);
strcat(szTemp, szTemp2);
temp += StringFromFormat("0x%02x ", _pBuffer[num^3]);
num++;

if ((num % 8) == 0)
{
DEBUG_LOG(SERIALINTERFACE, "%s", szTemp);
szTemp[0] = '\0';
DEBUG_LOG(SERIALINTERFACE, "%s", temp.c_str());
temp.clear();
}
}
DEBUG_LOG(SERIALINTERFACE, "%s", szTemp);

DEBUG_LOG(SERIALINTERFACE, "%s", temp.c_str());
#endif
return 0;
};
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_net.cpp
Expand Up @@ -1043,10 +1043,8 @@ bool CWII_IPC_HLE_Device_net_ip_top::IOCtl(u32 _CommandAddress)
for (int i = 0; remoteHost->h_addr_list[i]; ++i)
{
u32 ip = Common::swap32(*(u32*)(remoteHost->h_addr_list[i]));
char ip_s[16];
sprintf(ip_s, "%i.%i.%i.%i",
ip >> 24, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
DEBUG_LOG(WII_IPC_NET, "addr%i:%s", i, ip_s);
std::string ip_s = StringFromFormat("%i.%i.%i.%i", ip >> 24, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
DEBUG_LOG(WII_IPC_NET, "addr%i:%s", i, ip_s.c_str());
}

Memory::Memset(BufferOut, 0, BufferOutSize);
Expand Down
17 changes: 7 additions & 10 deletions Source/Core/Core/NetPlayServer.cpp
Expand Up @@ -2,8 +2,10 @@
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <string>
#include <vector>

#include "Common/StringUtil.h"
#include "Core/NetPlayServer.h"

NetPlayServer::~NetPlayServer()
Expand Down Expand Up @@ -686,15 +688,12 @@ bool NetPlayServer::initUPnP()
// Attempt to portforward!
bool NetPlayServer::UPnPMapPort(const std::string& addr, const u16 port)
{
char port_str[6] = { 0 };
int result;

if (m_upnp_mapped > 0)
UPnPUnmapPort(m_upnp_mapped);

sprintf(port_str, "%d", port);
result = UPNP_AddPortMapping(m_upnp_urls.controlURL, m_upnp_data.first.servicetype,
port_str, port_str, addr.c_str(),
std::string port_str = StringFromFormat("%d", port);
int result = UPNP_AddPortMapping(m_upnp_urls.controlURL, m_upnp_data.first.servicetype,
port_str.c_str(), port_str.c_str(), addr.c_str(),
(std::string("dolphin-emu TCP on ") + addr).c_str(),
"TCP", nullptr, nullptr);

Expand All @@ -716,11 +715,9 @@ bool NetPlayServer::UPnPMapPort(const std::string& addr, const u16 port)
// --
bool NetPlayServer::UPnPUnmapPort(const u16 port)
{
char port_str[6] = { 0 };

sprintf(port_str, "%d", port);
std::string port_str = StringFromFormat("%d", port);
UPNP_DeletePortMapping(m_upnp_urls.controlURL, m_upnp_data.first.servicetype,
port_str, "TCP", nullptr);
port_str.c_str(), "TCP", nullptr);

return true;
}
Expand Down
16 changes: 7 additions & 9 deletions Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp
Expand Up @@ -3,9 +3,11 @@
// Refer to the license.txt file included.

#include <cinttypes>
#include <string>

#include "PowerPCDisasm.h"

#include "Common/StringUtil.h"
#include "Core/Host.h"
#include "Core/Debugger/Debugger_SymbolMap.h"
#include "Core/IPC_HLE/WII_IPC_HLE.h"
Expand Down Expand Up @@ -65,22 +67,18 @@ static void patches()

int startTrace = 0;

void Trace( UGeckoInstruction &instCode )
void Trace(UGeckoInstruction& instCode)
{
char reg[25]="";
std::string regs = "";
for (int i=0; i<32; i++)
for (int i = 0; i < 32; i++)
{
sprintf(reg, "r%02d: %08x ", i, PowerPC::ppcState.gpr[i]);
regs.append(reg);
regs += StringFromFormat("r%02d: %08x ", i, PowerPC::ppcState.gpr[i]);
}

char freg[25]="";
std::string fregs = "";
for (int i=0; i<32; i++)
for (int i = 0; i < 32; i++)
{
sprintf(freg, "f%02d: %08" PRIx64 " %08" PRIx64 " ", i, PowerPC::ppcState.ps[i][0], PowerPC::ppcState.ps[i][1]);
fregs.append(freg);
fregs += StringFromFormat("f%02d: %08" PRIx64 " %08" PRIx64 " ", i, PowerPC::ppcState.ps[i][0], PowerPC::ppcState.ps[i][1]);
}

char ppcInst[256];
Expand Down
16 changes: 7 additions & 9 deletions Source/Core/Core/PowerPC/Jit64/Jit.cpp
Expand Up @@ -3,13 +3,15 @@
// Refer to the license.txt file included.

#include <map>
#include <string>

// for the PROFILER stuff
#ifdef _WIN32
#include <windows.h>
#endif

#include "Common/Common.h"
#include "Common/StringUtil.h"
#include "Core/PatchEngine.h"
#include "Core/HLE/HLE.h"
#include "Core/HW/ProcessorInterface.h"
Expand Down Expand Up @@ -363,31 +365,27 @@ void Jit64::SingleStep()

void Jit64::Trace()
{
char regs[500] = "";
char fregs[750] = "";
std::string regs;
std::string fregs;

#ifdef JIT_LOG_GPR
for (int i = 0; i < 32; i++)
{
char reg[50];
sprintf(reg, "r%02d: %08x ", i, PowerPC::ppcState.gpr[i]);
strncat(regs, reg, sizeof(regs) - 1);
regs += StringFromFormat("r%02d: %08x ", i, PowerPC::ppcState.gpr[i]);
}
#endif

#ifdef JIT_LOG_FPR
for (int i = 0; i < 32; i++)
{
char reg[50];
sprintf(reg, "f%02d: %016x ", i, riPS0(i));
strncat(fregs, reg, sizeof(fregs) - 1);
fregs += StringFromFormat("f%02d: %016x ", i, riPS0(i));
}
#endif

DEBUG_LOG(DYNA_REC, "JIT64 PC: %08x SRR0: %08x SRR1: %08x CRfast: %02x%02x%02x%02x%02x%02x%02x%02x FPSCR: %08x MSR: %08x LR: %08x %s %s",
PC, SRR0, SRR1, PowerPC::ppcState.cr_fast[0], PowerPC::ppcState.cr_fast[1], PowerPC::ppcState.cr_fast[2], PowerPC::ppcState.cr_fast[3],
PowerPC::ppcState.cr_fast[4], PowerPC::ppcState.cr_fast[5], PowerPC::ppcState.cr_fast[6], PowerPC::ppcState.cr_fast[7], PowerPC::ppcState.fpscr,
PowerPC::ppcState.msr, PowerPC::ppcState.spr[8], regs, fregs);
PowerPC::ppcState.msr, PowerPC::ppcState.spr[8], regs.c_str(), fregs.c_str());
}

void STACKALIGN Jit64::Jit(u32 em_address)
Expand Down
21 changes: 9 additions & 12 deletions Source/Core/Core/PowerPC/Jit64IL/JitIL.cpp
Expand Up @@ -6,9 +6,11 @@
#include <ctime> // For profiling
#include <map>
#include <memory>
#include <string>

#include "Common/Common.h"
#include "Common/StdMakeUnique.h"
#include "Common/StringUtil.h"
#include "Core/PatchEngine.h"
#include "Core/HLE/HLE.h"
#include "Core/PowerPC/Profiler.h"
Expand Down Expand Up @@ -205,9 +207,8 @@ namespace JitILProfiler
{
virtual ~JitILProfilerFinalizer()
{
char buffer[1024];
sprintf(buffer, "JitIL_profiling_%d.csv", (int)time(nullptr));
File::IOFile file(buffer, "w");
std::string filename = StringFromFormat("JitIL_profiling_%d.csv", (int)time(nullptr));
File::IOFile file(filename, "w");
setvbuf(file.GetHandle(), nullptr, _IOFBF, 1024 * 1024);
fprintf(file.GetHandle(), "code hash,total elapsed,number of calls,elapsed per call\n");
for (auto& block : blocks)
Expand Down Expand Up @@ -464,31 +465,27 @@ void JitIL::SingleStep()

void JitIL::Trace()
{
char regs[500] = "";
char fregs[750] = "";
std::string regs;
std::string fregs;

#ifdef JIT_LOG_GPR
for (int i = 0; i < 32; i++)
{
char reg[50];
sprintf(reg, "r%02d: %08x ", i, PowerPC::ppcState.gpr[i]);
strncat(regs, reg, sizeof(regs) - 1);
regs += StringFromFormat("r%02d: %08x ", i, PowerPC::ppcState.gpr[i]);
}
#endif

#ifdef JIT_LOG_FPR
for (int i = 0; i < 32; i++)
{
char reg[50];
sprintf(reg, "f%02d: %016x ", i, riPS0(i));
strncat(fregs, reg, sizeof(fregs) - 1);
fregs += StringFromFormat("f%02d: %016x ", i, riPS0(i));
}
#endif

DEBUG_LOG(DYNA_REC, "JITIL PC: %08x SRR0: %08x SRR1: %08x CRfast: %02x%02x%02x%02x%02x%02x%02x%02x FPSCR: %08x MSR: %08x LR: %08x %s %s",
PC, SRR0, SRR1, PowerPC::ppcState.cr_fast[0], PowerPC::ppcState.cr_fast[1], PowerPC::ppcState.cr_fast[2], PowerPC::ppcState.cr_fast[3],
PowerPC::ppcState.cr_fast[4], PowerPC::ppcState.cr_fast[5], PowerPC::ppcState.cr_fast[6], PowerPC::ppcState.cr_fast[7], PowerPC::ppcState.fpscr,
PowerPC::ppcState.msr, PowerPC::ppcState.spr[8], regs, fregs);
PowerPC::ppcState.msr, PowerPC::ppcState.spr[8], regs.c_str(), fregs.c_str());
}

void STACKALIGN JitIL::Jit(u32 em_address)
Expand Down
8 changes: 5 additions & 3 deletions Source/Core/Core/PowerPC/JitCommon/JitBase.cpp
Expand Up @@ -3,10 +3,12 @@
// Refer to the license.txt file included.

#include <sstream>
#include <string>

#include "disasm.h"
#include "PowerPCDisasm.h"

#include "Common/StringUtil.h"
#include "Core/PowerPC/JitCommon/JitBase.h"

JitBase *jit;
Expand All @@ -27,15 +29,15 @@ u32 Helper_Mask(u8 mb, u8 me)

void LogGeneratedX86(int size, PPCAnalyst::CodeBuffer *code_buffer, const u8 *normalEntry, JitBlock *b)
{
char pDis[1000] = "";
std::string ppcdisasm;

for (int i = 0; i < size; i++)
{
char temp[256] = "";
const PPCAnalyst::CodeOp &op = code_buffer->codebuffer[i];
DisassembleGekko(op.inst.hex, op.address, temp, 256);
sprintf(pDis, "%08x %s", op.address, temp);
DEBUG_LOG(DYNA_REC,"IR_X86 PPC: %s\n", pDis);
ppcdisasm += StringFromFormat("%08x %s", op.address, temp);
DEBUG_LOG(DYNA_REC, "IR_X86 PPC: %s\n", ppcdisasm.c_str());
}

disassembler x64disasm;
Expand Down
7 changes: 4 additions & 3 deletions Source/Core/Core/PowerPC/JitILCommon/IR.cpp
Expand Up @@ -124,8 +124,10 @@ TODO (in no particular order):
#include <ctime>
#include <memory>
#include <set>
#include <string>

#include "Common/StdMakeUnique.h"
#include "Common/StringUtil.h"
#include "Core/Core.h"
#include "Core/CoreTiming.h"
#include "Core/HW/GPFifo.h"
Expand Down Expand Up @@ -1220,9 +1222,8 @@ struct Writer
File::IOFile file;
Writer() : file(nullptr)
{
char buffer[1024];
sprintf(buffer, "JitIL_IR_%d.txt", (int)time(nullptr));
file.Open(buffer, "w");
std::string filename = StringFromFormat("JitIL_IR_%d.txt", (int)time(nullptr));
file.Open(filename, "w");
setvbuf(file.GetHandle(), nullptr, _IOFBF, 1024 * 1024);
}

Expand Down
19 changes: 8 additions & 11 deletions Source/Core/DiscIO/BannerLoaderWii.cpp
Expand Up @@ -24,42 +24,39 @@ CBannerLoaderWii::CBannerLoaderWii(DiscIO::IVolume *pVolume)
: m_pBannerFile(nullptr)
, m_IsValid(false)
{
char Filename[260];
u64 TitleID;

pVolume->GetTitleID((u8*)&TitleID);

TitleID = Common::swap64(TitleID);

sprintf(Filename, "%stitle/%08x/%08x/data/banner.bin",
File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);
std::string Filename = StringFromFormat("%stitle/%08x/%08x/data/banner.bin",
File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);

if (!File::Exists(Filename))
{
// TODO(XK): Finish the 'commented' code. Turns out the banner.bin
// from the savefiles is very different from the banner.bin
// inside opening.bnr
#if 0
char bnrFilename[260], titleFolder[260];

// Creating title folder
sprintf(titleFolder, "%stitle/%08x/%08x/data/",
std::string titleFolder = StringFromFormat("%stitle/%08x/%08x/data/",
File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);
if (!File::Exists(titleFolder))
File::CreateFullPath(titleFolder);

// Extracting banner.bin from opening.bnr
sprintf(bnrFilename, "%stitle/%08x/%08x/data/opening.bnr",
std::string bnrFilename = StringFromFormat("%stitle/%08x/%08x/data/opening.bnr",
File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);

if (!_rFileSystem.ExportFile("opening.bnr", bnrFilename)) {
if (!_rFileSystem.ExportFile("opening.bnr", bnrFilename))
{
m_IsValid = false;
return;
}

CARCFile bnrArc (bnrFilename, 0x600);

if (!bnrArc.ExportFile("meta/banner.bin", Filename)) {
if (!bnrArc.ExportFile("meta/banner.bin", Filename))
{
m_IsValid = false;
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/DiscIO/Blob.h
Expand Up @@ -66,7 +66,7 @@ class SectorReader : public IBlobReader
// Factory function - examines the path to choose the right type of IBlobReader, and returns one.
IBlobReader* CreateBlobReader(const std::string& filename);

typedef void (*CompressCB)(const char *text, float percent, void* arg);
typedef void (*CompressCB)(const std::string& text, float percent, void* arg);

bool CompressFileToBlob(const std::string& infile, const std::string& outfile, u32 sub_type = 0, int sector_size = 16384,
CompressCB callback = nullptr, void *arg = nullptr);
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/DiscIO/CompressedBlob.cpp
Expand Up @@ -17,6 +17,7 @@
#include "Common/Common.h"
#include "Common/FileUtil.h"
#include "Common/Hash.h"
#include "Common/StringUtil.h"
#include "DiscIO/Blob.h"
#include "DiscIO/CompressedBlob.h"
#include "DiscIO/DiscScrubber.h"
Expand Down Expand Up @@ -203,8 +204,8 @@ bool CompressFileToBlob(const std::string& infile, const std::string& outfile, u
int ratio = 0;
if (inpos != 0)
ratio = (int)(100 * position / inpos);
char temp[512];
sprintf(temp, "%i of %i blocks. Compression ratio %i%%", i, header.num_blocks, ratio);

std::string temp = StringFromFormat("%i of %i blocks. Compression ratio %i%%", i, header.num_blocks, ratio);
callback(temp, (float)i / (float)header.num_blocks, arg);
}

Expand Down

0 comments on commit 591a27e

Please sign in to comment.