From 22be1be389c824c50fbb2becf4b4042e46cfdeaf Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Wed, 30 Aug 2017 13:59:42 +0200 Subject: [PATCH] Fix and rename the `CEditor::ExtractName` function Fixes #846. --- src/engine/shared/storage.cpp | 23 +++++++++++++++++++++++ src/engine/storage.h | 2 ++ src/game/editor/editor.cpp | 8 ++++---- src/game/editor/editor.h | 15 --------------- src/game/editor/io.cpp | 2 +- src/tools/map_replace_image.cpp | 18 +----------------- 6 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/engine/shared/storage.cpp b/src/engine/shared/storage.cpp index 2c41cadbf..5858a6af5 100644 --- a/src/engine/shared/storage.cpp +++ b/src/engine/shared/storage.cpp @@ -1,5 +1,6 @@ /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ /* If you are missing that file, acquire a complete release at teeworlds.com. */ +#include #include #include #include "linereader.h" @@ -473,6 +474,28 @@ public: } }; +void IStorage::StripPathAndExtension(const char *pFilename, char *pBuffer, int BufferSize) +{ + const char *pFilenameEnd = pFilename + str_length(pFilename); + const char *pExtractedName = pFilename; + const char *pEnd = pFilenameEnd; + for(const char *pIter = pFilename; *pIter; pIter++) + { + if(*pIter == '/' || *pIter == '\\') + { + pExtractedName = pIter + 1; + pEnd = pFilenameEnd; + } + else if(*pIter == '.') + { + pEnd = pIter; + } + } + + int Length = min(BufferSize, (int)(pEnd - pExtractedName + 1)); + str_copy(pBuffer, pExtractedName, Length); +} + IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments) { return CStorage::Create(pApplicationName, StorageType, NumArgs, ppArguments); } IStorage *CreateLocalStorage() diff --git a/src/engine/storage.h b/src/engine/storage.h index 20be34654..86d10ee7d 100644 --- a/src/engine/storage.h +++ b/src/engine/storage.h @@ -32,6 +32,8 @@ public: virtual bool RemoveBinaryFile(const char *pFilename) = 0; virtual bool RenameBinaryFile(const char* pOldFilename, const char* pNewFilename) = 0; virtual const char* GetBinaryPath(const char *pDir, char *pBuffer, unsigned BufferSize) = 0; + + static void StripPathAndExtension(const char *pFilename, char *pBuffer, int BufferSize); }; extern IStorage *CreateStorage(const char *pApplicationName, int StorageType, int NumArgs, const char **ppArguments); diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 9f03228bb..379664c7f 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -3092,7 +3092,7 @@ void CEditor::ReplaceImage(const char *pFileName, int StorageType, void *pUser) } *pImg = ImgInfo; pImg->m_External = External; - pEditor->ExtractName(pFileName, pImg->m_aName, sizeof(pImg->m_aName)); + IStorage::StripPathAndExtension(pFileName, pImg->m_aName, sizeof(pImg->m_aName)); pImg->m_AutoMapper.Load(pImg->m_aName); pImg->m_TexID = pEditor->Graphics()->LoadTextureRaw(ImgInfo.m_Width, ImgInfo.m_Height, ImgInfo.m_Format, ImgInfo.m_pData, CImageInfo::FORMAT_AUTO, 0); ImgInfo.m_pData = 0; @@ -3114,7 +3114,7 @@ void CEditor::AddImage(const char *pFileName, int StorageType, void *pUser) // check if we have that image already char aBuf[128]; - ExtractName(pFileName, aBuf, sizeof(aBuf)); + IStorage::StripPathAndExtension(pFileName, aBuf, sizeof(aBuf)); for(int i = 0; i < pEditor->m_Map.m_lImages.size(); ++i) { if(!str_comp(pEditor->m_Map.m_lImages[i]->m_aName, aBuf)) @@ -3148,7 +3148,7 @@ void CEditor::AddSound(const char *pFileName, int StorageType, void *pUser) // check if we have that sound already char aBuf[128]; - ExtractName(pFileName, aBuf, sizeof(aBuf)); + IStorage::StripPathAndExtension(pFileName, aBuf, sizeof(aBuf)); for(int i = 0; i < pEditor->m_Map.m_lSounds.size(); ++i) { if(!str_comp(pEditor->m_Map.m_lSounds[i]->m_aName, aBuf)) @@ -3243,7 +3243,7 @@ void CEditor::ReplaceSound(const char *pFileName, int StorageType, void *pUser) // replace sound pSound->m_External = External; - pEditor->ExtractName(pFileName, pSound->m_aName, sizeof(pSound->m_aName)); + IStorage::StripPathAndExtension(pFileName, pSound->m_aName, sizeof(pSound->m_aName)); pSound->m_SoundID = pEditor->Sound()->LoadOpusFromMem(pData, (unsigned) DataSize, true); pSound->m_pData = pData; pSound->m_DataSize = DataSize; diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index 1d444c480..f8a384077 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -1002,21 +1002,6 @@ public: void AddFileDialogEntry(int Index, CUIRect *pView); void SortImages(); - static void ExtractName(const char *pFileName, char *pName, int BufferSize) - { - const char *pExtractedName = pFileName; - const char *pEnd = 0; - for(; *pFileName; ++pFileName) - { - if(*pFileName == '/' || *pFileName == '\\') - pExtractedName = pFileName+1; - else if(*pFileName == '.') - pEnd = pFileName; - } - - int Length = pEnd > pExtractedName ? min(BufferSize, (int)(pEnd-pExtractedName+1)) : BufferSize; - str_copy(pName, pExtractedName, Length); - } int GetLineDistance(); void ZoomMouseTarget(float ZoomFactor); diff --git a/src/game/editor/io.cpp b/src/game/editor/io.cpp index ab52d59c8..5b85da777 100644 --- a/src/game/editor/io.cpp +++ b/src/game/editor/io.cpp @@ -550,7 +550,7 @@ int CEditorMap::Save(class IStorage *pStorage, const char *pFileName) || !mem_comp(CurrentServerInfo.m_NetAddr.ip, ipv6Localhost, sizeof(ipv6Localhost))) { char aMapName[128]; - m_pEditor->ExtractName(pFileName, aMapName, sizeof(aMapName)); + IStorage::StripPathAndExtension(pFileName, aMapName, sizeof(aMapName)); if(!str_comp(aMapName, CurrentServerInfo.m_aMap)) m_pEditor->Client()->Rcon("reload"); } diff --git a/src/tools/map_replace_image.cpp b/src/tools/map_replace_image.cpp index 6a73fc3a4..29aa60743 100644 --- a/src/tools/map_replace_image.cpp +++ b/src/tools/map_replace_image.cpp @@ -60,22 +60,6 @@ int LoadPNG(CImageInfo *pImg, const char *pFilename) return 1; } -void ExtractName(const char *pFileName, char *pName, int BufferSize) -{ - const char *pExtractedName = pFileName; - const char *pEnd = 0; - for(; *pFileName; ++pFileName) - { - if(*pFileName == '/' || *pFileName == '\\') - pExtractedName = pFileName+1; - else if(*pFileName == '.') - pEnd = pFileName; - } - - int Length = pEnd > pExtractedName ? min(BufferSize, (int)(pEnd-pExtractedName+1)) : BufferSize; - str_copy(pName, pExtractedName, Length); -} - void *ReplaceImageItem(void *pItem, int Type, const char *pImgName, const char *pImgFile, CMapItemImage *pNewImgItem) { if(Type != MAPITEMTYPE_IMAGE) @@ -100,7 +84,7 @@ void *ReplaceImageItem(void *pItem, int Type, const char *pImgName, const char * int PixelSize = ImgInfo.m_Format == CImageInfo::FORMAT_RGB ? 3 : 4; g_NewNameID = pImgItem->m_ImageName; - ExtractName(pImgFile, g_aNewName, sizeof(g_aNewName)); + IStorage::StripPathAndExtension(pImgFile, g_aNewName, sizeof(g_aNewName)); g_NewDataID = pImgItem->m_ImageData; g_pNewData = ImgInfo.m_pData; g_NewDataSize = ImgInfo.m_Width * ImgInfo.m_Height * PixelSize;