Use str_startswith, str_endswith, str_truncate

Use this instead of ad-hoc implementations which are a bit more
error-prone.
This commit is contained in:
heinrich5991 2018-12-25 20:36:50 +01:00
parent 11862b495f
commit b3df4ffc08
12 changed files with 73 additions and 74 deletions

View file

@ -239,7 +239,7 @@ void CSmoothTime::Update(CGraph *pGraph, int64 Target, int TimeLeft, int AdjustD
UpdateInt(Target);
}
CClient::CClient() : m_DemoPlayer(&m_SnapshotDelta), m_DemoRecorder(&m_SnapshotDelta), m_pConLinkIdentifier("teeworlds:")
CClient::CClient() : m_DemoPlayer(&m_SnapshotDelta), m_DemoRecorder(&m_SnapshotDelta)
{
m_pEditor = 0;
m_pInput = 0;
@ -2419,9 +2419,9 @@ static CClient *CreateClient()
return new(pClient) CClient;
}
void CClient::HandleTeeworldsConnectLink(const char *pConLink)
void CClient::ConnectOnStart(const char *pAddress)
{
str_copy(m_aCmdConnect, pConLink, sizeof(m_aCmdConnect));
str_copy(m_aCmdConnect, pAddress, sizeof(m_aCmdConnect));
}
/*
@ -2555,22 +2555,19 @@ int main(int argc, const char **argv) // ignore_convention
// parse the command line arguments
if(argc > 1) // ignore_convention
{
switch(argc) // ignore_convention
const char *pAddress = 0;
if(argc == 2)
{
case 2:
pAddress = str_startswith(argv[1], "teeworlds:");
}
if(pAddress)
{
// handle Teeworlds connect link
const int Length = str_length(pClient->m_pConLinkIdentifier);
if(str_comp_num(pClient->m_pConLinkIdentifier, argv[1], Length) == 0) // ignore_convention
{
pClient->HandleTeeworldsConnectLink(argv[1] + Length); // ignore_convention
break;
}
pClient->ConnectOnStart(pAddress);
}
default:
pConsole->ParseArguments(argc - 1, &argv[1]); // ignore_convention
else
{
pConsole->ParseArguments(argc - 1, &argv[1]);
}
}
}

View file

@ -278,6 +278,7 @@ public:
bool LimitFps();
void Run();
void ConnectOnStart(const char *pAddress);
static void Con_Connect(IConsole::IResult *pResult, void *pUserData);
static void Con_Disconnect(IConsole::IResult *pResult, void *pUserData);
@ -318,9 +319,5 @@ public:
void ToggleFullscreen();
void ToggleWindowBordered();
void ToggleWindowVSync();
// Teeworlds connect link
const char * const m_pConLinkIdentifier;
void HandleTeeworldsConnectLink(const char *pConLink);
};
#endif

View file

@ -84,7 +84,7 @@ bool CMapChecker::ReadAndValidateMap(IStorage *pStorage, const char *pFilename,
int Length = (int)(pEnd - pExtractedName);
if(Length <= 0 || Length >= MAX_MAP_LENGTH)
return true;
str_copy(aMapName, pExtractedName, min((int)MAX_MAP_LENGTH, (int)(pEnd-pExtractedName+1)));
str_truncate(aMapName, MAX_MAP_LENGTH, pExtractedName, pEnd - pExtractedName);
str_format(aMapNameExt, sizeof(aMapNameExt), "%s.map", aMapName);
// check for valid map

View file

@ -111,8 +111,11 @@ public:
while((pLine = LineReader.Get()))
{
if(str_length(pLine) > 9 && !str_comp_num(pLine, "add_path ", 9))
AddPath(pLine+9);
const char *pLineWithoutPrefix = str_startswith(pLine, "add_path ");
if(pLineWithoutPrefix)
{
AddPath(pLineWithoutPrefix);
}
}
io_close(File);

View file

@ -222,7 +222,7 @@ bool CChat::OnInput(IInput::CEvent Event)
for(m_PlaceholderLength = 0; *pCursor && *pCursor != ' '; ++pCursor)
++m_PlaceholderLength;
str_copy(m_aCompletionBuffer, m_Input.GetString()+m_PlaceholderOffset, min(static_cast<int>(sizeof(m_aCompletionBuffer)), m_PlaceholderLength+1));
str_truncate(m_aCompletionBuffer, sizeof(m_aCompletionBuffer), m_Input.GetString()+m_PlaceholderOffset, m_PlaceholderLength);
}
// find next possible name
@ -280,7 +280,7 @@ bool CChat::OnInput(IInput::CEvent Event)
{
char aBuf[256];
// add part before the name
str_copy(aBuf, m_Input.GetString(), min(static_cast<int>(sizeof(aBuf)), m_PlaceholderOffset+1));
str_truncate(aBuf, sizeof(aBuf), m_Input.GetString(), m_PlaceholderOffset);
// add the name
str_append(aBuf, pCompletionString, sizeof(aBuf));

View file

@ -1403,8 +1403,7 @@ void CMenus::RenderBackButton(CUIRect MainView)
int CMenus::MenuImageScan(const char *pName, int IsDir, int DirType, void *pUser)
{
CMenus *pSelf = (CMenus *)pUser;
int l = str_length(pName);
if(l < 4 || IsDir || str_comp(pName+l-4, ".png") != 0)
if(IsDir || !str_endswith(pName, ".png"))
return 0;
char aBuf[512];
@ -1475,7 +1474,7 @@ int CMenus::MenuImageScan(const char *pName, int IsDir, int DirType, void *pUser
mem_free(Info.m_pData);
// set menu image data
str_copy(MenuImage.m_aName, pName, min((int)sizeof(MenuImage.m_aName),l-3));
str_truncate(MenuImage.m_aName, sizeof(MenuImage.m_aName), pName, str_length(pName) - 4);
str_format(aBuf, sizeof(aBuf), "load menu image %s", MenuImage.m_aName);
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "game", aBuf);
pSelf->m_lMenuImages.add(MenuImage);

View file

@ -2170,12 +2170,13 @@ void CMenus::DoGameIcon(const char *pName, const CUIRect *pRect, int Type)
int CMenus::GameIconScan(const char *pName, int IsDir, int DirType, void *pUser)
{
CMenus *pSelf = (CMenus *)pUser;
int l = str_length(pName);
if(l < 5 || IsDir || str_comp(pName + l - 4, ".png") != 0)
const char *pSuffix = str_endswith(pName, ".png");
if(IsDir || !pSuffix)
{
return 0;
char aGameIconName[128] = { 0 };
str_copy(aGameIconName, pName, min((int)sizeof(aGameIconName), l - 3));
}
char aGameIconName[128];
str_truncate(aGameIconName, sizeof(aGameIconName), pName, pSuffix - pName);
// add new game icon
char aBuf[512];

View file

@ -269,11 +269,12 @@ void CMenus::RenderDemoPlayer(CUIRect MainView)
int CMenus::DemolistFetchCallback(const char *pName, int IsDir, int StorageType, void *pUser)
{
CMenus *pSelf = (CMenus *)pUser;
int Length = str_length(pName);
if((pName[0] == '.' && (pName[1] == 0 ||
(pName[1] == '.' && pName[2] == 0 && !str_comp(pSelf->m_aCurrentDemoFolder, "demos")))) ||
(!IsDir && (Length < 5 || str_comp(pName+Length-5, ".demo"))))
if(str_comp(pName, ".") == 0
|| (str_comp(pName, "..") == 0 && str_comp(pSelf->m_aCurrentDemoFolder, "demos") == 0)
|| (!IsDir && str_endswith(pName, ".demo")))
{
return 0;
}
CDemoItem Item;
str_copy(Item.m_aFilename, pName, sizeof(Item.m_aFilename));
@ -284,7 +285,7 @@ int CMenus::DemolistFetchCallback(const char *pName, int IsDir, int StorageType,
}
else
{
str_copy(Item.m_aName, pName, min(static_cast<int>(sizeof(Item.m_aName)), Length-4));
str_truncate(Item.m_aName, sizeof(Item.m_aName), pName, str_length(pName) - 5);
Item.m_InfosLoaded = false;
}
Item.m_IsDir = IsDir != 0;

View file

@ -582,26 +582,24 @@ public:
int CMenus::ThemeScan(const char *pName, int IsDir, int DirType, void *pUser)
{
CMenus *pSelf = (CMenus *)pUser;
int l = str_length(pName);
if(l < 5 || IsDir || str_comp(pName+l-4, ".map") != 0)
const char *pSuffix = str_endswith(pName, ".map");
if(IsDir || !pSuffix)
return 0;
char aFullName[128];
char aThemeName[128];
str_copy(aFullName, pName, min((int)sizeof(aFullName),l-3));
str_truncate(aFullName, sizeof(aFullName), pName, pSuffix - pName);
l = str_length(aFullName);
bool isDay = false;
bool isNight = false;
if(l > 4 && str_comp(aFullName+l-4, "_day") == 0)
bool IsDay = false;
bool IsNight = false;
if((pSuffix = str_endswith(aFullName, "_day")))
{
str_copy(aThemeName, pName, min((int)sizeof(aThemeName),l-3));
isDay = true;
str_truncate(aThemeName, sizeof(aThemeName), pName, pSuffix - aThemeName);
IsDay = true;
}
else if(l > 6 && str_comp(aFullName+l-6, "_night") == 0)
else if((pSuffix = str_endswith(aFullName, "_night")))
{
str_copy(aThemeName, pName, min((int)sizeof(aThemeName),l-5));
isNight = true;
str_truncate(aThemeName, sizeof(aThemeName), pName, pSuffix - aThemeName);
IsNight = true;
}
else
str_copy(aThemeName, aFullName, sizeof(aThemeName));
@ -614,16 +612,16 @@ int CMenus::ThemeScan(const char *pName, int IsDir, int DirType, void *pUser)
{
if(str_comp(pSelf->m_lThemes[i].m_Name, aThemeName) == 0)
{
if(isDay)
if(IsDay)
pSelf->m_lThemes[i].m_HasDay = true;
if(isNight)
if(IsNight)
pSelf->m_lThemes[i].m_HasNight = true;
return 0;
}
}
// make new theme
CTheme Theme(aThemeName, isDay, isNight);
CTheme Theme(aThemeName, IsDay, IsNight);
char aBuf[512];
str_format(aBuf, sizeof(aBuf), "added theme %s from ui/themes/%s", aThemeName, pName);
pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_ADDINFO, "game", aBuf);
@ -634,12 +632,12 @@ int CMenus::ThemeScan(const char *pName, int IsDir, int DirType, void *pUser)
int CMenus::ThemeIconScan(const char *pName, int IsDir, int DirType, void *pUser)
{
CMenus *pSelf = (CMenus *)pUser;
int l = str_length(pName);
if(l < 4 || IsDir || str_comp(pName+l-4, ".png") != 0)
const char *pSuffix = str_endswith(pName, ".png");
if(IsDir || !pSuffix)
return 0;
char aThemeName[128];
str_copy(aThemeName, pName, min((int)sizeof(aThemeName),l-3));
str_truncate(aThemeName, sizeof(aThemeName), pName, pSuffix - pName);
// save icon for an existing theme
for(sorted_array<CTheme>::range r = pSelf->m_lThemes.all(); !r.empty(); r.pop_front()) // bit slow but whatever

View file

@ -28,8 +28,7 @@ int *const CSkins::ms_apColorVariables[NUM_SKINPARTS] = {&g_Config.m_PlayerColor
int CSkins::SkinPartScan(const char *pName, int IsDir, int DirType, void *pUser)
{
CSkins *pSelf = (CSkins *)pUser;
int l = str_length(pName);
if(l < 4 || IsDir || str_comp(pName+l-4, ".png") != 0)
if(IsDir || !str_endswith(pName, ".png"))
return 0;
char aBuf[512];
@ -93,7 +92,7 @@ int CSkins::SkinPartScan(const char *pName, int IsDir, int DirType, void *pUser)
Part.m_Flags |= SKINFLAG_SPECIAL;
if(DirType != IStorage::TYPE_SAVE)
Part.m_Flags |= SKINFLAG_STANDARD;
str_copy(Part.m_aName, pName, min((int)sizeof(Part.m_aName),l-3));
str_truncate(Part.m_aName, sizeof(Part.m_aName), pName, str_length(pName) - 4);
if(g_Config.m_Debug)
{
str_format(aBuf, sizeof(aBuf), "load skin part %s", Part.m_aName);
@ -106,8 +105,7 @@ int CSkins::SkinPartScan(const char *pName, int IsDir, int DirType, void *pUser)
int CSkins::SkinScan(const char *pName, int IsDir, int DirType, void *pUser)
{
int l = str_length(pName);
if(l < 5 || IsDir || str_comp(pName+l-5, ".json") != 0)
if(IsDir || !str_endswith(pName, ".json"))
return 0;
CSkins *pSelf = (CSkins *)pUser;
@ -125,7 +123,7 @@ int CSkins::SkinScan(const char *pName, int IsDir, int DirType, void *pUser)
// init
CSkin Skin = pSelf->m_DummySkin;
str_copy(Skin.m_aName, pName, min((int)sizeof(Skin.m_aName),l-4));
str_truncate(Skin.m_aName, sizeof(Skin.m_aName), pName, str_length(pName) - 5);
if(pSelf->Find(Skin.m_aName, true) != -1)
return 0;
bool SpecialSkin = pName[0] == 'x' && pName[1] == '_';

View file

@ -814,9 +814,8 @@ void CEditor::CallbackSaveMap(const char *pFileName, int StorageType, void *pUse
{
CEditor *pEditor = static_cast<CEditor*>(pUser);
char aBuf[1024];
const int Length = str_length(pFileName);
// add map extension
if(Length <= 4 || pFileName[Length-4] != '.' || str_comp_nocase(pFileName+Length-3, "map"))
if(!str_endswith(pFileName, ".map"))
{
str_format(aBuf, sizeof(aBuf), "%s.map", pFileName);
pFileName = aBuf;
@ -3024,19 +3023,25 @@ void CEditor::RenderImages(CUIRect ToolBox, CUIRect ToolBar, CUIRect View)
static int EditorListdirCallback(const char *pName, int IsDir, int StorageType, void *pUser)
{
CEditor *pEditor = (CEditor*)pUser;
int Length = str_length(pName);
if((pName[0] == '.' && (pName[1] == 0 ||
(pName[1] == '.' && pName[2] == 0 && (!str_comp(pEditor->m_pFileDialogPath, "maps") || !str_comp(pEditor->m_pFileDialogPath, "mapres"))))) ||
(!IsDir && ((pEditor->m_FileDialogFileType == CEditor::FILETYPE_MAP && (Length < 4 || str_comp(pName+Length-4, ".map"))) ||
(pEditor->m_FileDialogFileType == CEditor::FILETYPE_IMG && (Length < 4 || str_comp(pName+Length-4, ".png"))))))
const char *pExt = 0;
switch(pEditor->m_FileDialogFileType)
{
case CEditor::FILETYPE_MAP: pExt = ".map"; break;
case CEditor::FILETYPE_IMG: pExt = ".png"; break;
}
if(str_comp(pName, ".") == 0
|| (str_comp(pName, "..") == 0 && (str_comp(pEditor->m_pFileDialogPath, "maps") == 0 || str_comp(pEditor->m_pFileDialogPath, "mapres") == 0))
|| (pExt && !IsDir && str_endswith(pName, pExt)))
{
return 0;
}
CEditor::CFilelistItem Item;
str_copy(Item.m_aFilename, pName, sizeof(Item.m_aFilename));
if(IsDir)
str_format(Item.m_aName, sizeof(Item.m_aName), "%s/", pName);
else
str_copy(Item.m_aName, pName, min(static_cast<int>(sizeof(Item.m_aName)), Length-3));
str_truncate(Item.m_aName, sizeof(Item.m_aName), pName, str_length(pName) - 4);
Item.m_IsDir = IsDir != 0;
Item.m_IsLink = false;
Item.m_StorageType = StorageType;

View file

@ -390,14 +390,14 @@ void CGameContext::SendVoteStatus(int ClientID, int Total, int Yes, int No)
void CGameContext::AbortVoteOnDisconnect(int ClientID)
{
if(m_VoteCloseTime && ClientID == m_VoteClientID && (!str_comp_num(m_aVoteCommand, "kick ", 5) ||
!str_comp_num(m_aVoteCommand, "set_team ", 9) || (!str_comp_num(m_aVoteCommand, "ban ", 4) && Server()->IsBanned(ClientID))))
if(m_VoteCloseTime && ClientID == m_VoteClientID && (str_startswith(m_aVoteCommand, "kick ") ||
str_startswith(m_aVoteCommand, "set_team ") || (str_startswith(m_aVoteCommand, "ban ") && Server()->IsBanned(ClientID))))
m_VoteCloseTime = -1;
}
void CGameContext::AbortVoteOnTeamChange(int ClientID)
{
if(m_VoteCloseTime && ClientID == m_VoteClientID && !str_comp_num(m_aVoteCommand, "set_team ", 9))
if(m_VoteCloseTime && ClientID == m_VoteClientID && str_startswith(m_aVoteCommand, "set_team "))
m_VoteCloseTime = -1;
}