mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::string_view for console commands
parent
f04cf54939
commit
365eed533d
|
@ -156,7 +156,7 @@ static std::string RemoveUnderscores(std::string name)
|
||||||
* @param name name of the alias that will be used
|
* @param name name of the alias that will be used
|
||||||
* @param cmd name of the command that 'name' will be alias of
|
* @param cmd name of the command that 'name' will be alias of
|
||||||
*/
|
*/
|
||||||
/* static */ void IConsole::AliasRegister(const std::string &name, const std::string &cmd)
|
/* static */ void IConsole::AliasRegister(const std::string &name, std::string_view cmd)
|
||||||
{
|
{
|
||||||
auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd);
|
auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd);
|
||||||
if (!result.second) IConsolePrint(CC_ERROR, "An alias with the name '{}' already exists.", name);
|
if (!result.second) IConsolePrint(CC_ERROR, "An alias with the name '{}' already exists.", name);
|
||||||
|
@ -342,10 +342,10 @@ void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
|
||||||
ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
|
ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
|
||||||
switch (chr) {
|
switch (chr) {
|
||||||
case CHR_ALLOW: {
|
case CHR_ALLOW: {
|
||||||
std::vector<char *> c_strings;
|
std::vector<std::string_view> views;
|
||||||
for (auto &token : tokens) c_strings.emplace_back(token.data());
|
for (auto &token : tokens) views.emplace_back(token);
|
||||||
if (!cmd->proc(static_cast<uint8_t>(tokens.size()), c_strings.data())) { // index started with 0
|
if (!cmd->proc(views)) { // index started with 0
|
||||||
cmd->proc(0, nullptr); // if command failed, give help
|
cmd->proc({}); // if command failed, give help
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -29,8 +29,8 @@ enum ConsoleHookResult : uint8_t {
|
||||||
* If you want to handle multiple words as one, enclose them in double-quotes
|
* If you want to handle multiple words as one, enclose them in double-quotes
|
||||||
* eg. 'say "hello everybody"'
|
* eg. 'say "hello everybody"'
|
||||||
*/
|
*/
|
||||||
typedef bool IConsoleCmdProc(uint8_t argc, char *argv[]);
|
using IConsoleCmdProc = bool(std::span<std::string_view>);
|
||||||
typedef ConsoleHookResult IConsoleHook(bool echo);
|
using IConsoleHook = ConsoleHookResult(bool);
|
||||||
struct IConsoleCmd {
|
struct IConsoleCmd {
|
||||||
IConsoleCmd(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook) : name(name), proc(proc), hook(hook) {}
|
IConsoleCmd(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook) : name(name), proc(proc), hook(hook) {}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ struct IConsoleCmd {
|
||||||
* - ";" allows for combining commands (see example 'ng')
|
* - ";" allows for combining commands (see example 'ng')
|
||||||
*/
|
*/
|
||||||
struct IConsoleAlias {
|
struct IConsoleAlias {
|
||||||
IConsoleAlias(const std::string &name, const std::string &cmdline) : name(name), cmdline(cmdline) {}
|
IConsoleAlias(const std::string &name, std::string_view cmdline) : name(name), cmdline(cmdline) {}
|
||||||
|
|
||||||
std::string name; ///< name of the alias
|
std::string name; ///< name of the alias
|
||||||
std::string cmdline; ///< command(s) that is/are being aliased
|
std::string cmdline; ///< command(s) that is/are being aliased
|
||||||
|
@ -70,7 +70,7 @@ struct IConsole
|
||||||
/* Commands */
|
/* Commands */
|
||||||
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr);
|
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr);
|
||||||
static IConsoleCmd *CmdGet(const std::string &name);
|
static IConsoleCmd *CmdGet(const std::string &name);
|
||||||
static void AliasRegister(const std::string &name, const std::string &cmd);
|
static void AliasRegister(const std::string &name, std::string_view cmd);
|
||||||
static IConsoleAlias *AliasGet(const std::string &name);
|
static IConsoleAlias *AliasGet(const std::string &name);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1088,13 +1088,13 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool CmdDumpSMF(uint8_t argc, char *argv[])
|
static bool CmdDumpSMF(std::span<std::string_view> argv)
|
||||||
{
|
{
|
||||||
if (argc == 0) {
|
if (argv.empty()) {
|
||||||
IConsolePrint(CC_HELP, "Write the current song to a Standard MIDI File. Usage: 'dumpsmf <filename>'.");
|
IConsolePrint(CC_HELP, "Write the current song to a Standard MIDI File. Usage: 'dumpsmf <filename>'.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (argc != 2) {
|
if (argv.size() != 2) {
|
||||||
IConsolePrint(CC_WARNING, "You must specify a filename to write MIDI data to.");
|
IConsolePrint(CC_WARNING, "You must specify a filename to write MIDI data to.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue