#8196 GRDECL import : Handle multiple items of same value

This commit is contained in:
Magne Sjaastad 2021-10-26 21:11:39 +02:00
parent 32ac5b8e5c
commit da74a4f0d6
3 changed files with 63 additions and 12 deletions

View File

@ -26,6 +26,7 @@
#include "mio/mio.hpp"
#include "RiaPreferencesSystem.h"
#include "RiaStdStringTools.h"
#include <fstream>
#include <iosfwd>
@ -90,7 +91,7 @@ std::pair<std::string, std::vector<float>>
const auto commentChar = '-';
std::string keywordName;
std::string line;
std::string_view line;
bool isEndTokenKeywordRead = false;
bytesRead = 0;
@ -134,17 +135,39 @@ std::pair<std::string, std::vector<float>>
size_t start = 0;
size_t end = 0;
// Index in token for the '*' character used to define a multiplier for the float value
//
size_t multiplierIndex = 0;
while ( ( start = line.find_first_not_of( RifEclipseTextFileReader::m_whiteSpace, end ) ) != std::string::npos )
{
end = line.find_first_of( RifEclipseTextFileReader::m_whiteSpace, start );
int multiplier = 1;
multiplierIndex = line.find_first_of( '*', start );
if ( multiplierIndex < end )
{
int multiplierCandidate = 1;
if ( RiaStdStringTools::toInt( line.substr( start, multiplierIndex - start ), multiplierCandidate ) )
{
multiplier = multiplierCandidate;
}
start = multiplierIndex + 1;
}
auto resultObject = fast_float::from_chars( line.data() + start, line.data() + end, value );
if ( resultObject.ec == std::errc() )
{
for ( size_t i = 0; i < static_cast<size_t>( multiplier ); i++ )
{
values.emplace_back( value );
}
}
}
}
// End of keyword is defined by '/'
if ( line.find_first_of( '/' ) != std::string::npos )
@ -226,24 +249,30 @@ std::string_view RifEclipseTextFileReader::readLine( const std::string_view& sou
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseTextFileReader::rtrim( std::string& s, const char* t /*= ws */ )
void RifEclipseTextFileReader::rtrim( std::string_view& s, const char* t /*= ws */ )
{
s.erase( s.find_last_not_of( t ) + 1 );
if ( s.empty() ) return;
s = s.substr( 0, s.find_last_not_of( t ) + 1 );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseTextFileReader::ltrim( std::string& s, const char* t /*= ws */ )
void RifEclipseTextFileReader::ltrim( std::string_view& s, const char* t /*= ws */ )
{
s.erase( 0, s.find_first_not_of( t ) );
if ( s.empty() ) return;
s = s.substr( s.find_first_not_of( t ), s.size() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseTextFileReader::trim( std::string& s, const char* t /*= ws */ )
void RifEclipseTextFileReader::trim( std::string_view& s, const char* t /*= ws */ )
{
if ( s.empty() ) return;
rtrim( s, t );
ltrim( s, t );
}

View File

@ -51,13 +51,13 @@ public:
static std::string_view readLine( const std::string_view& source, const size_t offset, size_t& bytesRead );
// trim from end of string (right)
static void rtrim( std::string& s, const char* t = m_whiteSpace );
static void rtrim( std::string_view& s, const char* t = m_whiteSpace );
// trim from beginning of string (left)
static void ltrim( std::string& s, const char* t = m_whiteSpace );
static void ltrim( std::string_view& s, const char* t = m_whiteSpace );
// trim from both ends of string (right then left)
static void trim( std::string& s, const char* t = m_whiteSpace );
static void trim( std::string_view& s, const char* t = m_whiteSpace );
// Parse string data for Eclipse keywords
static std::vector<RifEclipseKeywordContent> parseStringData( const std::string_view& stringData );

View File

@ -159,3 +159,25 @@ TEST( RifEclipseTextFileReader, ReadLine_TwoLinesWithLineBreak )
EXPECT_EQ( size_t( 10 ), bytesRead );
EXPECT_EQ( size_t( 9 ), secondLine.size() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifEclipseTextFileReader, ValueMultiplier )
{
std::string fileContent = "ZCORN\n"
"2*2.21 0.5 3*12345.12\n"
"/\n";
auto keywordDataItems = RifEclipseTextFileReader::parseStringData( fileContent );
EXPECT_EQ( size_t( 1 ), keywordDataItems.size() );
auto firstKeyword = keywordDataItems.front();
EXPECT_EQ( size_t( 6 ), firstKeyword.values.size() );
EXPECT_FLOAT_EQ( 2.21f, firstKeyword.values[0] );
EXPECT_FLOAT_EQ( 2.21f, firstKeyword.values[1] );
EXPECT_FLOAT_EQ( 0.5f, firstKeyword.values[2] );
EXPECT_FLOAT_EQ( 12345.12f, firstKeyword.values[3] );
}