Fix and rename the CEditor::ExtractName function

Fixes #846.
This commit is contained in:
heinrich5991 2017-08-30 13:59:42 +02:00
parent 3555762e1a
commit 22be1be389
6 changed files with 31 additions and 37 deletions

View file

@ -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 <base/math.h>
#include <base/system.h>
#include <engine/storage.h>
#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()

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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");
}

View file

@ -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;