From fa3cc810d87295f92d535f085e29f25fad8441f2 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Wed, 9 Oct 2013 08:07:02 +0200 Subject: [PATCH 1/4] Updated GRIDUNIT keyword --- opm/parser/share/keywords/G/GRIDUNIT | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opm/parser/share/keywords/G/GRIDUNIT b/opm/parser/share/keywords/G/GRIDUNIT index 718cb845d..8209d6b19 100644 --- a/opm/parser/share/keywords/G/GRIDUNIT +++ b/opm/parser/share/keywords/G/GRIDUNIT @@ -1,3 +1,3 @@ {"name" : "GRIDUNIT" , "size" : 1, "items" : - [{"name" : "LengthUnit" , "value_type" : "STRING" , "default" : "METRES"}] -} \ No newline at end of file + [{"name" : "LengthUnit" , "value_type" : "STRING" , "default" : "METRES"}, + {"name" : "MAP" , "value_type" : "STRING"}]} From b63244228dbc019749c9e537bbe2ccb477f51d73 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Wed, 9 Oct 2013 08:22:43 +0200 Subject: [PATCH 2/4] Added Parser::dropKeyword() --- opm/parser/eclipse/Parser/Parser.cpp | 7 +++++++ opm/parser/eclipse/Parser/Parser.hpp | 1 + opm/parser/eclipse/Parser/tests/ParserTests.cpp | 8 ++++++++ opm/parser/share/keywords/M/MAPUNITS | 3 +++ 4 files changed, 19 insertions(+) create mode 100644 opm/parser/share/keywords/M/MAPUNITS diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index f4b6e0eac..1f5bdcbd6 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -72,6 +72,13 @@ namespace Opm { return m_parserKeywords.find(keyword) != m_parserKeywords.end(); } + bool Parser::dropKeyword(const std::string& keyword) { + if (m_parserKeywords.erase( keyword ) == 1) + return true; + else + return false; + } + ParserKeywordConstPtr Parser::getKeyword(const std::string& keyword) const { if (hasKeyword(keyword)) { return m_parserKeywords.at(keyword); diff --git a/opm/parser/eclipse/Parser/Parser.hpp b/opm/parser/eclipse/Parser/Parser.hpp index 23f2ed43a..116a5a37f 100644 --- a/opm/parser/eclipse/Parser/Parser.hpp +++ b/opm/parser/eclipse/Parser/Parser.hpp @@ -47,6 +47,7 @@ namespace Opm { /// Method to add ParserKeyword instances, these holding type and size information about the keywords and their data. void addKeyword(ParserKeywordConstPtr parserKeyword); bool hasKeyword(const std::string& keyword) const; + bool dropKeyword(const std::string& keyword); ParserKeywordConstPtr getKeyword(const std::string& keyword) const; void loadKeywords(const Json::JsonObject& jsonKeywords); diff --git a/opm/parser/eclipse/Parser/tests/ParserTests.cpp b/opm/parser/eclipse/Parser/tests/ParserTests.cpp index 0be2a6e26..91cf3cc64 100644 --- a/opm/parser/eclipse/Parser/tests/ParserTests.cpp +++ b/opm/parser/eclipse/Parser/tests/ParserTests.cpp @@ -216,6 +216,14 @@ BOOST_AUTO_TEST_CASE(loadConfigFromDirectory_default) { } +BOOST_AUTO_TEST_CASE(DropKeyword) { + ParserPtr parser(new Parser()); + BOOST_CHECK_EQUAL(false , parser->dropKeyword("DoesNotHaveThis")); + BOOST_CHECK_EQUAL(true , parser->dropKeyword("BPR")); + BOOST_CHECK_EQUAL(false , parser->dropKeyword("BPR")); +} + + /***************** Simple Int parsing ********************************/ ParserKeywordPtr setupParserKeywordInt(std::string name, int numberOfItems) { diff --git a/opm/parser/share/keywords/M/MAPUNITS b/opm/parser/share/keywords/M/MAPUNITS new file mode 100644 index 000000000..7946114c3 --- /dev/null +++ b/opm/parser/share/keywords/M/MAPUNITS @@ -0,0 +1,3 @@ +{"name" : "MAPUNITS" , "size" : 1 , "items" : [ + {"name" : "UNIT" , "value_type" : "STRING" , "default" : "METRES"}]} + From 2b118a660cd7fa3d21960d6c36d4a14dc76ac7d9 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Wed, 9 Oct 2013 17:01:47 +0200 Subject: [PATCH 3/4] Added test for parsing of stringdata with space --- opm/parser/eclipse/Deck/tests/DeckRecordTests.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/opm/parser/eclipse/Deck/tests/DeckRecordTests.cpp b/opm/parser/eclipse/Deck/tests/DeckRecordTests.cpp index a43de03f0..8f53a77d5 100644 --- a/opm/parser/eclipse/Deck/tests/DeckRecordTests.cpp +++ b/opm/parser/eclipse/Deck/tests/DeckRecordTests.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include using namespace Opm; @@ -112,4 +114,14 @@ BOOST_AUTO_TEST_CASE(get_oneoftwo_returnscorrectitem) { } +BOOST_AUTO_TEST_CASE(StringsWithSpaceOK) { + ParserStringItemPtr itemString(new ParserStringItem(std::string("STRINGITEM1"))); + ParserRecordPtr record1(new ParserRecord()); + RawRecordPtr rawRecord(new Opm::RawRecord(" ' VALUE ' /")); + record1->addItem( itemString ); + + + DeckRecordConstPtr deckRecord = record1->parse( rawRecord ); + BOOST_CHECK_EQUAL(" VALUE " , deckRecord->getItem(0)->getString(0)); +} From d5b6e84f180e5384d2ee47473df7202eeb0d737c Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Wed, 9 Oct 2013 17:04:08 +0200 Subject: [PATCH 4/4] Fixed bug with string items with embedded space - using an ugly template specialization hack --- opm/parser/eclipse/Parser/ParserIntItem.cpp | 1 + opm/parser/eclipse/Parser/ParserItem.cpp | 4 ++ .../eclipse/Parser/ParserItemTemplate.hpp | 44 ++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/opm/parser/eclipse/Parser/ParserIntItem.cpp b/opm/parser/eclipse/Parser/ParserIntItem.cpp index ab269c84f..0ceb2a990 100644 --- a/opm/parser/eclipse/Parser/ParserIntItem.cpp +++ b/opm/parser/eclipse/Parser/ParserIntItem.cpp @@ -27,6 +27,7 @@ namespace Opm { + ParserIntItem::ParserIntItem(const std::string& itemName) : ParserItem(itemName) { m_default = defaultInt(); } diff --git a/opm/parser/eclipse/Parser/ParserItem.cpp b/opm/parser/eclipse/Parser/ParserItem.cpp index 9071fa3db..f911dbb84 100644 --- a/opm/parser/eclipse/Parser/ParserItem.cpp +++ b/opm/parser/eclipse/Parser/ParserItem.cpp @@ -27,6 +27,10 @@ namespace Opm { + template<> void ParserItem::fillVectorFromStringStream(std::istringstream& inputStream , std::string& token , std::deque& dataVector) const { + dataVector.push_back(token); + } + ParserItem::ParserItem(const std::string& itemName, ParserItemSizeEnum sizeType) { m_name.assign(itemName); m_sizeType = sizeType; diff --git a/opm/parser/eclipse/Parser/ParserItemTemplate.hpp b/opm/parser/eclipse/Parser/ParserItemTemplate.hpp index 1e8425496..d3b3a2004 100644 --- a/opm/parser/eclipse/Parser/ParserItemTemplate.hpp +++ b/opm/parser/eclipse/Parser/ParserItemTemplate.hpp @@ -17,14 +17,39 @@ along with OPM. If not, see . */ + +/* + Pushing the converted values onto the dataVector is in this seperate + function to be able to specialize the implementation for type + std::string. The problem is that in the code: + + std::istringstream inputStream(" WITH_SPACE "); + inputStream >> stringValue; + + The leading and trailing spaces will be stripped from the final + stringValue (have tried manipulating the skipws flag to no + avail). To avoid this a specialized + fillVectorFromStringStream implementation is in + ParserItem.cpp. +*/ + +template void fillVectorFromStringStream(std::istringstream& inputStream , std::string& token , std::deque& dataVector) const { + T value; + inputStream >> value; + dataVector.push_back(value); + + inputStream.get(); + if (!inputStream.eof()) + throw std::invalid_argument("Spurious data at the end of: <" + token + ">"); +} + + template void fillVectorFromStringToken(std::string token, std::deque& dataVector, T defaultValue, bool& defaultActive) const { std::istringstream inputStream(token); size_t starPos = token.find('*'); - T value; bool hasStar = (starPos != std::string::npos); defaultActive = false; - if (hasStar) { bool singleDefault = (starPos == 0); @@ -37,6 +62,7 @@ template void fillVectorFromStringToken(std::string token, std::deque> multiplier; starChar = inputStream.get(); @@ -49,20 +75,16 @@ template void fillVectorFromStringToken(std::string token, std::deque> value; - + for (size_t i = 0; i < multiplier; i++) dataVector.push_back(value); } - } else { - inputStream >> value; - dataVector.push_back(value); - } - - inputStream.get(); - if (!inputStream.eof()) - throw std::invalid_argument("Spurious data at the end of: <" + token + ">"); + } else + fillVectorFromStringStream(inputStream , token , dataVector); } + + template std::deque readFromRawRecord(RawRecordPtr rawRecord, bool scanAll, T defaultValue, bool& defaultActive) const { std::deque data; if (rawRecord->size() == 0) {