Added simple DeckOutput class for output.

This commit is contained in:
Joakim Hove 2017-08-30 16:18:29 +02:00
parent e8bfc517ee
commit da55685b62
4 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,61 @@
/*
Copyright 2017 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 DECK_OUTPUT_HPP
#define DECK_OUTPUT_HPP
#include <ostream>
#include <string>
#include <cstddef>
namespace Opm {
class DeckOutput {
public:
explicit DeckOutput(std::ostream& s);
void stash_default( );
void start_record( );
void end_record( );
void split_record();
void start_keyword(const std::string& kw);
void end_keyword(bool add_slash);
void endl();
void write_string(const std::string& s);
template <typename T> void write(const T& value);
std::string item_sep = " "; // Separator between items on a row.
size_t columns = 16; // The maximum number of columns on a record.
std::string record_indent = " "; // The indentation when starting a new line.
std::string keyword_sep = "\n\n"; // The separation between keywords;
private:
std::ostream& os;
size_t default_count;
size_t row_count;
bool record_on;
template <typename T> void write_value(const T& value);
void write_sep( );
};
}
#endif

View File

@ -5,6 +5,7 @@ set(genkw_SOURCES Parser/createDefaultKeywordList.cpp
Deck/DeckItem.cpp
Deck/DeckKeyword.cpp
Deck/DeckRecord.cpp
Deck/DeckOutput.cpp
Generator/KeywordGenerator.cpp
Generator/KeywordLoader.cpp
Parser/MessageContainer.cpp
@ -45,6 +46,7 @@ set(opmparser_SOURCES Deck/Deck.cpp
Deck/DeckItem.cpp
Deck/DeckKeyword.cpp
Deck/DeckRecord.cpp
Deck/DeckOutput.cpp
Deck/Section.cpp
EclipseState/checkDeck.cpp
EclipseState/Eclipse3DProperties.cpp

View File

@ -0,0 +1,123 @@
/*
Copyright 2017 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 <ostream>
#include <opm/parser/eclipse/Deck/DeckOutput.hpp>
namespace Opm {
DeckOutput::DeckOutput( std::ostream& s) :
os( s ),
default_count( 0 ),
row_count( 0 ),
record_on( false )
{}
void DeckOutput::endl() {
this->os << std::endl;
}
void DeckOutput::write_string(const std::string& s) {
this->os << s;
}
template <typename T>
void DeckOutput::write( const T& value ) {
if (default_count > 0) {
write_sep( );
os << default_count << "*";
default_count = 0;
row_count++;
}
write_sep( );
write_value( value );
row_count++;
}
template <>
void DeckOutput::write_value( const std::string& value ) {
this->os << "'" << value << "'";
}
template <>
void DeckOutput::write_value( const int& value ) {
this->os << value;
}
template <>
void DeckOutput::write_value( const double& value ) {
this->os << value;
}
void DeckOutput::stash_default( ) {
this->default_count++;
}
void DeckOutput::start_keyword(const std::string& kw) {
this->os << kw << std::endl;
}
void DeckOutput::end_keyword(bool add_slash) {
if (add_slash)
this->os << "/" << std::endl;
}
void DeckOutput::write_sep( ) {
if (record_on) {
if ((row_count > 0) && ((row_count % columns) == 0))
split_record();
}
if (row_count > 0)
os << item_sep;
else if (record_on)
os << record_indent;
}
void DeckOutput::start_record( ) {
this->default_count = 0;
this->row_count = 0;
this->record_on = true;
}
void DeckOutput::split_record() {
this->os << std::endl;
this->row_count = 0;
}
void DeckOutput::end_record( ) {
this->os << " /" << std::endl;
this->record_on = false;
}
template void DeckOutput::write( const int& value);
template void DeckOutput::write( const double& value);
template void DeckOutput::write( const std::string& value);
}

View File

@ -19,11 +19,13 @@
#include <stdexcept>
#include <sstream>
#define BOOST_TEST_MODULE DeckTests
#include <boost/test/unit_test.hpp>
#include <opm/parser/eclipse/Deck/DeckOutput.hpp>
#include <opm/parser/eclipse/Deck/Deck.hpp>
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
#include <opm/parser/eclipse/Parser/ParseContext.hpp>
@ -559,3 +561,39 @@ BOOST_AUTO_TEST_CASE(setUnknown_wasknown_nowunknown) {
DeckKeyword deckKeyword( "KW", false );
BOOST_CHECK(!deckKeyword.isKnown());
}
BOOST_AUTO_TEST_CASE(DeckOutputTest) {
std::string expected = "KEYWORD\n\
==1-2\n\
==3-1*\n\
==5-1*\n\
==7-8\n\
==1*-10 /\n\
/\n\
ABC";
std::stringstream s;
DeckOutput out(s);
out.record_indent = "==";
out.item_sep = "-";
out.columns = 2;
out.keyword_sep = "ABC";
out.start_keyword("KEYWORD");
out.start_record();
out.write<int>(1);
out.write<int>(2);
out.write<int>(3);
out.stash_default( );
out.write<int>(5);
out.stash_default( );
out.write<int>(7);
out.write<int>(8);
out.stash_default( );
out.write<int>(10);
out.end_record();
out.end_keyword(true);
out.write_string( out.keyword_sep );
BOOST_CHECK_EQUAL( expected, s.str());
}