use shared SplitPath function

This commit is contained in:
Christoph Oelckers 2023-10-01 13:09:22 +02:00
parent ee07ae35aa
commit 716e059374
3 changed files with 40 additions and 67 deletions

View file

@ -69,13 +69,16 @@ FString progdir;
//
//==========================================================================
static inline bool IsSeperator (int c)
static inline bool IsSeperator (int c, bool forcebackslash = false)
{
if (c == '/')
return true;
#ifdef _WIN32
if (c == '\\')
return true;
#else
if (forcebackslash && c == '\\')
return true;
#endif
return false;
}
@ -243,11 +246,11 @@ bool GetFileInfo(const char* pathname, size_t *size, time_t *time)
//
//==========================================================================
void DefaultExtension (FString &path, const char *extension)
void DefaultExtension (FString &path, const char *extension, bool forcebackslash)
{
const char *src = &path[int(path.Len())-1];
while (src != &path[0] && !IsSeperator(*src))
while (src != &path[0] && !IsSeperator(*src, forcebackslash))
{
if (*src == '.')
return; // it has an extension
@ -269,7 +272,7 @@ void DefaultExtension (FString &path, const char *extension)
//
//==========================================================================
FString ExtractFilePath (const char *path)
FString ExtractFilePath (const char *path, bool forcebackslash)
{
const char *src;
@ -278,7 +281,7 @@ FString ExtractFilePath (const char *path)
//
// back up until a \ or the start
//
while (src != path && !IsSeperator(*(src-1)))
while (src != path && !IsSeperator(*(src-1), forcebackslash))
src--;
return FString(path, src - path);
@ -292,7 +295,7 @@ FString ExtractFilePath (const char *path)
//
//==========================================================================
FString ExtractFileBase (const char *path, bool include_extension)
FString ExtractFileBase (const char *path, bool include_extension, bool forcebackslash)
{
const char *src, *dot;
@ -301,7 +304,7 @@ FString ExtractFileBase (const char *path, bool include_extension)
if (src >= path)
{
// back up until a / or the start
while (src != path && !IsSeperator(*(src-1)))
while (src != path && !IsSeperator(*(src-1), forcebackslash))
src--;
// Check for files with drive specification but no path
@ -325,6 +328,29 @@ FString ExtractFileBase (const char *path, bool include_extension)
return FString();
}
//==========================================================================
//
// SplitPath
//
// splits a path into directory, base name and extension
//
//==========================================================================
void SplitPath(const char* path, FString& directory, FString& base, FString& ext, bool forcebackslash)
{
directory = ExtractFilePath(path, forcebackslash);
base = ExtractFileBase(path, forcebackslash);
auto dot = base.LastIndexOf('.');
if (dot > -1)
{
ext = base.Mid(dot + 1);
base.Truncate(dot);
}
else
ext = "";
}
//==========================================================================
//
// StripExtension

View file

@ -44,12 +44,13 @@ extern FString progdir;
void FixPathSeperator (char *path);
static void inline FixPathSeperator (FString &path) { path.ReplaceChars('\\', '/'); }
void DefaultExtension (FString &path, const char *extension);
void DefaultExtension (FString &path, const char *extension, bool forcebackslash = false);
void NormalizeFileName(FString &str);
FString ExtractFilePath (const char *path);
FString ExtractFileBase (const char *path, bool keep_extension=false);
FString ExtractFilePath (const char *path, bool forcebackslash = false);
FString ExtractFileBase (const char *path, bool keep_extension=false, bool forcebackslash = false);
FString StripExtension(const char* path);
void SplitPath(const char* path, FString& directory, FString& base, FString& ext, bool forcebackslash = false);
struct FScriptPosition;
bool IsNum (const char *str); // [RH] added

View file

@ -135,56 +135,6 @@ void AddCmdDefine(char* text, int value)
nCmdDefines++;
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
static void SplitPath(const char* pzPath, char* pzDirectory, char* pzFile, char* pzType)
{
int const nLength = (int)strlen(pzPath);
const char* pDot = NULL;
for (int i = nLength - 1; i >= 0; i--)
{
if (pzPath[i] == '/' || pzPath[i] == '\\')
{
strncpy(pzDirectory, pzPath, i);
pzDirectory[i] = 0;
if (!pDot)
{
strcpy(pzFile, pzPath + i + 1);
strcpy(pzType, "");
}
else
{
strncpy(pzFile, pzPath + i + 1, pDot - (pzPath + i + 1));
pzFile[pDot - (pzPath + i + 1)] = 0;
strcpy(pzType, pDot + 1);
}
return;
}
else if (pzPath[i] == '.')
{
pDot = pzPath + i;
}
}
strcpy(pzDirectory, "/");
if (!pDot)
{
strcpy(pzFile, pzPath);
strcpy(pzType, "");
}
else
{
strncpy(pzFile, pzPath, pDot - pzPath);
pzFile[pDot - pzPath] = 0;
strcpy(pzType, pDot + 1);
}
}
//---------------------------------------------------------------------------
//
@ -989,13 +939,9 @@ void ParseScript(int lumpnum)
void addMemoryResource(const char* filePath, int flags, int ID)
{
char zDirectory[BMAX_PATH];
char zFilename[BMAX_PATH];
char zType[BMAX_PATH];
SplitPath(filePath, zDirectory, zFilename, zType);
fileSystem.AddFromBuffer(zFilename, zType, buffer, nBytes, ID, flags);
FString zDirectory, zFilename, zType;
SplitPath(filePath, zDirectory, zFilename, zType, true);
fileSystem.AddFromBuffer(zFilename.GetChars(), zType.GetChars(), buffer, nBytes, ID, flags);
}