diff --git a/ApplicationLibCode/Commands/RicFaciesPropertiesImportTools.cpp b/ApplicationLibCode/Commands/RicFaciesPropertiesImportTools.cpp index e5d982f0fb..e11cbbfdcb 100644 --- a/ApplicationLibCode/Commands/RicFaciesPropertiesImportTools.cpp +++ b/ApplicationLibCode/Commands/RicFaciesPropertiesImportTools.cpp @@ -48,7 +48,8 @@ void RicFaciesPropertiesImportTools::importFaciesPropertiesFromFile( const QStri std::map codeNames; try { - RifRoffReader::readCodeNames( filePath, codeNames ); + const QString parameterTagName = "composite"; + RifRoffReader::readCodeNames( filePath, parameterTagName, codeNames ); } catch ( RifRoffReaderException& ex ) { diff --git a/ApplicationLibCode/FileInterface/RifRoffReader.cpp b/ApplicationLibCode/FileInterface/RifRoffReader.cpp index b29a6a4f42..f4d0208ac6 100644 --- a/ApplicationLibCode/FileInterface/RifRoffReader.cpp +++ b/ApplicationLibCode/FileInterface/RifRoffReader.cpp @@ -21,121 +21,48 @@ #include #include -const QString codeValuesString = QString( "array int codeValues" ); -const QString codeNamesString = QString( "array char codeNames" ); -const QString headerString = QString( "roff-asc" ); +#include "roffcpp/src/Reader.hpp" -bool RifRoffReader::isCodeValuesDefinition( const QString& line ) +#include + +void RifRoffReader::readCodeNames( const QString& filename, const QString& parameterTagName, std::map& codeNames ) { - return line.startsWith( codeValuesString ); -} + codeNames.clear(); -bool RifRoffReader::isCodeNamesDefinition( const QString& line ) -{ - 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& codeNames ) -{ - QFile file( filename ); - if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) + std::ifstream stream( filename.toStdString(), std::ios::binary ); + if ( !stream.good() ) { throw RifRoffReaderException( "Unable to open roff file." ); } - bool isFirstLine = true; - int numCodeValues = -1; - int numCodeNames = -1; + roff::Reader reader( stream ); + reader.parse(); - std::vector readCodeValues; - std::vector readCodeNames; + std::vector> arrayTypes = reader.getNamedArrayTypes(); - QTextStream in( &file ); - while ( !in.atEnd() ) - { - QString line = in.readLine(); + // Find array types by keywords + const std::string codeNamesKeyword = parameterTagName.toStdString() + ".codeNames"; + const std::string codeValuesKeyword = parameterTagName.toStdString() + ".codeValues"; + 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 ( isFirstLine ) - { - 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 ( codeNamesItr == arrayTypes.end() ) throw RifRoffReaderException( "Code names not found." ); + if ( codeValuesItr == arrayTypes.end() ) throw RifRoffReaderException( "Code values not found." ); - if ( numCodeValues == -1 ) - { - throw RifRoffReaderException( "Code values not found." ); - } + const auto readCodeNames = reader.getStringArray( codeNamesKeyword ); + const auto readCodeValues = reader.getIntArray( codeValuesKeyword ); - if ( numCodeNames == -1 ) - { - throw RifRoffReaderException( "Code names not found." ); - } - - if ( numCodeNames != numCodeValues ) + if ( readCodeNames.size() != readCodeValues.size() ) { 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] ); } } diff --git a/ApplicationLibCode/FileInterface/RifRoffReader.h b/ApplicationLibCode/FileInterface/RifRoffReader.h index 0073f019e5..7688a87d6f 100644 --- a/ApplicationLibCode/FileInterface/RifRoffReader.h +++ b/ApplicationLibCode/FileInterface/RifRoffReader.h @@ -45,14 +45,5 @@ class RifRoffReader { public: // Throws RifRoffReaderException on error - static void readCodeNames( const QString& filename, std::map& 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 ); + static void readCodeNames( const QString& filename, const QString& parameterTagName, std::map& codeNames ); }; diff --git a/ApplicationLibCode/UnitTests/RifRoffReader-Test.cpp b/ApplicationLibCode/UnitTests/RifRoffReader-Test.cpp index 444d881978..b26ffe99fb 100644 --- a/ApplicationLibCode/UnitTests/RifRoffReader-Test.cpp +++ b/ApplicationLibCode/UnitTests/RifRoffReader-Test.cpp @@ -16,7 +16,7 @@ TEST( RifRoffReader, ReadValidFile ) EXPECT_TRUE( QFile::exists( filePath ) ); std::map codeNames; - RifRoffReader::readCodeNames( filePath, codeNames ); + RifRoffReader::readCodeNames( filePath, "composite", codeNames ); ASSERT_EQ( 6u, codeNames.size() ); for ( int i = 0; i < 6; i++ ) @@ -35,7 +35,7 @@ std::string readIncorrectFile( const QString filename ) std::map codeNames; try { - RifRoffReader::readCodeNames( filePath, codeNames ); + RifRoffReader::readCodeNames( filePath, "composite", codeNames ); return ""; } 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 ) { // Read a non-existing file @@ -58,13 +51,6 @@ TEST( RifRoffReader, ReadNonExistingFileType ) 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 ) { // Read a file without code names