1
0
Fork 0

Codechange: replace fprintf(<FILE*> with fmt::print(<FILE*>

pull/10856/head
Rubidium 2023-05-21 08:13:28 +02:00 committed by rubidium42
parent c518293135
commit 275ebf4509
11 changed files with 39 additions and 41 deletions

View File

@ -112,7 +112,7 @@ void IConsolePrint(TextColour colour_code, const std::string &string)
if (_network_dedicated) { if (_network_dedicated) {
NetworkAdminConsole("console", str); NetworkAdminConsole("console", str);
fprintf(stdout, "%s%s\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 free(str); // free duplicated string since it's not used anymore

View File

@ -127,14 +127,14 @@ void DebugPrint(const char *level, const std::string &message)
static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR); static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
if (f == nullptr) return; if (f == nullptr) return;
fprintf(f, "%s%s\n", GetLogPrefix(), message.c_str()); fmt::print(f, "{}{}\n", GetLogPrefix(), message);
fflush(f); fflush(f);
#ifdef RANDOM_DEBUG #ifdef RANDOM_DEBUG
} else if (strcmp(level, "random") == 0) { } else if (strcmp(level, "random") == 0) {
static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR); static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
if (f == nullptr) return; if (f == nullptr) return;
fprintf(f, "%s\n", message.c_str()); fmt::print(f, "{}\n", message);
fflush(f); fflush(f);
#endif #endif
} else { } else {

View File

@ -87,7 +87,7 @@ TileIndex TileAdd(TileIndex tile, TileIndexDiff add,
seprintf(buf, lastof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed", seprintf(buf, lastof(buf), "TILE_ADD(%s) when adding 0x%.4X and 0x%.4X failed",
exp, (uint32)tile, add); exp, (uint32)tile, add);
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
fprintf(stderr, "%s:%d %s\n", file, line, buf); fmt::print(stderr, "{}:{} {}\n", file, line, buf);
#else #else
_assert(buf, (char*)file, line); _assert(buf, (char*)file, line);
#endif #endif

View File

@ -615,13 +615,11 @@ int openttd_main(int argc, char *argv[])
_load_check_data.Clear(); _load_check_data.Clear();
SaveOrLoadResult res = SaveOrLoad(mgo.opt, SLO_CHECK, DFT_GAME_FILE, SAVE_DIR, false); SaveOrLoadResult res = SaveOrLoad(mgo.opt, SLO_CHECK, DFT_GAME_FILE, SAVE_DIR, false);
if (res != SL_OK || _load_check_data.HasErrors()) { if (res != SL_OK || _load_check_data.HasErrors()) {
fprintf(stderr, "Failed to open savegame\n"); fmt::print(stderr, "Failed to open savegame\n");
if (_load_check_data.HasErrors()) { if (_load_check_data.HasErrors()) {
InitializeLanguagePacks(); // A language pack is needed for GetString() InitializeLanguagePacks(); // A language pack is needed for GetString()
char buf[256];
SetDParamStr(0, _load_check_data.error_msg); SetDParamStr(0, _load_check_data.error_msg);
GetString(buf, _load_check_data.error, lastof(buf)); fmt::print(stderr, "{}\n", GetString(_load_check_data.error));
fprintf(stderr, "%s\n", buf);
} }
return ret; return ret;
} }

View File

@ -135,7 +135,7 @@ void ShowMacDialog(const char *title, const char *message, const char *buttonLab
*/ */
void ShowMacDialog(const char *title, const char *message, const char *buttonLabel) void ShowMacDialog(const char *title, const char *message, const char *buttonLabel)
{ {
fprintf(stderr, "%s: %s\n", title, message); fmt::print(stderr, "{}: {}\n", title, message);
} }
#endif #endif

View File

@ -212,7 +212,7 @@ std::string FS2OTTD(const std::string &name)
void ShowInfoI(const std::string &str) void ShowInfoI(const std::string &str)
{ {
fprintf(stderr, "%s\n", str.c_str()); fmt::print(stderr, "{}\n", str);
} }
#if !defined(__APPLE__) #if !defined(__APPLE__)
@ -220,9 +220,9 @@ void ShowOSErrorBox(const char *buf, bool system)
{ {
/* All unix systems, except OSX. Only use escape codes on a TTY. */ /* All unix systems, except OSX. Only use escape codes on a TTY. */
if (isatty(fileno(stderr))) { if (isatty(fileno(stderr))) {
fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf); fmt::print(stderr, "\033[1;31mError: {}\033[0;39m\n", buf);
} else { } else {
fprintf(stderr, "Error: %s\n", buf); fmt::print(stderr, "Error: {}\n", buf);
} }
} }
#endif #endif

View File

@ -321,7 +321,7 @@ static INT_PTR CALLBACK HelpDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARAM
void ShowInfoI(const std::string &str) void ShowInfoI(const std::string &str)
{ {
if (_has_console) { if (_has_console) {
fprintf(stderr, "%s\n", str.c_str()); fmt::print(stderr, "{}\n", str);
} else { } else {
bool old; bool old;
ReleaseCapture(); ReleaseCapture();

View File

@ -221,7 +221,7 @@ void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s)
/* Check if we have a custom print function */ /* Check if we have a custom print function */
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func; SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
if (func == nullptr) { if (func == nullptr) {
fprintf(stderr, "%s", s.c_str()); fmt::print(stderr, "{}", s);
} else { } else {
(*func)(true, s); (*func)(true, s);
} }
@ -238,7 +238,7 @@ void Squirrel::RunError(HSQUIRRELVM vm, const SQChar *error)
Squirrel *engine = (Squirrel *)sq_getforeignptr(vm); Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
SQPrintFunc *func = engine->print_func; SQPrintFunc *func = engine->print_func;
if (func == nullptr) { if (func == nullptr) {
fprintf(stderr, "%s", msg.c_str()); fmt::print(stderr, "{}", msg);
} else { } else {
(*func)(true, msg); (*func)(true, msg);
} }

View File

@ -29,7 +29,7 @@
*/ */
void NORETURN FatalErrorI(const std::string &msg) void NORETURN FatalErrorI(const std::string &msg)
{ {
fprintf(stderr, "settingsgen: FATAL: %s\n", msg.c_str()); fmt::print(stderr, "settingsgen: FATAL: {}\n", msg);
exit(1); exit(1);
} }
@ -483,7 +483,7 @@ int CDECL main(int argc, char *argv[])
break; break;
case -2: case -2:
fprintf(stderr, "Invalid arguments\n"); fmt::print(stderr, "Invalid arguments\n");
return 1; return 1;
} }
} }

View File

@ -34,41 +34,41 @@
#ifdef _MSC_VER #ifdef _MSC_VER
# define LINE_NUM_FMT(s) "%s (%d): warning: %s (" s ")\n" # define LINE_NUM_FMT(s) "{} ({}): warning: {} (" s ")\n"
#else #else
# define LINE_NUM_FMT(s) "%s:%d: " s ": %s\n" # define LINE_NUM_FMT(s) "{}:{}: " s ": {}\n"
#endif #endif
void StrgenWarningI(const std::string &msg) void StrgenWarningI(const std::string &msg)
{ {
if (_show_todo > 0) { if (_show_todo > 0) {
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, msg.c_str()); fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, msg);
} else { } else {
fprintf(stderr, LINE_NUM_FMT("info"), _file, _cur_line, msg.c_str()); fmt::print(stderr, LINE_NUM_FMT("info"), _file, _cur_line, msg);
} }
_warnings++; _warnings++;
} }
void StrgenErrorI(const std::string &msg) void StrgenErrorI(const std::string &msg)
{ {
fprintf(stderr, LINE_NUM_FMT("error"), _file, _cur_line, msg.c_str()); fmt::print(stderr, LINE_NUM_FMT("error"), _file, _cur_line, msg);
_errors++; _errors++;
} }
void NORETURN StrgenFatalI(const std::string &msg) void NORETURN StrgenFatalI(const std::string &msg)
{ {
fprintf(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg.c_str()); fmt::print(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg);
#ifdef _MSC_VER #ifdef _MSC_VER
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled"); fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
#endif #endif
throw std::exception(); throw std::exception();
} }
void NORETURN FatalErrorI(const std::string &msg) void NORETURN FatalErrorI(const std::string &msg)
{ {
fprintf(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg.c_str()); fmt::print(stderr, LINE_NUM_FMT("FATAL"), _file, _cur_line, msg);
#ifdef _MSC_VER #ifdef _MSC_VER
fprintf(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled"); fmt::print(stderr, LINE_NUM_FMT("warning"), _file, _cur_line, "language is not compiled");
#endif #endif
exit(2); exit(2);
} }
@ -266,9 +266,9 @@ struct HeaderFileWriter : HeaderWriter, FileWriter {
HeaderFileWriter(const char *filename) : FileWriter("tmp.xxx"), HeaderFileWriter(const char *filename) : FileWriter("tmp.xxx"),
real_filename(stredup(filename)), prev(0), total_strings(0) real_filename(stredup(filename)), prev(0), total_strings(0)
{ {
fprintf(this->fh, "/* This file is automatically generated. Do not modify */\n\n"); fmt::print(this->fh, "/* This file is automatically generated. Do not modify */\n\n");
fprintf(this->fh, "#ifndef TABLE_STRINGS_H\n"); fmt::print(this->fh, "#ifndef TABLE_STRINGS_H\n");
fprintf(this->fh, "#define TABLE_STRINGS_H\n"); fmt::print(this->fh, "#define TABLE_STRINGS_H\n");
} }
/** Free the filename. */ /** Free the filename. */
@ -279,8 +279,8 @@ struct HeaderFileWriter : HeaderWriter, FileWriter {
void WriteStringID(const char *name, int stringid) void WriteStringID(const char *name, int stringid)
{ {
if (prev + 1 != stringid) fprintf(this->fh, "\n"); if (prev + 1 != stringid) fmt::print(this->fh, "\n");
fprintf(this->fh, "static const StringID %s = 0x%X;\n", name, stringid); fmt::print(this->fh, "static const StringID {} = 0x{:X};\n", name, stringid);
prev = stringid; prev = stringid;
total_strings++; total_strings++;
} }
@ -293,17 +293,17 @@ struct HeaderFileWriter : HeaderWriter, FileWriter {
max_plural_forms = std::max(max_plural_forms, _plural_forms[i].plural_count); max_plural_forms = std::max(max_plural_forms, _plural_forms[i].plural_count);
} }
fprintf(this->fh, fmt::print(this->fh,
"\n" "\n"
"static const uint LANGUAGE_PACK_VERSION = 0x%X;\n" "static const uint LANGUAGE_PACK_VERSION = 0x{:X};\n"
"static const uint LANGUAGE_MAX_PLURAL = %u;\n" "static const uint LANGUAGE_MAX_PLURAL = {};\n"
"static const uint LANGUAGE_MAX_PLURAL_FORMS = %d;\n" "static const uint LANGUAGE_MAX_PLURAL_FORMS = {};\n"
"static const uint LANGUAGE_TOTAL_STRINGS = %u;\n" "static const uint LANGUAGE_TOTAL_STRINGS = {};\n"
"\n", "\n",
(uint)data.Version(), (uint)lengthof(_plural_forms), max_plural_forms, total_strings data.Version(), lengthof(_plural_forms), max_plural_forms, total_strings
); );
fprintf(this->fh, "#endif /* TABLE_STRINGS_H */\n"); fmt::print(this->fh, "#endif /* TABLE_STRINGS_H */\n");
this->FileWriter::Finalise(); this->FileWriter::Finalise();
@ -493,7 +493,7 @@ int CDECL main(int argc, char *argv[])
break; break;
case -2: case -2:
fprintf(stderr, "Invalid arguments\n"); fmt::print(stderr, "Invalid arguments\n");
return 0; return 0;
} }
} }
@ -556,7 +556,7 @@ int CDECL main(int argc, char *argv[])
/* if showing warnings, print a summary of the language */ /* if showing warnings, print a summary of the language */
if ((_show_todo & 2) != 0) { if ((_show_todo & 2) != 0) {
fprintf(stdout, "%d warnings and %d errors for %s\n", _warnings, _errors, pathbuf); fmt::print("{} warnings and {} errors for {}\n", _warnings, _errors, pathbuf);
} }
} }
} }

View File

@ -397,7 +397,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
if (VideoDriver::GetInstance() == nullptr) { if (VideoDriver::GetInstance() == nullptr) {
CocoaSetupApplication(); // Setup application before showing dialog CocoaSetupApplication(); // Setup application before showing dialog
} else if (!_cocoa_video_started && VideoDriver::GetInstance()->Start({}) != nullptr) { } else if (!_cocoa_video_started && VideoDriver::GetInstance()->Start({}) != nullptr) {
fprintf(stderr, "%s: %s\n", title, message); fmt::print(stderr, "{}: {}\n", title, message);
return; return;
} }