From 19126d96ecadfb11069a5e9e55e89533744cde69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20St=C3=B8ren?= Date: Fri, 16 Aug 2019 13:54:10 +0200 Subject: [PATCH] #4589 Recursive Import Dialog: Add a method to extract the root path from a search path string. Also added unit test for this --- .../Application/Tools/RiaFilePathTools.cpp | 34 ++++++++ .../Application/Tools/RiaFilePathTools.h | 1 + .../UnitTests/CMakeLists_files.cmake | 1 + .../UnitTests/RiaFilePathTools-Test.cpp | 85 +++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 ApplicationCode/UnitTests/RiaFilePathTools-Test.cpp diff --git a/ApplicationCode/Application/Tools/RiaFilePathTools.cpp b/ApplicationCode/Application/Tools/RiaFilePathTools.cpp index 75db3144d9..859b1951ab 100644 --- a/ApplicationCode/Application/Tools/RiaFilePathTools.cpp +++ b/ApplicationCode/Application/Tools/RiaFilePathTools.cpp @@ -20,6 +20,7 @@ #include "RiaFilePathTools.h" #include +#include //-------------------------------------------------------------------------------------------------- @@ -128,3 +129,36 @@ QString RiaFilePathTools::removeDuplicatePathSeparators(const QString& path) return correctedPath; } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString RiaFilePathTools::rootSearchPathFromSearchFilter(const QString& searchFilter) +{ + std::set globStartCharacters = {'*', '?', '['}; // ']' not needed + + QStringList pathPartList = searchFilter.split(SEPARATOR); + + QStringList::iterator pathPartIt= pathPartList.begin(); + + for ( ; pathPartIt != pathPartList.end(); ++pathPartIt) + { + QString pathPart = *pathPartIt; + + // Remove allowed escaping of wildcards + + pathPart.replace("[[]", ""); + pathPart.replace("[]]", ""); + pathPart.replace("[?]", ""); + pathPart.replace("[*]", ""); + + if (pathPart.contains("*")) break; + if (pathPart.contains("?")) break; + if (pathPart.contains("[")) break; + + } + + pathPartList.erase(pathPartIt, pathPartList.end()); + + return pathPartList.join(SEPARATOR); +} diff --git a/ApplicationCode/Application/Tools/RiaFilePathTools.h b/ApplicationCode/Application/Tools/RiaFilePathTools.h index 3384fa29e6..c9fa71a1ab 100644 --- a/ApplicationCode/Application/Tools/RiaFilePathTools.h +++ b/ApplicationCode/Application/Tools/RiaFilePathTools.h @@ -41,4 +41,5 @@ public: static QString canonicalPath(const QString& path); static std::pair toFolderAndFileName(const QString& absFileName); static QString removeDuplicatePathSeparators(const QString& path); + static QString rootSearchPathFromSearchFilter(const QString& searchFilter); }; diff --git a/ApplicationCode/UnitTests/CMakeLists_files.cmake b/ApplicationCode/UnitTests/CMakeLists_files.cmake index 7f7dec4dcb..6b60930eee 100644 --- a/ApplicationCode/UnitTests/CMakeLists_files.cmake +++ b/ApplicationCode/UnitTests/CMakeLists_files.cmake @@ -51,6 +51,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaWeightedMean-Test.cpp ${CMAKE_CURRENT_LIST_DIR}/RiaWeightedGeometricMeanCalculator-Test.cpp ${CMAKE_CURRENT_LIST_DIR}/RiaWeightedHarmonicMeanCalculator-Test.cpp ${CMAKE_CURRENT_LIST_DIR}/RiaCellDividingTools-Test.cpp +${CMAKE_CURRENT_LIST_DIR}/RiaFilePathTools-Test.cpp ${CMAKE_CURRENT_LIST_DIR}/Intersect-Test.cpp ${CMAKE_CURRENT_LIST_DIR}/RifPerforationIntervalReader-Test.cpp ${CMAKE_CURRENT_LIST_DIR}/RimWellPathCompletions-Test.cpp diff --git a/ApplicationCode/UnitTests/RiaFilePathTools-Test.cpp b/ApplicationCode/UnitTests/RiaFilePathTools-Test.cpp new file mode 100644 index 0000000000..af7a91d979 --- /dev/null +++ b/ApplicationCode/UnitTests/RiaFilePathTools-Test.cpp @@ -0,0 +1,85 @@ +#include "gtest/gtest.h" + +#include "RiaFilePathTools.h" + +#include + +std::ostream& operator<< (std::ostream& out, const QString & text) +{ + out << text.toStdString(); + return out; +} + +//-------------------------------------------------------------------------------------------------- +TEST(RiaFilePathTools, rootSearchPathFromSearchFilter) +{ + { + QString testPath(""); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString(""), resultRootPath); + } + + { + QString testPath("D:/"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("D:/"), resultRootPath); + } + { + QString testPath("D:/A"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("D:/A"), resultRootPath); + } + + { + QString testPath("D:/A/B[cd]/E"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("D:/A"), resultRootPath); + } + { + QString testPath("/A/B[cd]/E"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("/A"), resultRootPath); + } + { + QString testPath("/A/B?/E"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("/A"), resultRootPath); + } + { + QString testPath("//A/B/E*"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("//A/B"), resultRootPath); + } + { + QString testPath("//A/B/E"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("//A/B/E"), resultRootPath); + } + { + QString testPath("//A/B/E/"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("//A/B/E/"), resultRootPath); + } + + { + QString testPath("//A/B[[]/E/"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("//A/B[[]/E/"), resultRootPath); + } + { + QString testPath("//A/B[]]/E/"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("//A/B[]]/E/"), resultRootPath); + } + { + QString testPath("//A/B[*]/E/"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("//A/B[*]/E/"), resultRootPath); + } + { + QString testPath("//A/B[?]/E/"); + QString resultRootPath = RiaFilePathTools::rootSearchPathFromSearchFilter(testPath); + EXPECT_EQ(QString("//A/B[?]/E/"), resultRootPath); + } + +} \ No newline at end of file