ResInsight/ApplicationCode/CommandFileInterface/Core/RicfObjectCapability.cpp

217 lines
8.5 KiB
C++
Raw Normal View History

2017-06-29 04:48:26 -05:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil ASA
//
2017-06-29 04:48:26 -05:00
// 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.
//
2017-06-29 04:48:26 -05:00
// 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>
2017-06-29 04:48:26 -05:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RicfObjectCapability.h"
#include "RicfFieldHandle.h"
#include "RicfMessages.h"
#include "cafPdmObjectHandle.h"
#include "cafPdmXmlFieldHandle.h"
#include <QTextStream>
2017-06-29 04:48:26 -05:00
//--------------------------------------------------------------------------------------------------
///
2017-06-29 04:48:26 -05:00
//--------------------------------------------------------------------------------------------------
RicfObjectCapability::RicfObjectCapability( caf::PdmObjectHandle* owner, bool giveOwnership )
2017-06-29 04:48:26 -05:00
{
m_owner = owner;
m_owner->addCapability( this, giveOwnership );
2017-06-29 04:48:26 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2017-06-29 04:48:26 -05:00
//--------------------------------------------------------------------------------------------------
RicfObjectCapability::~RicfObjectCapability() {}
2017-06-29 04:48:26 -05:00
//--------------------------------------------------------------------------------------------------
///
2017-06-29 04:48:26 -05:00
//--------------------------------------------------------------------------------------------------
void RicfObjectCapability::readFields( QTextStream& inputStream,
caf::PdmObjectFactory* objectFactory,
RicfMessages* errorMessageContainer )
2017-06-29 04:48:26 -05:00
{
std::set<QString> readFields;
bool isLastArgumentRead = false;
while ( !inputStream.atEnd() && !isLastArgumentRead )
2017-06-29 04:48:26 -05:00
{
// Read field keyword
bool fieldDataFound = false;
bool isEndOfArgumentFound = false;
2017-06-29 04:48:26 -05:00
QString keyword;
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
2017-06-29 04:48:26 -05:00
{
QChar currentChar;
while ( !inputStream.atEnd() )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
if ( currentChar == QChar( '=' ) || currentChar == QChar( ')' ) || currentChar == QChar( ',' ) ||
currentChar.isSpace() )
{
break;
}
else
{
keyword += currentChar;
}
}
if ( currentChar.isSpace() )
2017-06-29 04:48:26 -05:00
{
errorMessageContainer->skipWhiteSpaceWithLineNumberCount( inputStream );
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
2017-06-29 04:48:26 -05: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 04:48:26 -05:00
}
if ( readFields.count( keyword ) )
2017-06-29 04:48:26 -05:00
{
// Warning message: Referenced the same argument several times
errorMessageContainer->addWarning( "The argument: \"" + keyword +
"\" is referenced several times in the command: \"" +
errorMessageContainer->currentCommand + "\"" );
2017-06-29 04:48:26 -05:00
}
}
if ( fieldDataFound )
2017-06-29 04:48:26 -05:00
{
// Make field read its data
2017-06-29 04:48:26 -05:00
caf::PdmFieldHandle* fieldHandle = m_owner->findField( keyword );
if ( fieldHandle && fieldHandle->xmlCapability() && fieldHandle->capability<RicfFieldHandle>() )
2017-06-29 04:48:26 -05:00
{
caf::PdmXmlFieldHandle* xmlFieldHandle = fieldHandle->xmlCapability();
RicfFieldHandle* rcfField = fieldHandle->capability<RicfFieldHandle>();
2017-06-29 04:48:26 -05:00
if ( xmlFieldHandle->isIOReadable() )
{
errorMessageContainer->currentArgument = keyword;
rcfField->readFieldData( inputStream, objectFactory, errorMessageContainer );
errorMessageContainer->currentArgument = keyword;
}
}
else
{
// Error message: Unknown argument name
errorMessageContainer->addWarning( "The argument: \"" + keyword + "\" does not exist in the command: \"" +
errorMessageContainer->currentCommand + "\"" );
}
2017-06-29 04:48:26 -05:00
}
// Skip to end of argument ',' or end of call ')'
if ( !( isLastArgumentRead || isEndOfArgumentFound ) )
2017-06-29 04:48:26 -05:00
{
QChar currentChar;
bool isOutsideQuotes = true;
2017-06-29 04:48:26 -05:00
while ( !inputStream.atEnd() )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
2017-06-29 04:48:26 -05:00
if ( isOutsideQuotes )
{
if ( currentChar == QChar( ',' ) )
2017-06-29 04:48:26 -05:00
{
break;
}
if ( currentChar == QChar( ')' ) )
2017-06-29 04:48:26 -05:00
{
isLastArgumentRead = true;
2017-06-29 04:48:26 -05:00
break;
}
if ( currentChar == QChar( '\"' ) )
2017-06-29 04:48:26 -05:00
{
isOutsideQuotes = false;
}
}
else
{
if ( currentChar == QChar( '\"' ) )
2017-06-29 04:48:26 -05:00
{
isOutsideQuotes = true;
}
if ( currentChar == QChar( '\\' ) )
2017-06-29 04:48:26 -05:00
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
2017-06-29 04:48:26 -05:00
}
}
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
2017-06-29 04:48:26 -05:00
//--------------------------------------------------------------------------------------------------
void RicfObjectCapability::writeFields( QTextStream& outputStream ) const
2017-06-29 04:48:26 -05:00
{
std::vector<caf::PdmFieldHandle*> fields;
m_owner->fields( fields );
2017-06-29 04:48:26 -05:00
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>();
2017-06-29 04:48:26 -05:00
if ( rcfField && xmlField && xmlField->isIOWritable() )
{
QString keyword = xmlField->fieldHandle()->keyword();
CAF_ASSERT( caf::PdmXmlObjectHandle::isValidXmlElementName( keyword ) );
2017-06-29 04:48:26 -05:00
if ( writtenFieldCount >= 1 )
{
outputStream << ", ";
}
outputStream << keyword << " = ";
rcfField->writeFieldData( outputStream );
writtenFieldCount++;
2017-06-29 04:48:26 -05:00
}
}
}