diff --git a/src/console.cpp b/src/console.cpp index d3d908935a..fa977469ed 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -47,13 +47,13 @@ void IConsoleInit() IConsoleStdLibRegister(); } -static void IConsoleWriteToLogFile(const char *string) +static void IConsoleWriteToLogFile(const std::string &string) { if (_iconsole_output_file != nullptr) { /* if there is an console output file ... also print it there */ const char *header = GetLogPrefix(); 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) { fclose(_iconsole_output_file); _iconsole_output_file = nullptr; @@ -104,23 +104,20 @@ void IConsolePrint(TextColour colour_code, const std::string &string) 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 */ - char *str = stredup(string.c_str()); - StrMakeValidInPlace(str); + std::string str = StrMakeValid(string); if (_network_dedicated) { NetworkAdminConsole("console", str); fmt::print("{}{}\n", GetLogPrefix(), str); fflush(stdout); IConsoleWriteToLogFile(str); - free(str); // free duplicated string since it's not used anymore return; } IConsoleWriteToLogFile(str); IConsoleGUIPrint(colour_code, str); - free(str); } /** diff --git a/src/console_gui.cpp b/src/console_gui.cpp index 1d4e9b29c7..497c430489 100644 --- a/src/console_gui.cpp +++ b/src/console_gui.cpp @@ -462,7 +462,7 @@ static void IConsoleHistoryNavigate(int direction) * @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.) */ -void IConsoleGUIPrint(TextColour colour_code, char *str) +void IConsoleGUIPrint(TextColour colour_code, const std::string &str) { _iconsole_buffer.push_front(IConsoleLine(str, colour_code)); SetWindowDirty(WC_CONSOLE, 0); diff --git a/src/console_internal.h b/src/console_internal.h index 19e798c19e..8137a49b6d 100644 --- a/src/console_internal.h +++ b/src/console_internal.h @@ -86,6 +86,6 @@ bool GetArgumentInteger(uint32 *value, const char *arg); void IConsoleGUIInit(); void IConsoleGUIFree(); -void IConsoleGUIPrint(TextColour colour_code, char *string); +void IConsoleGUIPrint(TextColour colour_code, const std::string &string); #endif /* CONSOLE_INTERNAL_H */