1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 03:28:20 +02:00

Added GetRelativePath

This commit is contained in:
Brian Fiete 2023-02-20 14:46:24 -05:00
parent 3dbab0f42b
commit 990b509111
2 changed files with 135 additions and 42 deletions

View file

@ -1044,6 +1044,98 @@ String Beefy::GetFileExtension(const StringImpl& path)
return path.Substring(dotPos);
}
static String GetDriveStringTo(String path)
{
if ((path.length() >= 2) && (path[1] == ':'))
return String(path, 0, 2);
return "";
}
String Beefy::GetRelativePath(const StringImpl& fullPath, const StringImpl& curDir)
{
String curPath1 = String(curDir);
String curPath2 = String(fullPath);
for (int i = 0; i < (int)curPath1.length(); i++)
if (curPath1[i] == DIR_SEP_CHAR_ALT)
curPath1[i] = DIR_SEP_CHAR;
for (int i = 0; i < (int)curPath2.length(); i++)
if (curPath2[i] == DIR_SEP_CHAR_ALT)
curPath2[i] = DIR_SEP_CHAR;
String driveString1 = GetDriveStringTo(curPath1);
String driveString2 = GetDriveStringTo(curPath2);
#ifdef _WIN32
StringImpl::CompareKind compareType = StringImpl::CompareKind_OrdinalIgnoreCase;
#else
StringImpl::CompareKind compareType = StringImpl::CompareKind_Ordinal;
#endif
// On separate drives?
if (!driveString1.Equals(driveString2, compareType))
{
return fullPath;
}
if (driveString1.mLength > 0)
curPath1.Remove(0, BF_MIN(driveString1.mLength + 1, curPath1.mLength));
if (driveString2.mLength > 0)
curPath2.Remove(0, BF_MIN(driveString2.mLength + 1, curPath2.mLength));
while ((curPath1.mLength > 0) && (curPath2.mLength > 0))
{
int slashPos1 = (int)curPath1.IndexOf(DIR_SEP_CHAR);
if (slashPos1 == -1)
slashPos1 = curPath1.mLength;
int slashPos2 = (int)curPath2.IndexOf(DIR_SEP_CHAR);
if (slashPos2 == -1)
slashPos2 = curPath2.mLength;
String section1;
section1.Append(StringView(curPath1, 0, slashPos1));
String section2;
section2.Append(StringView(curPath2, 0, slashPos2));
if (!section1.Equals(section2, compareType))
{
// a/b/c
// d/e/f
while (curPath1.mLength > 0)
{
slashPos1 = (int)curPath1.IndexOf(DIR_SEP_CHAR);
if (slashPos1 == -1)
slashPos1 = curPath1.mLength;
if (slashPos1 + 1 >= curPath1.mLength)
curPath1.Clear();
else
curPath1.Remove(0, slashPos1 + 1);
if (DIR_SEP_CHAR == '\\')
curPath2.Insert(0, "..\\");
else
curPath2.Insert(0, "../");
}
}
else
{
if (slashPos1 + 1 >= curPath1.mLength)
curPath1.Clear();
else
curPath1.Remove(0, slashPos1 + 1);
if (slashPos2 + 2 >= curPath2.mLength)
curPath1 = "";
else
curPath2.Remove(0, slashPos2 + 1);
}
}
return curPath2;
}
String Beefy::GetAbsPath(const StringImpl& relPathIn, const StringImpl& dir)
{
String relPath = relPathIn;

View file

@ -226,6 +226,7 @@ int64 GetFileTimeWrite(const StringImpl& path);
String GetFileDir(const StringImpl& path);
String GetFileName(const StringImpl& path);
String GetFileExtension(const StringImpl& path);
String GetRelativePath(const StringImpl& fullPath, const StringImpl& curDir);
String GetAbsPath(const StringImpl& relPath, const StringImpl& dir);
String FixPath(const StringImpl& path);
String FixPathAndCase(const StringImpl& path);