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 == '/') if (c == '/')
return true; return true;
#ifdef _WIN32 #ifdef _WIN32
if (c == '\\') if (c == '\\')
return true; return true;
#else
if (forcebackslash && c == '\\')
return true;
#endif #endif
return false; 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]; const char *src = &path[int(path.Len())-1];
while (src != &path[0] && !IsSeperator(*src)) while (src != &path[0] && !IsSeperator(*src, forcebackslash))
{ {
if (*src == '.') if (*src == '.')
return; // it has an extension 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; const char *src;
@ -278,7 +281,7 @@ FString ExtractFilePath (const char *path)
// //
// back up until a \ or the start // back up until a \ or the start
// //
while (src != path && !IsSeperator(*(src-1))) while (src != path && !IsSeperator(*(src-1), forcebackslash))
src--; src--;
return FString(path, src - path); 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; const char *src, *dot;
@ -301,7 +304,7 @@ FString ExtractFileBase (const char *path, bool include_extension)
if (src >= path) if (src >= path)
{ {
// back up until a / or the start // back up until a / or the start
while (src != path && !IsSeperator(*(src-1))) while (src != path && !IsSeperator(*(src-1), forcebackslash))
src--; src--;
// Check for files with drive specification but no path // Check for files with drive specification but no path
@ -325,6 +328,29 @@ FString ExtractFileBase (const char *path, bool include_extension)
return FString(); 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 // StripExtension

View file

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

View file

@ -135,56 +135,6 @@ void AddCmdDefine(char* text, int value)
nCmdDefines++; 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) void addMemoryResource(const char* filePath, int flags, int ID)
{ {
char zDirectory[BMAX_PATH]; FString zDirectory, zFilename, zType;
char zFilename[BMAX_PATH]; SplitPath(filePath, zDirectory, zFilename, zType, true);
char zType[BMAX_PATH]; fileSystem.AddFromBuffer(zFilename.GetChars(), zType.GetChars(), buffer, nBytes, ID, flags);
SplitPath(filePath, zDirectory, zFilename, zType);
fileSystem.AddFromBuffer(zFilename, zType, buffer, nBytes, ID, flags);
} }