Add relocatePathPattern

Remove unused code from relocateFile
This commit is contained in:
Magne Sjaastad committed 2025-05-26 11:17:53 +02:00
1 parent b95a49e755
commit edc9cec0a4
5 files changed
+59 -23

No files matched your search

@@ -569,12 +569,9 @@ void RimProject::setProjectFileNameAndUpdateDependencies( const QString& project
std::vector<caf::FilePath*> filePaths = allFilePaths();
for ( caf::FilePath* filePath : filePaths )
{
bool foundFile = false;
std::vector<QString> searchedPaths;
QString filePathCandidate = filePath->path();
QString newFilePath = RimTools::relocateFile( filePathCandidate, newProjectPath, oldProjectPath, &foundFile, &searchedPaths );
QString newFilePath = RimTools::relocateFile( filePathCandidate, newProjectPath, oldProjectPath );
filePath->setPath( newFilePath );
}
@@ -83,14 +83,8 @@ QString RimTools::getCacheRootDirectoryPathFromProject()
/// such that the common start of oldProjectPath and m_gridFileName is removed from m_gridFileName
/// and replaced with the start of newProjectPath up to where newProjectPath starts to be equal to oldProjectPath
//--------------------------------------------------------------------------------------------------
QString RimTools::relocateFile( const QString& originalFileName,
const QString& currentProjectPath,
const QString& previousProjectPath,
bool* foundFile,
std::vector<QString>* searchedPaths )
QString RimTools::relocateFile( const QString& originalFileName, const QString& currentProjectPath, const QString& previousProjectPath )
{
if ( foundFile ) *foundFile = true;
// Make sure we have a Qt formatted path ( using "/" not "\")
QString fileName = QDir::fromNativeSeparators( originalFileName );
QString newProjectPath = QDir::fromNativeSeparators( currentProjectPath );
@@ -115,7 +109,6 @@ QString RimTools::relocateFile( const QString& originalFileName,
fileName.replace( QString( "\\" ), QString( "/" ) );
}
if ( searchedPaths ) searchedPaths->push_back( fileName );
if ( caf::Utils::fileExists( fileName ) )
{
return fileName;
@@ -125,7 +118,6 @@ QString RimTools::relocateFile( const QString& originalFileName,
{
QString fileNameWithoutPath = QFileInfo( fileName ).fileName();
QString candidate = QDir::fromNativeSeparators( newProjectPath + QDir::separator() + fileNameWithoutPath );
if ( searchedPaths ) searchedPaths->push_back( candidate );
if ( caf::Utils::fileExists( candidate ) )
{
@@ -223,8 +215,6 @@ QString RimTools::relocateFile( const QString& originalFileName,
QString relocatedFileName = relocationPath + fileNameWithoutPath;
if ( searchedPaths ) searchedPaths->push_back( relocatedFileName );
if ( caf::Utils::fileExists( relocatedFileName ) )
{
return relocatedFileName;
@@ -237,12 +227,43 @@ QString RimTools::relocateFile( const QString& originalFileName,
}
}
// return the unchanged filename, if we could not find a valid relocation file
if ( foundFile ) *foundFile = false;
return fileName;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimTools::relocatePathPattern( const QString& originalPattern, const QString& currentProjectPath, const QString& previousProjectPath )
{
auto previousProjectPathStd = previousProjectPath.toStdString();
auto currentProjectPathStd = currentProjectPath.toStdString();
auto originalPatternStd = originalPattern.toStdString();
size_t equalIndex = 0;
for ( size_t i = 0; i < std::min( previousProjectPathStd.size(), originalPatternStd.size() ); ++i )
{
if ( originalPatternStd[i] != previousProjectPathStd[i] )
{
equalIndex = i;
break;
}
}
if ( equalIndex > 0 )
{
auto theRest = previousProjectPathStd.substr( equalIndex );
// remove theRest from the back of newProjectPath
auto newProjectPathWithoutTheRest = currentProjectPathStd.substr( 0, currentProjectPathStd.size() - theRest.size() );
auto newPattern = newProjectPathWithoutTheRest + originalPatternStd.substr( equalIndex );
return QString::fromStdString( newPattern );
}
return originalPattern;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -52,11 +52,8 @@ class RimTools
public:
static QString getCacheRootDirectoryPathFromProject();
static QString relocateFile( const QString& originalFileName,
const QString& currentProjectPath,
const QString& previousProjectPath,
bool* foundFile,
std::vector<QString>* searchedPaths );
static QString relocateFile( const QString& originalFileName, const QString& currentProjectPath, const QString& previousProjectPath );
static QString relocatePathPattern( const QString& originalPattern, const QString& currentProjectPath, const QString& previousProjectPath );
static void wellPathOptionItemsSubset( const std::vector<RimWellPath*>& wellPathsToExclude, QList<caf::PdmOptionItemInfo>* options );
static void wellPathOptionItems( QList<caf::PdmOptionItemInfo>* options );
@@ -110,6 +110,7 @@ set(SOURCE_UNITTEST_FILES
${CMAKE_CURRENT_LIST_DIR}/RifVtkSurfaceImporter-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaKeyValueStore-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaEnsembleImportTools-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RimTools-Test.cpp
)
if(RESINSIGHT_ENABLE_GRPC)
@@ -0,0 +1,20 @@
#include "gtest/gtest.h"
#include "RimTools.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RimToolsTest, ReplacePathPattern )
{
QString previousProjectPath = "C:/path/to/projectFileFolder";
QString realization = "realization-*/MY_CASE-*";
QString originalPattern = "C:/path/to/" + realization;
QString newProjectPath = "/mnt/new/path/to/projectFileFolder";
QString expectedPattern = "/mnt/new/path/to/" + realization;
QString resultPattern = RimTools::relocatePathPattern( originalPattern, newProjectPath, previousProjectPath );
EXPECT_EQ( resultPattern, expectedPattern );
}