remove leading and trailing space from server name and forbit the use of multiple spaces

(cherry picked from commit a9cc1e8de2)
This commit is contained in:
KillaBilla 2013-09-04 16:44:04 +02:00 committed by ChillerDragon
parent 77a4cd5b01
commit ba1abac52f
4 changed files with 51 additions and 0 deletions

View file

@ -2420,6 +2420,42 @@ void str_sanitize_filename(char *str_in)
}
}
/* removes leading and trailing spaces and limits the use of multiple spaces */
void str_clean_whitespaces(char *str_in)
{
int Len = strlen(str_in);
int FirstIndex;
int LastIndex;
int SpaceStart = -1;
int i;
// remove leading and trailing spaces
for(FirstIndex = 0; FirstIndex < Len; FirstIndex++)
if(str_in[FirstIndex] != ' ')
break;
for(LastIndex = Len - 1; LastIndex > FirstIndex; LastIndex--)
if(str_in[LastIndex] != ' ')
break;
str_copy(str_in, str_in + FirstIndex, LastIndex - FirstIndex + 2);
// remove multiple spaces
Len = strlen(str_in);
for(i = 0; i < Len; i++)
{
if(str_in[i] == ' ' && SpaceStart == -1)
SpaceStart = i;
else if(str_in[i] != ' ' && SpaceStart != -1)
{
str_copy(str_in + SpaceStart + 1, str_in + i, Len - i + 1);
i = SpaceStart + 1;
SpaceStart = -1;
}
}
}
char *str_skip_to_whitespace(char *str)
{
while(*str && (*str != ' ' && *str != '\t' && *str != '\n'))

View file

@ -1111,6 +1111,16 @@ void str_sanitize(char *str);
*/
void str_sanitize_filename(char *str);
/*
Function: str_clean_whitespaces
Removes leading and trailing spaces and limits the use of multiple spaces.
Parameters:
str - String to clean up
Remarks:
- The strings are treated as zero-termineted strings.
*/
void str_clean_whitespaces(char *str);
/*
Function: str_skip_to_whitespace
Skips leading non-whitespace characters(all but ' ', '\t', '\n', '\r').

View file

@ -1455,6 +1455,8 @@ void CClient::ProcessServerInfo(int RawType, NETADDR *pFrom, const void *pData,
}
}
str_clean_whitespaces(Info.m_aName);
if(!Up.Error() || IgnoreError)
{
qsort(Info.m_aClients, Info.m_NumReceivedClients, sizeof(*Info.m_aClients), PlayerScoreNameComp);

View file

@ -2798,7 +2798,10 @@ void CServer::ConchainSpecialInfoupdate(IConsole::IResult *pResult, void *pUserD
{
pfnCallback(pResult, pCallbackUserData);
if(pResult->NumArguments())
{
str_clean_whitespaces(g_Config.m_SvName);
((CServer *)pUserData)->UpdateServerInfo(true);
}
}
void CServer::ConchainMaxclientsperipUpdate(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)