mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Replace RifRoffReader code to utilize roffcpp::reader (#10098)
- Replace current content if RifRoffReader to utilize roffcpp - Remove unit tests not applicable for function (some previous tests now belongs to roffcpp)
This commit is contained in:
parent
6f6dc80bc6
commit
c7b915c141
@ -48,7 +48,8 @@ void RicFaciesPropertiesImportTools::importFaciesPropertiesFromFile( const QStri
|
|||||||
std::map<int, QString> codeNames;
|
std::map<int, QString> codeNames;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
RifRoffReader::readCodeNames( filePath, codeNames );
|
const QString parameterTagName = "composite";
|
||||||
|
RifRoffReader::readCodeNames( filePath, parameterTagName, codeNames );
|
||||||
}
|
}
|
||||||
catch ( RifRoffReaderException& ex )
|
catch ( RifRoffReaderException& ex )
|
||||||
{
|
{
|
||||||
|
@ -21,121 +21,48 @@
|
|||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
const QString codeValuesString = QString( "array int codeValues" );
|
#include "roffcpp/src/Reader.hpp"
|
||||||
const QString codeNamesString = QString( "array char codeNames" );
|
|
||||||
const QString headerString = QString( "roff-asc" );
|
|
||||||
|
|
||||||
bool RifRoffReader::isCodeValuesDefinition( const QString& line )
|
#include <fstream>
|
||||||
|
|
||||||
|
void RifRoffReader::readCodeNames( const QString& filename, const QString& parameterTagName, std::map<int, QString>& codeNames )
|
||||||
{
|
{
|
||||||
return line.startsWith( codeValuesString );
|
codeNames.clear();
|
||||||
}
|
|
||||||
|
|
||||||
bool RifRoffReader::isCodeNamesDefinition( const QString& line )
|
std::ifstream stream( filename.toStdString(), std::ios::binary );
|
||||||
{
|
if ( !stream.good() )
|
||||||
return line.startsWith( codeNamesString );
|
|
||||||
}
|
|
||||||
|
|
||||||
bool RifRoffReader::isCorrectHeader( const QString& line )
|
|
||||||
{
|
|
||||||
return line.startsWith( headerString );
|
|
||||||
}
|
|
||||||
|
|
||||||
int RifRoffReader::extractNumberAfterString( const QString& line, const QString& prefix )
|
|
||||||
{
|
|
||||||
QString copiedLine( line );
|
|
||||||
|
|
||||||
bool ok;
|
|
||||||
int num = copiedLine.remove( prefix ).toInt( &ok );
|
|
||||||
if ( !ok )
|
|
||||||
{
|
|
||||||
throw RifRoffReaderException( "Unexpected value: not an integer." );
|
|
||||||
}
|
|
||||||
return num;
|
|
||||||
}
|
|
||||||
|
|
||||||
int RifRoffReader::extractCodeValuesCount( const QString& line )
|
|
||||||
{
|
|
||||||
return extractNumberAfterString( line, codeValuesString );
|
|
||||||
}
|
|
||||||
|
|
||||||
int RifRoffReader::extractCodeNamesCount( const QString& line )
|
|
||||||
{
|
|
||||||
return extractNumberAfterString( line, codeNamesString );
|
|
||||||
}
|
|
||||||
|
|
||||||
void RifRoffReader::readCodeNames( const QString& filename, std::map<int, QString>& codeNames )
|
|
||||||
{
|
|
||||||
QFile file( filename );
|
|
||||||
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
|
|
||||||
{
|
{
|
||||||
throw RifRoffReaderException( "Unable to open roff file." );
|
throw RifRoffReaderException( "Unable to open roff file." );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isFirstLine = true;
|
roff::Reader reader( stream );
|
||||||
int numCodeValues = -1;
|
reader.parse();
|
||||||
int numCodeNames = -1;
|
|
||||||
|
|
||||||
std::vector<int> readCodeValues;
|
std::vector<std::pair<std::string, roff::Token::Kind>> arrayTypes = reader.getNamedArrayTypes();
|
||||||
std::vector<QString> readCodeNames;
|
|
||||||
|
|
||||||
QTextStream in( &file );
|
// Find array types by keywords
|
||||||
while ( !in.atEnd() )
|
const std::string codeNamesKeyword = parameterTagName.toStdString() + ".codeNames";
|
||||||
{
|
const std::string codeValuesKeyword = parameterTagName.toStdString() + ".codeValues";
|
||||||
QString line = in.readLine();
|
auto codeNamesItr = std::find_if( arrayTypes.begin(),
|
||||||
|
arrayTypes.end(),
|
||||||
|
[&codeNamesKeyword]( const auto& arrayType ) { return arrayType.first == codeNamesKeyword; } );
|
||||||
|
auto codeValuesItr = std::find_if( arrayTypes.begin(),
|
||||||
|
arrayTypes.end(),
|
||||||
|
[&codeValuesKeyword]( const auto& arrayType ) { return arrayType.first == codeValuesKeyword; } );
|
||||||
|
|
||||||
// Check that the first line has the roff-asc header
|
if ( codeNamesItr == arrayTypes.end() ) throw RifRoffReaderException( "Code names not found." );
|
||||||
if ( isFirstLine )
|
if ( codeValuesItr == arrayTypes.end() ) throw RifRoffReaderException( "Code values not found." );
|
||||||
{
|
|
||||||
if ( !isCorrectHeader( line ) )
|
|
||||||
{
|
|
||||||
throw RifRoffReaderException( "Unexpected file type: roff-asc header missing." );
|
|
||||||
}
|
|
||||||
isFirstLine = false;
|
|
||||||
}
|
|
||||||
else if ( isCodeValuesDefinition( line ) )
|
|
||||||
{
|
|
||||||
// Expected line:
|
|
||||||
// array int codeValues 99
|
|
||||||
numCodeValues = extractCodeValuesCount( line );
|
|
||||||
for ( int i = 0; i < numCodeValues; i++ )
|
|
||||||
{
|
|
||||||
// The code values comes next, can be multiple per line.
|
|
||||||
int codeValue;
|
|
||||||
in >> codeValue;
|
|
||||||
readCodeValues.push_back( codeValue );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if ( isCodeNamesDefinition( line ) )
|
|
||||||
{
|
|
||||||
// Expected line:
|
|
||||||
// array char codeNames 99
|
|
||||||
numCodeNames = extractCodeNamesCount( line );
|
|
||||||
for ( int i = 0; i < numCodeNames; i++ )
|
|
||||||
{
|
|
||||||
// Read code names. Assumes one name per line.
|
|
||||||
QString codeName = in.readLine();
|
|
||||||
readCodeNames.push_back( codeName.trimmed().remove( "\"" ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( numCodeValues == -1 )
|
const auto readCodeNames = reader.getStringArray( codeNamesKeyword );
|
||||||
{
|
const auto readCodeValues = reader.getIntArray( codeValuesKeyword );
|
||||||
throw RifRoffReaderException( "Code values not found." );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( numCodeNames == -1 )
|
if ( readCodeNames.size() != readCodeValues.size() )
|
||||||
{
|
|
||||||
throw RifRoffReaderException( "Code names not found." );
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( numCodeNames != numCodeValues )
|
|
||||||
{
|
{
|
||||||
throw RifRoffReaderException( "Inconsistent code names and values: must be equal length." );
|
throw RifRoffReaderException( "Inconsistent code names and values: must be equal length." );
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( int i = 0; i < numCodeNames; i++ )
|
for ( size_t i = 0; i < readCodeNames.size(); ++i )
|
||||||
{
|
{
|
||||||
codeNames[readCodeValues[i]] = readCodeNames[i];
|
codeNames[readCodeValues[i]] = QString::fromStdString( readCodeNames[i] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,14 +45,5 @@ class RifRoffReader
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// Throws RifRoffReaderException on error
|
// Throws RifRoffReaderException on error
|
||||||
static void readCodeNames( const QString& filename, std::map<int, QString>& codeNames );
|
static void readCodeNames( const QString& filename, const QString& parameterTagName, std::map<int, QString>& codeNames );
|
||||||
|
|
||||||
private:
|
|
||||||
static bool isCorrectHeader( const QString& line );
|
|
||||||
static bool isCodeNamesDefinition( const QString& line );
|
|
||||||
static bool isCodeValuesDefinition( const QString& line );
|
|
||||||
|
|
||||||
static int extractNumberAfterString( const QString& line, const QString& prefix );
|
|
||||||
static int extractCodeNamesCount( const QString& line );
|
|
||||||
static int extractCodeValuesCount( const QString& line );
|
|
||||||
};
|
};
|
||||||
|
@ -16,7 +16,7 @@ TEST( RifRoffReader, ReadValidFile )
|
|||||||
EXPECT_TRUE( QFile::exists( filePath ) );
|
EXPECT_TRUE( QFile::exists( filePath ) );
|
||||||
|
|
||||||
std::map<int, QString> codeNames;
|
std::map<int, QString> codeNames;
|
||||||
RifRoffReader::readCodeNames( filePath, codeNames );
|
RifRoffReader::readCodeNames( filePath, "composite", codeNames );
|
||||||
|
|
||||||
ASSERT_EQ( 6u, codeNames.size() );
|
ASSERT_EQ( 6u, codeNames.size() );
|
||||||
for ( int i = 0; i < 6; i++ )
|
for ( int i = 0; i < 6; i++ )
|
||||||
@ -35,7 +35,7 @@ std::string readIncorrectFile( const QString filename )
|
|||||||
std::map<int, QString> codeNames;
|
std::map<int, QString> codeNames;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
RifRoffReader::readCodeNames( filePath, codeNames );
|
RifRoffReader::readCodeNames( filePath, "composite", codeNames );
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
catch ( RifRoffReaderException& ex )
|
catch ( RifRoffReaderException& ex )
|
||||||
@ -44,13 +44,6 @@ std::string readIncorrectFile( const QString filename )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST( RifRoffReader, ReadWrongFileType )
|
|
||||||
{
|
|
||||||
// Read a surface file: no expected to work
|
|
||||||
QString filename( "RifSurfaceImporter/test.ptl" );
|
|
||||||
ASSERT_EQ( readIncorrectFile( filename ), std::string( "Unexpected file type: roff-asc header missing." ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST( RifRoffReader, ReadNonExistingFileType )
|
TEST( RifRoffReader, ReadNonExistingFileType )
|
||||||
{
|
{
|
||||||
// Read a non-existing file
|
// Read a non-existing file
|
||||||
@ -58,13 +51,6 @@ TEST( RifRoffReader, ReadNonExistingFileType )
|
|||||||
ASSERT_EQ( readIncorrectFile( filename ), std::string( "Unable to open roff file." ) );
|
ASSERT_EQ( readIncorrectFile( filename ), std::string( "Unable to open roff file." ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST( RifRoffReader, ReadFileWithIncorrectInteger )
|
|
||||||
{
|
|
||||||
// Read a file with incorrect integer for code values
|
|
||||||
QString filename( "RifRoffReader/code_values_integer_wrong.roff" );
|
|
||||||
ASSERT_EQ( readIncorrectFile( filename ), std::string( "Unexpected value: not an integer." ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST( RifRoffReader, ReadFileCodeNamesMissing )
|
TEST( RifRoffReader, ReadFileCodeNamesMissing )
|
||||||
{
|
{
|
||||||
// Read a file without code names
|
// Read a file without code names
|
||||||
|
Loading…
Reference in New Issue
Block a user