mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#4763 Refactor complex code
This commit is contained in:
parent
88662a00d8
commit
66cbc198c4
@ -35,7 +35,6 @@ RifCaseRealizationReader::RifCaseRealizationReader( const QString& fileName )
|
|||||||
{
|
{
|
||||||
m_parameters = std::shared_ptr<RigCaseRealizationParameters>( new RigCaseRealizationParameters() );
|
m_parameters = std::shared_ptr<RigCaseRealizationParameters>( new RigCaseRealizationParameters() );
|
||||||
m_fileName = fileName;
|
m_fileName = fileName;
|
||||||
m_file = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -43,7 +42,6 @@ RifCaseRealizationReader::RifCaseRealizationReader( const QString& fileName )
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
RifCaseRealizationReader::~RifCaseRealizationReader()
|
RifCaseRealizationReader::~RifCaseRealizationReader()
|
||||||
{
|
{
|
||||||
closeFile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -72,36 +70,6 @@ std::shared_ptr<RifCaseRealizationReader> RifCaseRealizationReader::createReader
|
|||||||
return reader;
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
///
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
QFile* RifCaseRealizationReader::openFile()
|
|
||||||
{
|
|
||||||
if ( !m_file )
|
|
||||||
{
|
|
||||||
m_file = new QFile( m_fileName );
|
|
||||||
if ( !m_file->open( QIODevice::ReadOnly | QIODevice::Text ) )
|
|
||||||
{
|
|
||||||
closeFile();
|
|
||||||
throw FileParseException( QString( "Failed to open %1" ).arg( m_fileName ) );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
///
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
void RifCaseRealizationReader::closeFile()
|
|
||||||
{
|
|
||||||
if ( m_file )
|
|
||||||
{
|
|
||||||
m_file->close();
|
|
||||||
delete m_file;
|
|
||||||
m_file = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -124,7 +92,6 @@ QString RifCaseRealizationReader::runSpecificationFileName()
|
|||||||
RifCaseRealizationParametersReader::RifCaseRealizationParametersReader( const QString& fileName )
|
RifCaseRealizationParametersReader::RifCaseRealizationParametersReader( const QString& fileName )
|
||||||
: RifCaseRealizationReader( fileName )
|
: RifCaseRealizationReader( fileName )
|
||||||
{
|
{
|
||||||
m_textStream = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -132,10 +99,6 @@ RifCaseRealizationParametersReader::RifCaseRealizationParametersReader( const QS
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
RifCaseRealizationParametersReader::~RifCaseRealizationParametersReader()
|
RifCaseRealizationParametersReader::~RifCaseRealizationParametersReader()
|
||||||
{
|
{
|
||||||
if ( m_textStream )
|
|
||||||
{
|
|
||||||
delete m_textStream;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -143,55 +106,48 @@ RifCaseRealizationParametersReader::~RifCaseRealizationParametersReader()
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RifCaseRealizationParametersReader::parse()
|
void RifCaseRealizationParametersReader::parse()
|
||||||
{
|
{
|
||||||
int lineNo = 0;
|
QFile file( m_fileName );
|
||||||
QTextStream* dataStream = openDataStream();
|
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) return;
|
||||||
|
|
||||||
|
QTextStream dataStream( &file );
|
||||||
|
|
||||||
|
int lineNo = 0;
|
||||||
QStringList errors;
|
QStringList errors;
|
||||||
|
|
||||||
try
|
while ( !dataStream.atEnd() )
|
||||||
{
|
{
|
||||||
while ( !dataStream->atEnd() )
|
QString line = dataStream.readLine();
|
||||||
|
|
||||||
|
lineNo++;
|
||||||
|
QStringList cols = RifFileParseTools::splitLineAndTrim( line, QRegExp( "[ \t]" ), true );
|
||||||
|
|
||||||
|
if ( cols.size() != 2 )
|
||||||
{
|
{
|
||||||
QString line = dataStream->readLine();
|
errors << QString( "RifEnsembleParametersReader: Invalid file format in line %1" ).arg( lineNo );
|
||||||
|
|
||||||
lineNo++;
|
continue;
|
||||||
QStringList cols = RifFileParseTools::splitLineAndTrim( line, QRegExp( "[ \t]" ), true );
|
}
|
||||||
|
|
||||||
if ( cols.size() != 2 )
|
QString& name = cols[0];
|
||||||
|
QString& strValue = cols[1];
|
||||||
|
|
||||||
|
if ( RiaStdStringTools::isNumber( strValue.toStdString(), QLocale::c().decimalPoint().toLatin1() ) )
|
||||||
|
{
|
||||||
|
bool parseOk = true;
|
||||||
|
double value = QLocale::c().toDouble( strValue, &parseOk );
|
||||||
|
if ( parseOk )
|
||||||
{
|
{
|
||||||
errors << QString( "RifEnsembleParametersReader: Invalid file format in line %1" ).arg( lineNo );
|
m_parameters->addParameter( name, value );
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString& name = cols[0];
|
|
||||||
QString& strValue = cols[1];
|
|
||||||
|
|
||||||
if ( RiaStdStringTools::isNumber( strValue.toStdString(), QLocale::c().decimalPoint().toLatin1() ) )
|
|
||||||
{
|
|
||||||
bool parseOk = true;
|
|
||||||
double value = QLocale::c().toDouble( strValue, &parseOk );
|
|
||||||
if ( parseOk )
|
|
||||||
{
|
|
||||||
m_parameters->addParameter( name, value );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
errors << QString( "RifEnsembleParametersReader: Invalid number format in line %1" ).arg( lineNo );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_parameters->addParameter( name, strValue );
|
errors << QString( "RifEnsembleParametersReader: Invalid number format in line %1" ).arg( lineNo );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
closeDataStream();
|
{
|
||||||
}
|
m_parameters->addParameter( name, strValue );
|
||||||
catch ( ... )
|
}
|
||||||
{
|
|
||||||
closeDataStream();
|
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( const auto& s : errors )
|
for ( const auto& s : errors )
|
||||||
@ -200,30 +156,6 @@ void RifCaseRealizationParametersReader::parse()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
///
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
QTextStream* RifCaseRealizationParametersReader::openDataStream()
|
|
||||||
{
|
|
||||||
auto file = openFile();
|
|
||||||
|
|
||||||
m_textStream = new QTextStream( file );
|
|
||||||
return m_textStream;
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
///
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
|
||||||
void RifCaseRealizationParametersReader::closeDataStream()
|
|
||||||
{
|
|
||||||
if ( m_textStream )
|
|
||||||
{
|
|
||||||
delete m_textStream;
|
|
||||||
m_textStream = nullptr;
|
|
||||||
}
|
|
||||||
closeFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -244,8 +176,10 @@ RifCaseRealizationRunspecificationReader::~RifCaseRealizationRunspecificationRea
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
void RifCaseRealizationRunspecificationReader::parse()
|
void RifCaseRealizationRunspecificationReader::parse()
|
||||||
{
|
{
|
||||||
auto file = openFile();
|
QFile file( m_fileName );
|
||||||
QXmlStreamReader xml( file );
|
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) return;
|
||||||
|
|
||||||
|
QXmlStreamReader xml( &file );
|
||||||
|
|
||||||
QStringList errors;
|
QStringList errors;
|
||||||
|
|
||||||
@ -303,8 +237,6 @@ void RifCaseRealizationRunspecificationReader::parse()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFile();
|
|
||||||
|
|
||||||
for ( const auto& s : errors )
|
for ( const auto& s : errors )
|
||||||
{
|
{
|
||||||
RiaLogging::warning( s );
|
RiaLogging::warning( s );
|
||||||
|
@ -56,14 +56,9 @@ public:
|
|||||||
static QString runSpecificationFileName();
|
static QString runSpecificationFileName();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QFile* openFile();
|
|
||||||
void closeFile();
|
|
||||||
|
|
||||||
std::shared_ptr<RigCaseRealizationParameters> m_parameters;
|
std::shared_ptr<RigCaseRealizationParameters> m_parameters;
|
||||||
|
|
||||||
private:
|
|
||||||
QString m_fileName;
|
QString m_fileName;
|
||||||
QFile* m_file;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//==================================================================================================
|
//==================================================================================================
|
||||||
@ -77,13 +72,6 @@ public:
|
|||||||
~RifCaseRealizationParametersReader() override;
|
~RifCaseRealizationParametersReader() override;
|
||||||
|
|
||||||
void parse() override;
|
void parse() override;
|
||||||
|
|
||||||
private:
|
|
||||||
QTextStream* openDataStream();
|
|
||||||
void closeDataStream();
|
|
||||||
|
|
||||||
private:
|
|
||||||
QTextStream* m_textStream;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//==================================================================================================
|
//==================================================================================================
|
||||||
|
Loading…
Reference in New Issue
Block a user