Replace sprintf() with StrFormat() (PR #1048)

This commit is contained in:
Kelvin Lee 2022-03-03 08:10:41 +11:00 committed by GitHub
parent 80f3eaeb91
commit dfb8802763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 99 additions and 111 deletions

View File

@ -108,33 +108,24 @@ BYTE Disk2InterfaceCard::GetCurrentLSSBitMask(void) { return m_floppyDrive[m_cur
double Disk2InterfaceCard::GetCurrentExtraCycles(void) { return m_floppyDrive[m_currDrive].m_disk.m_extraCycles; }
int Disk2InterfaceCard::GetTrack(const int drive) { return ImagePhaseToTrack(m_floppyDrive[drive].m_disk.m_imagehandle, m_floppyDrive[drive].m_phasePrecise, false); }
std::string Disk2InterfaceCard::FormatPhaseString(float phase)
{
const UINT phaseInt = (UINT)phase;
const UINT phaseFrac = (UINT)((phase - (float)phaseInt) * 100 + 0.5);
return StrFormat("%02X.%2d", phaseInt, phaseFrac); // "$NN.nn"
}
std::string Disk2InterfaceCard::GetCurrentTrackString(void)
{
const UINT trackInt = (UINT)(m_floppyDrive[m_currDrive].m_phasePrecise / 2);
const float trackFrac = (m_floppyDrive[m_currDrive].m_phasePrecise / 2) - (float)trackInt;
char szInt[8] = "";
sprintf(szInt, "%02X", trackInt); // "$NN"
char szFrac[8] = "";
sprintf(szFrac, "%.02f", trackFrac); // "0.nn"
return std::string(szInt) + std::string(szFrac+1);
return FormatPhaseString(m_floppyDrive[m_currDrive].m_phasePrecise / 2);
}
std::string Disk2InterfaceCard::GetCurrentPhaseString(void)
{
const UINT phaseInt = (UINT)(m_floppyDrive[m_currDrive].m_phasePrecise);
const float phaseFrac = m_floppyDrive[m_currDrive].m_phasePrecise - (float)phaseInt;
char szInt[8] = "";
sprintf(szInt, "%02X", phaseInt); // "$NN"
char szFrac[8] = "";
sprintf(szFrac, "%.02f", phaseFrac); // "0.nn"
return std::string(szInt) + std::string(szFrac+1);
return FormatPhaseString(m_floppyDrive[m_currDrive].m_phasePrecise);
}
LPCTSTR Disk2InterfaceCard::GetCurrentState(void)
{
if (m_floppyDrive[m_currDrive].m_disk.m_imagehandle == NULL)

View File

@ -159,6 +159,7 @@ public:
BYTE GetCurrentLSSBitMask(void);
double GetCurrentExtraCycles(void);
int GetTrack(const int drive);
static std::string FormatPhaseString(float phase);
std::string GetCurrentTrackString(void);
std::string GetCurrentPhaseString(void);
LPCTSTR GetCurrentState(void);

View File

@ -443,9 +443,7 @@ bool Saturn128K::LoadSnapshot(YamlLoadHelper& yamlLoadHelper, UINT version)
}
// "Memory Bankxx"
char szBank[3];
sprintf(szBank, "%02X", uBank);
std::string memName = GetSnapshotMemStructName() + szBank;
std::string memName = GetSnapshotMemStructName() + StrFormat("%02X", uBank);
if (!yamlLoadHelper.GetSubMap(memName))
throw std::runtime_error("Memory: Missing map name: " + memName);

View File

@ -1079,22 +1079,34 @@ void SetMemMode(DWORD uNewMemMode)
if (dwOldDiff != dwDiff)
{
dwOldDiff = dwDiff;
char szStr[100];
char* psz = szStr;
psz += sprintf(psz, "diff = %08X ", dwDiff);
psz += sprintf(psz, "80=%d " , SW_80STORE ? 1 : 0);
psz += sprintf(psz, "ALTZP=%d ", SW_ALTZP ? 1 : 0);
psz += sprintf(psz, "AUXR=%d " , SW_AUXREAD ? 1 : 0);
psz += sprintf(psz, "AUXW=%d " , SW_AUXWRITE ? 1 : 0);
psz += sprintf(psz, "BANK2=%d ", SW_BANK2 ? 1 : 0);
psz += sprintf(psz, "HIRAM=%d ", SW_HIGHRAM ? 1 : 0);
psz += sprintf(psz, "HIRES=%d ", SW_HIRES ? 1 : 0);
psz += sprintf(psz, "PAGE2=%d ", SW_PAGE2 ? 1 : 0);
psz += sprintf(psz, "C3=%d " , SW_SLOTC3ROM ? 1 : 0);
psz += sprintf(psz, "CX=%d " , SW_INTCXROM ? 1 : 0);
psz += sprintf(psz, "WRAM=%d " , SW_WRITERAM ? 1 : 0);
psz += sprintf(psz, "\n");
OutputDebugString(szStr);
std::string str = StrFormat(
/*01*/ "diff = %08X "
/*02*/ "80=%d "
/*03*/ "ALTZP=%d "
/*04*/ "AUXR=%d "
/*05*/ "AUXW=%d "
/*06*/ "BANK2=%d "
/*07*/ "HIRAM=%d "
/*08*/ "HIRES=%d "
/*09*/ "PAGE2=%d "
/*10*/ "C3=%d "
/*11*/ "CX=%d "
/*12*/ "WRAM=%d "
"\n",
/*01*/ dwDiff,
/*02*/ SW_80STORE ? 1 : 0,
/*03*/ SW_ALTZP ? 1 : 0,
/*04*/ SW_AUXREAD ? 1 : 0,
/*05*/ SW_AUXWRITE ? 1 : 0,
/*06*/ SW_BANK2 ? 1 : 0,
/*07*/ SW_HIGHRAM ? 1 : 0,
/*08*/ SW_HIRES ? 1 : 0,
/*09*/ SW_PAGE2 ? 1 : 0,
/*10*/ SW_SLOTC3ROM ? 1 : 0,
/*11*/ SW_INTCXROM ? 1 : 0,
/*12*/ SW_WRITERAM ? 1 : 0
);
OutputDebugString(str.c_str());
}
#endif
memmode = uNewMemMode;
@ -2430,9 +2442,7 @@ static void MemLoadSnapshotAuxCommon(YamlLoadHelper& yamlLoadHelper, const std::
}
// "Auxiliary Memory Bankxx"
char szBank[3];
sprintf(szBank, "%02X", uBank-1);
std::string auxMemName = MemGetSnapshotAuxMemStructName() + szBank;
std::string auxMemName = MemGetSnapshotAuxMemStructName() + StrFormat("%02X", uBank-1);
if (!yamlLoadHelper.GetSubMap(auxMemName))
throw std::runtime_error("Memory: Missing map name: " + auxMemName);

View File

@ -84,11 +84,10 @@ void SSI_Output(void)
int ssi2 = ssiRegs[SSI_RATEINF];
LogOutput("SSI: ");
for (int i=0; i<=4; i++)
for (int i = 0; i <= 4; i++)
{
char r[3]="--";
if (ssiRegs[i]>=0) sprintf(r,"%02X",ssiRegs[i]);
LogOutput("%s ", r);
std::string r = (ssiRegs[i] >= 0) ? StrFormat("%02X", ssiRegs[i]) : "--";
LogOutput("%s ", r.c_str());
ssiRegs[i] = -1;
}

View File

@ -1,7 +1,14 @@
#pragma once
#include <string>
#include <cstdarg>
#include <string>
#if defined(_MSC_VER) && _MSC_VER < 1600
#include <basetsd.h>
typedef UINT8 uint8_t;
#else
#include <cstdint>
#endif
#ifdef _MSC_VER
#define ATTRIBUTE_FORMAT_PRINTF(a, b)
@ -11,3 +18,11 @@
std::string StrFormat(const char* format, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
std::string StrFormatV(const char* format, va_list va);
inline std::string& StrAppendByteAsHex(std::string& s, uint8_t n)
{
const char szHex[] = "0123456789ABCDEF";
s += szHex[(n >> 4) & 0x0f];
s += szHex[n & 0x0f];
return s;
}

View File

@ -185,26 +185,24 @@ static pcap_if_t *TfePcapAlldevs = NULL;
static char TfePcapErrbuf[PCAP_ERRBUF_SIZE];
#ifdef TFE_DEBUG_PKTDUMP
static
void debug_output( const char *text, BYTE *what, int count )
void debug_output( const char *text, const BYTE *what, int count )
{
char buffer[256];
char *p = buffer;
char *pbuffer1 = what;
int len1 = count;
int i;
LogOutput("\n%s: length = %u\n", text, len1);
do {
p = buffer;
for (i=0; (i<8) && len1>0; len1--, i++) {
sprintf( p, "%02x ", (unsigned int)(unsigned char)*pbuffer1++);
p += 3;
}
*(p-1) = '\n'; *p = 0;
OutputDebugString(buffer);
} while (len1>0);
std::string buffer;
buffer.reserve(8 * 3 + 1);
int len1 = count;
const BYTE *pb = what;
LogOutput("\n%s: length = %u\n", text, len1);
do {
buffer.clear();
for (int i = 0; i < 8 && len1 > 0; ++i, --len1, ++pb)
{
StrAppendByteAsHex(buffer, *pb);
buffer += ' ';
}
buffer += '\n';
OutputDebugString(buffer.c_str());
} while (len1 > 0);
}
#endif // #ifdef TFE_DEBUG_PKTDUMP

View File

@ -115,32 +115,17 @@
/* debugging functions */
#ifdef TFE_DEBUG_FRAMES
static int TfeDebugMaxFrameLengthToDump = 150;
char *debug_outbuffer(const int length, const unsigned char * const buffer)
std::string debug_outbuffer(size_t length, const unsigned char* buffer)
{
#define MAXLEN_DEBUG 1600
int i;
static char outbuffer[MAXLEN_DEBUG*4+1];
char *p = outbuffer;
assert( TfeDebugMaxFrameLengthToDump <= MAXLEN_DEBUG );
*p = 0;
for (i=0; i<TfeDebugMaxFrameLengthToDump; i++) {
if (i>=length)
break;
sprintf( p, "%02X%c", buffer[i], ((i+1)%16==0)?'*':(((i+1)%8==0)?'-':' '));
p+=3;
}
return outbuffer;
std::string outbuffer;
outbuffer.reserve(length * 3);
for (size_t i = 0; i < length; i++)
{
StrAppendByteAsHex(outbuffer, buffer[i]);
outbuffer += ((i+1)%16==0)?'*':(((i+1)%8==0)?'-':' ');
}
return outbuffer;
}
#endif
@ -150,26 +135,17 @@ char *debug_outbuffer(const int length, const unsigned char * const buffer)
void Uthernet1::tfe_debug_output_general( const char *what, WORD (Uthernet1::*getFunc)(int), int count )
{
int i;
char buffer[7+(6*NUMBER_PER_LINE)+2];
if(g_fh) fprintf(g_fh, "%s contents:", what );
for (i=0; i<count; i += 2*NUMBER_PER_LINE )
{
int j;
char *p = buffer + 7;
sprintf( buffer, "%04X: ", i );
for (j=0; j<NUMBER_PER_LINE; j++)
{
sprintf( p, "%04X, ", (this->*getFunc)(i+j+j) );
p += 6;
}
*p = 0;
if(g_fh) fprintf(g_fh, "%s", buffer );
}
if (!g_fh) return;
fprintf(g_fh, "%s contents:\n", what);
for (int i = 0; i < count; i += 2*NUMBER_PER_LINE)
{
fprintf(g_fh, "%04X: ", i);
for (int j = 0; j < NUMBER_PER_LINE; j++)
{
fprintf(g_fh, "%04X, ", (this->*getFunc)(i+j+j));
}
fputc('\n', g_fh);
}
}
WORD Uthernet1::tfe_debug_output_io_getFunc( int i )
@ -315,7 +291,7 @@ void Uthernet1::tfe_sideeffects_write_pp_on_txframe(WORD ppaddress)
#ifdef TFE_DEBUG_FRAMES
if(g_fh) fprintf(g_fh, "tfe_arch_transmit() called with: "
"length=%4u and buffer %s", txlen,
debug_outbuffer(txlen, &tfe_packetpage[TFE_PP_ADDR_TX_FRAMELOC])
debug_outbuffer(txlen, &tfe_packetpage[TFE_PP_ADDR_TX_FRAMELOC]).c_str()
);
#endif
@ -819,7 +795,7 @@ int Uthernet1::tfe_should_accept(unsigned char *buffer, int length, int *phashed
tfe_ia_mac[0], tfe_ia_mac[1], tfe_ia_mac[2],
tfe_ia_mac[3], tfe_ia_mac[4], tfe_ia_mac[5],
length,
debug_outbuffer(length, buffer)
debug_outbuffer(length, buffer).c_str()
);
#endif