From edbeb4dfb1cf50984a21093ea1adac0e3dbf942e Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Thu, 16 Feb 2023 13:04:16 +0100 Subject: [PATCH] Fix invalid parsing of line with keyword end tag and comment * Add error text when size differs * #9845: Remove invalid parsing of data in comment line The line '/ -- 123 123' was parsed incorrectly, and values 123 123 was added to the list of valid values for the keyword. --- .../RifEclipseInputFileTools.cpp | 6 ++++- .../RifEclipseInputPropertyLoader.cpp | 25 +++++++++++++++---- .../RifEclipseInputPropertyLoader.h | 6 +++-- .../RifEclipseTextFileReader.cpp | 10 ++++++-- .../RifEclipseTextFileReader-Test.cpp | 11 ++++++-- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/ApplicationLibCode/FileInterface/RifEclipseInputFileTools.cpp b/ApplicationLibCode/FileInterface/RifEclipseInputFileTools.cpp index e61adfb5f7..a949745dde 100644 --- a/ApplicationLibCode/FileInterface/RifEclipseInputFileTools.cpp +++ b/ApplicationLibCode/FileInterface/RifEclipseInputFileTools.cpp @@ -193,8 +193,12 @@ bool RifEclipseInputFileTools::openGridFile( const QString& fileName, ecl_grid_free( inputGrid ); + QString errorMessages; + // Import additional keywords as input properties - RifEclipseInputPropertyLoader::createInputPropertiesFromKeywords( eclipseCase, objects ); + RifEclipseInputPropertyLoader::createInputPropertiesFromKeywords( eclipseCase, objects, &errorMessages ); + + if ( !errorMessages.isEmpty() ) RiaLogging::error( errorMessages ); return true; } diff --git a/ApplicationLibCode/FileInterface/RifEclipseInputPropertyLoader.cpp b/ApplicationLibCode/FileInterface/RifEclipseInputPropertyLoader.cpp index 48b11b4746..9f9602866a 100644 --- a/ApplicationLibCode/FileInterface/RifEclipseInputPropertyLoader.cpp +++ b/ApplicationLibCode/FileInterface/RifEclipseInputPropertyLoader.cpp @@ -40,7 +40,7 @@ QString RifEclipseInputPropertyLoader::evaluateAndCreateInputPropertyResult( Rig QString* errorMessage ) { auto eclipseKeyword = keywordContent.keyword; - if ( isInputPropertyCandidate( eclipseCase, eclipseKeyword, keywordContent.values.size() ) ) + if ( isInputPropertyCandidate( eclipseCase, eclipseKeyword, keywordContent.values.size(), errorMessage ) ) { QString newResultName = eclipseCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL ) ->makeResultNameUnique( QString::fromStdString( eclipseKeyword ) ); @@ -58,11 +58,12 @@ QString RifEclipseInputPropertyLoader::evaluateAndCreateInputPropertyResult( Rig /// //-------------------------------------------------------------------------------------------------- void RifEclipseInputPropertyLoader::createInputPropertiesFromKeywords( RigEclipseCaseData* eclipseCase, - const std::vector& keywordContent ) + const std::vector& keywordContent, + QString* errorText ) { for ( const auto& keywordAndValues : keywordContent ) { - RifEclipseInputPropertyLoader::evaluateAndCreateInputPropertyResult( eclipseCase, keywordAndValues, nullptr ); + RifEclipseInputPropertyLoader::evaluateAndCreateInputPropertyResult( eclipseCase, keywordAndValues, errorText ); } } @@ -71,13 +72,27 @@ void RifEclipseInputPropertyLoader::createInputPropertiesFromKeywords( RigEclips //-------------------------------------------------------------------------------------------------- bool RifEclipseInputPropertyLoader::isInputPropertyCandidate( const RigEclipseCaseData* caseData, const std::string& eclipseKeyword, - size_t numberOfValues ) + size_t numberOfValues, + QString* errorText ) { CVF_ASSERT( caseData ); if ( !isValidDataKeyword( QString::fromStdString( eclipseKeyword ) ) ) return false; - return ( numberOfValues == caseData->mainGrid()->cellCount() ); + if ( numberOfValues != caseData->mainGrid()->cellCount() ) + { + if ( errorText ) + { + *errorText += QString( "Keyword %1 has %2 values, but the grid has %3 cells" ) + .arg( QString::fromStdString( eclipseKeyword ) ) + .arg( numberOfValues ) + .arg( caseData->mainGrid()->cellCount() ); + } + + return false; + } + + return true; } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/FileInterface/RifEclipseInputPropertyLoader.h b/ApplicationLibCode/FileInterface/RifEclipseInputPropertyLoader.h index d178464b14..ad2092f0a0 100644 --- a/ApplicationLibCode/FileInterface/RifEclipseInputPropertyLoader.h +++ b/ApplicationLibCode/FileInterface/RifEclipseInputPropertyLoader.h @@ -42,7 +42,8 @@ class RifEclipseInputPropertyLoader { public: static void createInputPropertiesFromKeywords( RigEclipseCaseData* eclipseCase, - const std::vector& keywordContent ); + const std::vector& keywordContent, + QString* errorText ); // Returns map of assigned resultName and Eclipse Keyword. static std::map readProperties( const QString& fileName, RigEclipseCaseData* eclipseCase ); @@ -56,7 +57,8 @@ private: static bool isInputPropertyCandidate( const RigEclipseCaseData* caseData, const std::string& eclipseKeyword, - size_t numberOfValues ); + size_t numberOfValues, + QString* errorText ); static bool appendNewInputPropertyResult( RigEclipseCaseData* caseData, const QString& resultName, diff --git a/ApplicationLibCode/FileInterface/RifEclipseTextFileReader.cpp b/ApplicationLibCode/FileInterface/RifEclipseTextFileReader.cpp index 5c548606b1..6e98f31cbf 100644 --- a/ApplicationLibCode/FileInterface/RifEclipseTextFileReader.cpp +++ b/ApplicationLibCode/FileInterface/RifEclipseTextFileReader.cpp @@ -147,6 +147,13 @@ std::pair> continue; } + // End of keyword is defined by '/', parse values of line until '/' is found + auto positionOfEndToken = line.find_first_of( '/' ); + if ( positionOfEndToken != std::string::npos ) + { + line = line.substr( 0, positionOfEndToken ); + } + if ( !isEndTokenKeywordRead ) { size_t start = 0; @@ -186,8 +193,7 @@ std::pair> } } - // End of keyword is defined by '/' - if ( line.find_first_of( '/' ) != std::string::npos ) + if ( positionOfEndToken != std::string::npos ) { isEndTokenKeywordRead = true; continue; diff --git a/ApplicationLibCode/UnitTests/RifEclipseTextFileReader-Test.cpp b/ApplicationLibCode/UnitTests/RifEclipseTextFileReader-Test.cpp index f7a56132e6..2bb1888017 100644 --- a/ApplicationLibCode/UnitTests/RifEclipseTextFileReader-Test.cpp +++ b/ApplicationLibCode/UnitTests/RifEclipseTextFileReader-Test.cpp @@ -216,11 +216,14 @@ TEST( RifEclipseTextFileReader, KeywordsWithoutValue ) "/\n" "SOIL\n" "6.0 7.0 8.0 9.0\n" - "/\n"; + "/\n" + "G4\n" + "1.0 2.0 3.0\n" + "0.123/ -- 1213 values in comment 123.123 \n"; auto keywordDataItems = RifEclipseTextFileReader::parseStringData( fileContent ); - EXPECT_EQ( 3u, keywordDataItems.size() ); + EXPECT_EQ( 4u, keywordDataItems.size() ); auto noEchoKeyword = keywordDataItems[0]; EXPECT_EQ( 0u, noEchoKeyword.values.size() ); @@ -233,4 +236,8 @@ TEST( RifEclipseTextFileReader, KeywordsWithoutValue ) EXPECT_FLOAT_EQ( 1.0f, swatKeyword.values[0] ); EXPECT_FLOAT_EQ( 6.0f, soilKeyword.values[0] ); + + auto g4Keyword = keywordDataItems[3]; + EXPECT_EQ( 4u, g4Keyword.values.size() ); + EXPECT_FLOAT_EQ( 0.123f, g4Keyword.values[3] ); }