mirror of
https://github.com/OPM/ResInsight.git
synced 2025-01-08 23:23:01 -06:00
Moved relocateFile to RimTools
This commit is contained in:
parent
cd27ae2462
commit
124585b96a
@ -49,163 +49,6 @@ RimCase::~RimCase()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Relocate the supplied file name, based on the search path as follows:
|
||||
/// fileName, newProjectPath/fileNameWoPath, relocatedPath/fileNameWoPath
|
||||
/// If the file is not found in any of the positions, the fileName is returned but converted to Qt Style path separators: "/"
|
||||
///
|
||||
/// The relocatedPath is found in this way:
|
||||
/// use the start of newProjectPath
|
||||
/// plus the end of the path to m_gridFileName
|
||||
/// 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 RimCase::relocateFile(const QString& orgFileName, const QString& orgNewProjectPath, const QString& orgOldProjectPath,
|
||||
bool* foundFile, std::vector<QString>* searchedPaths)
|
||||
{
|
||||
if (foundFile) *foundFile = true;
|
||||
|
||||
// Make sure we have a Qt formatted path ( using "/" not "\")
|
||||
QString fileName = QDir::fromNativeSeparators(orgFileName);
|
||||
QString newProjectPath = QDir::fromNativeSeparators(orgNewProjectPath);
|
||||
QString oldProjectPath = QDir::fromNativeSeparators(orgOldProjectPath);
|
||||
|
||||
// If we from a file or whatever gets a real windows path on linux, we need to manually convert it
|
||||
// because Qt will not. QDir::fromNativeSeparators does nothing on linux.
|
||||
|
||||
bool isWindowsPath = false;
|
||||
if (orgFileName.count("/")) isWindowsPath = false; // "/" are not allowed in a windows path
|
||||
else if (orgFileName.count("\\")
|
||||
&& !QFile::exists(orgFileName)) // To make sure we do not convert single linux files containing "\"
|
||||
{
|
||||
isWindowsPath = true;
|
||||
}
|
||||
|
||||
if (isWindowsPath)
|
||||
{
|
||||
// Windows absolute path detected. transform.
|
||||
fileName.replace(QString("\\"), QString("/"));
|
||||
}
|
||||
|
||||
if (searchedPaths) searchedPaths->push_back(fileName);
|
||||
if (QFile::exists(fileName))
|
||||
{
|
||||
return fileName;
|
||||
}
|
||||
|
||||
// First check in the new project file directory
|
||||
{
|
||||
QString fileNameWithoutPath = QFileInfo(fileName).fileName();
|
||||
QString candidate = QDir::fromNativeSeparators(newProjectPath + QDir::separator() + fileNameWithoutPath);
|
||||
if (searchedPaths) searchedPaths->push_back(candidate);
|
||||
|
||||
if (QFile::exists(candidate))
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
// Then find the possible move of a directory structure where projects and files referenced are moved in "paralell"
|
||||
|
||||
QFileInfo gridFileInfo(QDir::fromNativeSeparators(fileName));
|
||||
QString gridFilePath = gridFileInfo.path();
|
||||
QString gridFileNameWoPath = gridFileInfo.fileName();
|
||||
QStringList gridPathElements = gridFilePath.split("/", QString::KeepEmptyParts);
|
||||
|
||||
QString oldProjPath = QDir::fromNativeSeparators(oldProjectPath);
|
||||
QStringList oldProjPathElements = oldProjPath.split("/", QString::KeepEmptyParts);
|
||||
|
||||
QString newProjPath = QDir::fromNativeSeparators(newProjectPath);
|
||||
QStringList newProjPathElements = newProjPath.split("/", QString::KeepEmptyParts);
|
||||
|
||||
// Find the possible equal start of the old project path, and the referenced file
|
||||
|
||||
bool pathStartsAreEqual = false;
|
||||
bool pathEndsDiffer = false;
|
||||
int firstDiffIdx = 0;
|
||||
for ( firstDiffIdx = 0; firstDiffIdx < gridPathElements.size() && firstDiffIdx < oldProjPathElements.size(); ++firstDiffIdx)
|
||||
{
|
||||
if (gridPathElements[firstDiffIdx] == oldProjPathElements[firstDiffIdx])
|
||||
{
|
||||
pathStartsAreEqual = pathStartsAreEqual || !gridPathElements[firstDiffIdx].isEmpty();
|
||||
}
|
||||
else
|
||||
{
|
||||
pathEndsDiffer = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!pathEndsDiffer && firstDiffIdx < gridPathElements.size() || firstDiffIdx < oldProjPathElements.size())
|
||||
{
|
||||
pathEndsDiffer = true;
|
||||
}
|
||||
|
||||
// If the path starts are equal, try to substitute it in the referenced file, with the corresponding new project path start
|
||||
|
||||
if (pathStartsAreEqual)
|
||||
{
|
||||
if (pathEndsDiffer)
|
||||
{
|
||||
QString oldGridFilePathEnd;
|
||||
for (int i = firstDiffIdx; i < gridPathElements.size(); ++i)
|
||||
{
|
||||
oldGridFilePathEnd += gridPathElements[i];
|
||||
oldGridFilePathEnd += "/";
|
||||
}
|
||||
|
||||
// Find the new Project File Start Path
|
||||
|
||||
QStringList oldProjectFilePathEndElements;
|
||||
for (int i = firstDiffIdx; i < oldProjPathElements.size(); ++i)
|
||||
{
|
||||
oldProjectFilePathEndElements.push_back(oldProjPathElements[i]);
|
||||
}
|
||||
|
||||
int ppIdx = oldProjectFilePathEndElements.size() -1;
|
||||
int lastDiffIdx = newProjPathElements.size() -1;
|
||||
|
||||
for (; lastDiffIdx >= 0 && ppIdx >= 0; --lastDiffIdx, --ppIdx)
|
||||
{
|
||||
if (oldProjectFilePathEndElements[ppIdx] != newProjPathElements[lastDiffIdx])
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
QString newProjecetFileStartPath;
|
||||
for (int i = 0; i <= lastDiffIdx; ++i)
|
||||
{
|
||||
newProjecetFileStartPath += newProjPathElements[i];
|
||||
newProjecetFileStartPath += "/";
|
||||
}
|
||||
|
||||
QString relocationPath = newProjecetFileStartPath + oldGridFilePathEnd;
|
||||
|
||||
QString relocatedFileName = relocationPath + gridFileNameWoPath;
|
||||
|
||||
if (searchedPaths) searchedPaths->push_back(relocatedFileName);
|
||||
|
||||
if (QFile::exists(relocatedFileName))
|
||||
{
|
||||
return relocatedFileName;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// The Grid file was located in the same dir as the Project file. This is supposed to be handled above.
|
||||
// So we did not find it
|
||||
}
|
||||
}
|
||||
|
||||
// return the unchanged filename, if we could not find a valid relocation file
|
||||
if (foundFile) *foundFile = false;
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -55,9 +55,6 @@ public:
|
||||
|
||||
virtual cvf::Vec3d displayModelOffset() const;
|
||||
|
||||
protected:
|
||||
static QString relocateFile(const QString& fileName, const QString& newProjectPath, const QString& oldProjectPath,
|
||||
bool* foundFile, std::vector<QString>* searchedPaths);
|
||||
private:
|
||||
virtual caf::PdmFieldHandle* userDescriptionField() override { return &caseUserDescription; }
|
||||
};
|
||||
|
@ -21,17 +21,21 @@
|
||||
#include "RimEclipseInputCase.h"
|
||||
|
||||
#include "RiaPreferences.h"
|
||||
|
||||
#include "RifEclipseInputFileTools.h"
|
||||
#include "RifReaderEclipseInput.h"
|
||||
#include "RifReaderInterface.h"
|
||||
#include "RifReaderMockModel.h"
|
||||
#include "RifReaderSettings.h"
|
||||
|
||||
#include "RigCaseCellResultsData.h"
|
||||
#include "RigCaseData.h"
|
||||
#include "RimDefines.h"
|
||||
|
||||
#include "RimEclipseInputProperty.h"
|
||||
#include "RimEclipseInputPropertyCollection.h"
|
||||
#include "RimReservoirCellResultsStorage.h"
|
||||
#include "RimTools.h"
|
||||
|
||||
#include "cafProgressInfo.h"
|
||||
|
||||
@ -394,10 +398,10 @@ void RimEclipseInputCase::updateFilePathsFromProjectPath(const QString& newProje
|
||||
bool foundFile = false;
|
||||
std::vector<QString> searchedPaths;
|
||||
|
||||
m_gridFileName = relocateFile(m_gridFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
m_gridFileName = RimTools::relocateFile(m_gridFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
|
||||
for (size_t i = 0; i < m_additionalFileNames().size(); i++)
|
||||
{
|
||||
m_additionalFileNames.v()[i] = relocateFile(m_additionalFileNames()[i], newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
m_additionalFileNames.v()[i] = RimTools::relocateFile(m_additionalFileNames()[i], newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
}
|
||||
}
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "RimEclipseInputProperty.h"
|
||||
#include "RimEclipseInputPropertyCollection.h"
|
||||
#include "RimReservoirCellResultsStorage.h"
|
||||
#include "RimTools.h"
|
||||
|
||||
#include "cafProgressInfo.h"
|
||||
|
||||
@ -100,7 +101,7 @@ void RimEclipseInputCaseOpm::updateFilePathsFromProjectPath(const QString& newPr
|
||||
bool foundFile = false;
|
||||
std::vector<QString> searchedPaths;
|
||||
|
||||
m_gridFileName = relocateFile(m_gridFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
m_gridFileName = RimTools::relocateFile(m_gridFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -21,15 +21,19 @@
|
||||
#include "RimEclipseResultCase.h"
|
||||
|
||||
#include "RiaPreferences.h"
|
||||
|
||||
#include "RifEclipseOutputFileTools.h"
|
||||
#include "RifReaderEclipseOutput.h"
|
||||
#include "RifReaderMockModel.h"
|
||||
#include "RifReaderSettings.h"
|
||||
|
||||
#include "RigCaseCellResultsData.h"
|
||||
#include "RigCaseData.h"
|
||||
|
||||
#include "RimMockModelSettings.h"
|
||||
#include "RimProject.h"
|
||||
#include "RimReservoirCellResultsStorage.h"
|
||||
#include "RimTools.h"
|
||||
|
||||
#include "cafPdmSettings.h"
|
||||
#include "cafPdmUiPropertyViewDialog.h"
|
||||
@ -322,7 +326,7 @@ void RimEclipseResultCase::updateFilePathsFromProjectPath(const QString& newProj
|
||||
std::vector<QString> searchedPaths;
|
||||
|
||||
// Update filename and folder paths when opening project from a different file location
|
||||
caseFileName = relocateFile(caseFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
caseFileName = RimTools::relocateFile(caseFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
|
||||
#if 0 // Output the search path for debugging
|
||||
for (size_t i = 0; i < searchedPaths.size(); ++i)
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "RimGeoMechView.h"
|
||||
#include "RimMainPlotCollection.h"
|
||||
#include "RimProject.h"
|
||||
#include "RimTools.h"
|
||||
#include "RimWellLogPlotCollection.h"
|
||||
|
||||
#include <QFile>
|
||||
@ -126,7 +127,7 @@ void RimGeoMechCase::updateFilePathsFromProjectPath(const QString& newProjectPat
|
||||
std::vector<QString> searchedPaths;
|
||||
|
||||
// Update filename and folder paths when opening project from a different file location
|
||||
m_caseFileName = relocateFile(m_caseFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
m_caseFileName = RimTools::relocateFile(m_caseFileName(), newProjectPath, oldProjectPath, &foundFile, &searchedPaths);
|
||||
|
||||
#if 0 // Output the search path for debugging
|
||||
for (size_t i = 0; i < searchedPaths.size(); ++i)
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include "RimProject.h"
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QDir>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
@ -44,3 +45,158 @@ QString RimTools::getCacheRootDirectoryPathFromProject()
|
||||
|
||||
return cacheRootFolderPath;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Relocate the supplied file name, based on the search path as follows:
|
||||
/// fileName, newProjectPath/fileNameWoPath, relocatedPath/fileNameWoPath
|
||||
/// If the file is not found in any of the positions, the fileName is returned but converted to Qt Style path separators: "/"
|
||||
///
|
||||
/// The relocatedPath is found in this way:
|
||||
/// use the start of newProjectPath
|
||||
/// plus the end of the path to m_gridFileName
|
||||
/// 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& orgFileName, const QString& orgNewProjectPath, const QString& orgOldProjectPath,
|
||||
bool* foundFile, std::vector<QString>* searchedPaths)
|
||||
{
|
||||
if (foundFile) *foundFile = true;
|
||||
|
||||
// Make sure we have a Qt formatted path ( using "/" not "\")
|
||||
QString fileName = QDir::fromNativeSeparators(orgFileName);
|
||||
QString newProjectPath = QDir::fromNativeSeparators(orgNewProjectPath);
|
||||
QString oldProjectPath = QDir::fromNativeSeparators(orgOldProjectPath);
|
||||
|
||||
// If we from a file or whatever gets a real windows path on linux, we need to manually convert it
|
||||
// because Qt will not. QDir::fromNativeSeparators does nothing on linux.
|
||||
|
||||
bool isWindowsPath = false;
|
||||
if (orgFileName.count("/")) isWindowsPath = false; // "/" are not allowed in a windows path
|
||||
else if (orgFileName.count("\\")
|
||||
&& !QFile::exists(orgFileName)) // To make sure we do not convert single linux files containing "\"
|
||||
{
|
||||
isWindowsPath = true;
|
||||
}
|
||||
|
||||
if (isWindowsPath)
|
||||
{
|
||||
// Windows absolute path detected. transform.
|
||||
fileName.replace(QString("\\"), QString("/"));
|
||||
}
|
||||
|
||||
if (searchedPaths) searchedPaths->push_back(fileName);
|
||||
if (QFile::exists(fileName))
|
||||
{
|
||||
return fileName;
|
||||
}
|
||||
|
||||
// First check in the new project file directory
|
||||
{
|
||||
QString fileNameWithoutPath = QFileInfo(fileName).fileName();
|
||||
QString candidate = QDir::fromNativeSeparators(newProjectPath + QDir::separator() + fileNameWithoutPath);
|
||||
if (searchedPaths) searchedPaths->push_back(candidate);
|
||||
|
||||
if (QFile::exists(candidate))
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
// Then find the possible move of a directory structure where projects and files referenced are moved in "paralell"
|
||||
|
||||
QFileInfo gridFileInfo(QDir::fromNativeSeparators(fileName));
|
||||
QString gridFilePath = gridFileInfo.path();
|
||||
QString gridFileNameWoPath = gridFileInfo.fileName();
|
||||
QStringList gridPathElements = gridFilePath.split("/", QString::KeepEmptyParts);
|
||||
|
||||
QString oldProjPath = QDir::fromNativeSeparators(oldProjectPath);
|
||||
QStringList oldProjPathElements = oldProjPath.split("/", QString::KeepEmptyParts);
|
||||
|
||||
QString newProjPath = QDir::fromNativeSeparators(newProjectPath);
|
||||
QStringList newProjPathElements = newProjPath.split("/", QString::KeepEmptyParts);
|
||||
|
||||
// Find the possible equal start of the old project path, and the referenced file
|
||||
|
||||
bool pathStartsAreEqual = false;
|
||||
bool pathEndsDiffer = false;
|
||||
int firstDiffIdx = 0;
|
||||
for (firstDiffIdx = 0; firstDiffIdx < gridPathElements.size() && firstDiffIdx < oldProjPathElements.size(); ++firstDiffIdx)
|
||||
{
|
||||
if (gridPathElements[firstDiffIdx] == oldProjPathElements[firstDiffIdx])
|
||||
{
|
||||
pathStartsAreEqual = pathStartsAreEqual || !gridPathElements[firstDiffIdx].isEmpty();
|
||||
}
|
||||
else
|
||||
{
|
||||
pathEndsDiffer = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!pathEndsDiffer && firstDiffIdx < gridPathElements.size() || firstDiffIdx < oldProjPathElements.size())
|
||||
{
|
||||
pathEndsDiffer = true;
|
||||
}
|
||||
|
||||
// If the path starts are equal, try to substitute it in the referenced file, with the corresponding new project path start
|
||||
|
||||
if (pathStartsAreEqual)
|
||||
{
|
||||
if (pathEndsDiffer)
|
||||
{
|
||||
QString oldGridFilePathEnd;
|
||||
for (int i = firstDiffIdx; i < gridPathElements.size(); ++i)
|
||||
{
|
||||
oldGridFilePathEnd += gridPathElements[i];
|
||||
oldGridFilePathEnd += "/";
|
||||
}
|
||||
|
||||
// Find the new Project File Start Path
|
||||
|
||||
QStringList oldProjectFilePathEndElements;
|
||||
for (int i = firstDiffIdx; i < oldProjPathElements.size(); ++i)
|
||||
{
|
||||
oldProjectFilePathEndElements.push_back(oldProjPathElements[i]);
|
||||
}
|
||||
|
||||
int ppIdx = oldProjectFilePathEndElements.size() - 1;
|
||||
int lastDiffIdx = newProjPathElements.size() - 1;
|
||||
|
||||
for (; lastDiffIdx >= 0 && ppIdx >= 0; --lastDiffIdx, --ppIdx)
|
||||
{
|
||||
if (oldProjectFilePathEndElements[ppIdx] != newProjPathElements[lastDiffIdx])
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
QString newProjecetFileStartPath;
|
||||
for (int i = 0; i <= lastDiffIdx; ++i)
|
||||
{
|
||||
newProjecetFileStartPath += newProjPathElements[i];
|
||||
newProjecetFileStartPath += "/";
|
||||
}
|
||||
|
||||
QString relocationPath = newProjecetFileStartPath + oldGridFilePathEnd;
|
||||
|
||||
QString relocatedFileName = relocationPath + gridFileNameWoPath;
|
||||
|
||||
if (searchedPaths) searchedPaths->push_back(relocatedFileName);
|
||||
|
||||
if (QFile::exists(relocatedFileName))
|
||||
{
|
||||
return relocatedFileName;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// The Grid file was located in the same dir as the Project file. This is supposed to be handled above.
|
||||
// So we did not find it
|
||||
}
|
||||
}
|
||||
|
||||
// return the unchanged filename, if we could not find a valid relocation file
|
||||
if (foundFile) *foundFile = false;
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
@ -22,8 +22,12 @@
|
||||
|
||||
#include <QString>
|
||||
|
||||
#include <vector>
|
||||
|
||||
class RimTools
|
||||
{
|
||||
public:
|
||||
static QString getCacheRootDirectoryPathFromProject();
|
||||
|
||||
static QString relocateFile(const QString& fileName, const QString& newProjectPath, const QString& oldProjectPath, bool* foundFile, std::vector<QString>* searchedPaths);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user