From cd0fdf15ffc81f01c1e221ad50ea67fd04296fc6 Mon Sep 17 00:00:00 2001 From: Kelvin Lee Date: Mon, 6 Jun 2022 04:47:40 +1000 Subject: [PATCH] Replace/remove StringCat() and friends (PR #1098) - Simply use std::string --- source/Debugger/Debugger_Help.cpp | 185 +++++++----------------------- source/Debugger/Debugger_Help.h | 4 - 2 files changed, 41 insertions(+), 148 deletions(-) diff --git a/source/Debugger/Debugger_Help.cpp b/source/Debugger/Debugger_Help.cpp index 208cdbfe..830c9b79 100644 --- a/source/Debugger/Debugger_Help.cpp +++ b/source/Debugger/Debugger_Help.cpp @@ -52,65 +52,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -// tests if pSrc fits into pDst -// returns true if pSrc safely fits into pDst, else false (pSrc would of overflowed pDst) -//=========================================================================== -bool TestStringCat ( TCHAR * pDst, LPCSTR pSrc, const int nDstSize ) -{ - int nLenDst = _tcslen( pDst ); - int nLenSrc = _tcslen( pSrc ); - int nSpcDst = nDstSize - nLenDst; - - bool bOverflow = (nSpcDst <= nLenSrc); // 2.5.6.25 BUGFIX - if (bOverflow) - { - return false; - } - return true; -} - - -// tests if pSrc fits into pDst -// returns true if pSrc safely fits into pDst, else false (pSrc would of overflowed pDst) -//=========================================================================== -bool TryStringCat ( TCHAR * pDst, LPCSTR pSrc, const int nDstSize ) -{ - int nLenDst = _tcslen( pDst ); - int nLenSrc = _tcslen( pSrc ); - int nSpcDst = nDstSize - nLenDst; - int nChars = MIN( nLenSrc, nSpcDst ); - - bool bOverflow = (nSpcDst < nLenSrc); - if (bOverflow) - { - return false; - } - - _tcsncat( pDst, pSrc, nChars ); - return true; -} - -// cats string as much as possible -// returns true if pSrc safely fits into pDst, else false (pSrc would of overflowed pDst) -//=========================================================================== -int StringCat ( TCHAR * pDst, LPCSTR pSrc, const int nDstSize ) -{ - int nLenDst = _tcslen( pDst ); - int nLenSrc = _tcslen( pSrc ); - int nSpcDst = nDstSize - nLenDst; - int nChars = MIN( nLenSrc, nSpcDst ); - - _tcsncat( pDst, pSrc, nChars ); - - bool bOverflow = (nSpcDst < nLenSrc); - if (bOverflow) - return 0; - - return nChars; -} - - - // Help ___________________________________________________________________________________________ @@ -136,74 +77,48 @@ Update_t Help_Arg_1( int iCommandHelp ) //=========================================================================== void Help_Categories() { - const int nBuf = CONSOLE_WIDTH * 2; - - char sText[ nBuf ] = ""; - int nLen = 0; - // TODO/FIXME: Colorize( sText, ... ) // Colorize("Usage:") - nLen += StringCat( sText, CHC_USAGE , nBuf ); - nLen += StringCat( sText, "Usage", nBuf ); - - nLen += StringCat( sText, CHC_DEFAULT, nBuf ); - nLen += StringCat( sText, ": " , nBuf ); - - nLen += StringCat( sText, CHC_ARG_OPT, nBuf ); - nLen += StringCat( sText, "[ ", nBuf ); - - nLen += StringCat( sText, CHC_ARG_MAND, nBuf ); - nLen += StringCat( sText, "< ", nBuf ); + std::string sText = CHC_USAGE "Usage" CHC_DEFAULT ": " CHC_ARG_OPT "[ " CHC_ARG_MAND "< "; for (int iCategory = _PARAM_HELPCATEGORIES_BEGIN ; iCategory < _PARAM_HELPCATEGORIES_END; iCategory++) { - char *pName = g_aParameters[ iCategory ].m_sName; + const char *pName = g_aParameters[ iCategory ].m_sName; - if (nLen + strlen( pName ) >= (CONSOLE_WIDTH - 1)) + if (sText.length() + strlen(pName) >= (CONSOLE_WIDTH - 1)) { - ConsolePrint( sText ); - sText[ 0 ] = 0; - nLen = StringCat( sText, " ", nBuf ); // indent + ConsolePrint( sText.c_str() ); + sText = " "; // indent } - StringCat( sText, CHC_COMMAND, nBuf ); - nLen += StringCat( sText, pName , nBuf ); + sText += CHC_COMMAND; + sText += pName; if (iCategory < (_PARAM_HELPCATEGORIES_END - 1)) { - char sSep[] = " | "; + const char sSep[] = " | "; - if (nLen + strlen( sSep ) >= (CONSOLE_WIDTH - 1)) + if (sText.length() + strlen(sSep) >= (CONSOLE_WIDTH - 1)) { - ConsolePrint( sText ); - sText[ 0 ] = 0; - nLen = StringCat( sText, " ", nBuf ); // indent + ConsolePrint( sText.c_str() ); + sText = " "; // indent } - StringCat( sText, CHC_ARG_SEP, nBuf ); - nLen += StringCat( sText, sSep, nBuf ); + + sText += CHC_ARG_SEP; + sText += sSep; } } - StringCat( sText, CHC_ARG_MAND, nBuf ); - StringCat( sText, " >", nBuf); + sText += CHC_ARG_MAND " >" CHC_ARG_OPT " ]"; - StringCat( sText, CHC_ARG_OPT, nBuf ); - StringCat( sText, " ]", nBuf); - -// ConsoleBufferPush( sText ); - ConsolePrint( sText ); // Transcode colored text to native console color text +// ConsoleBufferPush( sText.c_str() ); + ConsolePrint( sText.c_str() ); // Transcode colored text to native console color text - ConsolePrintFormat( "%sNotes%s: %s<>%s = mandatory, %s[]%s = optional, %s|%s argument option" - , CHC_USAGE - , CHC_DEFAULT - , CHC_ARG_MAND - , CHC_DEFAULT - , CHC_ARG_OPT - , CHC_DEFAULT - , CHC_ARG_SEP - , CHC_DEFAULT - ); -// ConsoleBufferPush( sText ); + ConsolePrintFormat( CHC_USAGE "Notes" CHC_DEFAULT ": " + CHC_ARG_MAND "<>" CHC_DEFAULT " = mandatory, " + CHC_ARG_OPT "[]" CHC_DEFAULT " = optional, " + CHC_ARG_SEP "|" CHC_DEFAULT " argument option" ); +// ConsoleBufferPush( sText.c_str() ); } void Help_Examples() @@ -257,21 +172,18 @@ void Help_Operators() // ConsoleBufferPush( " Operators: (Breakpoint)" ); ConsolePrintFormat( " Operators: (%sBreakpoint%s)" , CHC_USAGE, CHC_DEFAULT ); - char sText[CONSOLE_WIDTH]; - _tcscpy( sText, " " ); - strcat( sText, CHC_USAGE ); - int iBreakOp = 0; - for ( iBreakOp = 0; iBreakOp < NUM_BREAKPOINT_OPERATORS; iBreakOp++ ) + std::string sText = " " CHC_USAGE; + for ( int iBreakOp = 0; iBreakOp < NUM_BREAKPOINT_OPERATORS; iBreakOp++ ) { if ((iBreakOp >= PARAM_BP_LESS_EQUAL) && (iBreakOp <= PARAM_BP_GREATER_EQUAL)) { - strcat( sText, g_aBreakpointSymbols[ iBreakOp ] ); - strcat( sText, " " ); + sText += g_aBreakpointSymbols[ iBreakOp ]; + sText += ' '; } } - strcat( sText, CHC_DEFAULT ); - ConsolePrint( sText ); + sText += CHC_DEFAULT; + ConsolePrint( sText.c_str() ); } void Help_KeyboardShortcuts() @@ -1487,33 +1399,23 @@ Update_t CmdHelpSpecific (int nArgs) //=========================================================================== Update_t CmdHelpList (int nArgs) { - const int nBuf = CONSOLE_WIDTH * 2; - - char sText[ nBuf ] = ""; - - int nMaxWidth = g_nConsoleDisplayWidth - 1; - int iCommand; + const size_t nMaxWidth = g_nConsoleDisplayWidth - 1; extern std::vector g_vSortedCommands; if (! g_vSortedCommands.size()) { - for (iCommand = 0; iCommand < NUM_COMMANDS_WITH_ALIASES; iCommand++ ) + for ( int iCommand = 0; iCommand < NUM_COMMANDS_WITH_ALIASES; iCommand++ ) { g_vSortedCommands.push_back( g_aCommands[ iCommand ] ); } std::sort( g_vSortedCommands.begin(), g_vSortedCommands.end(), commands_functor_compare() ); } - int nLen = 0; -// Colorize( sText, "Commands: " ); - StringCat( sText, CHC_USAGE , nBuf ); - nLen += StringCat( sText, "Commands", nBuf ); + //Colorize( sText, "Commands: " ); + std::string sText = CHC_USAGE "Commands" CHC_DEFAULT ": "; - StringCat( sText, CHC_DEFAULT, nBuf ); - nLen += StringCat( sText, ": " , nBuf ); - - for ( iCommand = 0; iCommand < NUM_COMMANDS_WITH_ALIASES; iCommand++ ) // aliases are not printed + for ( int iCommand = 0; iCommand < NUM_COMMANDS_WITH_ALIASES; iCommand++ ) // aliases are not printed { Command_t *pCommand = & g_vSortedCommands.at( iCommand ); // Command_t *pCommand = & g_aCommands[ iCommand ]; @@ -1522,27 +1424,22 @@ Update_t CmdHelpList (int nArgs) if (! pCommand->pFunction) continue; // not implemented function - int nLenCmd = strlen( pName ); - if ((nLen + nLenCmd) < (nMaxWidth)) + if ((sText.length() + strlen(pName)) < nMaxWidth) { - StringCat( sText, CHC_COMMAND, nBuf ); - nLen += StringCat( sText, pName , nBuf ); + sText += CHC_COMMAND; } else { - ConsolePrint( sText ); - nLen = 1; - strcpy( sText, " " ); - StringCat( sText, CHC_COMMAND, nBuf ); - nLen += StringCat( sText, pName, nBuf ); + ConsolePrint( sText.c_str() ); + sText = " " CHC_COMMAND; } + sText += pName; - strcat( sText, " " ); - nLen++; + sText += ' '; } - //ConsoleBufferPush( sText ); - ConsolePrint( sText ); + //ConsoleBufferPush( sText.c_str() ); + ConsolePrint( sText.c_str() ); ConsoleUpdate(); return UPDATE_CONSOLE_DISPLAY; diff --git a/source/Debugger/Debugger_Help.h b/source/Debugger/Debugger_Help.h index b3ecdba7..7a42247f 100644 --- a/source/Debugger/Debugger_Help.h +++ b/source/Debugger/Debugger_Help.h @@ -26,8 +26,4 @@ inline void UnpackVersion( const unsigned int nVersion, nFixMinor_ = (nVersion >> 0) & 0xFF; } - bool TestStringCat ( TCHAR * pDst, LPCSTR pSrc, const int nDstSize ); - bool TryStringCat ( TCHAR * pDst, LPCSTR pSrc, const int nDstSize ); - int StringCat( TCHAR * pDst, LPCSTR pSrc, const int nDstSize ); - #endif