Codechange: use fmt::format_to to format the help message

pull/564/head
Rubidium 1 year ago committed by rubidium42
parent 8d2a0a7da4
commit 07860e67e2

@ -112,9 +112,9 @@ public:
static void Save(CompanyID company); static void Save(CompanyID company);
/** Wrapper function for AIScanner::GetAIConsoleList */ /** Wrapper function for AIScanner::GetAIConsoleList */
static std::string GetConsoleList(bool newest_only = false); static void GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only);
/** Wrapper function for AIScanner::GetAIConsoleLibraryList */ /** Wrapper function for AIScanner::GetAIConsoleLibraryList */
static std::string GetConsoleLibraryList(); static void GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator);
/** Wrapper function for AIScanner::GetAIInfoList */ /** Wrapper function for AIScanner::GetAIInfoList */
static const ScriptInfoList *GetInfoList(); static const ScriptInfoList *GetInfoList();
/** Wrapper function for AIScanner::GetUniqueAIInfoList */ /** Wrapper function for AIScanner::GetUniqueAIInfoList */

@ -289,14 +289,14 @@
} }
} }
/* static */ std::string AI::GetConsoleList(bool newest_only) /* static */ void AI::GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only)
{ {
return AI::scanner_info->GetConsoleList(newest_only); AI::scanner_info->GetConsoleList(output_iterator, newest_only);
} }
/* static */ std::string AI::GetConsoleLibraryList() /* static */ void AI::GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator)
{ {
return AI::scanner_library->GetConsoleList(true); AI::scanner_library->GetConsoleList(output_iterator, true);
} }
/* static */ const ScriptInfoList *AI::GetInfoList() /* static */ const ScriptInfoList *AI::GetInfoList()

@ -192,7 +192,7 @@ public:
static Tbase_set *GetAvailableSets(); static Tbase_set *GetAvailableSets();
static bool SetSet(const std::string &name); static bool SetSet(const std::string &name);
static char *GetSetsList(char *p, const char *last); static void GetSetsList(std::back_insert_iterator<std::string> &output_iterator);
static int GetNumSets(); static int GetNumSets();
static int GetIndexOfUsedSet(); static int GetIndexOfUsedSet();
static const Tbase_set *GetSet(int index); static const Tbase_set *GetSet(int index);

@ -248,31 +248,27 @@ template <class Tbase_set>
/** /**
* Returns a list with the sets. * Returns a list with the sets.
* @param p where to print to * @param output_iterator The iterator to write the string to.
* @param last the last character to print to
* @return the last printed character
*/ */
template <class Tbase_set> template <class Tbase_set>
/* static */ char *BaseMedia<Tbase_set>::GetSetsList(char *p, const char *last) /* static */ void BaseMedia<Tbase_set>::GetSetsList(std::back_insert_iterator<std::string> &output_iterator)
{ {
p += seprintf(p, last, "List of " SET_TYPE " sets:\n"); fmt::format_to(output_iterator, "List of " SET_TYPE " sets:\n");
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) { for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
p += seprintf(p, last, "%18s: %s", s->name.c_str(), s->GetDescription({})); fmt::format_to(output_iterator, "{:>18}: {}", s->name, s->GetDescription({}));
int invalid = s->GetNumInvalid(); int invalid = s->GetNumInvalid();
if (invalid != 0) { if (invalid != 0) {
int missing = s->GetNumMissing(); int missing = s->GetNumMissing();
if (missing == 0) { if (missing == 0) {
p += seprintf(p, last, " (%i corrupt file%s)\n", invalid, invalid == 1 ? "" : "s"); fmt::format_to(output_iterator, " ({} corrupt file{})\n", invalid, invalid == 1 ? "" : "s");
} else { } else {
p += seprintf(p, last, " (unusable: %i missing file%s)\n", missing, missing == 1 ? "" : "s"); fmt::format_to(output_iterator, " (unusable: {} missing file{})\n", missing, missing == 1 ? "" : "s");
} }
} else { } else {
p += seprintf(p, last, "\n"); fmt::format_to(output_iterator, "\n");
} }
} }
p += seprintf(p, last, "\n"); fmt::format_to(output_iterator, "\n");
return p;
} }
#include "network/core/tcp_content_type.h" #include "network/core/tcp_content_type.h"
@ -378,7 +374,7 @@ template <class Tbase_set>
template bool repl_type::AddFile(const std::string &filename, size_t pathlength, const std::string &tar_filename); \ template bool repl_type::AddFile(const std::string &filename, size_t pathlength, const std::string &tar_filename); \
template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \ template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
template bool repl_type::SetSet(const std::string &name); \ template bool repl_type::SetSet(const std::string &name); \
template char *repl_type::GetSetsList(char *p, const char *last); \ template void repl_type::GetSetsList(std::back_insert_iterator<std::string> &output_iterator); \
template int repl_type::GetNumSets(); \ template int repl_type::GetNumSets(); \
template int repl_type::GetIndexOfUsedSet(); \ template int repl_type::GetIndexOfUsedSet(); \
template const set_type *repl_type::GetSet(int index); \ template const set_type *repl_type::GetSet(int index); \

@ -146,16 +146,14 @@ public:
* @param last The last element of the buffer. * @param last The last element of the buffer.
* @return p The location till where we filled the buffer. * @return p The location till where we filled the buffer.
*/ */
static char *GetBlittersInfo(char *p, const char *last) static void GetBlittersInfo(std::back_insert_iterator<std::string> &output_iterator)
{ {
p += seprintf(p, last, "List of blitters:\n"); fmt::format_to(output_iterator, "List of blitters:\n");
for (auto &it : GetBlitters()) { for (auto &it : GetBlitters()) {
BlitterFactory *b = it.second; BlitterFactory *b = it.second;
p += seprintf(p, last, "%18s: %s\n", b->name.c_str(), b->GetDescription().c_str()); fmt::format_to(output_iterator, "{:>18}: {}\n", b->name, b->GetDescription());
} }
p += seprintf(p, last, "\n"); fmt::format_to(output_iterator, "\n");
return p;
} }
/** /**

@ -1180,6 +1180,17 @@ static void PrintLineByLine(const std::string &full_string)
} }
} }
template <typename F, typename ... Args>
bool PrintList(F list_function, Args... args)
{
std::string output_str;
auto inserter = std::back_inserter(output_str);
list_function(inserter, args...);
PrintLineByLine(output_str);
return true;
}
DEF_CONSOLE_CMD(ConListAILibs) DEF_CONSOLE_CMD(ConListAILibs)
{ {
if (argc == 0) { if (argc == 0) {
@ -1187,10 +1198,7 @@ DEF_CONSOLE_CMD(ConListAILibs)
return true; return true;
} }
const std::string output_str = AI::GetConsoleLibraryList(); return PrintList(AI::GetConsoleLibraryList);
PrintLineByLine(output_str);
return true;
} }
DEF_CONSOLE_CMD(ConListAI) DEF_CONSOLE_CMD(ConListAI)
@ -1200,10 +1208,7 @@ DEF_CONSOLE_CMD(ConListAI)
return true; return true;
} }
const std::string output_str = AI::GetConsoleList(); return PrintList(AI::GetConsoleList, false);
PrintLineByLine(output_str);
return true;
} }
DEF_CONSOLE_CMD(ConListGameLibs) DEF_CONSOLE_CMD(ConListGameLibs)
@ -1213,10 +1218,7 @@ DEF_CONSOLE_CMD(ConListGameLibs)
return true; return true;
} }
const std::string output_str = Game::GetConsoleLibraryList(); return PrintList(Game::GetConsoleLibraryList);
PrintLineByLine(output_str);
return true;
} }
DEF_CONSOLE_CMD(ConListGame) DEF_CONSOLE_CMD(ConListGame)
@ -1226,10 +1228,7 @@ DEF_CONSOLE_CMD(ConListGame)
return true; return true;
} }
const std::string output_str = Game::GetConsoleList(); return PrintList(Game::GetConsoleList, false);
PrintLineByLine(output_str);
return true;
} }
DEF_CONSOLE_CMD(ConStartAI) DEF_CONSOLE_CMD(ConStartAI)

@ -85,27 +85,23 @@ struct DebugLevel {
/** /**
* Dump the available debug facility names in the help text. * Dump the available debug facility names in the help text.
* @param buf Start address for storing the output. * @param output_iterator The iterator to write the string to.
* @param last Last valid address for storing the output.
* @return Next free position in the output.
*/ */
char *DumpDebugFacilityNames(char *buf, char *last) void DumpDebugFacilityNames(std::back_insert_iterator<std::string> &output_iterator)
{ {
size_t length = 0; bool written = false;
for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) { for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
if (length == 0) { if (!written) {
buf = strecpy(buf, "List of debug facility names:\n", last); fmt::format_to(output_iterator, "List of debug facility names:\n");
} else { } else {
buf = strecpy(buf, ", ", last); fmt::format_to(output_iterator, ", ");
length += 2;
} }
buf = strecpy(buf, i->name, last); fmt::format_to(output_iterator, i->name);
length += strlen(i->name); written = true;
} }
if (length > 0) { if (written) {
buf = strecpy(buf, "\n\n", last); fmt::format_to(output_iterator, "\n\n");
} }
return buf;
} }
/** /**

@ -56,7 +56,7 @@ extern int _debug_console_level;
extern int _debug_random_level; extern int _debug_random_level;
#endif #endif
char *DumpDebugFacilityNames(char *buf, char *last); void DumpDebugFacilityNames(std::back_insert_iterator<std::string> &output_iterator);
void SetDebugString(const char *s, void (*error_func)(const std::string &)); void SetDebugString(const char *s, void (*error_func)(const std::string &));
const char *GetDebugString(); const char *GetDebugString();

@ -179,28 +179,24 @@ bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type t
/** /**
* Build a human readable list of available drivers, grouped by type. * Build a human readable list of available drivers, grouped by type.
* @param p The buffer to write to. * @param output_iterator The iterator to write the string to.
* @param last The last element in the buffer.
* @return The end of the written buffer.
*/ */
char *DriverFactoryBase::GetDriversInfo(char *p, const char *last) void DriverFactoryBase::GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator)
{ {
for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) { for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) {
p += seprintf(p, last, "List of %s drivers:\n", GetDriverTypeName(type)); fmt::format_to(output_iterator, "List of {} drivers:\n", GetDriverTypeName(type));
for (int priority = 10; priority >= 0; priority--) { for (int priority = 10; priority >= 0; priority--) {
for (auto &it : GetDrivers()) { for (auto &it : GetDrivers()) {
DriverFactoryBase *d = it.second; DriverFactoryBase *d = it.second;
if (d->type != type) continue; if (d->type != type) continue;
if (d->priority != priority) continue; if (d->priority != priority) continue;
p += seprintf(p, last, "%18s: %s\n", d->name, d->GetDescription()); fmt::format_to(output_iterator, "{:>18}: {}\n", d->name, d->GetDescription());
} }
} }
p += seprintf(p, last, "\n"); fmt::format_to(output_iterator, "\n");
} }
return p;
} }
/** /**

@ -127,7 +127,7 @@ public:
} }
static void SelectDriver(const std::string &name, Driver::Type type); static void SelectDriver(const std::string &name, Driver::Type type);
static char *GetDriversInfo(char *p, const char *last); static void GetDriversInfo(std::back_insert_iterator<std::string> &output_iterator);
/** /**
* Get a nice description of the driver-class. * Get a nice description of the driver-class.

@ -83,9 +83,9 @@ public:
static void Save(); static void Save();
/** Wrapper function for GameScanner::GetConsoleList */ /** Wrapper function for GameScanner::GetConsoleList */
static std::string GetConsoleList(bool newest_only = false); static void GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only);
/** Wrapper function for GameScanner::GetConsoleLibraryList */ /** Wrapper function for GameScanner::GetConsoleLibraryList */
static std::string GetConsoleLibraryList(); static void GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator);
/** Wrapper function for GameScanner::GetInfoList */ /** Wrapper function for GameScanner::GetInfoList */
static const ScriptInfoList *GetInfoList(); static const ScriptInfoList *GetInfoList();
/** Wrapper function for GameScanner::GetUniqueInfoList */ /** Wrapper function for GameScanner::GetUniqueInfoList */

@ -219,14 +219,14 @@
} }
} }
/* static */ std::string Game::GetConsoleList(bool newest_only) /* static */ void Game::GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only)
{ {
return Game::scanner_info->GetConsoleList(newest_only); Game::scanner_info->GetConsoleList(output_iterator, newest_only);
} }
/* static */ std::string Game::GetConsoleLibraryList() /* static */ void Game::GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator)
{ {
return Game::scanner_library->GetConsoleList(true); Game::scanner_library->GetConsoleList(output_iterator, true);
} }
/* static */ const ScriptInfoList *Game::GetInfoList() /* static */ const ScriptInfoList *Game::GetInfoList()

@ -155,11 +155,12 @@ void FatalErrorI(const std::string &str)
*/ */
static void ShowHelp() static void ShowHelp()
{ {
char buf[8192]; std::string str;
char *p = buf; str.reserve(8192);
p += seprintf(p, lastof(buf), "OpenTTD %s\n", _openttd_revision); std::back_insert_iterator<std::string> output_iterator = std::back_inserter(str);
p = strecpy(p, fmt::format_to(output_iterator, "OpenTTD {}\n", _openttd_revision);
str +=
"\n" "\n"
"\n" "\n"
"Command line options:\n" "Command line options:\n"
@ -191,46 +192,42 @@ static void ShowHelp()
" -q savegame = Write some information about the savegame and exit\n" " -q savegame = Write some information about the savegame and exit\n"
" -Q = Don't scan for/load NewGRF files on startup\n" " -Q = Don't scan for/load NewGRF files on startup\n"
" -QQ = Disable NewGRF scanning/loading entirely\n" " -QQ = Disable NewGRF scanning/loading entirely\n"
"\n", "\n";
lastof(buf)
);
/* List the graphics packs */ /* List the graphics packs */
p = BaseGraphics::GetSetsList(p, lastof(buf)); BaseGraphics::GetSetsList(output_iterator);
/* List the sounds packs */ /* List the sounds packs */
p = BaseSounds::GetSetsList(p, lastof(buf)); BaseSounds::GetSetsList(output_iterator);
/* List the music packs */ /* List the music packs */
p = BaseMusic::GetSetsList(p, lastof(buf)); BaseMusic::GetSetsList(output_iterator);
/* List the drivers */ /* List the drivers */
p = DriverFactoryBase::GetDriversInfo(p, lastof(buf)); DriverFactoryBase::GetDriversInfo(output_iterator);
/* List the blitters */ /* List the blitters */
p = BlitterFactory::GetBlittersInfo(p, lastof(buf)); BlitterFactory::GetBlittersInfo(output_iterator);
/* List the debug facilities. */ /* List the debug facilities. */
p = DumpDebugFacilityNames(p, lastof(buf)); DumpDebugFacilityNames(output_iterator);
/* We need to initialize the AI, so it finds the AIs */ /* We need to initialize the AI, so it finds the AIs */
AI::Initialize(); AI::Initialize();
const std::string ai_list = AI::GetConsoleList(true); AI::GetConsoleList(output_iterator, true);
p = strecpy(p, ai_list.c_str(), lastof(buf));
AI::Uninitialize(true); AI::Uninitialize(true);
/* We need to initialize the GameScript, so it finds the GSs */ /* We need to initialize the GameScript, so it finds the GSs */
Game::Initialize(); Game::Initialize();
const std::string game_list = Game::GetConsoleList(true); Game::GetConsoleList(output_iterator, true);
p = strecpy(p, game_list.c_str(), lastof(buf));
Game::Uninitialize(true); Game::Uninitialize(true);
/* ShowInfo put output to stderr, but version information should go /* ShowInfo put output to stderr, but version information should go
* to stdout; this is the only exception */ * to stdout; this is the only exception */
#if !defined(_WIN32) #if !defined(_WIN32)
printf("%s\n", buf); printf("%s\n", str.c_str());
#else #else
ShowInfoI(buf); ShowInfoI(str);
#endif #endif
} }

@ -138,18 +138,15 @@ void ScriptScanner::RegisterScript(ScriptInfo *info)
} }
} }
std::string ScriptScanner::GetConsoleList(bool newest_only) const void ScriptScanner::GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only) const
{ {
std::string p; fmt::format_to(output_iterator, "List of {}:\n", this->GetScannerName());
p += fmt::format("List of {}:\n", this->GetScannerName());
const ScriptInfoList &list = newest_only ? this->info_single_list : this->info_list; const ScriptInfoList &list = newest_only ? this->info_single_list : this->info_list;
for (const auto &item : list) { for (const auto &item : list) {
ScriptInfo *i = item.second; ScriptInfo *i = item.second;
p += fmt::format("{:>10} (v{:d}): {}\n", i->GetName(), i->GetVersion(), i->GetDescription()); fmt::format_to(output_iterator, "{:>10} (v{:d}): {}\n", i->GetName(), i->GetVersion(), i->GetDescription());
} }
p += "\n"; fmt::format_to(output_iterator, "\n");
return p;
} }
/** Helper for creating a MD5sum of all files within of a script. */ /** Helper for creating a MD5sum of all files within of a script. */

@ -55,8 +55,10 @@ public:
/** /**
* Get the list of registered scripts to print on the console. * Get the list of registered scripts to print on the console.
* @param output_iterator The iterator to write the output to.
* @param newest_only Whether to only show the newest scripts.
*/ */
std::string GetConsoleList(bool newest_only) const; void GetConsoleList(std::back_insert_iterator<std::string> &output_iterator, bool newest_only) const;
/** /**
* Check whether we have a script with the exact characteristics as ci. * Check whether we have a script with the exact characteristics as ci.

Loading…
Cancel
Save