Changed KeywordRawData to a one-per-keyword object, moved into data folder
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
add_library(Parser Parser.cpp EclipseDeck.cpp KeywordRawData.cpp)
|
||||
|
||||
add_library(Parser Parser.cpp data/KeywordDataToken.cpp)
|
||||
add_library(Logger Logger.cpp)
|
||||
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
|
||||
#include "EclipseDeck.hpp"
|
||||
|
||||
namespace Opm {
|
||||
|
||||
EclipseDeck::EclipseDeck() {
|
||||
}
|
||||
|
||||
int EclipseDeck::getNumberOfKeywords() {
|
||||
return m_keywords.size();
|
||||
}
|
||||
|
||||
const std::vector<std::string> EclipseDeck::getKeywords() {
|
||||
return m_keywords;
|
||||
}
|
||||
|
||||
void EclipseDeck::addKeyword(const std::string &keyword) {
|
||||
m_keywords.push_back(keyword);
|
||||
}
|
||||
|
||||
EclipseDeck::~EclipseDeck() {
|
||||
}
|
||||
} // namespace Opm
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
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 ECLIPSEDECK_H
|
||||
#define ECLIPSEDECK_H
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace Opm {
|
||||
class EclipseDeck {
|
||||
public:
|
||||
EclipseDeck();
|
||||
int getNumberOfKeywords();
|
||||
const std::vector<std::string> getKeywords();
|
||||
void addKeyword(const std::string &keyword);
|
||||
virtual ~EclipseDeck();
|
||||
private:
|
||||
int m_numberOfKeywords;
|
||||
std::vector<std::string> m_keywords;
|
||||
};
|
||||
} // namespace Opm
|
||||
#endif /* ECLIPSEDECK_H */
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* File: KeywordRawData.hpp
|
||||
* Author: kflik
|
||||
*
|
||||
* Created on March 20, 2013, 3:59 PM
|
||||
*/
|
||||
|
||||
#ifndef KEYWORDRAWDATA_HPP
|
||||
#define KEYWORDRAWDATA_HPP
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <list>
|
||||
|
||||
namespace Opm {
|
||||
|
||||
class KeywordRawData {
|
||||
public:
|
||||
KeywordRawData();
|
||||
void addKeywordDataBlob(const std::string& keyword, const std::list<std::string>& blob);
|
||||
void getListOfKeywords(std::list<std::string>& keywords);
|
||||
int numberOfKeywords();
|
||||
virtual ~KeywordRawData();
|
||||
private:
|
||||
std::list< std::pair< std::string, std::list<std::string > > > m_keywordRawData;
|
||||
};
|
||||
}
|
||||
#endif /* KEYWORDRAWDATA_HPP */
|
||||
|
||||
@@ -29,66 +29,46 @@ namespace fs = boost::filesystem;
|
||||
namespace Opm {
|
||||
|
||||
Parser::Parser() {
|
||||
m_keywordRawData = new KeywordRawData();
|
||||
}
|
||||
|
||||
Parser::Parser(const std::string &path) {
|
||||
m_keywordRawData = new KeywordRawData();
|
||||
m_dataFilePath = path;
|
||||
}
|
||||
|
||||
void Parser::parse(const std::string &path) {
|
||||
m_logger.setLogLevel(Opm::Logger::DEBUG);
|
||||
|
||||
fs::path pathToCheck(path);
|
||||
if (!fs::is_regular_file(pathToCheck)) {
|
||||
m_logger.error("Unable to open file with path: " + path);
|
||||
throw std::invalid_argument("Given path is not a valid file-path, path: " + path);
|
||||
}
|
||||
|
||||
std::ifstream file;
|
||||
initInputStream(path, file);
|
||||
createKeywordAndRawData(file);
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
void Parser::parse() {
|
||||
parse(m_dataFilePath);
|
||||
}
|
||||
|
||||
void Parser::parse(const std::string &path) {
|
||||
checkInputFile(path);
|
||||
std::ifstream file;
|
||||
initInputStream(path, file);
|
||||
|
||||
readKeywordAndDataTokens(file);
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
int Parser::getNumberOfKeywords() {
|
||||
return m_keywordRawData -> numberOfKeywords();
|
||||
return m_keywordRawDatas.size();
|
||||
}
|
||||
|
||||
void Parser::getListOfKeywords(std::list<std::string>& list) {
|
||||
m_keywordRawData->getListOfKeywords(list);
|
||||
}
|
||||
|
||||
void Parser::createKeywordAndRawData(std::ifstream& inputstream) {
|
||||
EclipseDeck deck;
|
||||
void Parser::readKeywordAndDataTokens(std::ifstream& inputstream) {
|
||||
std::string line;
|
||||
std::string currentKeyword = "";
|
||||
std::list<std::string> currentDataBlob;
|
||||
KeywordDataToken currentKeywordDataToken;
|
||||
while (std::getline(inputstream, line)) {
|
||||
if (isKeyword(line)) {
|
||||
if (currentKeyword != "") {
|
||||
m_keywordRawData->addKeywordDataBlob(currentKeyword, currentDataBlob);
|
||||
}
|
||||
currentDataBlob = std::list<std::string>();
|
||||
currentKeyword = line;
|
||||
currentKeywordDataToken = KeywordDataToken(line);
|
||||
m_keywordRawDatas.push_back(currentKeywordDataToken);
|
||||
} else {
|
||||
if (currentKeyword != "")
|
||||
addDataToBlob(line, currentDataBlob);
|
||||
addDataToDataToken(line, currentKeywordDataToken);
|
||||
}
|
||||
}
|
||||
if (currentKeyword != "") {
|
||||
m_keywordRawData->addKeywordDataBlob(currentKeyword, currentDataBlob);
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::addDataToBlob(const std::string& line, std::list<std::string>& currentDataBlob) {
|
||||
if (looksLikeData(line)) {
|
||||
currentDataBlob.push_back(line);
|
||||
void Parser::addDataToDataToken(const std::string& line, KeywordDataToken& currentKeywordDataToken) {
|
||||
if (currentKeywordDataToken.getKeyword() != "" && looksLikeData(line)) {
|
||||
currentKeywordDataToken.addDataElement(line);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +94,7 @@ namespace Opm {
|
||||
m_logger.error("Unable to compile regular expression for keyword! Expression: " + keywordRegex);
|
||||
throw std::runtime_error("Unable to compile regular expression for keyword! Expression: " + keywordRegex);
|
||||
}
|
||||
|
||||
|
||||
std::string trimmedRight = boost::trim_right_copy(line);
|
||||
status = regexec(&re, trimmedRight.c_str(), 1, &rm, 0);
|
||||
regfree(&re);
|
||||
@@ -131,7 +111,14 @@ namespace Opm {
|
||||
file.open(path.c_str());
|
||||
}
|
||||
|
||||
void Parser::checkInputFile(const std::string& inputPath) {
|
||||
fs::path pathToInputFile(inputPath);
|
||||
if (!fs::is_regular_file(pathToInputFile)) {
|
||||
m_logger.error("Unable to open file with path: " + inputPath);
|
||||
throw std::invalid_argument("Given path is not a valid file-path, path: " + inputPath);
|
||||
}
|
||||
}
|
||||
|
||||
Parser::~Parser() {
|
||||
delete m_keywordRawData;
|
||||
}
|
||||
} // namespace Opm
|
||||
|
||||
@@ -22,9 +22,8 @@
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
#include "EclipseDeck.hpp"
|
||||
#include "Logger.hpp"
|
||||
#include "KeywordRawData.hpp"
|
||||
#include "data/KeywordDataToken.hpp"
|
||||
|
||||
namespace Opm {
|
||||
|
||||
@@ -40,12 +39,13 @@ namespace Opm {
|
||||
private:
|
||||
std::string m_dataFilePath;
|
||||
Logger m_logger;
|
||||
KeywordRawData* m_keywordRawData;
|
||||
std::list<KeywordDataToken> m_keywordRawDatas;
|
||||
void initInputStream(const std::string &path, std::ifstream& file);
|
||||
void createKeywordAndRawData(std::ifstream& inputstream);
|
||||
void readKeywordAndDataTokens(std::ifstream& inputstream);
|
||||
bool isKeyword(const std::string& line);
|
||||
void addDataToBlob(const std::string& line, std::list<std::string>& dataBlob);
|
||||
void addDataToDataToken(const std::string& line, KeywordDataToken& currentKeywordDataToken);
|
||||
bool looksLikeData(const std::string& line);
|
||||
void checkInputFile(const std::string& pathToInputFile);
|
||||
|
||||
};
|
||||
} // namespace Opm
|
||||
|
||||
+21
-16
@@ -18,29 +18,34 @@
|
||||
*/
|
||||
|
||||
#include <utility>
|
||||
#include "KeywordRawData.hpp"
|
||||
#include "KeywordDataToken.hpp"
|
||||
namespace Opm {
|
||||
|
||||
KeywordRawData::KeywordRawData() {
|
||||
KeywordDataToken::KeywordDataToken() {
|
||||
}
|
||||
|
||||
KeywordDataToken::KeywordDataToken(const std::string& keyword, const std::list<std::string>& dataElements) {
|
||||
setKeyword(keyword);
|
||||
m_data = dataElements;
|
||||
}
|
||||
|
||||
KeywordDataToken::KeywordDataToken(const std::string& keyword) {
|
||||
m_keyword = keyword;
|
||||
}
|
||||
|
||||
void KeywordRawData::addKeywordDataBlob(const std::string& keyword, const std::list<std::string>& blob) {
|
||||
m_keywordRawData.push_back(std::make_pair(keyword, blob));
|
||||
void KeywordDataToken::setKeyword(const std::string& keyword) {
|
||||
m_keyword = keyword;
|
||||
}
|
||||
|
||||
void KeywordDataToken::addDataElement(const std::string& element) {
|
||||
m_data.push_back(element);
|
||||
}
|
||||
|
||||
int KeywordRawData::numberOfKeywords() {
|
||||
return m_keywordRawData.size();
|
||||
std::string KeywordDataToken::getKeyword() {
|
||||
return m_keyword;
|
||||
}
|
||||
|
||||
void KeywordRawData::getListOfKeywords(std::list<std::string>& keywords) {
|
||||
keywords.clear();
|
||||
std::list< std::pair< std::string, std::list<std::string > > >::iterator iterator;
|
||||
for (iterator = m_keywordRawData.begin(); iterator != m_keywordRawData.end(); iterator++) {
|
||||
keywords.push_back(iterator->first);
|
||||
}
|
||||
}
|
||||
|
||||
KeywordRawData::~KeywordRawData() {
|
||||
|
||||
KeywordDataToken::~KeywordDataToken() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* File: KeywordRawData.hpp
|
||||
* Author: kflik
|
||||
*
|
||||
* Created on March 20, 2013, 3:59 PM
|
||||
*/
|
||||
|
||||
#ifndef KEYWORDATATOKEN_HPP
|
||||
#define KEYWORDATATOKEN_HPP
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <list>
|
||||
|
||||
namespace Opm {
|
||||
|
||||
class KeywordDataToken {
|
||||
public:
|
||||
KeywordDataToken();
|
||||
KeywordDataToken(const std::string& keyword, const std::list<std::string>& dataElements);
|
||||
KeywordDataToken(const std::string& keyword);
|
||||
|
||||
std::string getKeyword();
|
||||
void setKeyword(const std::string& keyword);
|
||||
void addDataElement(const std::string& element);
|
||||
virtual ~KeywordDataToken();
|
||||
|
||||
private:
|
||||
std::string m_keyword;
|
||||
std::list<std::string> m_data;
|
||||
};
|
||||
}
|
||||
#endif /* KEYWORDATATOKEN_HPP */
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include "opm/parser/eclipse/Parser.hpp"
|
||||
#include "opm/parser/eclipse/EclipseDeck.hpp"
|
||||
|
||||
using namespace Opm;
|
||||
|
||||
@@ -82,14 +81,4 @@ BOOST_AUTO_TEST_CASE(ParseFullTestFile) {
|
||||
Parser parser(multipleKeywordFile.string());
|
||||
parser.parse();
|
||||
BOOST_REQUIRE_EQUAL(73, parser.getNumberOfKeywords());
|
||||
std::list<std::string> keywords;
|
||||
parser.getListOfKeywords(keywords);
|
||||
BOOST_REQUIRE_EQUAL((unsigned int)73, keywords.size());
|
||||
|
||||
// std::ofstream file("keywords.out");
|
||||
// std::list<std::string>::iterator iterator;
|
||||
// for (iterator = keywords.begin(); iterator != keywords.end(); ++iterator) {
|
||||
// file << *iterator << std::endl;
|
||||
// }
|
||||
// file.close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user