#4504 Framework : Add CSV text formatter

This commit is contained in:
Magne Sjaastad 2019-06-23 18:05:51 +02:00
parent 7879fe186d
commit 5dc4e08a03
6 changed files with 245 additions and 4 deletions

View File

@ -44,6 +44,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RifFileParseTools.h
${CMAKE_CURRENT_LIST_DIR}/RifEnsembleStatisticsReader.h
${CMAKE_CURRENT_LIST_DIR}/RifDerivedEnsembleReader.h
${CMAKE_CURRENT_LIST_DIR}/RifActiveCellsReader.h
${CMAKE_CURRENT_LIST_DIR}/RifCsvDataTableFormatter.h
# HDF5 file reader is directly included in ResInsight main CmakeList.txt
@ -93,6 +94,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RifFileParseTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RifEnsembleStatisticsReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifDerivedEnsembleReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifActiveCellsReader.cpp
${CMAKE_CURRENT_LIST_DIR}/RifCsvDataTableFormatter.cpp
# HDF5 file reader is directly included in ResInsight main CmakeList.txt
#${CMAKE_CURRENT_LIST_DIR}/RifHdf5Reader.cpp

View File

@ -0,0 +1,138 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- Equinor ASA
//
// ResInsight 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.
//
// ResInsight 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 at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RifCsvDataTableFormatter.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifCsvDataTableFormatter::RifCsvDataTableFormatter(QTextStream& out, const QString fieldSeparator)
: m_out(out)
, m_fieldSeparator(fieldSeparator)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifCsvDataTableFormatter& RifCsvDataTableFormatter::header(const std::vector<RifEclipseOutputTableColumn>& tableHeader)
{
outputBuffer();
m_columnHeaders = tableHeader;
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifCsvDataTableFormatter& RifCsvDataTableFormatter::add(const QString& str)
{
QString quotedString = "\"" + str + "\"";
m_lineBuffer.push_back(quotedString);
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifCsvDataTableFormatter& RifCsvDataTableFormatter::add(double num)
{
size_t column = m_lineBuffer.size();
m_lineBuffer.push_back(RifEclipseDataTableFormatter::format(num, m_columnHeaders[column].doubleFormat));
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifCsvDataTableFormatter& RifCsvDataTableFormatter::add(int num)
{
m_lineBuffer.push_back(RifEclipseDataTableFormatter::format(num));
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RifCsvDataTableFormatter& RifCsvDataTableFormatter::add(size_t num)
{
m_lineBuffer.push_back(RifEclipseDataTableFormatter::format(num));
return *this;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifCsvDataTableFormatter::rowCompleted()
{
RifEclipseOutputTableLine line;
line.data = m_lineBuffer;
line.lineType = CONTENTS;
line.appendTextSet = false;
m_buffer.push_back(line);
m_lineBuffer.clear();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifCsvDataTableFormatter::tableCompleted()
{
outputBuffer();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifCsvDataTableFormatter::outputBuffer()
{
if (!m_columnHeaders.empty())
{
for (size_t i = 0; i < m_columnHeaders.size(); i++)
{
m_out << m_columnHeaders[i].title;
if (i < m_columnHeaders.size() - 1)
{
m_out << m_fieldSeparator;
}
}
m_out << "\n";
}
for (const auto& line : m_buffer)
{
if (line.lineType == CONTENTS)
{
QString lineText;
for (size_t i = 0; i < line.data.size(); i++)
{
lineText += line.data[i];
if (i < line.data.size() - 1)
{
lineText += m_fieldSeparator;
}
}
m_out << lineText << "\n";
}
}
m_columnHeaders.clear();
m_buffer.clear();
}

View File

@ -0,0 +1,50 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- Equinor ASA
//
// ResInsight 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.
//
// ResInsight 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 at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "RifEclipseDataTableFormatter.h"
//==================================================================================================
//
// CSV text formatter using the same pattern as RifEclipseDataTableFormatter so it will be easy to switch formatters
//
//==================================================================================================
class RifCsvDataTableFormatter
{
public:
RifCsvDataTableFormatter(QTextStream& out, const QString fieldSeparator = ",");
RifCsvDataTableFormatter& header(const std::vector<RifEclipseOutputTableColumn>& tableHeader);
RifCsvDataTableFormatter& add(const QString& str);
RifCsvDataTableFormatter& add(double num);
RifCsvDataTableFormatter& add(int num);
RifCsvDataTableFormatter& add(size_t num);
void rowCompleted();
void tableCompleted();
private:
void outputBuffer();
private:
QTextStream& m_out;
std::vector<RifEclipseOutputTableColumn> m_columnHeaders;
std::vector<RifEclipseOutputTableLine> m_buffer;
std::vector<QString> m_lineBuffer;
QString m_fieldSeparator;
};

View File

@ -139,15 +139,17 @@ public:
int tableWidth() const;
private:
protected:
friend class RifCsvDataTableFormatter;
int measure(const QString str);
int measure(double num, RifEclipseOutputTableDoubleFormatting doubleFormat);
int measure(int num);
int measure(size_t num);
QString format(double num, RifEclipseOutputTableDoubleFormatting doubleFormat);
QString format(int num);
QString format(size_t num);
static QString format(double num, RifEclipseOutputTableDoubleFormatting doubleFormat);
static QString format(int num);
static QString format(size_t num);
QString formatColumn(const QString str, size_t columnIndex) const;
void outputBuffer();

View File

@ -56,6 +56,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RifPerforationIntervalReader-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RimWellPathCompletions-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RimSummaryCaseCollection-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifActiveCellsReader-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifCsvDataTableFormatter-Test.cpp
)
if (RESINSIGHT_ENABLE_GRPC)

View File

@ -0,0 +1,48 @@
#include "gtest/gtest.h"
#include "RifCsvDataTableFormatter.h"
#include <QString>
#include <QStringList>
TEST(RifCsvDataTableFormatter, BasicUsage)
{
QString tableText;
QTextStream stream(&tableText);
RifCsvDataTableFormatter formatter(stream, ";");
std::vector<RifEclipseOutputTableColumn> header = {
RifEclipseOutputTableColumn("Well"),
RifEclipseOutputTableColumn("Integer Number"),
RifEclipseOutputTableColumn("sci", RifEclipseOutputTableDoubleFormat::RIF_SCIENTIFIC),
RifEclipseOutputTableColumn("float", RifEclipseOutputTableDoubleFormat::RIF_FLOAT),
RifEclipseOutputTableColumn("consise", RifEclipseOutputTableDoubleFormat::RIF_CONSISE),
};
formatter.header(header);
formatter.add("well a");
formatter.add(1);
formatter.add(2.123456789);
formatter.add(2.123456789);
formatter.add(2.123456789);
formatter.rowCompleted();
formatter.add("well B");
formatter.add(12);
formatter.add(0.3e-12);
formatter.add(0.3e-12);
formatter.add(0.3e-12);
formatter.rowCompleted();
formatter.add("well c");
formatter.add(123);
formatter.add(0.3e+12);
formatter.add(0.3e+12);
formatter.add(0.3e+12);
formatter.rowCompleted();
formatter.tableCompleted();
std::cout << tableText.toStdString();
}