Refactor RifFormationNamesReader to return std::expected

Replace the QString* errorMessage out-parameter with std::expected<RigFormationNames, QString> in RifFormationNamesReader, and propagate the pattern to RimFormationNames::readFormationNamesFile() which now returns std::expected<void, QString>.

Fatal errors (file cannot be opened, FMU line-parse failure) return std::unexpected. Malformed .lyr lines are still skipped so the rest of the file loads, with warnings logged via RiaLogging instead of collected in the out-parameter. RimFormationNamesCollection::readAllFormationNames() now logs errors it previously discarded.
This commit is contained in:
Kristian Bendiksen committed 2026-08-05 16:03:42 +02:00
1 parent db36c761dd
commit d87a0d3048
9 files changed
+66 -75

No files matched your search

@@ -18,11 +18,10 @@
#include "RifFormationNamesReader.h"
#include "RiaLogging.h"
#include "RiaTextStringTools.h"
#include "RigFormationNames.h"
#include <memory>
#include "cafAssert.h"
#include "cafPdmUiFilePathEditor.h"
@@ -34,35 +33,34 @@
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::unique_ptr<RigFormationNames> RifFormationNamesReader::readFormationNamesFile( const QString& fileName, QString* errorMessage )
std::expected<RigFormationNames, QString> RifFormationNamesReader::readFormationNamesFile( const QString& fileName )
{
QFileInfo fileInfo( fileName );
if ( fileInfo.fileName() == "layer_zone_table.txt" )
{
return RifFormationNamesReader::readFmuFormationNameFile( fileName, errorMessage );
return RifFormationNamesReader::readFmuFormationNameFile( fileName );
}
else
{
return RifFormationNamesReader::readLyrFormationNameFile( fileName, errorMessage );
return RifFormationNamesReader::readLyrFormationNameFile( fileName );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::unique_ptr<RigFormationNames> RifFormationNamesReader::readLyrFormationNameFile( const QString& fileName, QString* errorMessage )
std::expected<RigFormationNames, QString> RifFormationNamesReader::readLyrFormationNameFile( const QString& fileName )
{
QFile dataFile( fileName );
if ( !dataFile.open( QFile::ReadOnly ) )
{
if ( errorMessage ) ( *errorMessage ) += "Could not open file: " + fileName + "\n";
return nullptr;
return std::unexpected( "Could not open file: " + fileName );
}
auto formationNames = std::make_unique<RigFormationNames>();
QTextStream stream( &dataFile );
RigFormationNames formationNames;
QTextStream stream( &dataFile );
int lineNumber = 1;
while ( !stream.atEnd() )
@@ -74,7 +72,7 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readLyrFormationName
if ( lineSegs.size() == 1 ) continue; // No name present. Comment line ?
if ( lineSegs.size() == 2 )
{
if ( errorMessage ) ( *errorMessage ) += "Missing quote on line : " + QString::number( lineNumber ) + "\n";
RiaLogging::warning( ( fileName + ": missing quote on line " + QString::number( lineNumber ) ).toStdString() );
continue; // One quote present
}
@@ -103,7 +101,7 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readLyrFormationName
if ( !( isNumber2 && isNumber1 ) )
{
if ( errorMessage ) ( *errorMessage ) += "Format error on line: " + QString::number( lineNumber ) + "\n";
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
continue;
}
@@ -116,11 +114,11 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readLyrFormationName
cvf::Color3f formationColor;
convertStringToColor( colorWord, &formationColor );
formationNames->appendFormationRange( formationName, formationColor, startK - 1, endK - 1 );
formationNames.appendFormationRange( formationName, formationColor, startK - 1, endK - 1 );
}
else // no color present
{
formationNames->appendFormationRange( formationName, startK - 1, endK - 1 );
formationNames.appendFormationRange( formationName, startK - 1, endK - 1 );
}
}
else if ( numberWords.size() == 1 )
@@ -130,7 +128,7 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readLyrFormationName
if ( !isNumber1 )
{
if ( errorMessage ) ( *errorMessage ) += "Format error on line: " + QString::number( lineNumber ) + "\n";
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
continue;
}
@@ -139,16 +137,16 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readLyrFormationName
cvf::Color3f formationColor;
convertStringToColor( colorWord, &formationColor );
formationNames->appendFormationRangeHeight( formationName, formationColor, kLayerCount );
formationNames.appendFormationRangeHeight( formationName, formationColor, kLayerCount );
}
else // no color present
{
formationNames->appendFormationRangeHeight( formationName, kLayerCount );
formationNames.appendFormationRangeHeight( formationName, kLayerCount );
}
}
else
{
if ( errorMessage ) ( *errorMessage ) += "Format error on line: " + QString::number( lineNumber ) + "\n";
RiaLogging::warning( ( fileName + ": format error on line " + QString::number( lineNumber ) ).toStdString() );
}
}
@@ -161,18 +159,17 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readLyrFormationName
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::unique_ptr<RigFormationNames> RifFormationNamesReader::readFmuFormationNameFile( const QString& fileName, QString* errorMessage )
std::expected<RigFormationNames, QString> RifFormationNamesReader::readFmuFormationNameFile( const QString& fileName )
{
QFile dataFile( fileName );
if ( !dataFile.open( QFile::ReadOnly ) )
{
if ( errorMessage ) ( *errorMessage ) += "Could not open file: " + fileName + "\n";
return nullptr;
return std::unexpected( "Could not open file: " + fileName );
}
auto formationNames = std::make_unique<RigFormationNames>();
QTextStream stream( &dataFile );
RigFormationNames formationNames;
QTextStream stream( &dataFile );
int lineNumber = 1;
@@ -188,7 +185,7 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readFmuFormationName
// Make sure we append the last formation
if ( !currentFormationName.isEmpty() )
{
formationNames->appendFormationRange( currentFormationName, startK - 1, endK - 1 );
formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 );
}
break;
}
@@ -203,8 +200,7 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readFmuFormationName
if ( lineStream.status() != QTextStream::Ok )
{
*errorMessage = QString( "Failed to parse line %1 of '%2'" ).arg( lineNumber ).arg( fileName );
return formationNames;
return std::unexpected( QString( "Failed to parse line %1 of '%2'" ).arg( lineNumber ).arg( fileName ) );
}
if ( formationName != currentFormationName )
@@ -212,7 +208,7 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readFmuFormationName
// Append previous formation
if ( !currentFormationName.isEmpty() )
{
formationNames->appendFormationRange( currentFormationName, startK - 1, endK - 1 );
formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 );
}
// Start new formation
@@ -230,7 +226,7 @@ std::unique_ptr<RigFormationNames> RifFormationNamesReader::readFmuFormationName
// Append previous formation at the end of the stream
if ( !currentFormationName.isEmpty() )
{
formationNames->appendFormationRange( currentFormationName, startK - 1, endK - 1 );
formationNames.appendFormationRange( currentFormationName, startK - 1, endK - 1 );
}
return formationNames;
@@ -20,10 +20,11 @@
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include <memory>
#include "RigFormationNames.h"
class RigFormationNames;
class QString;
#include <expected>
#include <QString>
namespace cvf
{
@@ -36,11 +37,11 @@ class Color3f;
class RifFormationNamesReader
{
public:
[[nodiscard]] static std::unique_ptr<RigFormationNames> readFormationNamesFile( const QString& fileName, QString* errorMessage );
[[nodiscard]] static std::expected<RigFormationNames, QString> readFormationNamesFile( const QString& fileName );
private:
static std::unique_ptr<RigFormationNames> readLyrFormationNameFile( const QString& fileName, QString* errorMessage );
static std::unique_ptr<RigFormationNames> readFmuFormationNameFile( const QString& fileName, QString* errorMessage );
static std::expected<RigFormationNames, QString> readLyrFormationNameFile( const QString& fileName );
static std::expected<RigFormationNames, QString> readFmuFormationNameFile( const QString& fileName );
static bool convertStringToColor( const QString& word, cvf::Color3f* color );
};