DeckRecordConstPtr ParserRecord::parse(RawRecordPtr rawRecord) const { std::string recordBeforeParsing = rawRecord->getRecordString(); DeckRecordPtr deckRecord(new DeckRecord()); for (size_t i = 0; i < size(); i++) { ParserItemConstPtr parserItem = get(i); DeckItemPtr deckItem = parserItem->scan(rawRecord); deckRecord->addItem(deckItem); } const size_t recordSize = rawRecord->size(); if (recordSize > 0) throw std::invalid_argument("The RawRecord for keyword \"" + rawRecord->getKeywordName() + "\" in file\"" + rawRecord->getFileName() + "\" contained " + boost::lexical_cast<std::string>(recordSize) + " too many items according to the spec. RawRecord was: " + recordBeforeParsing); return deckRecord; }
/// Scans the rawRecords data according to the ParserItems definition. /// returns a DeckItem object. /// NOTE: data are popped from the rawRecords deque! DeckItemPtr ParserIntItem::scan(RawRecordPtr rawRecord) const { DeckIntItemPtr deckItem(new DeckIntItem( name() , scalar() )); int defaultValue = m_default; if (sizeType() == ALL) { // The '*' should be interpreted either as a default indicator in the cases '*' and '5*' // or as multiplier in the case: '5*99'. while (rawRecord->size() > 0) { std::string token = rawRecord->pop_front(); if (tokenContainsStar( token )) { StarToken<int> st(token); int value = defaultValue; if (st.hasValue()) value = st.value(); deckItem->push_backMultiple( value , st.multiplier() ); } else { int value = readValueToken<int>(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<int> 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 { int value = readValueToken<int>(token); deckItem->push_back(value); } } else deckItem->push_backDefault( defaultValue ); } return deckItem; }
/// Scans the rawRecords data according to the ParserItems definition. /// returns a DeckItem object. /// NOTE: data are popped from the rawRecords deque! DeckItemPtr ParserFloatItem::scan(RawRecordPtr rawRecord) const { DeckFloatItemPtr deckItem(new DeckFloatItem(name())); float 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<float> st(token); float value = defaultValue; // This probably does never apply if (st.hasValue()) value = st.value(); deckItem->push_backMultiple( value , st.multiplier() ); } else { float value = readValueToken<float>(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<float> 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 { float value = readValueToken<float>(token); deckItem->push_back(value); } } else deckItem->push_backDefault( defaultValue ); } return deckItem; }