Debugger: replace sprintf() part 1 (PR #1060)

- Add MemoryTextFile_t::PushLineFormat()
- Replace some sprintf() with PushLineFormat()
This commit is contained in:
Kelvin Lee 2022-03-14 03:37:25 +11:00 committed by GitHub
parent 35ec3fcc7f
commit e38e48e3a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 20 deletions

View File

@ -672,8 +672,6 @@ Update_t CmdBookmarkLoad (int nArgs)
//=========================================================================== //===========================================================================
Update_t CmdBookmarkSave (int nArgs) Update_t CmdBookmarkSave (int nArgs)
{ {
TCHAR sText[ CONSOLE_WIDTH ];
g_ConfigState.Reset(); g_ConfigState.Reset();
ConfigSave_PrepareHeader( PARAM_CAT_BOOKMARKS, CMD_BOOKMARK_CLEAR ); ConfigSave_PrepareHeader( PARAM_CAT_BOOKMARKS, CMD_BOOKMARK_CLEAR );
@ -683,12 +681,11 @@ Update_t CmdBookmarkSave (int nArgs)
{ {
if (g_aBookmarks[ iBookmark ].bSet) if (g_aBookmarks[ iBookmark ].bSet)
{ {
sprintf( sText, "%s %x %04X\n" g_ConfigState.PushLineFormat( "%s %x %04X\n"
, g_aCommands[ CMD_BOOKMARK_ADD ].m_sName , g_aCommands[ CMD_BOOKMARK_ADD ].m_sName
, iBookmark , iBookmark
, g_aBookmarks[ iBookmark ].nAddress , g_aBookmarks[ iBookmark ].nAddress
); );
g_ConfigState.PushLine( sText );
} }
iBookmark++; iBookmark++;
} }
@ -1768,8 +1765,6 @@ Update_t CmdBreakpointLoad (int nArgs)
//=========================================================================== //===========================================================================
Update_t CmdBreakpointSave (int nArgs) Update_t CmdBreakpointSave (int nArgs)
{ {
TCHAR sText[ CONSOLE_WIDTH ];
g_ConfigState.Reset(); g_ConfigState.Reset();
ConfigSave_PrepareHeader( PARAM_CAT_BREAKPOINTS, CMD_BREAKPOINT_CLEAR ); ConfigSave_PrepareHeader( PARAM_CAT_BREAKPOINTS, CMD_BREAKPOINT_CLEAR );
@ -1779,21 +1774,19 @@ Update_t CmdBreakpointSave (int nArgs)
{ {
if (g_aBreakpoints[ iBreakpoint ].bSet) if (g_aBreakpoints[ iBreakpoint ].bSet)
{ {
sprintf( sText, "%s %x %04X,%04X\n" g_ConfigState.PushLineFormat( "%s %x %04X,%04X\n"
, g_aCommands[ CMD_BREAKPOINT_ADD_REG ].m_sName , g_aCommands[ CMD_BREAKPOINT_ADD_REG ].m_sName
, iBreakpoint , iBreakpoint
, g_aBreakpoints[ iBreakpoint ].nAddress , g_aBreakpoints[ iBreakpoint ].nAddress
, g_aBreakpoints[ iBreakpoint ].nLength , g_aBreakpoints[ iBreakpoint ].nLength
); );
g_ConfigState.PushLine( sText );
} }
if (! g_aBreakpoints[ iBreakpoint ].bEnabled) if (! g_aBreakpoints[ iBreakpoint ].bEnabled)
{ {
sprintf( sText, "%s %x\n" g_ConfigState.PushLineFormat( "%s %x\n"
, g_aCommands[ CMD_BREAKPOINT_DISABLE ].m_sName , g_aCommands[ CMD_BREAKPOINT_DISABLE ].m_sName
, iBreakpoint , iBreakpoint
); );
g_ConfigState.PushLine( sText );
} }
iBreakpoint++; iBreakpoint++;
@ -2435,20 +2428,16 @@ bool ConfigSave_BufferToDisk ( const char *pFileName, ConfigSave_t eConfigSave )
//=========================================================================== //===========================================================================
void ConfigSave_PrepareHeader ( const Parameters_e eCategory, const Commands_e eCommandClear ) void ConfigSave_PrepareHeader ( const Parameters_e eCategory, const Commands_e eCommandClear )
{ {
char sText[ CONSOLE_WIDTH ]; g_ConfigState.PushLineFormat( "%s %s = %s\n"
sprintf( sText, "%s %s = %s\n"
, g_aTokens[ TOKEN_COMMENT_EOL ].sToken , g_aTokens[ TOKEN_COMMENT_EOL ].sToken
, g_aParameters[ PARAM_CATEGORY ].m_sName , g_aParameters[ PARAM_CATEGORY ].m_sName
, g_aParameters[ eCategory ].m_sName , g_aParameters[ eCategory ].m_sName
); );
g_ConfigState.PushLine( sText );
sprintf( sText, "%s %s\n" g_ConfigState.PushLineFormat( "%s %s\n"
, g_aCommands[ eCommandClear ].m_sName , g_aCommands[ eCommandClear ].m_sName
, g_aParameters[ PARAM_WILDSTAR ].m_sName , g_aParameters[ PARAM_WILDSTAR ].m_sName
); );
g_ConfigState.PushLine( sText );
} }

View File

@ -25,6 +25,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "Util_Text.h" #include "Util_Text.h"
#include "Util_MemoryTextFile.h" #include "Util_MemoryTextFile.h"
#include "StrFormat.h"
// MemoryTextFile _________________________________________________________________________________ // MemoryTextFile _________________________________________________________________________________
@ -121,9 +122,9 @@ void MemoryTextFile_t::GetLinePointers()
//=========================================================================== //===========================================================================
void MemoryTextFile_t::PushLine( char *pLine ) void MemoryTextFile_t::PushLine( const char *pLine )
{ {
char *pSrc = pLine; const char *pSrc = pLine;
while (pSrc && *pSrc) while (pSrc && *pSrc)
{ {
if (*pSrc == CHAR_CR) if (*pSrc == CHAR_CR)
@ -141,4 +142,10 @@ void MemoryTextFile_t::PushLine( char *pLine )
m_bDirty = true; m_bDirty = true;
} }
void MemoryTextFile_t::PushLineFormat( const char *pFormat, ... )
{
va_list va;
va_start(va, pFormat);
PushLine(StrFormatV(pFormat, va).c_str());
va_end(va);
}

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "StrFormat.h"
// Memory Text File _________________________________________________________ // Memory Text File _________________________________________________________
class MemoryTextFile_t class MemoryTextFile_t
@ -43,6 +45,7 @@ inline char *GetLine( const int iLine ) const
void GetLine( const int iLine, char *pLine, const int n ); void GetLine( const int iLine, char *pLine, const int n );
void PushLine( char *pLine ); void PushLine( const char *pLine );
void PushLineFormat( const char *pFormat, ... ) ATTRIBUTE_FORMAT_PRINTF(2, 3); // 1 is "this"
}; };