]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
cmd: Reduce duplicate code with command lookup
authorcloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 19 Aug 2020 14:24:03 +0000 (14:24 +0000)
committercloudwalk <cloudwalk@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 19 Aug 2020 14:24:03 +0000 (14:24 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12908 d7cf8633-e32d-0410-b094-e92efae38249

cmd.c
cmd.h

diff --git a/cmd.c b/cmd.c
index f58730bfa612985a325f239cba8d65de5029a6c3..6f76df95c3fa740679f3e285010179119b4d2336 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -1949,6 +1949,30 @@ next:
        }
 }
 
+static int Cmd_Compare(const char *s1, const char *s2, size_t len, qboolean casesensitive)
+{
+       if(len)
+               return (casesensitive ? strncmp(s1, s2, len) : strncasecmp(s1, s2, len));
+       else
+               return (casesensitive ? strcmp(s1, s2) : strcasecmp(s1, s2));
+}
+
+cmd_function_t *Cmd_GetCommand(cmd_state_t *cmd, const char *partial, size_t len, qboolean casesensitive)
+{
+       cmd_function_t *func = NULL;
+
+       // check functions
+       for (func = cmd->userdefined->csqc_functions; func; func = func->next)
+               if (!Cmd_Compare(partial, func->name, len, casesensitive))
+                       break;
+
+       for (func=cmd->engine_functions ; func ; func=func->next)
+               if (!Cmd_Compare(partial, func->name, len, casesensitive))
+                       break;
+
+       return func;
+}
+
 /*
 ============
 Cmd_Exists
@@ -1956,20 +1980,11 @@ Cmd_Exists
 */
 qboolean Cmd_Exists (cmd_state_t *cmd, const char *cmd_name)
 {
-       cmd_function_t  *func;
-
-       for (func = cmd->userdefined->csqc_functions; func; func = func->next)
-               if (!strcmp(cmd_name, func->name))
-                       return true;
-
-       for (func=cmd->engine_functions ; func ; func=func->next)
-               if (!strcmp (cmd_name,func->name))
-                       return true;
-
+       if(Cmd_GetCommand(cmd, cmd_name, 0, true))
+               return true;
        return false;
 }
 
-
 /*
 ============
 Cmd_CompleteCommand
@@ -1978,22 +1993,10 @@ Cmd_CompleteCommand
 const char *Cmd_CompleteCommand (cmd_state_t *cmd, const char *partial)
 {
        cmd_function_t *func;
-       size_t len;
-
-       len = strlen(partial);
-
-       if (!len)
-               return NULL;
-
-// check functions
-       for (func = cmd->userdefined->csqc_functions; func; func = func->next)
-               if (!strncasecmp(partial, func->name, len))
-                       return func->name;
-
-       for (func = cmd->engine_functions; func; func = func->next)
-               if (!strncasecmp(partial, func->name, len))
-                       return func->name;
 
+       func = Cmd_GetCommand(cmd, partial, strlen(partial), false);
+       if(func)
+               return func->name;
        return NULL;
 }
 
@@ -2213,19 +2216,12 @@ void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qb
                goto done; // no tokens
 
 // check functions
-       for (func = cmd->userdefined->csqc_functions; func; func = func->next)
+       func = Cmd_GetCommand(cmd, cmd->argv[0], 0, false);
+       if(func)
        {
-               if (!strcasecmp(cmd->argv[0], func->name))
-               {
-                       if (func->csqcfunc && CL_VM_ConsoleCommand(text))       //[515]: csqc
-                               goto done;
-                       break;
-               }
-       }
-
-       for (func = cmd->engine_functions; func; func=func->next)
-       {
-               if (!strcasecmp (cmd->argv[0], func->name))
+               if (func->csqcfunc && CL_VM_ConsoleCommand(text))       //[515]: csqc
+                       goto done;
+               else
                {
                        switch (src)
                        {
@@ -2245,7 +2241,6 @@ void Cmd_ExecuteString (cmd_state_t *cmd, const char *text, cmd_source_t src, qb
                                        goto done;
                                }
                        }
-                       break;
                }
        }
 
diff --git a/cmd.h b/cmd.h
index b1218a73783f8f529ea5d527c1df3957685f08d4..8a8d32b9475b7c132e3fed3c1aae4f569f28df7e 100644 (file)
--- a/cmd.h
+++ b/cmd.h
@@ -222,6 +222,8 @@ void Cmd_AddCommand(int flags, const char *cmd_name, xcommand_t function, const
 // register commands and functions to call for them.
 // The cmd_name is referenced later, so it should not be in temp memory
 
+cmd_function_t *Cmd_GetCommand(cmd_state_t *cmd, const char *partial, size_t len, qboolean casesensitive);
+
 /// used by the cvar code to check for cvar / command name overlap
 qboolean Cmd_Exists (cmd_state_t *cmd, const char *cmd_name);