Changeset - r27398:53658248e31b
[Not reviewed]
master
0 15 0
Rubidium - 16 months ago 2023-05-19 21:22:30
rubidium@openttd.org
Codechange: use fmt::format_to to format the help message
15 files changed with 81 insertions and 100 deletions:
0 comments (0 inline, 0 general)
src/ai/ai.hpp
Show inline comments
 
@@ -109,15 +109,15 @@ public:
 
	/**
 
	 * Save data from an AI to a savegame.
 
	 */
 
	static void Save(CompanyID company);
 

	
 
	/** 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 */
 
	static std::string GetConsoleLibraryList();
 
	static void GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator);
 
	/** Wrapper function for AIScanner::GetAIInfoList */
 
	static const ScriptInfoList *GetInfoList();
 
	/** Wrapper function for AIScanner::GetUniqueAIInfoList */
 
	static const ScriptInfoList *GetUniqueInfoList();
 
	/** Wrapper function for AIScanner::FindInfo */
 
	static class AIInfo *FindInfo(const std::string &name, int version, bool force_exact_match);
src/ai/ai_core.cpp
Show inline comments
 
@@ -286,20 +286,20 @@
 
		cur_company.Restore();
 
	} else {
 
		AIInstance::SaveEmpty();
 
	}
 
}
 

	
 
/* 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()
 
{
 
	return AI::scanner_info->GetInfoList();
 
}
src/base_media_base.h
Show inline comments
 
@@ -189,13 +189,13 @@ public:
 
		return num + fs.Scan(GetExtension(), BASESET_DIR, Tbase_set::SEARCH_IN_TARS);
 
	}
 

	
 
	static Tbase_set *GetAvailableSets();
 

	
 
	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 GetIndexOfUsedSet();
 
	static const Tbase_set *GetSet(int index);
 
	static const Tbase_set *GetUsedSet();
 

	
 
	/**
src/base_media_func.h
Show inline comments
 
@@ -245,37 +245,33 @@ template <class Tbase_set>
 
	}
 
	return false;
 
}
 

	
 
/**
 
 * Returns a list with the sets.
 
 * @param p    where to print to
 
 * @param last the last character to print to
 
 * @return the last printed character
 
 * @param output_iterator The iterator to write the string to.
 
 */
 
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) {
 
		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();
 
		if (invalid != 0) {
 
			int missing = s->GetNumMissing();
 
			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 {
 
				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 {
 
			p += seprintf(p, last, "\n");
 
			fmt::format_to(output_iterator, "\n");
 
		}
 
	}
 
	p += seprintf(p, last, "\n");
 

	
 
	return p;
 
	fmt::format_to(output_iterator, "\n");
 
}
 

	
 
#include "network/core/tcp_content_type.h"
 

	
 
template <class Tbase_set> const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
 
{
 
@@ -375,13 +371,13 @@ template <class Tbase_set>
 
#define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
 
	template std::string repl_type::ini_set; \
 
	template const char *repl_type::GetExtension(); \
 
	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::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::GetIndexOfUsedSet(); \
 
	template const set_type *repl_type::GetSet(int index); \
 
	template const set_type *repl_type::GetUsedSet(); \
 
	template bool repl_type::DetermineBestSet(); \
 
	template set_type *repl_type::GetAvailableSets(); \
src/blitter/factory.hpp
Show inline comments
 
@@ -143,22 +143,20 @@ public:
 
	/**
 
	 * Fill a buffer with information about the blitters.
 
	 * @param p The buffer to fill.
 
	 * @param last The last element of 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()) {
 
			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");
 

	
 
		return p;
 
		fmt::format_to(output_iterator, "\n");
 
	}
 

	
 
	/**
 
	 * Get the long, human readable, name for the Blitter-class.
 
	 */
 
	const std::string &GetName() const
src/console_cmds.cpp
Show inline comments
 
@@ -1177,62 +1177,61 @@ static void PrintLineByLine(const std::s
 
	std::string line;
 
	while (std::getline(in, line)) {
 
		IConsolePrint(CC_DEFAULT, line.c_str());
 
	}
 
}
 

	
 
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)
 
{
 
	if (argc == 0) {
 
		IConsolePrint(CC_HELP, "List installed AI libraries. Usage: 'list_ai_libs'.");
 
		return true;
 
	}
 

	
 
	const std::string output_str = AI::GetConsoleLibraryList();
 
	PrintLineByLine(output_str);
 

	
 
	return true;
 
	return PrintList(AI::GetConsoleLibraryList);
 
}
 

	
 
DEF_CONSOLE_CMD(ConListAI)
 
{
 
	if (argc == 0) {
 
		IConsolePrint(CC_HELP, "List installed AIs. Usage: 'list_ai'.");
 
		return true;
 
	}
 

	
 
	const std::string output_str = AI::GetConsoleList();
 
	PrintLineByLine(output_str);
 

	
 
	return true;
 
	return PrintList(AI::GetConsoleList, false);
 
}
 

	
 
DEF_CONSOLE_CMD(ConListGameLibs)
 
{
 
	if (argc == 0) {
 
		IConsolePrint(CC_HELP, "List installed Game Script libraries. Usage: 'list_game_libs'.");
 
		return true;
 
	}
 

	
 
	const std::string output_str = Game::GetConsoleLibraryList();
 
	PrintLineByLine(output_str);
 

	
 
	return true;
 
	return PrintList(Game::GetConsoleLibraryList);
 
}
 

	
 
DEF_CONSOLE_CMD(ConListGame)
 
{
 
	if (argc == 0) {
 
		IConsolePrint(CC_HELP, "List installed Game Scripts. Usage: 'list_game'.");
 
		return true;
 
	}
 

	
 
	const std::string output_str = Game::GetConsoleList();
 
	PrintLineByLine(output_str);
 

	
 
	return true;
 
	return PrintList(Game::GetConsoleList, false);
 
}
 

	
 
DEF_CONSOLE_CMD(ConStartAI)
 
{
 
	if (argc == 0 || argc > 3) {
 
		IConsolePrint(CC_HELP, "Start a new AI. Usage: 'start_ai [<AI>] [<settings>]'.");
src/debug.cpp
Show inline comments
 
@@ -82,33 +82,29 @@ struct DebugLevel {
 
#endif
 
	};
 
#undef DEBUG_LEVEL
 

	
 
/**
 
 * Dump the available debug facility names in the help text.
 
 * @param buf Start address for storing the output.
 
 * @param last Last valid address for storing the output.
 
 * @return Next free position in the output.
 
 * @param output_iterator The iterator to write the string to.
 
 */
 
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) {
 
		if (length == 0) {
 
			buf = strecpy(buf, "List of debug facility names:\n", last);
 
		if (!written) {
 
			fmt::format_to(output_iterator, "List of debug facility names:\n");
 
		} else {
 
			buf = strecpy(buf, ", ", last);
 
			length += 2;
 
			fmt::format_to(output_iterator, ", ");
 
		}
 
		buf = strecpy(buf, i->name, last);
 
		length += strlen(i->name);
 
		fmt::format_to(output_iterator, i->name);
 
		written = true;
 
	}
 
	if (length > 0) {
 
		buf = strecpy(buf, "\n\n", last);
 
	if (written) {
 
		fmt::format_to(output_iterator, "\n\n");
 
	}
 
	return buf;
 
}
 

	
 
/**
 
 * Internal function for outputting the debug line.
 
 * @param level Debug category.
 
 * @param message The message to output.
src/debug.h
Show inline comments
 
@@ -53,13 +53,13 @@ extern int _debug_gamelog_level;
 
extern int _debug_desync_level;
 
extern int _debug_console_level;
 
#ifdef RANDOM_DEBUG
 
extern int _debug_random_level;
 
#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 &));
 
const char *GetDebugString();
 

	
 
/* Shorter form for passing filename and linenumber */
 
#define FILE_LINE __FILE__, __LINE__
 

	
src/driver.cpp
Show inline comments
 
@@ -176,34 +176,30 @@ bool DriverFactoryBase::SelectDriverImpl
 
		UserError("No such {} driver: {}\n", GetDriverTypeName(type), dname);
 
	}
 
}
 

	
 
/**
 
 * Build a human readable list of available drivers, grouped by type.
 
 * @param p The buffer to write to.
 
 * @param last The last element in the buffer.
 
 * @return The end of the written buffer.
 
 * @param output_iterator The iterator to write the string to.
 
 */
 
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++) {
 
		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 (auto &it : GetDrivers()) {
 
				DriverFactoryBase *d = it.second;
 
				if (d->type != type) 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;
 
}
 

	
 
/**
 
 * Construct a new DriverFactory.
 
 * @param type        The type of driver.
 
 * @param priority    The priority within the driver class.
src/driver.h
Show inline comments
 
@@ -124,13 +124,13 @@ public:
 
			Driver *driver = *GetActiveDriver(dt);
 
			if (driver != nullptr) driver->Stop();
 
		}
 
	}
 

	
 
	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.
 
	 * @return The description.
 
	 */
 
	const char *GetDescription() const
src/game/game.hpp
Show inline comments
 
@@ -80,15 +80,15 @@ public:
 
	/**
 
	 * Save data from a GameScript to a savegame.
 
	 */
 
	static void Save();
 

	
 
	/** 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 */
 
	static std::string GetConsoleLibraryList();
 
	static void GetConsoleLibraryList(std::back_insert_iterator<std::string> &output_iterator);
 
	/** Wrapper function for GameScanner::GetInfoList */
 
	static const ScriptInfoList *GetInfoList();
 
	/** Wrapper function for GameScanner::GetUniqueInfoList */
 
	static const ScriptInfoList *GetUniqueInfoList();
 
	/** Wrapper function for GameScannerInfo::FindInfo */
 
	static class GameInfo *FindInfo(const std::string &name, int version, bool force_exact_match);
src/game/game_core.cpp
Show inline comments
 
@@ -216,20 +216,20 @@
 
		cur_company.Restore();
 
	} else {
 
		GameInstance::SaveEmpty();
 
	}
 
}
 

	
 
/* 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()
 
{
 
	return Game::scanner_info->GetInfoList();
 
}
src/openttd.cpp
Show inline comments
 
@@ -152,17 +152,18 @@ void FatalErrorI(const std::string &str)
 

	
 
/**
 
 * Show the help message when someone passed a wrong parameter.
 
 */
 
static void ShowHelp()
 
{
 
	char buf[8192];
 
	char *p = buf;
 
	std::string str;
 
	str.reserve(8192);
 

	
 
	p += seprintf(p, lastof(buf), "OpenTTD %s\n", _openttd_revision);
 
	p = strecpy(p,
 
	std::back_insert_iterator<std::string> output_iterator = std::back_inserter(str);
 
	fmt::format_to(output_iterator, "OpenTTD {}\n", _openttd_revision);
 
	str +=
 
		"\n"
 
		"\n"
 
		"Command line options:\n"
 
		"  -v drv              = Set video driver (see below)\n"
 
		"  -s drv              = Set sound driver (see below)\n"
 
		"  -m drv              = Set music driver (see below)\n"
 
@@ -188,52 +189,48 @@ static void ShowHelp()
 
		"  -c config_file      = Use 'config_file' instead of 'openttd.cfg'\n"
 
		"  -x                  = Never save configuration changes to disk\n"
 
		"  -X                  = Don't use global folders to search for files\n"
 
		"  -q savegame         = Write some information about the savegame and exit\n"
 
		"  -Q                  = Don't scan for/load NewGRF files on startup\n"
 
		"  -QQ                 = Disable NewGRF scanning/loading entirely\n"
 
		"\n",
 
		lastof(buf)
 
	);
 
		"\n";
 

	
 
	/* List the graphics packs */
 
	p = BaseGraphics::GetSetsList(p, lastof(buf));
 
	BaseGraphics::GetSetsList(output_iterator);
 

	
 
	/* List the sounds packs */
 
	p = BaseSounds::GetSetsList(p, lastof(buf));
 
	BaseSounds::GetSetsList(output_iterator);
 

	
 
	/* List the music packs */
 
	p = BaseMusic::GetSetsList(p, lastof(buf));
 
	BaseMusic::GetSetsList(output_iterator);
 

	
 
	/* List the drivers */
 
	p = DriverFactoryBase::GetDriversInfo(p, lastof(buf));
 
	DriverFactoryBase::GetDriversInfo(output_iterator);
 

	
 
	/* List the blitters */
 
	p = BlitterFactory::GetBlittersInfo(p, lastof(buf));
 
	BlitterFactory::GetBlittersInfo(output_iterator);
 

	
 
	/* List the debug facilities. */
 
	p = DumpDebugFacilityNames(p, lastof(buf));
 
	DumpDebugFacilityNames(output_iterator);
 

	
 
	/* We need to initialize the AI, so it finds the AIs */
 
	AI::Initialize();
 
	const std::string ai_list = AI::GetConsoleList(true);
 
	p = strecpy(p, ai_list.c_str(), lastof(buf));
 
	AI::GetConsoleList(output_iterator, true);
 
	AI::Uninitialize(true);
 

	
 
	/* We need to initialize the GameScript, so it finds the GSs */
 
	Game::Initialize();
 
	const std::string game_list = Game::GetConsoleList(true);
 
	p = strecpy(p, game_list.c_str(), lastof(buf));
 
	Game::GetConsoleList(output_iterator, true);
 
	Game::Uninitialize(true);
 

	
 
	/* ShowInfo put output to stderr, but version information should go
 
	 * to stdout; this is the only exception */
 
#if !defined(_WIN32)
 
	printf("%s\n", buf);
 
	printf("%s\n", str.c_str());
 
#else
 
	ShowInfoI(buf);
 
	ShowInfoI(str);
 
#endif
 
}
 

	
 
static void WriteSavegameInfo(const char *name)
 
{
 
	extern SaveLoadVersion _sl_version;
src/script/script_scanner.cpp
Show inline comments
 
@@ -135,24 +135,21 @@ void ScriptScanner::RegisterScript(Scrip
 
		} else if (it->second->GetVersion() < info->GetVersion()) {
 
			it->second = 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;
 
	p += fmt::format("List of {}:\n", this->GetScannerName());
 
	fmt::format_to(output_iterator, "List of {}:\n", this->GetScannerName());
 
	const ScriptInfoList &list = newest_only ? this->info_single_list : this->info_list;
 
	for (const auto &item : list) {
 
		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";
 

	
 
	return p;
 
	fmt::format_to(output_iterator, "\n");
 
}
 

	
 
/** Helper for creating a MD5sum of all files within of a script. */
 
struct ScriptFileChecksumCreator : FileScanner {
 
	MD5Hash md5sum; ///< The final md5sum.
 
	Subdirectory dir; ///< The directory to look in.
src/script/script_scanner.hpp
Show inline comments
 
@@ -52,14 +52,16 @@ public:
 
	 * Register a ScriptInfo to the scanner.
 
	 */
 
	void RegisterScript(class ScriptInfo *info);
 

	
 
	/**
 
	 * 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.
 
	 * @param ci The characteristics to search on (shortname and md5sum).
 
	 * @param md5sum Whether to check the MD5 checksum.
 
	 * @return True iff we have a script matching.
0 comments (0 inline, 0 general)