Replace/remove StringCat() and friends (PR #1098)

- Simply use std::string
This commit is contained in:
Kelvin Lee 2022-06-06 04:47:40 +10:00 committed by GitHub
parent 240b1fd6c7
commit cd0fdf15ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 148 deletions

View File

@ -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 ___________________________________________________________________________________________ // Help ___________________________________________________________________________________________
@ -136,74 +77,48 @@ Update_t Help_Arg_1( int iCommandHelp )
//=========================================================================== //===========================================================================
void Help_Categories() void Help_Categories()
{ {
const int nBuf = CONSOLE_WIDTH * 2;
char sText[ nBuf ] = "";
int nLen = 0;
// TODO/FIXME: Colorize( sText, ... ) // TODO/FIXME: Colorize( sText, ... )
// Colorize("Usage:") // Colorize("Usage:")
nLen += StringCat( sText, CHC_USAGE , nBuf ); std::string sText = CHC_USAGE "Usage" CHC_DEFAULT ": " CHC_ARG_OPT "[ " CHC_ARG_MAND "< ";
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 );
for (int iCategory = _PARAM_HELPCATEGORIES_BEGIN ; iCategory < _PARAM_HELPCATEGORIES_END; iCategory++) 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 ); ConsolePrint( sText.c_str() );
sText[ 0 ] = 0; sText = " "; // indent
nLen = StringCat( sText, " ", nBuf ); // indent
} }
StringCat( sText, CHC_COMMAND, nBuf ); sText += CHC_COMMAND;
nLen += StringCat( sText, pName , nBuf ); sText += pName;
if (iCategory < (_PARAM_HELPCATEGORIES_END - 1)) 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 ); ConsolePrint( sText.c_str() );
sText[ 0 ] = 0; sText = " "; // indent
nLen = StringCat( sText, " ", nBuf ); // indent
} }
StringCat( sText, CHC_ARG_SEP, nBuf );
nLen += StringCat( sText, sSep, nBuf ); sText += CHC_ARG_SEP;
sText += sSep;
} }
} }
StringCat( sText, CHC_ARG_MAND, nBuf ); sText += CHC_ARG_MAND " >" CHC_ARG_OPT " ]";
StringCat( sText, " >", nBuf);
StringCat( sText, CHC_ARG_OPT, nBuf ); // ConsoleBufferPush( sText.c_str() );
StringCat( sText, " ]", nBuf); ConsolePrint( sText.c_str() ); // Transcode colored text to native console color text
// ConsoleBufferPush( sText );
ConsolePrint( sText ); // Transcode colored text to native console color text
ConsolePrintFormat( "%sNotes%s: %s<>%s = mandatory, %s[]%s = optional, %s|%s argument option" ConsolePrintFormat( CHC_USAGE "Notes" CHC_DEFAULT ": "
, CHC_USAGE CHC_ARG_MAND "<>" CHC_DEFAULT " = mandatory, "
, CHC_DEFAULT CHC_ARG_OPT "[]" CHC_DEFAULT " = optional, "
, CHC_ARG_MAND CHC_ARG_SEP "|" CHC_DEFAULT " argument option" );
, CHC_DEFAULT // ConsoleBufferPush( sText.c_str() );
, CHC_ARG_OPT
, CHC_DEFAULT
, CHC_ARG_SEP
, CHC_DEFAULT
);
// ConsoleBufferPush( sText );
} }
void Help_Examples() void Help_Examples()
@ -257,21 +172,18 @@ void Help_Operators()
// ConsoleBufferPush( " Operators: (Breakpoint)" ); // ConsoleBufferPush( " Operators: (Breakpoint)" );
ConsolePrintFormat( " Operators: (%sBreakpoint%s)" , CHC_USAGE, CHC_DEFAULT ); ConsolePrintFormat( " Operators: (%sBreakpoint%s)" , CHC_USAGE, CHC_DEFAULT );
char sText[CONSOLE_WIDTH]; std::string sText = " " CHC_USAGE;
_tcscpy( sText, " " ); for ( int iBreakOp = 0; iBreakOp < NUM_BREAKPOINT_OPERATORS; iBreakOp++ )
strcat( sText, CHC_USAGE );
int iBreakOp = 0;
for ( iBreakOp = 0; iBreakOp < NUM_BREAKPOINT_OPERATORS; iBreakOp++ )
{ {
if ((iBreakOp >= PARAM_BP_LESS_EQUAL) && if ((iBreakOp >= PARAM_BP_LESS_EQUAL) &&
(iBreakOp <= PARAM_BP_GREATER_EQUAL)) (iBreakOp <= PARAM_BP_GREATER_EQUAL))
{ {
strcat( sText, g_aBreakpointSymbols[ iBreakOp ] ); sText += g_aBreakpointSymbols[ iBreakOp ];
strcat( sText, " " ); sText += ' ';
} }
} }
strcat( sText, CHC_DEFAULT ); sText += CHC_DEFAULT;
ConsolePrint( sText ); ConsolePrint( sText.c_str() );
} }
void Help_KeyboardShortcuts() void Help_KeyboardShortcuts()
@ -1487,33 +1399,23 @@ Update_t CmdHelpSpecific (int nArgs)
//=========================================================================== //===========================================================================
Update_t CmdHelpList (int nArgs) Update_t CmdHelpList (int nArgs)
{ {
const int nBuf = CONSOLE_WIDTH * 2; const size_t nMaxWidth = g_nConsoleDisplayWidth - 1;
char sText[ nBuf ] = "";
int nMaxWidth = g_nConsoleDisplayWidth - 1;
int iCommand;
extern std::vector<Command_t> g_vSortedCommands; extern std::vector<Command_t> g_vSortedCommands;
if (! g_vSortedCommands.size()) 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 ] ); g_vSortedCommands.push_back( g_aCommands[ iCommand ] );
} }
std::sort( g_vSortedCommands.begin(), g_vSortedCommands.end(), commands_functor_compare() ); std::sort( g_vSortedCommands.begin(), g_vSortedCommands.end(), commands_functor_compare() );
} }
int nLen = 0; //Colorize( sText, "Commands: " );
// Colorize( sText, "Commands: " ); std::string sText = CHC_USAGE "Commands" CHC_DEFAULT ": ";
StringCat( sText, CHC_USAGE , nBuf );
nLen += StringCat( sText, "Commands", nBuf );
StringCat( sText, CHC_DEFAULT, nBuf ); for ( int iCommand = 0; iCommand < NUM_COMMANDS_WITH_ALIASES; iCommand++ ) // aliases are not printed
nLen += StringCat( sText, ": " , nBuf );
for ( iCommand = 0; iCommand < NUM_COMMANDS_WITH_ALIASES; iCommand++ ) // aliases are not printed
{ {
Command_t *pCommand = & g_vSortedCommands.at( iCommand ); Command_t *pCommand = & g_vSortedCommands.at( iCommand );
// Command_t *pCommand = & g_aCommands[ iCommand ]; // Command_t *pCommand = & g_aCommands[ iCommand ];
@ -1522,27 +1424,22 @@ Update_t CmdHelpList (int nArgs)
if (! pCommand->pFunction) if (! pCommand->pFunction)
continue; // not implemented function continue; // not implemented function
int nLenCmd = strlen( pName ); if ((sText.length() + strlen(pName)) < nMaxWidth)
if ((nLen + nLenCmd) < (nMaxWidth))
{ {
StringCat( sText, CHC_COMMAND, nBuf ); sText += CHC_COMMAND;
nLen += StringCat( sText, pName , nBuf );
} }
else else
{ {
ConsolePrint( sText ); ConsolePrint( sText.c_str() );
nLen = 1; sText = " " CHC_COMMAND;
strcpy( sText, " " );
StringCat( sText, CHC_COMMAND, nBuf );
nLen += StringCat( sText, pName, nBuf );
} }
sText += pName;
strcat( sText, " " ); sText += ' ';
nLen++;
} }
//ConsoleBufferPush( sText ); //ConsoleBufferPush( sText.c_str() );
ConsolePrint( sText ); ConsolePrint( sText.c_str() );
ConsoleUpdate(); ConsoleUpdate();
return UPDATE_CONSOLE_DISPLAY; return UPDATE_CONSOLE_DISPLAY;

View File

@ -26,8 +26,4 @@ inline void UnpackVersion( const unsigned int nVersion,
nFixMinor_ = (nVersion >> 0) & 0xFF; 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 #endif