Added splitting of record into items in a string vector. Added tests. Must enhance readability of this splitting code before review
This commit is contained in:
@@ -16,9 +16,10 @@
|
||||
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/algorithm/string.hpp>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
#include "RawRecord.hpp"
|
||||
using namespace std;
|
||||
|
||||
@@ -35,10 +36,60 @@ namespace Opm {
|
||||
* manual.
|
||||
*/
|
||||
RawRecord::RawRecord(const std::string& singleRecordString) {
|
||||
sanitizeInputString(singleRecordString);
|
||||
if (isCompleteRecordString(singleRecordString)) {
|
||||
setRecordString(singleRecordString);
|
||||
} else {
|
||||
throw std::invalid_argument("Input string is not a complete record string,"
|
||||
" offending string: " + singleRecordString);
|
||||
}
|
||||
|
||||
std::string tokenSeparators = "\t ";
|
||||
std::string quoteSeparators = "\'\"";
|
||||
char currentChar;
|
||||
char tokenStarter;
|
||||
std::string currentToken = "";
|
||||
for (unsigned i = 0; i < m_sanitizedRecordString.size(); i++) {
|
||||
currentChar = m_sanitizedRecordString[i];
|
||||
if (stringContains(tokenSeparators, currentChar)) {
|
||||
if (stringContains(quoteSeparators, tokenStarter)) {
|
||||
currentToken += currentChar;
|
||||
} else {
|
||||
if (currentToken.size() > 0) {
|
||||
m_recordItems.push_back(currentToken);
|
||||
currentToken.clear();
|
||||
}
|
||||
tokenStarter = currentChar;
|
||||
}
|
||||
} else if (stringContains(quoteSeparators, currentChar)) {
|
||||
if (currentChar == tokenStarter) {
|
||||
if (currentToken.size() > 0) {
|
||||
m_recordItems.push_back(currentToken);
|
||||
currentToken.clear();
|
||||
}
|
||||
tokenStarter = '\0';
|
||||
} else {
|
||||
tokenStarter = currentChar;
|
||||
currentToken.clear();
|
||||
}
|
||||
} else {
|
||||
currentToken += currentChar;
|
||||
}
|
||||
}
|
||||
if (currentToken.size() > 0) {
|
||||
m_recordItems.push_back(currentToken);
|
||||
currentToken.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void RawRecord::getRecord(std::string& recordString) {
|
||||
bool RawRecord::stringContains(std::string collection, char candidate) {
|
||||
return std::string::npos != collection.find(candidate);
|
||||
}
|
||||
|
||||
void RawRecord::getRecords(std::vector<std::string>& recordItems) {
|
||||
recordItems = m_recordItems;
|
||||
}
|
||||
|
||||
void RawRecord::getRecordString(std::string& recordString) {
|
||||
recordString = m_sanitizedRecordString;
|
||||
}
|
||||
|
||||
@@ -47,7 +98,7 @@ namespace Opm {
|
||||
return (terminatingSlash < candidateRecordString.size());
|
||||
}
|
||||
|
||||
void RawRecord::sanitizeInputString(const std::string& singleRecordString) {
|
||||
void RawRecord::setRecordString(const std::string& singleRecordString) {
|
||||
unsigned terminatingSlash = findTerminatingSlash(singleRecordString);
|
||||
m_sanitizedRecordString = singleRecordString.substr(0, terminatingSlash);
|
||||
boost::trim(m_sanitizedRecordString);
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#define RECORD_HPP
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
|
||||
@@ -21,12 +20,15 @@ namespace Opm {
|
||||
static const char QUOTE;
|
||||
RawRecord();
|
||||
RawRecord(const std::string& singleRecordString);
|
||||
void getRecord(std::string& recordString);
|
||||
void getRecordString(std::string& recordString);
|
||||
void getRecords(std::vector<std::string>& recordItems);
|
||||
static bool isCompleteRecordString(const std::string& candidateRecordString);
|
||||
virtual ~RawRecord();
|
||||
private:
|
||||
std::string m_sanitizedRecordString;
|
||||
void sanitizeInputString(const std::string& singleRecordString);
|
||||
std::vector<std::string> m_recordItems;
|
||||
void setRecordString(const std::string& singleRecordString);
|
||||
bool stringContains(std::string collection, char candidate);
|
||||
static unsigned int findTerminatingSlash(const std::string& singleRecordString);
|
||||
};
|
||||
typedef boost::shared_ptr<RawRecord> RawRecordPtr;
|
||||
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
--Use saturation table end-point scaling
|
||||
ENDSCALE
|
||||
'NODIR' 'REVERS' 1 20 /
|
||||
+30
-3
@@ -43,9 +43,8 @@ BOOST_AUTO_TEST_CASE(ParseWithValidFileSetOnParseCallNoThrow) {
|
||||
|
||||
boost::filesystem::path singleKeywordFile("testdata/small.data");
|
||||
ParserPtr parser(new Parser());
|
||||
|
||||
|
||||
BOOST_REQUIRE_NO_THROW(parser -> parse(singleKeywordFile.string()));
|
||||
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseWithInValidFileSetOnParseCallThrows) {
|
||||
@@ -54,6 +53,34 @@ BOOST_AUTO_TEST_CASE(ParseWithInValidFileSetOnParseCallThrows) {
|
||||
BOOST_REQUIRE_THROW(parser -> parse(singleKeywordFile.string()), std::invalid_argument);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseFileWithOneKeyword) {
|
||||
boost::filesystem::path singleKeywordFile("testdata/mini.data");
|
||||
|
||||
ParserPtr parser(new Parser());
|
||||
|
||||
RawDeckPtr rawDeck = parser -> parse(singleKeywordFile.string());
|
||||
BOOST_REQUIRE_EQUAL((unsigned) 1, rawDeck -> getNumberOfKeywords());
|
||||
RawKeywordPtr rawKeyword = rawDeck->getKeyword("ENDSCALE");
|
||||
std::list<RawRecordPtr> records;
|
||||
rawKeyword -> getRecords(records);
|
||||
BOOST_REQUIRE_EQUAL((unsigned) 1, records.size());
|
||||
RawRecordPtr record = records.back();
|
||||
|
||||
std::string recordString;
|
||||
record -> getRecordString(recordString);
|
||||
BOOST_REQUIRE_EQUAL("'NODIR' 'REVERS' 1 20", recordString);
|
||||
|
||||
std::vector<std::string> recordElements;
|
||||
record -> getRecords(recordElements);
|
||||
BOOST_REQUIRE_EQUAL((unsigned)4, recordElements.size());
|
||||
|
||||
BOOST_REQUIRE_EQUAL("NODIR", recordElements[0]);
|
||||
BOOST_REQUIRE_EQUAL("REVERS", recordElements[1]);
|
||||
BOOST_REQUIRE_EQUAL("1", recordElements[2]);
|
||||
BOOST_REQUIRE_EQUAL("20", recordElements[3]);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
|
||||
boost::filesystem::path singleKeywordFile("testdata/small.data");
|
||||
|
||||
@@ -71,7 +98,7 @@ BOOST_AUTO_TEST_CASE(ParseFileWithFewKeywords) {
|
||||
|
||||
RawRecordPtr theRecord = records.front();
|
||||
std::string recordString;
|
||||
theRecord -> getRecord(recordString);
|
||||
theRecord -> getRecordString(recordString);
|
||||
BOOST_REQUIRE_EQUAL("\'sti til fil/den er her\'", recordString);
|
||||
|
||||
RawKeywordPtr matchingKeyword2 = rawDeck -> getKeyword("ABCDAD");
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
#include <stdexcept>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <opm/parser/eclipse/data/RawKeyword.hpp>
|
||||
#include <opm/parser/eclipse/data/RawRecord.hpp>
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(EmptyConstructorEmptyKeyword) {
|
||||
Opm::RawKeyword keyword;
|
||||
@@ -64,3 +66,18 @@ BOOST_AUTO_TEST_CASE(Set8CharKeywordWithTrailingWhitespaceKeywordTrimmed) {
|
||||
BOOST_CHECK(keyword.getKeyword() == "GOODONEE");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(RawRecordSetRecordCheckCorrectTrimAndSplit) {
|
||||
Opm::RawRecordPtr record(new Opm::RawRecord(" 'NODIR ' 'REVERS' 1 20 /"));
|
||||
std::string recordString;
|
||||
record -> getRecordString(recordString);
|
||||
BOOST_REQUIRE_EQUAL("'NODIR ' 'REVERS' 1 20", recordString);
|
||||
|
||||
std::vector<std::string> recordElements;
|
||||
record -> getRecords(recordElements);
|
||||
BOOST_REQUIRE_EQUAL((unsigned)4, recordElements.size());
|
||||
|
||||
BOOST_REQUIRE_EQUAL("NODIR ", recordElements[0]);
|
||||
BOOST_REQUIRE_EQUAL("REVERS", recordElements[1]);
|
||||
BOOST_REQUIRE_EQUAL("1", recordElements[2]);
|
||||
BOOST_REQUIRE_EQUAL("20", recordElements[3]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user