Skip to content

Commit 10af729

Browse files
author
G_Moris
committed
std::string from CCommands
1 parent 903f812 commit 10af729

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

Client/core/CCommands.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,41 +84,38 @@ bool CCommands::Execute(const char* szCommandLine)
8484
bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool bHandleRemotely, bool bIsScriptedBind)
8585
{
8686
// Copy szParametersIn so the contents can be changed
87-
std::unique_ptr<char[]> szParameters = nullptr;
87+
std::string strParameters;
8888
if (szParametersIn)
8989
{
90-
szParameters = std::make_unique<char[]>(strlen(szParametersIn) + 1);
91-
std::strcpy(szParameters.get(), szParametersIn);
90+
strParameters = std::string(szParametersIn, strlen(szParametersIn));
9291
}
9392

9493
// HACK: if its a 'chatboxsay' command, use the next parameter
9594
// Is the command "say" and the arguments start with /? (command comes from the chatbox)
9695
if (!bIsScriptedBind && !stricmp(szCommand, "chatboxsay"))
9796
{
98-
if (szParameters)
97+
if (!strParameters.empty())
9998
{
10099
// His line starts with '/'?
101-
if (szParameters[0] == '/')
100+
if (strParameters[0] == '/')
102101
{
103102
// Copy the characters after the slash to the 0 terminator to a seperate buffer
104103
std::array<char, 256> szBuffer = {};
105-
strncpy(szBuffer.data(), szParameters.get() + 1, szBuffer.size() - 1);
104+
strncpy(szBuffer.data(), strParameters.c_str() + 1, szBuffer.size() - 1);
106105
szBuffer.back() = '\0';
107106

108107
// Split it into command and arguments
109108
szCommand = strtok(szBuffer.data(), " ");
110109
if (!szCommand)
111110
return false;
112111

113-
if (char* szNewParameters = strtok(nullptr, "\0"); szNewParameters)
112+
if (char* szNewParameters = strtok(nullptr, "\0"))
114113
{
115-
szParameters = std::make_unique<char[]>(strlen(szNewParameters) + 1);
116-
std::strcpy(szParameters.get(), szNewParameters);
114+
strParameters = std::string(szNewParameters, strlen(szNewParameters));
117115
}
118116
else
119117
{
120-
szParameters = std::make_unique<char[]>(1);
121-
szParameters[0] = '\0';
118+
strParameters.clear();
122119
}
123120
}
124121
}
@@ -136,14 +133,14 @@ bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool
136133
{
137134
// Execute it
138135
if (!bIsScriptedBind || pEntry->bAllowScriptedBind)
139-
ExecuteHandler(pEntry->pfnCmdFunc, szParameters.get());
136+
ExecuteHandler(pEntry->pfnCmdFunc, strParameters.c_str());
140137

141138
wasHandled = true;
142139
}
143140
}
144141

145142
// Recompose the original command text
146-
std::string val = std::string(szCommand) + " " + std::string(szParameters ? szParameters.get() : "");
143+
std::string val = std::string(szCommand) + " " + std::string(!strParameters.empty() ? strParameters : "");
147144

148145
// Is it a cvar? (syntax: cvar[ = value])
149146
if (!wasHandled)
@@ -194,15 +191,15 @@ bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool
194191

195192
// HACK: if its a 'nick' command, save it here
196193
bool bIsNickCommand = !stricmp(szCommand, "nick");
197-
if (!wasHandled && bIsNickCommand && szParameters && !bIsScriptedBind)
194+
if (!wasHandled && bIsNickCommand && !strParameters.empty() && !bIsScriptedBind)
198195
{
199-
if (CCore::GetSingleton().IsValidNick(szParameters.get()))
196+
if (CCore::GetSingleton().IsValidNick(strParameters.c_str()))
200197
{
201-
CVARS_SET("nick", std::string(szParameters.get()));
198+
CVARS_SET("nick", strParameters);
202199

203200
if (!CCore::GetSingleton().IsConnected())
204201
{
205-
CCore::GetSingleton().GetConsole()->Printf("nick: You are now known as %s", szParameters.get());
202+
CCore::GetSingleton().GetConsole()->Print(std::format("nick: You are now known as {}", strParameters).c_str());
206203
}
207204
}
208205
else if (!CCore::GetSingleton().IsConnected())
@@ -215,7 +212,7 @@ bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool
215212
if (m_pfnExecuteHandler)
216213
{
217214
bool bAllowScriptedBind = (!pEntry || pEntry->bAllowScriptedBind);
218-
if (m_pfnExecuteHandler(szCommand, szParameters.get(), bHandleRemotely, wasHandled, bIsScriptedBind, bAllowScriptedBind))
215+
if (m_pfnExecuteHandler(szCommand, strParameters.c_str(), bHandleRemotely, wasHandled, bIsScriptedBind, bAllowScriptedBind))
219216
return true;
220217
}
221218

0 commit comments

Comments
 (0)