2017-06-29 11:48:26 +02:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RicfObjectCapability.h"
#include "cafPdmObjectHandle.h"
#include <QTextStream>
#include "RicfFieldHandle.h"
#include "cafPdmXmlFieldHandle.h"
2017-06-29 16:12:51 +02:00
#include "RicfMessages.h"
2017-06-29 11:48:26 +02:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfObjectCapability :: RicfObjectCapability ( caf :: PdmObjectHandle * owner , bool giveOwnership )
{
m_owner = owner ;
m_owner -> addCapability ( this , giveOwnership );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicfObjectCapability ::~ RicfObjectCapability ()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2017-06-29 16:12:51 +02:00
void RicfObjectCapability :: readFields ( QTextStream & inputStream ,
caf :: PdmObjectFactory * objectFactory ,
RicfMessages * errorMessageContainer )
2017-06-29 11:48:26 +02:00
{
std :: set < QString > readFields ;
2017-06-29 16:12:51 +02:00
bool isLastArgumentRead = false ;
while ( ! inputStream . atEnd () && ! isLastArgumentRead )
2017-06-29 11:48:26 +02:00
{
// Read field keyword
2017-08-04 13:58:32 +02:00
bool fieldDataFound = false ;
2017-06-29 16:12:51 +02:00
bool isEndOfArgumentFound = false ;
2017-06-29 11:48:26 +02:00
QString keyword ;
{
2017-06-29 17:44:05 +02:00
errorMessageContainer -> skipWhiteSpaceWithLineNumberCount ( inputStream );
2017-06-29 11:48:26 +02:00
{
QChar currentChar ;
2017-08-04 13:58:32 +02:00
while ( ! inputStream . atEnd ())
2017-06-29 13:18:19 +02:00
{
2017-06-29 17:44:05 +02:00
currentChar = errorMessageContainer -> readCharWithLineNumberCount ( inputStream );
2017-06-29 13:18:19 +02:00
2017-08-04 13:58:32 +02:00
if ( currentChar == QChar ( '=' ) || currentChar == QChar ( ')' ) || currentChar == QChar ( ',' ) || currentChar . isSpace ())
2017-06-29 13:18:19 +02:00
{
2017-08-04 13:58:32 +02:00
break ;
2017-06-29 13:18:19 +02:00
}
2017-08-04 13:58:32 +02:00
else
{
keyword += currentChar ;
}
2017-06-29 13:18:19 +02:00
}
2017-08-04 13:58:32 +02:00
if ( currentChar . isSpace ())
2017-06-29 11:48:26 +02:00
{
2017-08-04 13:58:32 +02:00
errorMessageContainer -> skipWhiteSpaceWithLineNumberCount ( inputStream );
currentChar = errorMessageContainer -> readCharWithLineNumberCount ( inputStream );
2017-06-29 11:48:26 +02:00
}
2017-08-04 13:58:32 +02:00
if ( currentChar == QChar ( '=' ))
{
fieldDataFound = true ;
}
else if ( currentChar == QChar ( ' ) ' ))
{
if ( ! keyword . isNull ())
{
errorMessageContainer -> addError ( QString ( "Can't find the '=' after the argument named '%1' in the command '%2'" ). arg ( keyword ). arg ( errorMessageContainer -> currentCommand ));
}
isLastArgumentRead = true ;
}
else if ( currentChar == QChar ( ',' ))
{
errorMessageContainer -> addError ( QString ( "Can't find the '=' after the argument named '%1' in the command '%2'" ). arg ( keyword ). arg ( errorMessageContainer -> currentCommand ));
isEndOfArgumentFound = true ;
}
else
{
errorMessageContainer -> addError ( QString ( "Can't find the '=' after the argument named '%1' in the command '%2'" ). arg ( keyword ). arg ( errorMessageContainer -> currentCommand ));
}
2017-06-29 11:48:26 +02:00
}
if ( readFields . count ( keyword ) )
{
// Warning message: Referenced the same argument several times
2017-06-29 16:29:50 +02:00
errorMessageContainer -> addWarning ( "The argument: \" " + keyword + " \" is referenced several times in the command: \" " + errorMessageContainer -> currentCommand + " \" " );
2017-06-29 11:48:26 +02:00
}
}
2017-06-29 16:12:51 +02:00
if ( fieldDataFound )
2017-06-29 11:48:26 +02:00
{
2017-06-29 16:12:51 +02:00
// Make field read its data
2017-06-29 11:48:26 +02:00
2017-06-29 16:12:51 +02:00
caf :: PdmFieldHandle * fieldHandle = m_owner -> findField ( keyword );
if ( fieldHandle && fieldHandle -> xmlCapability () && fieldHandle -> capability < RicfFieldHandle > () )
2017-06-29 11:48:26 +02:00
{
2017-06-29 16:12:51 +02:00
caf :: PdmXmlFieldHandle * xmlFieldHandle = fieldHandle -> xmlCapability ();
RicfFieldHandle * rcfField = fieldHandle -> capability < RicfFieldHandle > ();
2017-06-29 11:48:26 +02:00
2017-06-29 16:12:51 +02:00
if ( xmlFieldHandle -> isIOReadable () )
{
2017-06-29 16:29:50 +02:00
errorMessageContainer -> currentArgument = keyword ;
2017-06-29 16:12:51 +02:00
rcfField -> readFieldData ( inputStream , objectFactory , errorMessageContainer );
2017-06-29 16:29:50 +02:00
errorMessageContainer -> currentArgument = keyword ;
2017-06-29 16:12:51 +02:00
}
}
else
{
// Error message: Unknown argument name
2017-06-29 16:29:50 +02:00
errorMessageContainer -> addWarning ( "The argument: \" " + keyword + " \" does not exist in the command: \" " + errorMessageContainer -> currentCommand + " \" " );
2017-06-29 16:12:51 +02:00
}
2017-06-29 11:48:26 +02:00
}
// Skip to end of argument ',' or end of call ')'
2017-06-29 16:12:51 +02:00
if ( ! ( isLastArgumentRead || isEndOfArgumentFound ) )
2017-06-29 11:48:26 +02:00
{
QChar currentChar ;
bool isOutsideQuotes = true ;
while ( ! inputStream . atEnd () )
{
2017-06-29 17:44:05 +02:00
currentChar = errorMessageContainer -> readCharWithLineNumberCount ( inputStream );
2017-06-29 11:48:26 +02:00
if ( isOutsideQuotes )
{
if ( currentChar == QChar ( ',' ) )
{
break ;
}
if ( currentChar == QChar ( ')' ) )
{
2017-06-29 16:12:51 +02:00
isLastArgumentRead = true ;
2017-06-29 11:48:26 +02:00
break ;
}
if ( currentChar == QChar ( '\"' ) )
{
isOutsideQuotes = false ;
}
}
else
{
if ( currentChar == QChar ( '\"' ) )
{
isOutsideQuotes = true ;
}
if ( currentChar == QChar ( '\\' ) )
{
2017-06-29 17:44:05 +02:00
currentChar = errorMessageContainer -> readCharWithLineNumberCount ( inputStream );
2017-06-29 11:48:26 +02:00
}
}
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicfObjectCapability :: writeFields ( QTextStream & outputStream ) const
{
std :: vector < caf :: PdmFieldHandle *> fields ;
m_owner -> fields ( fields );
int writtenFieldCount = 0 ;
for ( size_t it = 0 ; it < fields . size (); ++ it )
{
const caf :: PdmXmlFieldHandle * xmlField = fields [ it ] -> xmlCapability ();
const RicfFieldHandle * rcfField = fields [ it ] -> capability < RicfFieldHandle > ();
if ( rcfField && xmlField && xmlField -> isIOWritable () )
{
QString keyword = xmlField -> fieldHandle () -> keyword ();
CAF_ASSERT ( caf :: PdmXmlObjectHandle :: isValidXmlElementName ( keyword ));
if ( writtenFieldCount >= 1 )
{
outputStream << ", " ;
++ writtenFieldCount ;
}
outputStream << keyword << " = " ;
rcfField -> writeFieldData ( outputStream );
}
}
}