From 7bc3c79301f76253a2116b6fb43dec160eba1f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Kvalsvik?= Date: Mon, 22 Feb 2016 09:56:46 +0100 Subject: [PATCH] PATHS keyword has global scope. ECLIPSE resolves path aliases defined in an included file globally, i.e. / |> include.inc |-- PATHS 'alias' 'path/to/dir' |> main.data |-- INCLUDE 'include.inc' / | [...] |-- INCLUDE '$alias/file.inc' / will resolve as 'path/to/dir/file.inc'. This behaviour is now adopted by opm-parser. --- opm/parser/eclipse/Parser/Parser.cpp | 20 +++++++++++-------- .../eclipse/Parser/tests/ParserTests.cpp | 6 ++++++ testdata/parser/PATHSInInclude.data | 7 +++++++ testdata/parser/PATHSInIncludeInvalid.data | 7 +++++++ testdata/parser/include/foo.inc | 3 +++ testdata/parser/include/foo_invalid.inc | 3 +++ .../parser/include/foopath/foo_target.inc | 1 + 7 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 testdata/parser/PATHSInInclude.data create mode 100644 testdata/parser/PATHSInIncludeInvalid.data create mode 100644 testdata/parser/include/foo.inc create mode 100644 testdata/parser/include/foo_invalid.inc create mode 100644 testdata/parser/include/foopath/foo_target.inc diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index 2f309ecb7..e697ed866 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -50,7 +50,11 @@ namespace Opm { Deck * deck; boost::filesystem::path dataFile; boost::filesystem::path rootPath; - std::map pathMap; + /* the path map is shared between all state instances, as it has global + * resolution, i.e. PATHS specified in an included file can be used + * from the file that included it. + */ + std::shared_ptr< std::map > pathMap; size_t lineNR; std::shared_ptr inputstream; RawKeywordPtr rawKeyword; @@ -66,11 +70,11 @@ namespace Opm { } ParserState(const ParseMode& __parseMode) - : parseMode( __parseMode ) - { - deck = new Deck(); - lineNR = 0; - } + : parseMode( __parseMode ), + deck( new Deck() ), + pathMap( std::make_shared< std::map< std::string, std::string > >() ), + lineNR( 0 ) + {} std::shared_ptr includeState(boost::filesystem::path& filename) { std::shared_ptr childState = std::make_shared( *this ); @@ -149,7 +153,7 @@ namespace Opm { std::string stringStartingAtPathName = path.substr(positionOfPathName+1); size_t cutOffPosition = stringStartingAtPathName.find_first_not_of(validPathNameCharacters); std::string stringToFind = stringStartingAtPathName.substr(0, cutOffPosition); - std::string stringToReplace = parserState.pathMap[stringToFind]; + std::string stringToReplace = parserState.pathMap->at(stringToFind); boost::replace_all(path, pathKeywordPrefix + stringToFind, stringToReplace); } @@ -386,7 +390,7 @@ bool Parser::parseState(std::shared_ptr parserState) const { RawRecordConstPtr record = parserState->rawKeyword->getRecord(i); std::string pathName = readValueToken(record->getItem(0)); std::string pathValue = readValueToken(record->getItem(1)); - parserState->pathMap.insert(std::pair(pathName, pathValue)); + parserState->pathMap->insert(std::pair(pathName, pathValue)); } } else if (parserState->rawKeyword->getKeywordName() == Opm::RawConsts::include) { diff --git a/opm/parser/eclipse/Parser/tests/ParserTests.cpp b/opm/parser/eclipse/Parser/tests/ParserTests.cpp index 97de9d8dd..3be4e77d9 100644 --- a/opm/parser/eclipse/Parser/tests/ParserTests.cpp +++ b/opm/parser/eclipse/Parser/tests/ParserTests.cpp @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -302,3 +303,8 @@ BOOST_AUTO_TEST_CASE( quoted_comments ) { BOOST_CHECK_EQUAL( Parser::stripComments("ABC'--'DEF'--GHI") , "ABC'--'DEF'--GHI"); } +BOOST_AUTO_TEST_CASE( PATHS_has_global_scope ) { + Parser parser; + parser.newDeckFromFile( "testdata/parser/PATHSInInclude.data", ParseMode() ); + BOOST_CHECK_THROW( parser.newDeckFromFile( "testdata/parser/PATHSInIncludeInvalid.data", ParseMode() ), std::runtime_error ); +} diff --git a/testdata/parser/PATHSInInclude.data b/testdata/parser/PATHSInInclude.data new file mode 100644 index 000000000..013a00d4e --- /dev/null +++ b/testdata/parser/PATHSInInclude.data @@ -0,0 +1,7 @@ +INCLUDE + 'include/foo.inc' +/ + +INCLUDE + '$FOOPATH/foo_target.inc' +/ diff --git a/testdata/parser/PATHSInIncludeInvalid.data b/testdata/parser/PATHSInIncludeInvalid.data new file mode 100644 index 000000000..7d2817b9c --- /dev/null +++ b/testdata/parser/PATHSInIncludeInvalid.data @@ -0,0 +1,7 @@ +INCLUDE + 'include/foo_invalid.inc' +/ + +INCLUDE + '$FOOPATH/foo_target.inc' +/ diff --git a/testdata/parser/include/foo.inc b/testdata/parser/include/foo.inc new file mode 100644 index 000000000..057c9a77f --- /dev/null +++ b/testdata/parser/include/foo.inc @@ -0,0 +1,3 @@ +PATHS + 'FOOPATH' 'include/foopath' / +/ diff --git a/testdata/parser/include/foo_invalid.inc b/testdata/parser/include/foo_invalid.inc new file mode 100644 index 000000000..c010f0c2e --- /dev/null +++ b/testdata/parser/include/foo_invalid.inc @@ -0,0 +1,3 @@ +PATHS + 'FOOPATH' 'include/invalid' / +/ diff --git a/testdata/parser/include/foopath/foo_target.inc b/testdata/parser/include/foopath/foo_target.inc new file mode 100644 index 000000000..c3e31e44a --- /dev/null +++ b/testdata/parser/include/foopath/foo_target.inc @@ -0,0 +1 @@ +OIL