mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#2813 Fix logic for removing common root path
This commit is contained in:
@@ -100,22 +100,30 @@ QString RiaFilePathTools::canonicalPath(const QString& path)
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
QString RiaFilePathTools::commonRootPath(const QStringList& paths)
|
QString RiaFilePathTools::commonRootPath(const QStringList& paths)
|
||||||
{
|
{
|
||||||
QString root = paths.front();
|
if (paths.size() < 2) return "";
|
||||||
for (const auto& item : paths)
|
|
||||||
|
int i = 0;
|
||||||
|
int iDir = 0;
|
||||||
|
for(i = 0; ; i++)
|
||||||
{
|
{
|
||||||
if (root.length() > item.length()) root.truncate(item.length());
|
bool isCommon = true;
|
||||||
|
QChar ch = i < paths.front().size() ? paths.front()[i] : 0;
|
||||||
|
|
||||||
int iDir = 0;
|
// Remember last directory separator
|
||||||
for (int i = 0; i < root.length(); ++i)
|
if (i > 0 && (ch == '/' || ch == '\\')) iDir = i;
|
||||||
|
|
||||||
|
// Compare characters at position i
|
||||||
|
for (const QString& path : paths)
|
||||||
{
|
{
|
||||||
if (i > 0 && (root[i-1] == '/' || root[i-1] == '\\')) iDir = i;
|
if (ch == 0 || path[i] != ch)
|
||||||
|
|
||||||
if (root[i] != item[i] || i == root.length() - 1)
|
|
||||||
{
|
{
|
||||||
root.truncate(std::min(i, iDir));
|
isCommon = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isCommon) break;
|
||||||
}
|
}
|
||||||
return root;
|
|
||||||
|
return paths.front().left(iDir + 1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user