diff --git a/opm/parser/eclipse/Parser/Parser.cpp b/opm/parser/eclipse/Parser/Parser.cpp index b898aa471..e4e90ea49 100644 --- a/opm/parser/eclipse/Parser/Parser.cpp +++ b/opm/parser/eclipse/Parser/Parser.cpp @@ -27,7 +27,6 @@ namespace Opm { RawDeckPtr Parser::parse(const std::string &path) { Logger::initLogger(); - Logger::setLogLevel(Logger::DEBUG); Logger::info("Starting parsing of file: " + path); RawDeckPtr rawDeck(new RawDeck(RawParserKWsConstPtr(new RawParserKWs()))); rawDeck->readDataIntoDeck(path); diff --git a/opm/parser/eclipse/Parser/Parser.hpp b/opm/parser/eclipse/Parser/Parser.hpp index 49ed63b19..152577cfe 100644 --- a/opm/parser/eclipse/Parser/Parser.hpp +++ b/opm/parser/eclipse/Parser/Parser.hpp @@ -24,7 +24,6 @@ #include #include -#include #include #include diff --git a/opm/parser/eclipse/RawDeck/RawConsts.hpp b/opm/parser/eclipse/RawDeck/RawConsts.hpp new file mode 100644 index 000000000..e362b63c5 --- /dev/null +++ b/opm/parser/eclipse/RawDeck/RawConsts.hpp @@ -0,0 +1,36 @@ +/* + Copyright 2013 Statoil ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OPM is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with OPM. If not, see . + */ + +#ifndef RAWCONSTS_HPP +#define RAWCONSTS_HPP + + +namespace Opm { + namespace RawConsts { + const char slash = '/'; + const char quote = '\''; + const std::string separators = "\t "; + const std::string include = "INCLUDE"; + const unsigned int maxKeywordLength = 8; + } +} + + +#endif /* RAWCONSTS_HPP */ + diff --git a/opm/parser/eclipse/RawDeck/RawDeck.cpp b/opm/parser/eclipse/RawDeck/RawDeck.cpp index 9a87a41e2..fbbe4382e 100644 --- a/opm/parser/eclipse/RawDeck/RawDeck.cpp +++ b/opm/parser/eclipse/RawDeck/RawDeck.cpp @@ -19,6 +19,7 @@ #include #include #include "RawDeck.hpp" +#include "RawConsts.hpp" namespace Opm { @@ -93,7 +94,7 @@ namespace Opm { } void RawDeck::popAndProcessInclude(std::list& keywordList, boost::filesystem::path dataFolderPath) { - if (!keywordList.empty() && keywordList.back()->getKeyword() == "INCLUDE") { + if (!keywordList.empty() && keywordList.back()->getKeyword() == Opm::RawConsts::include) { RawKeywordPtr includeKeyword = keywordList.back(); keywordList.pop_back(); // Don't need the include keyword in the deck. diff --git a/opm/parser/eclipse/RawDeck/RawKeyword.cpp b/opm/parser/eclipse/RawDeck/RawKeyword.cpp index c2746706f..39a69f40e 100644 --- a/opm/parser/eclipse/RawDeck/RawKeyword.cpp +++ b/opm/parser/eclipse/RawDeck/RawKeyword.cpp @@ -20,101 +20,107 @@ #include #include #include "RawKeyword.hpp" +#include "RawConsts.hpp" #include namespace Opm { - RawKeyword::RawKeyword() { + + RawKeyword::RawKeyword() { + } + + RawKeyword::RawKeyword(const std::string& keyword) { + setKeyword(keyword); + } + + bool RawKeyword::tryParseKeyword(const std::string& keywordCandidate, std::string& result) { + result = boost::trim_right_copy(keywordCandidate.substr(0, 8)); + if (isValidKeyword(result)) { + Logger::debug("KEYWORD <" + keywordCandidate + ">"); + return true; + } + return false; + } + + bool RawKeyword::isValidKeyword(const std::string& keywordCandidate) { + + std::string keywordRegex = "^[A-Z][A-Z,0-9]{1,7}$"; + int status; + regex_t regularExpression; + regmatch_t rm; + if (regcomp(®ularExpression, keywordRegex.c_str(), REG_EXTENDED) != 0) { + throw std::runtime_error("Unable to compile regular expression for keyword! Expression: " + keywordRegex); } - RawKeyword::RawKeyword(const std::string& keyword) { - setKeyword(keyword); - } + status = regexec(®ularExpression, keywordCandidate.c_str(), 1, &rm, 0); + regfree(®ularExpression); - bool RawKeyword::tryParseKeyword(const std::string& keywordCandidate, std::string& result) { - result = boost::trim_right_copy(keywordCandidate.substr(0, 8)); - if (isValidKeyword(result)) { - Logger::debug("KEYWORD <" + keywordCandidate + ">"); - return true; - } - return false; + if (status == 0) { + return true; } + return false; + } - bool RawKeyword::isValidKeyword(const std::string& keywordCandidate) { - - std::string keywordRegex = "^[A-Z][A-Z,0-9]{1,7}$"; - int status; - regex_t regularExpression; - regmatch_t rm; - if (regcomp(®ularExpression, keywordRegex.c_str(), REG_EXTENDED) != 0) { - throw std::runtime_error("Unable to compile regular expression for keyword! Expression: " + keywordRegex); - } - - status = regexec(®ularExpression, keywordCandidate.c_str(), 1, &rm, 0); - regfree(®ularExpression); - - if (status == 0) { - return true; - } - return false; + bool RawKeyword::lineContainsData(const std::string& line) { + if (boost::algorithm::trim_left_copy(line).substr(0, 2) == "--") { + Logger::debug("COMMENT LINE <" + line + ">"); + return false; + } else if (boost::algorithm::trim_copy(line).length() == 0) { + Logger::debug("EMPTY LINE <" + line + ">"); + return false; + } else if (lineTerminatesKeyword(line)) { + Logger::debug("END OF RECORD <" + line + ">"); + return false; + } else { + Logger::debug("LOOKS LIKE DATA<" + line + ">"); + return true; } + } - bool RawKeyword::lineContainsData(const std::string& line) { - if (boost::algorithm::trim_left_copy(line).substr(0, 2) == "--") { - Logger::debug("COMMENT LINE <" + line + ">"); - return false; - } else if (boost::algorithm::trim_copy(line).length() == 0) { - Logger::debug("EMPTY LINE <" + line + ">"); - return false; - } else if (lineTerminatesKeyword(line)) { - Logger::debug("END OF RECORD <" + line + ">"); - return false; - } else { - Logger::debug("LOOKS LIKE DATA<" + line + ">"); - return true; - } + bool RawKeyword::lineTerminatesKeyword(const std::string& line) { + std::string firstNonBlank = boost::algorithm::trim_left_copy(line).substr(0, 1); + if (firstNonBlank.size() > (unsigned) 0) { + return firstNonBlank[0] == Opm::RawConsts::slash; } + return false; + } - bool RawKeyword::lineTerminatesKeyword(const std::string& line) { - return boost::algorithm::trim_left_copy(line).substr(0,1) == "/"; + void RawKeyword::setKeyword(const std::string& keyword) { + m_keyword = boost::algorithm::trim_right_copy(keyword); + if (!isValidKeyword(m_keyword)) { + throw std::invalid_argument("Not a valid keyword:" + keyword); + } else if (m_keyword.size() > Opm::RawConsts::maxKeywordLength) { + throw std::invalid_argument("Too long keyword:" + keyword); + } else if (boost::algorithm::trim_left_copy(m_keyword) != m_keyword) { + throw std::invalid_argument("Illegal whitespace start of keyword:" + keyword); } + } - void RawKeyword::setKeyword(const std::string& keyword) { - m_keyword = boost::algorithm::trim_right_copy(keyword); - if (!isValidKeyword(m_keyword)) { - throw std::invalid_argument("Not a valid keyword:" + keyword); - } else if (m_keyword.size() > 8) { - throw std::invalid_argument("Too long keyword:" + keyword); - } else if (boost::algorithm::trim_left_copy(m_keyword) != m_keyword) { - throw std::invalid_argument("Illegal whitespace start of keyword:" + keyword); - } + void RawKeyword::addRawRecordString(const std::string& partialRecordString) { + m_partialRecordString += partialRecordString; + if (RawRecord::isTerminatedRecordString(partialRecordString)) { + RawRecordPtr record(new RawRecord(m_partialRecordString)); + m_records.push_back(record); + m_partialRecordString.clear(); } + } - void RawKeyword::addRawRecordString(const std::string& partialRecordString) { - m_partialRecordString += partialRecordString; - if (RawRecord::isTerminatedRecordString(partialRecordString)) { - RawRecordPtr record(new RawRecord(m_partialRecordString)); - m_records.push_back(record); - m_partialRecordString.clear(); - } - } - - bool RawKeyword::isPartialRecordStringEmpty() { - return m_partialRecordString.size() == 0; - } + bool RawKeyword::isPartialRecordStringEmpty() { + return m_partialRecordString.size() == 0; + } - unsigned int RawKeyword::getNumberOfRecords() { - return m_records.size(); - } + unsigned int RawKeyword::getNumberOfRecords() { + return m_records.size(); + } - const std::list& RawKeyword::getRecords() const{ - return m_records; - } + const std::list& RawKeyword::getRecords() const { + return m_records; + } - std::string RawKeyword::getKeyword() const{ - return m_keyword; - } + std::string RawKeyword::getKeyword() const { + return m_keyword; + } - RawKeyword::~RawKeyword() { - } + RawKeyword::~RawKeyword() { + } } diff --git a/opm/parser/eclipse/RawDeck/RawRecord.cpp b/opm/parser/eclipse/RawDeck/RawRecord.cpp index cb47388c4..9750087d2 100644 --- a/opm/parser/eclipse/RawDeck/RawRecord.cpp +++ b/opm/parser/eclipse/RawDeck/RawRecord.cpp @@ -21,13 +21,12 @@ #include #include "RawRecord.hpp" +#include "RawConsts.hpp" + +using namespace Opm; using namespace std; namespace Opm { - const char RawRecord::SLASH = '/'; - const char RawRecord::QUOTE = '\''; - const std::string RawRecord::SEPARATORS = "\t "; - RawRecord::RawRecord() { } @@ -61,7 +60,7 @@ namespace Opm { bool RawRecord::isTerminatedRecordString(const std::string& candidateRecordString) { unsigned int terminatingSlash = findTerminatingSlash(candidateRecordString); bool hasTerminatingSlash = (terminatingSlash < candidateRecordString.size()); - int numberOfQuotes = std::count(candidateRecordString.begin(), candidateRecordString.end(), QUOTE); + int numberOfQuotes = std::count(candidateRecordString.begin(), candidateRecordString.end(), RawConsts::quote); bool hasEvenNumberOfQuotes = (numberOfQuotes % 2) == 0; return hasTerminatingSlash && hasEvenNumberOfQuotes; } @@ -74,7 +73,7 @@ namespace Opm { currentChar = m_sanitizedRecordString[i]; if (charIsSeparator(currentChar)) { processSeparatorCharacter(currentToken, currentChar, tokenStartCharacter); - } else if (currentChar == QUOTE) { + } else if (currentChar == RawConsts::quote) { processQuoteCharacters(currentToken, currentChar, tokenStartCharacter); } else { processNonSpecialCharacters(currentToken, currentChar); @@ -87,7 +86,7 @@ namespace Opm { } void RawRecord::processSeparatorCharacter(std::string& currentToken, const char& currentChar, char& tokenStartCharacter) { - if (tokenStartCharacter == QUOTE) { + if (tokenStartCharacter == RawConsts::quote) { currentToken += currentChar; } else { if (currentToken.size() > 0) { @@ -116,7 +115,7 @@ namespace Opm { } bool RawRecord::charIsSeparator(char candidate) { - return std::string::npos != SEPARATORS.find(candidate); + return std::string::npos != RawConsts::separators.find(candidate); } void RawRecord::setRecordString(const std::string& singleRecordString) { @@ -126,14 +125,14 @@ namespace Opm { } unsigned int RawRecord::findTerminatingSlash(const std::string& singleRecordString) { - unsigned int terminatingSlash = singleRecordString.find_first_of(SLASH); - unsigned int lastQuotePosition = singleRecordString.find_last_of(QUOTE); + unsigned int terminatingSlash = singleRecordString.find_first_of(RawConsts::slash); + unsigned int lastQuotePosition = singleRecordString.find_last_of(RawConsts::quote); // Checks lastQuotePosition vs terminatingSlashPosition, // since specifications of WELLS, FILENAMES etc can include slash, but // these are always in quotes (and there are no quotes after record-end). if (terminatingSlash < lastQuotePosition && lastQuotePosition < singleRecordString.size()) { - terminatingSlash = singleRecordString.find_first_of(SLASH, lastQuotePosition); + terminatingSlash = singleRecordString.find_first_of(RawConsts::slash, lastQuotePosition); } return terminatingSlash; } diff --git a/opm/parser/eclipse/RawDeck/RawRecord.hpp b/opm/parser/eclipse/RawDeck/RawRecord.hpp index 2493e19d7..a36b09b7a 100644 --- a/opm/parser/eclipse/RawDeck/RawRecord.hpp +++ b/opm/parser/eclipse/RawDeck/RawRecord.hpp @@ -28,10 +28,6 @@ namespace Opm { class RawRecord { public: - static const char SLASH; - static const char QUOTE; - static const std::string SEPARATORS; - RawRecord(); RawRecord(const std::string& singleRecordString); const std::string& getRecordString() const;