More tests, changed logging, and adding record
This commit is contained in:
@@ -27,23 +27,20 @@ namespace Opm {
|
||||
const int Logger::INFO = 1;
|
||||
const int Logger::ERROR = 2;
|
||||
|
||||
Logger::Logger(int logLevel) {
|
||||
m_logLevel = logLevel;
|
||||
m_logFile = "log.log";
|
||||
initLogger();
|
||||
}
|
||||
|
||||
Logger::Logger(const std::string& path, int logLevel) {
|
||||
m_logLevel = logLevel;
|
||||
m_logFile = path;
|
||||
initLogger();
|
||||
}
|
||||
std::string Logger::m_logFile = "log.log";
|
||||
int Logger::m_logLevel = INFO;
|
||||
std::ofstream Logger::m_logStream;
|
||||
|
||||
void Logger::setLogLevel(int logLevel) {
|
||||
m_logLevel = logLevel;
|
||||
}
|
||||
|
||||
void Logger::setPath(const std::string& path) {
|
||||
m_logFile = path;
|
||||
}
|
||||
|
||||
void Logger::debug(const std::string& message) {
|
||||
|
||||
if (m_logLevel <= DEBUG) {
|
||||
log(message, "DEBUG");
|
||||
}
|
||||
|
||||
@@ -31,21 +31,19 @@ namespace Opm {
|
||||
static const int INFO;
|
||||
static const int ERROR;
|
||||
|
||||
Logger(int logLevel = INFO);
|
||||
Logger(const std::string& path, int logLevel = INFO);
|
||||
|
||||
void setLogLevel(int logLevel);
|
||||
void debug(const std::string& message);
|
||||
void info(const std::string& message);
|
||||
void error(const std::string& message);
|
||||
static void setLogLevel(int logLevel);
|
||||
static void setPath(const std::string& path);
|
||||
static void debug(const std::string& message);
|
||||
static void info(const std::string& message);
|
||||
static void error(const std::string& message);
|
||||
virtual ~Logger();
|
||||
private:
|
||||
std::string m_logFile;
|
||||
std::ofstream m_logStream;
|
||||
int m_logLevel;
|
||||
void initLogger();
|
||||
void log(const std::string& message, std::string logLevel);
|
||||
void initLoggingConstants();
|
||||
static std::string m_logFile;
|
||||
static std::ofstream m_logStream;
|
||||
static int m_logLevel;
|
||||
static void initLogger();
|
||||
static void log(const std::string& message, std::string logLevel);
|
||||
static void initLoggingConstants();
|
||||
};
|
||||
}// namespace Opm
|
||||
#endif /* LOGGER_HPP */
|
||||
|
||||
@@ -16,13 +16,6 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <regex.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace Opm {
|
||||
@@ -31,63 +24,9 @@ namespace Opm {
|
||||
}
|
||||
|
||||
void Parser::parse(const std::string &path, RawDeck& outputDeck) {
|
||||
checkInputFile(path);
|
||||
std::ifstream file;
|
||||
initInputStream(path, file);
|
||||
|
||||
readRawDeck(file, outputDeck);
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
void Parser::readRawDeck(std::ifstream& inputstream, RawDeck& outputDeck) {
|
||||
std::string line;
|
||||
std::string keyword;
|
||||
RawKeyword currentRawKeyword;
|
||||
while (std::getline(inputstream, line)) {
|
||||
if (RawKeyword::tryGetValidKeyword(line, keyword)) {
|
||||
currentRawKeyword = RawKeyword(keyword);
|
||||
outputDeck.addKeyword(currentRawKeyword);
|
||||
}
|
||||
else {
|
||||
addRawRecordStringToRawKeyword(line, currentRawKeyword);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::addRawRecordStringToRawKeyword(const std::string& line, RawKeyword& currentRawKeyword) {
|
||||
if (currentRawKeyword.getKeyword() != "" && looksLikeData(line)) {
|
||||
currentRawKeyword.addRawRecordString(line);
|
||||
}
|
||||
}
|
||||
|
||||
bool Parser::looksLikeData(const std::string& line) {
|
||||
if (line.substr(0, 2) == "--") {
|
||||
m_logger.debug("COMMENT LINE <" + line + ">");
|
||||
return false;
|
||||
} else if (boost::algorithm::trim_copy(line).length() == 0) {
|
||||
m_logger.debug("EMPTY LINE <" + line + ">");
|
||||
return false;
|
||||
} else if (line.substr(0, 1) == "/") {
|
||||
m_logger.debug("END OF RECORD <" + line + ">");
|
||||
return false;
|
||||
} else {
|
||||
m_logger.debug("LOOKS LIKE DATA<" + line + ">");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void Parser::initInputStream(const std::string &path, std::ifstream& file) {
|
||||
m_logger.info("Initializing from file: " + path);
|
||||
file.open(path.c_str());
|
||||
}
|
||||
|
||||
void Parser::checkInputFile(const std::string& inputPath) {
|
||||
boost::filesystem::path pathToInputFile(inputPath);
|
||||
if (!boost::filesystem::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);
|
||||
}
|
||||
Logger::info("Starting parsing of file: " + path);
|
||||
outputDeck.readDataIntoDeck(path);
|
||||
Logger::info("Done parsing of file: " + path);
|
||||
}
|
||||
|
||||
Parser::~Parser() {
|
||||
|
||||
@@ -35,13 +35,7 @@ namespace Opm {
|
||||
void parse(const std::string &path, RawDeck& outputDeck);
|
||||
virtual ~Parser();
|
||||
private:
|
||||
Logger m_logger;
|
||||
void initInputStream(const std::string &path, std::ifstream& file);
|
||||
void readRawDeck(std::ifstream& inputstream, RawDeck& outputDeck);
|
||||
void addRawRecordStringToRawKeyword(const std::string& line, RawKeyword& currentKeywordRecordSet);
|
||||
bool looksLikeData(const std::string& line);
|
||||
void checkInputFile(const std::string& pathToInputFile);
|
||||
|
||||
//Logger m_logger;
|
||||
};
|
||||
} // namespace Opm
|
||||
#endif /* PARSER_H */
|
||||
|
||||
@@ -16,22 +16,83 @@
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <iostream>
|
||||
#include "RawDeck.hpp"
|
||||
|
||||
namespace Opm {
|
||||
|
||||
RawDeck::RawDeck() {
|
||||
}
|
||||
|
||||
void RawDeck::addKeyword(const RawKeyword& keyword){
|
||||
m_keywords.push_back(keyword);
|
||||
}
|
||||
|
||||
RawKeyword* RawDeck::getKeyword(const std::string& keyword) {
|
||||
for(std::list<RawKeyword*>::iterator it = m_keywords.begin(); it != m_keywords.end(); it++) {
|
||||
if ((*it)->getKeyword() == keyword) {
|
||||
return (*it);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void RawDeck::readDataIntoDeck(const std::string& path) {
|
||||
checkInputFile(path);
|
||||
std::ifstream inputstream;
|
||||
Logger::info("Initializing from file: " + path);
|
||||
inputstream.open(path.c_str());
|
||||
|
||||
std::string line;
|
||||
std::string keywordString;
|
||||
RawKeyword* currentRawKeyword;
|
||||
while (std::getline(inputstream, line)) {
|
||||
if (currentRawKeyword -> tryGetValidKeyword(line, keywordString)) {
|
||||
currentRawKeyword = new RawKeyword(keywordString);
|
||||
m_keywords.push_back(currentRawKeyword);
|
||||
} else if (currentRawKeyword != NULL) {
|
||||
addRawRecordStringToRawKeyword(line, currentRawKeyword);
|
||||
}
|
||||
}
|
||||
inputstream.close();
|
||||
}
|
||||
|
||||
void RawDeck::addRawRecordStringToRawKeyword(const std::string& line, RawKeyword* currentRawKeyword) {
|
||||
if (looksLikeData(line)) {
|
||||
currentRawKeyword->addRawRecordString(line);
|
||||
}
|
||||
}
|
||||
|
||||
bool RawDeck::looksLikeData(const std::string& line) {
|
||||
if (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 (line.substr(0, 1) == "/") {
|
||||
Logger::debug("END OF RECORD <" + line + ">");
|
||||
return false;
|
||||
} else {
|
||||
Logger::debug("LOOKS LIKE DATA<" + line + ">");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void RawDeck::checkInputFile(const std::string& inputPath) {
|
||||
boost::filesystem::path pathToInputFile(inputPath);
|
||||
if (!boost::filesystem::is_regular_file(pathToInputFile)) {
|
||||
Logger::error("Unable to open file with path: " + inputPath);
|
||||
throw std::invalid_argument("Given path is not a valid file-path, path: " + inputPath);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int RawDeck::getNumberOfKeywords() {
|
||||
return m_keywords.size();
|
||||
}
|
||||
|
||||
RawDeck::~RawDeck() {
|
||||
while(!m_keywords.empty()) {
|
||||
delete m_keywords.front();
|
||||
m_keywords.pop_front();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#ifndef RAWDECK_HPP
|
||||
#define RAWDECK_HPP
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include "opm/parser/eclipse/Logger.hpp"
|
||||
#include "RawKeyword.hpp"
|
||||
|
||||
namespace Opm {
|
||||
@@ -15,11 +17,15 @@ namespace Opm {
|
||||
class RawDeck {
|
||||
public:
|
||||
RawDeck();
|
||||
void addKeyword(const RawKeyword& keyword);
|
||||
void readDataIntoDeck(const std::string& path);
|
||||
RawKeyword* getKeyword(const std::string& keyword);
|
||||
unsigned int getNumberOfKeywords();
|
||||
virtual ~RawDeck();
|
||||
private:
|
||||
std::list<RawKeyword> m_keywords;
|
||||
std::list<RawKeyword*> m_keywords;
|
||||
void addRawRecordStringToRawKeyword(const std::string& line, RawKeyword* currentRawKeyword);
|
||||
bool looksLikeData(const std::string& line);
|
||||
void checkInputFile(const std::string& inputPath);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <regex.h>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include "RawKeyword.hpp"
|
||||
#include <iostream>
|
||||
|
||||
namespace Opm {
|
||||
|
||||
@@ -50,13 +51,25 @@ namespace Opm {
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int RawKeyword::getNumberOfRecords() {
|
||||
return m_records.size();
|
||||
}
|
||||
|
||||
void RawKeyword::getRecords(std::list<RawRecord>& records) {
|
||||
records = m_records;
|
||||
}
|
||||
|
||||
std::string RawKeyword::getKeyword() {
|
||||
return m_keyword;
|
||||
}
|
||||
|
||||
bool RawKeyword::tryGetValidKeyword(const std::string& keywordCandidate, std::string& result) {
|
||||
result = boost::trim_right_copy(keywordCandidate.substr(0, 8));
|
||||
return isValidKeyword(result);
|
||||
if (isValidKeyword(result)) {
|
||||
Logger::debug("KEYWORD <" + keywordCandidate + ">");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RawKeyword::isValidKeyword(const std::string& keywordCandidate) {
|
||||
@@ -76,7 +89,7 @@ namespace Opm {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
RawKeyword::~RawKeyword() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,16 +12,18 @@
|
||||
#include <utility>
|
||||
#include <list>
|
||||
|
||||
#include "opm/parser/eclipse/Logger.hpp"
|
||||
#include "RawRecord.hpp"
|
||||
|
||||
namespace Opm {
|
||||
|
||||
class RawKeyword {
|
||||
public:
|
||||
RawKeyword();
|
||||
RawKeyword(const std::string& keyword);
|
||||
static bool tryGetValidKeyword(const std::string& line, std::string& result);
|
||||
bool tryGetValidKeyword(const std::string& line, std::string& result);
|
||||
std::string getKeyword();
|
||||
void getRecords(std::list<RawRecord>& records);
|
||||
unsigned int getNumberOfRecords();
|
||||
void setKeyword(const std::string& keyword);
|
||||
void addRawRecordString(const std::string& partialRecordString);
|
||||
virtual ~RawKeyword();
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
*/
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include "RawRecord.hpp"
|
||||
using namespace std;
|
||||
|
||||
@@ -38,6 +38,10 @@ namespace Opm {
|
||||
sanitizeInputString(singleRecordString);
|
||||
}
|
||||
|
||||
void RawRecord::getRecord(std::string& recordString) {
|
||||
recordString = m_sanitizedRecordString;
|
||||
}
|
||||
|
||||
bool RawRecord::isCompleteRecordString(const std::string& candidateRecordString) {
|
||||
unsigned int terminatingSlash = findTerminatingSlash(candidateRecordString);
|
||||
return (terminatingSlash < candidateRecordString.size());
|
||||
|
||||
@@ -21,11 +21,11 @@ namespace Opm {
|
||||
static const char QUOTE;
|
||||
RawRecord();
|
||||
RawRecord(const std::string& singleRecordString);
|
||||
void getRecord(std::string& recordString);
|
||||
static bool isCompleteRecordString(const std::string& candidateRecordString);
|
||||
virtual ~RawRecord();
|
||||
private:
|
||||
std::string m_sanitizedRecordString;
|
||||
std::list<std::string> m_recordElements;
|
||||
void sanitizeInputString(const std::string& singleRecordString);
|
||||
static unsigned int findTerminatingSlash(const std::string& singleRecordString);
|
||||
};
|
||||
|
||||
+18
-4
@@ -35,18 +35,20 @@ BOOST_AUTO_TEST_CASE(Initializing) {
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseWithInvalidInputFileThrows) {
|
||||
Parser parser;
|
||||
Parser *parser = new Parser();
|
||||
RawDeck rawDeck;
|
||||
BOOST_REQUIRE_THROW(parser.parse("nonexistingfile.asdf", rawDeck), std::invalid_argument);
|
||||
BOOST_REQUIRE_THROW(parser -> parse("nonexistingfile.asdf", rawDeck), std::invalid_argument);
|
||||
delete parser;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseWithValidFileSetOnParseCallNoThrow) {
|
||||
|
||||
boost::filesystem::path singleKeywordFile("testdata/small.data");
|
||||
Parser parser;
|
||||
Parser *parser = new Parser();
|
||||
RawDeck rawDeck;
|
||||
|
||||
BOOST_REQUIRE_NO_THROW(parser.parse(singleKeywordFile.string(), rawDeck));
|
||||
BOOST_REQUIRE_NO_THROW(parser -> parse(singleKeywordFile.string(), rawDeck));
|
||||
delete parser;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseWithInValidFileSetOnParseCallThrows) {
|
||||
@@ -65,6 +67,18 @@ BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
|
||||
|
||||
parser.parse(singleKeywordFile.string(), rawDeck);
|
||||
BOOST_REQUIRE_EQUAL((unsigned)4, rawDeck.getNumberOfKeywords());
|
||||
|
||||
RawKeyword* matchingKeyword = rawDeck.getKeyword("A");
|
||||
BOOST_REQUIRE_EQUAL("A", matchingKeyword->getKeyword());
|
||||
|
||||
std::list<RawRecord> records;
|
||||
matchingKeyword->getRecords(records);
|
||||
BOOST_REQUIRE_EQUAL((unsigned) 1, records.size());
|
||||
|
||||
RawRecord theRecord = records.front();
|
||||
std::string recordString;
|
||||
theRecord.getRecord(recordString);
|
||||
BOOST_REQUIRE_EQUAL("\'sti til fil/den er her\'", recordString);
|
||||
}
|
||||
|
||||
//NOTE: needs statoil dataset
|
||||
|
||||
Reference in New Issue
Block a user