Removed template for ParserItem for decoding default/repetetive tokens for String and Double
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#include <opm/parser/eclipse/Parser/ParserEnums.hpp>
|
||||
|
||||
#include <opm/parser/eclipse/RawDeck/RawRecord.hpp>
|
||||
#include <opm/parser/eclipse/RawDeck/StarToken.hpp>
|
||||
#include <opm/parser/eclipse/Deck/DeckDoubleItem.hpp>
|
||||
|
||||
namespace Opm
|
||||
@@ -74,27 +75,26 @@ namespace Opm
|
||||
/// returns a DeckItem object.
|
||||
/// NOTE: data are popped from the rawRecords deque!
|
||||
|
||||
DeckItemConstPtr
|
||||
ParserDoubleItem::scan(RawRecordPtr rawRecord) const
|
||||
{
|
||||
DeckDoubleItemPtr deckItem(new DeckDoubleItem(name()));
|
||||
|
||||
bool scanAll = (sizeType() == ALL);
|
||||
bool defaultActive;
|
||||
std::deque<double> doublesPreparedForDeckItem = readFromRawRecord(
|
||||
rawRecord, scanAll, m_default, defaultActive);
|
||||
|
||||
if (scanAll)
|
||||
deckItem->push_back(doublesPreparedForDeckItem);
|
||||
else
|
||||
{
|
||||
deckItem->push_back(doublesPreparedForDeckItem.front());
|
||||
doublesPreparedForDeckItem.pop_front();
|
||||
pushBackToRecord(rawRecord, doublesPreparedForDeckItem,
|
||||
defaultActive);
|
||||
}
|
||||
return deckItem;
|
||||
}
|
||||
// DeckItemConstPtr ParserDoubleItem::scan(RawRecordPtr rawRecord) const
|
||||
// {
|
||||
// DeckDoubleItemPtr deckItem(new DeckDoubleItem(name()));
|
||||
//
|
||||
// bool scanAll = (sizeType() == ALL);
|
||||
// bool defaultActive;
|
||||
// std::deque<double> doublesPreparedForDeckItem = readFromRawRecord(
|
||||
// rawRecord, scanAll, m_default, defaultActive);
|
||||
//
|
||||
// if (scanAll)
|
||||
// deckItem->push_back(doublesPreparedForDeckItem);
|
||||
// else
|
||||
// {
|
||||
// deckItem->push_back(doublesPreparedForDeckItem.front());
|
||||
// doublesPreparedForDeckItem.pop_front();
|
||||
// pushBackToRecord(rawRecord, doublesPreparedForDeckItem,
|
||||
// defaultActive);
|
||||
// }
|
||||
// return deckItem;
|
||||
// }
|
||||
|
||||
bool ParserDoubleItem::equal(const ParserDoubleItem& other) const
|
||||
{
|
||||
@@ -104,6 +104,54 @@ namespace Opm
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Scans the rawRecords data according to the ParserItems definition.
|
||||
/// returns a DeckItem object.
|
||||
/// NOTE: data are popped from the rawRecords deque!
|
||||
DeckItemConstPtr ParserDoubleItem::scan(RawRecordPtr rawRecord) const {
|
||||
DeckDoubleItemPtr deckItem(new DeckDoubleItem(name()));
|
||||
double defaultValue = m_default;
|
||||
|
||||
if (sizeType() == ALL) { // This can probably not be combined with a default value ....
|
||||
// The '*' should be interpreted as a multiplication sign
|
||||
while (rawRecord->size() > 0) {
|
||||
std::string token = rawRecord->pop_front();
|
||||
if (tokenContainsStar( token )) {
|
||||
StarToken<double> st(token);
|
||||
double value = defaultValue; // This probably does never apply
|
||||
if (st.hasValue())
|
||||
value = st.value();
|
||||
deckItem->push_backMultiple( value , st.multiplier() );
|
||||
} else {
|
||||
double value = readValueToken<double>(token);
|
||||
deckItem->push_back(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// The '*' should be interpreted as a default indicator
|
||||
if (rawRecord->size() > 0) {
|
||||
std::string token = rawRecord->pop_front();
|
||||
if (tokenContainsStar( token )) {
|
||||
StarToken<double> st(token);
|
||||
|
||||
if (st.hasValue()) { // Probably never true
|
||||
deckItem->push_back( st.value() );
|
||||
std::string stringValue = boost::lexical_cast<std::string>(st.value());
|
||||
for (size_t i=1; i < st.multiplier(); i++)
|
||||
rawRecord->push_front( stringValue );
|
||||
} else {
|
||||
deckItem->push_backDefault( defaultValue );
|
||||
for (size_t i=1; i < st.multiplier(); i++)
|
||||
rawRecord->push_front( "*" );
|
||||
}
|
||||
} else {
|
||||
double value = readValueToken<double>(token);
|
||||
deckItem->push_back(value);
|
||||
}
|
||||
} else
|
||||
deckItem->push_backDefault( defaultValue );
|
||||
}
|
||||
return deckItem;
|
||||
}
|
||||
|
||||
void ParserDoubleItem::inlineNew(std::ostream& os) const {
|
||||
os << "new ParserDoubleItem(" << "\"" << name() << "\"" << "," << ParserItemSizeEnum2String( sizeType() );
|
||||
|
||||
@@ -54,13 +54,6 @@ namespace Opm {
|
||||
|
||||
typedef boost::shared_ptr<const ParserIntItem> ParserIntItemConstPtr;
|
||||
typedef boost::shared_ptr<ParserIntItem> ParserIntItemPtr;
|
||||
|
||||
|
||||
template<class T>
|
||||
class ParserXItem : public ParserItem{
|
||||
public:
|
||||
ParserXItem(const std::string& itemName) { };
|
||||
};
|
||||
}
|
||||
|
||||
#endif /* PARSERINTITEM_HPP */
|
||||
|
||||
@@ -25,10 +25,6 @@
|
||||
|
||||
namespace Opm {
|
||||
|
||||
template<> void ParserItem::fillVectorFromStringStream<std::string>(std::istringstream& inputStream , std::string& token , std::deque<std::string>& dataVector) const {
|
||||
dataVector.push_back(token);
|
||||
}
|
||||
|
||||
ParserItem::ParserItem(const std::string& itemName, ParserItemSizeEnum sizeType) {
|
||||
m_name.assign(itemName);
|
||||
m_sizeType = sizeType;
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace Opm {
|
||||
protected:
|
||||
bool m_defaultSet;
|
||||
|
||||
#include <opm/parser/eclipse/Parser/ParserItemTemplate.hpp>
|
||||
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
|
||||
@@ -1,119 +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/>.
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
Pushing the converted values onto the dataVector is in this seperate
|
||||
function to be able to specialize the implementation for type
|
||||
std::string. The problem is that in the code:
|
||||
|
||||
std::istringstream inputStream(" WITH_SPACE ");
|
||||
inputStream >> stringValue;
|
||||
|
||||
The leading and trailing spaces will be stripped from the final
|
||||
stringValue (have tried manipulating the skipws flag to no
|
||||
avail). To avoid this a specialized
|
||||
fillVectorFromStringStream<std::string> implementation is in
|
||||
ParserItem.cpp.
|
||||
*/
|
||||
|
||||
template<class T> void fillVectorFromStringStream(std::istringstream& inputStream , std::string& token , std::deque<T>& dataVector) const {
|
||||
T value;
|
||||
inputStream >> value;
|
||||
dataVector.push_back(value);
|
||||
|
||||
inputStream.get();
|
||||
if (!inputStream.eof())
|
||||
throw std::invalid_argument("Spurious data at the end of: <" + token + ">");
|
||||
}
|
||||
|
||||
|
||||
template<class T> void fillVectorFromStringToken(std::string token, std::deque<T>& dataVector, T defaultValue, bool& defaultActive) const {
|
||||
std::istringstream inputStream(token);
|
||||
size_t starPos = token.find('*');
|
||||
bool hasStar = (starPos != std::string::npos);
|
||||
|
||||
defaultActive = false;
|
||||
if (hasStar) {
|
||||
bool singleDefault = (starPos == 0);
|
||||
|
||||
if (singleDefault) {
|
||||
defaultActive = true;
|
||||
inputStream.get();
|
||||
if (token.size() > 1)
|
||||
throw std::invalid_argument("Token : " + token + " is invalid.");
|
||||
dataVector.push_back(defaultValue);
|
||||
} else {
|
||||
size_t multiplier;
|
||||
int starChar;
|
||||
T value;
|
||||
|
||||
inputStream >> multiplier;
|
||||
starChar = inputStream.get();
|
||||
if (starChar != '*')
|
||||
throw std::invalid_argument("Error ...");
|
||||
|
||||
defaultActive = (inputStream.peek() == std::char_traits<char>::eof());
|
||||
|
||||
if (defaultActive)
|
||||
value = defaultValue;
|
||||
else
|
||||
inputStream >> value;
|
||||
|
||||
for (size_t i = 0; i < multiplier; i++)
|
||||
dataVector.push_back(value);
|
||||
}
|
||||
} else
|
||||
fillVectorFromStringStream(inputStream , token , dataVector);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class T> std::deque<T> readFromRawRecord(RawRecordPtr rawRecord, bool scanAll, T defaultValue, bool& defaultActive) const {
|
||||
std::deque<T> data;
|
||||
if (rawRecord->size() == 0) {
|
||||
if (!scanAll)
|
||||
data.push_back(defaultValue);
|
||||
} else {
|
||||
bool continueReading = scanAll;
|
||||
do {
|
||||
std::string token = rawRecord->pop_front();
|
||||
fillVectorFromStringToken(token, data, defaultValue, defaultActive);
|
||||
|
||||
if (rawRecord->size() == 0)
|
||||
continueReading = false;
|
||||
|
||||
} while (continueReading);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
template <class T> void pushBackToRecord(RawRecordPtr rawRecord, std::deque<T>& data, bool defaultActive) const {
|
||||
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
if (defaultActive)
|
||||
rawRecord->push_front("*");
|
||||
else {
|
||||
T value = data[i];
|
||||
std::string stringValue = boost::lexical_cast<std::string>(value);
|
||||
|
||||
rawRecord->push_front(stringValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
#include <opm/parser/eclipse/Parser/ParserStringItem.hpp>
|
||||
#include <opm/parser/eclipse/Deck/DeckStringItem.hpp>
|
||||
|
||||
#include <opm/parser/eclipse/RawDeck/StarToken.hpp>
|
||||
namespace Opm {
|
||||
|
||||
ParserStringItem::ParserStringItem(const std::string& itemName) : ParserItem(itemName) {
|
||||
@@ -63,23 +63,53 @@ namespace Opm {
|
||||
/// returns a DeckItem object.
|
||||
/// NOTE: data are popped from the rawRecords deque!
|
||||
|
||||
DeckItemConstPtr ParserStringItem::scan(RawRecordPtr rawRecord) const {
|
||||
DeckItemConstPtr ParserStringItem::scan(RawRecordPtr rawRecord) const {
|
||||
DeckStringItemPtr deckItem(new DeckStringItem(name()));
|
||||
|
||||
bool scanAll = (sizeType() == ALL);
|
||||
bool defaultActive;
|
||||
std::deque<std::string> stringsPreparedForDeckItem = readFromRawRecord(rawRecord, scanAll, m_default, defaultActive);
|
||||
std::string defaultValue = m_default;
|
||||
|
||||
if (scanAll)
|
||||
deckItem->push_back(stringsPreparedForDeckItem);
|
||||
else {
|
||||
deckItem->push_back(stringsPreparedForDeckItem.front());
|
||||
stringsPreparedForDeckItem.pop_front();
|
||||
pushBackToRecord(rawRecord, stringsPreparedForDeckItem, defaultActive);
|
||||
if (sizeType() == ALL) { // This can probably not be combined with a default value ....
|
||||
// The '*' should be interpreted as a multiplication sign
|
||||
while (rawRecord->size() > 0) {
|
||||
std::string token = rawRecord->pop_front();
|
||||
if (tokenContainsStar( token )) {
|
||||
StarToken<std::string> st(token);
|
||||
std::string value = defaultValue; // This probably does never apply
|
||||
if (st.hasValue())
|
||||
value = st.value();
|
||||
deckItem->push_backMultiple( value , st.multiplier() );
|
||||
} else {
|
||||
std::string value = readValueToken<std::string>(token);
|
||||
deckItem->push_back(value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// The '*' should be interpreted as a default indicator
|
||||
if (rawRecord->size() > 0) {
|
||||
std::string token = rawRecord->pop_front();
|
||||
if (tokenContainsStar( token )) {
|
||||
StarToken<std::string> st(token);
|
||||
|
||||
if (st.hasValue()) { // Probably never true
|
||||
deckItem->push_back( st.value() );
|
||||
std::string stringValue = boost::lexical_cast<std::string>(st.value());
|
||||
for (size_t i=1; i < st.multiplier(); i++)
|
||||
rawRecord->push_front( stringValue );
|
||||
} else {
|
||||
deckItem->push_backDefault( defaultValue );
|
||||
for (size_t i=1; i < st.multiplier(); i++)
|
||||
rawRecord->push_front( "*" );
|
||||
}
|
||||
} else {
|
||||
std::string value = readValueToken<std::string>(token);
|
||||
deckItem->push_back(value);
|
||||
}
|
||||
} else
|
||||
deckItem->push_backDefault( defaultValue );
|
||||
}
|
||||
return deckItem;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool ParserStringItem::equal(const ParserStringItem& other) const
|
||||
{
|
||||
if (ParserItem::equal(other) && (getDefault() == other.getDefault()))
|
||||
|
||||
Reference in New Issue
Block a user