Codechange: replace stredup + StrMakeValidInPlace with std::string + StrMakeValid

This commit is contained in:
Rubidium 2023-06-08 17:15:32 +02:00 committed by rubidium42
parent e762855201
commit 3f35787458
3 changed files with 6 additions and 9 deletions

View File

@ -47,13 +47,13 @@ void IConsoleInit()
IConsoleStdLibRegister(); IConsoleStdLibRegister();
} }
static void IConsoleWriteToLogFile(const char *string) static void IConsoleWriteToLogFile(const std::string &string)
{ {
if (_iconsole_output_file != nullptr) { if (_iconsole_output_file != nullptr) {
/* if there is an console output file ... also print it there */ /* if there is an console output file ... also print it there */
const char *header = GetLogPrefix(); const char *header = GetLogPrefix();
if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) || if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 || fwrite(string.c_str(), string.size(), 1, _iconsole_output_file) != 1 ||
fwrite("\n", 1, 1, _iconsole_output_file) != 1) { fwrite("\n", 1, 1, _iconsole_output_file) != 1) {
fclose(_iconsole_output_file); fclose(_iconsole_output_file);
_iconsole_output_file = nullptr; _iconsole_output_file = nullptr;
@ -104,23 +104,20 @@ void IConsolePrint(TextColour colour_code, const std::string &string)
return; return;
} }
/* Create a copy of the string, strip if of colours and invalid /* Create a copy of the string, strip it of colours and invalid
* characters and (when applicable) assign it to the console buffer */ * characters and (when applicable) assign it to the console buffer */
char *str = stredup(string.c_str()); std::string str = StrMakeValid(string);
StrMakeValidInPlace(str);
if (_network_dedicated) { if (_network_dedicated) {
NetworkAdminConsole("console", str); NetworkAdminConsole("console", str);
fmt::print("{}{}\n", GetLogPrefix(), str); fmt::print("{}{}\n", GetLogPrefix(), str);
fflush(stdout); fflush(stdout);
IConsoleWriteToLogFile(str); IConsoleWriteToLogFile(str);
free(str); // free duplicated string since it's not used anymore
return; return;
} }
IConsoleWriteToLogFile(str); IConsoleWriteToLogFile(str);
IConsoleGUIPrint(colour_code, str); IConsoleGUIPrint(colour_code, str);
free(str);
} }
/** /**

View File

@ -462,7 +462,7 @@ static void IConsoleHistoryNavigate(int direction)
* @param colour_code the colour of the command. Red in case of errors, etc. * @param colour_code the colour of the command. Red in case of errors, etc.
* @param str the message entered or output on the console (notice, error, etc.) * @param str the message entered or output on the console (notice, error, etc.)
*/ */
void IConsoleGUIPrint(TextColour colour_code, char *str) void IConsoleGUIPrint(TextColour colour_code, const std::string &str)
{ {
_iconsole_buffer.push_front(IConsoleLine(str, colour_code)); _iconsole_buffer.push_front(IConsoleLine(str, colour_code));
SetWindowDirty(WC_CONSOLE, 0); SetWindowDirty(WC_CONSOLE, 0);

View File

@ -86,6 +86,6 @@ bool GetArgumentInteger(uint32 *value, const char *arg);
void IConsoleGUIInit(); void IConsoleGUIInit();
void IConsoleGUIFree(); void IConsoleGUIFree();
void IConsoleGUIPrint(TextColour colour_code, char *string); void IConsoleGUIPrint(TextColour colour_code, const std::string &string);
#endif /* CONSOLE_INTERNAL_H */ #endif /* CONSOLE_INTERNAL_H */