@@ -8,39 +8,56 @@ void freeCommandList(List *list)
88 free (list );
99}
1010
11- // attention: modifies usercommand to lowercase
12- void * getCommandlistCommand (List * list , char * usercommand )
11+ /**
12+ * getCommandlistCommand
13+ * ---------------------
14+ * Refactor - Caskey, Damon V.
15+ * 2025-05-17
16+ *
17+ * Looks up a command in the given list by its name, case-insensitively.
18+ *
19+ * Notes:
20+ * - The search is case-insensitive.
21+ * - Memory is dynamically allocated for the lowercase copy and freed before return.
22+ */
23+ void * getCommandlistCommand (List * list , const char * usercommand )
1324{
14- if (!usercommand || !usercommand [0 ])
15- {
16- goto fail ;
25+ if (!list || !usercommand || !usercommand [0 ]) {
26+ return NULL ;
1727 }
18- lc (usercommand , strlen (usercommand ));
19- Node * n = List_GetNodeByName (list , usercommand );
20- if (n )
21- {
22- return n -> value ;
28+
29+ size_t len = strlen (usercommand );
30+ char * buf = (char * )malloc (len + 1 );
31+
32+ if (!buf ){
33+ return NULL ;
2334 }
24- fail :
25- return NULL ;
35+
36+ memcpy (buf , usercommand , len + 1 ); // Copy including null terminator
37+ lc (buf , len );
38+
39+ Node * n = List_GetNodeByName (list , buf );
40+ free (buf );
41+
42+ return n ? n -> value : NULL ;
2643}
2744
28- modelCommands getModelCommand (List * list , char * usercommand )
45+ modelCommands getModelCommand (List * list , const char * usercommand )
2946{
3047 return (modelCommands ) getCommandlistCommand (list , usercommand );
3148}
3249
33- modelstxtCommands getModelstxtCommand (List * list , char * usercommand )
50+ modelstxtCommands getModelstxtCommand (List * list , const char * usercommand )
3451{
3552 return (modelstxtCommands ) getCommandlistCommand (list , usercommand );
3653}
3754
38- levelCommands getLevelCommand (List * list , char * usercommand )
55+ levelCommands getLevelCommand (List * list , const char * usercommand )
3956{
4057 return (levelCommands ) getCommandlistCommand (list , usercommand );
4158}
4259
43- levelOrderCommands getLevelOrderCommand (List * list , char * usercommand )
60+ levelOrderCommands getLevelOrderCommand (List * list , const char * usercommand )
4461{
4562 return (levelOrderCommands ) getCommandlistCommand (list , usercommand );
4663}
0 commit comments