From ba1abac52f1a418381cb96e8af54a880a2cda823 Mon Sep 17 00:00:00 2001 From: KillaBilla Date: Wed, 4 Sep 2013 16:44:04 +0200 Subject: [PATCH] remove leading and trailing space from server name and forbit the use of multiple spaces (cherry picked from commit https://github.com/teeworlds/teeworlds/commit/a9cc1e8de2aca298b68342c509373a539c84c3cc) --- src/base/system.c | 36 ++++++++++++++++++++++++++++++++++++ src/base/system.h | 10 ++++++++++ src/engine/client/client.cpp | 2 ++ src/engine/server/server.cpp | 3 +++ 4 files changed, 51 insertions(+) diff --git a/src/base/system.c b/src/base/system.c index 55dae60a8..fa436991b 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -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')) diff --git a/src/base/system.h b/src/base/system.h index 6d355c184..bfcba4856 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -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'). diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 164933437..421abc118 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -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); diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index bd9cd6eaf..9d1e74f8a 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -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)