Moved some consts for the Raw classes to a separate file, characters and sizes

This commit is contained in:
Kristian Flikka
2013-04-08 10:31:54 +02:00
parent 76b8df09ad
commit 1868615a4a
7 changed files with 130 additions and 94 deletions
-1
View File
@@ -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);
-1
View File
@@ -24,7 +24,6 @@
#include <boost/shared_ptr.hpp>
#include <opm/parser/eclipse/Logger.hpp>
#include <opm/parser/eclipse/RawDeck/RawKeyword.hpp>
#include <opm/parser/eclipse/RawDeck/RawDeck.hpp>
#include <opm/parser/eclipse/Parser/ParserKW.hpp>
+36
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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 */
+2 -1
View File
@@ -19,6 +19,7 @@
#include <boost/algorithm/string.hpp>
#include <iostream>
#include "RawDeck.hpp"
#include "RawConsts.hpp"
namespace Opm {
@@ -93,7 +94,7 @@ namespace Opm {
}
void RawDeck::popAndProcessInclude(std::list<RawKeywordPtr>& 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.
+82 -76
View File
@@ -20,101 +20,107 @@
#include <regex.h>
#include <boost/algorithm/string.hpp>
#include "RawKeyword.hpp"
#include "RawConsts.hpp"
#include <iostream>
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(&regularExpression, 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(&regularExpression, keywordCandidate.c_str(), 1, &rm, 0);
regfree(&regularExpression);
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(&regularExpression, keywordRegex.c_str(), REG_EXTENDED) != 0) {
throw std::runtime_error("Unable to compile regular expression for keyword! Expression: " + keywordRegex);
}
status = regexec(&regularExpression, keywordCandidate.c_str(), 1, &rm, 0);
regfree(&regularExpression);
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<RawRecordPtr>& RawKeyword::getRecords() const{
return m_records;
}
const std::list<RawRecordPtr>& 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() {
}
}
+10 -11
View File
@@ -21,13 +21,12 @@
#include <boost/algorithm/string.hpp>
#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;
}
-4
View File
@@ -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;