From 888389c28d5f3ad06154e4d2e36de568b6f7966e Mon Sep 17 00:00:00 2001 From: Loïc Guilloux Date: Sat, 24 Apr 2021 15:19:57 +0200 Subject: Codechange: Use std::string in console commands/aliases registration, and std::map instead our sorted linked list (#9057) * Codechange: Use std::string in console commands and aliases registration * Codechange: Use std::map to register console commands * Codechange: Use std::map to register console aliases * Cleanup: Remove now unused function --- src/console_internal.h | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) (limited to 'src/console_internal.h') diff --git a/src/console_internal.h b/src/console_internal.h index 630f69607..ca2f92193 100644 --- a/src/console_internal.h +++ b/src/console_internal.h @@ -11,6 +11,7 @@ #define CONSOLE_INTERNAL_H #include "gfx_type.h" +#include static const uint ICON_CMDLN_SIZE = 1024; ///< maximum length of a typed in command static const uint ICON_MAX_STREAMSIZE = 2048; ///< maximum length of a totally expanded command @@ -33,9 +34,9 @@ enum ConsoleHookResult { typedef bool IConsoleCmdProc(byte argc, char *argv[]); typedef ConsoleHookResult IConsoleHook(bool echo); struct IConsoleCmd { - char *name; ///< name of command - IConsoleCmd *next; ///< next command in list + IConsoleCmd(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook) : name(name), proc(proc), hook(hook) {} + std::string name; ///< name of command IConsoleCmdProc *proc; ///< process executed when command is typed IConsoleHook *hook; ///< any special trigger action that needs executing }; @@ -53,25 +54,31 @@ struct IConsoleCmd { * - ";" allows for combining commands (see example 'ng') */ struct IConsoleAlias { - char *name; ///< name of the alias - IConsoleAlias *next; ///< next alias in list + IConsoleAlias(const std::string &name, const std::string &cmdline) : name(name), cmdline(cmdline) {} - char *cmdline; ///< command(s) that is/are being aliased + std::string name; ///< name of the alias + std::string cmdline; ///< command(s) that is/are being aliased }; -/* console parser */ -extern IConsoleCmd *_iconsole_cmds; ///< List of registered commands. -extern IConsoleAlias *_iconsole_aliases; ///< List of registered aliases. +struct IConsole +{ + typedef std::map CommandList; + typedef std::map AliasList; + + /* console parser */ + static CommandList &Commands(); + static AliasList &Aliases(); + + /* Commands */ + static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr); + static IConsoleCmd *CmdGet(const std::string &name); + static void AliasRegister(const std::string &name, const std::string &cmd); + static IConsoleAlias *AliasGet(const std::string &name); +}; /* console functions */ void IConsoleClearBuffer(); -/* Commands */ -void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr); -void IConsoleAliasRegister(const char *name, const char *cmd); -IConsoleCmd *IConsoleCmdGet(const char *name); -IConsoleAlias *IConsoleAliasGet(const char *name); - /* console std lib (register ingame commands/aliases) */ void IConsoleStdLibRegister(); @@ -81,6 +88,5 @@ bool GetArgumentInteger(uint32 *value, const char *arg); void IConsoleGUIInit(); void IConsoleGUIFree(); void IConsoleGUIPrint(TextColour colour_code, char *string); -char *RemoveUnderscores(char *name); #endif /* CONSOLE_INTERNAL_H */ -- cgit v1.2.3-54-g00ecf