mirror of
https://git.salome-platform.org/gitpub/modules/smesh.git
synced 2025-01-15 04:50:36 +05:00
PAL11400. put string literals describing patterns into separate functions
This commit is contained in:
parent
be55a1922a
commit
0f463dbca5
@ -294,6 +294,100 @@ namespace SMESH
|
|||||||
<< P.z << " ))";
|
<< P.z << " ))";
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TCollection_AsciiString myLongStringStart( "TPythonDump::LongStringStart" );
|
||||||
|
TCollection_AsciiString myLongStringEnd ( "TPythonDump::LongStringEnd" );
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
/*!
|
||||||
|
* \brief Return marker of long string literal beginning
|
||||||
|
* \param type - a name of functionality producing the string literal
|
||||||
|
* \retval TCollection_AsciiString - the marker string to be written into
|
||||||
|
* a raw python script
|
||||||
|
*/
|
||||||
|
//================================================================================
|
||||||
|
|
||||||
|
TCollection_AsciiString TPythonDump::LongStringStart(const char* type)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
myLongStringStart +
|
||||||
|
(Standard_Integer) strlen(type) +
|
||||||
|
" " +
|
||||||
|
(char*) type;
|
||||||
|
}
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
/*!
|
||||||
|
* \brief Return marker of long string literal end
|
||||||
|
* \retval TCollection_AsciiString - the marker string to be written into
|
||||||
|
* a raw python script
|
||||||
|
*/
|
||||||
|
//================================================================================
|
||||||
|
|
||||||
|
TCollection_AsciiString TPythonDump::LongStringEnd()
|
||||||
|
{
|
||||||
|
return myLongStringEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
/*!
|
||||||
|
* \brief Cut out a long string literal from a string
|
||||||
|
* \param theText - text possibly containing string literals
|
||||||
|
* \param theFrom - position in the text to search from
|
||||||
|
* \param theLongString - the retrieved literal
|
||||||
|
* \param theStringType - a name of functionality produced the literal
|
||||||
|
* \retval bool - true if a string literal found
|
||||||
|
*
|
||||||
|
* The literal is removed from theText; theFrom points position right after
|
||||||
|
* the removed literal
|
||||||
|
*/
|
||||||
|
//================================================================================
|
||||||
|
|
||||||
|
bool TPythonDump::CutoutLongString( TCollection_AsciiString & theText,
|
||||||
|
int & theFrom,
|
||||||
|
TCollection_AsciiString & theLongString,
|
||||||
|
TCollection_AsciiString & theStringType)
|
||||||
|
{
|
||||||
|
if ( theFrom < 1 || theFrom > theText.Length() )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// ...script \ beg marker \ \ type \ literal \ end marker \ script...
|
||||||
|
// "theText myLongStringStart7 Pattern!!! SALOME Mesh Pattern file myLongStringEndtextEnd"
|
||||||
|
// 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
|
||||||
|
// 0 1 2 3 4 5 6 7 8
|
||||||
|
|
||||||
|
theFrom = theText.Location( myLongStringStart, theFrom, theText.Length() ); // = 09
|
||||||
|
if ( !theFrom )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// find where literal begins
|
||||||
|
int literalBeg = theFrom + myLongStringStart.Length(); // = 26
|
||||||
|
char* typeLenStr = theText.ToCString() + literalBeg - 1; // = "7 Pattern!!! SALO...."
|
||||||
|
int typeLen = atoi ( typeLenStr ); // = 7
|
||||||
|
while ( *typeLenStr != ' ' ) { // look for ' ' after typeLen
|
||||||
|
literalBeg++; // 26 -> 27
|
||||||
|
typeLenStr++;
|
||||||
|
}
|
||||||
|
literalBeg += typeLen + 1; // = 35
|
||||||
|
if ( literalBeg > theText.Length() )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// where literal ends (i.e. end marker begins)
|
||||||
|
int literalEnd = theText.Location( myLongStringEnd, literalBeg, theText.Length() ); // = 64
|
||||||
|
if ( !literalEnd )
|
||||||
|
literalEnd = theText.Length();
|
||||||
|
|
||||||
|
// literal
|
||||||
|
theLongString = theText.SubString( literalBeg, literalEnd - 1); // "!!! SALOME Mesh Pattern file "
|
||||||
|
// type
|
||||||
|
theStringType = theText.SubString( literalBeg - typeLen, literalBeg - 1 ); // "Pattern"
|
||||||
|
// cut off literal
|
||||||
|
literalEnd += myLongStringEnd.Length(); // = 79
|
||||||
|
TCollection_AsciiString textEnd = theText.SubString( literalEnd, theText.Length() ); // "textE..."
|
||||||
|
theText = theText.SubString( 1, theFrom - 1 ) + textEnd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//=======================================================================
|
//=======================================================================
|
||||||
@ -464,6 +558,39 @@ Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theStri
|
|||||||
return aSeq;
|
return aSeq;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
//================================================================================
|
||||||
|
/*!
|
||||||
|
* \brief Make a string be a valid python name
|
||||||
|
* \param aName - a string to fix
|
||||||
|
* \retval bool - true if aName was not modified
|
||||||
|
*/
|
||||||
|
//================================================================================
|
||||||
|
|
||||||
|
bool fixPythonName(TCollection_AsciiString & aName )
|
||||||
|
{
|
||||||
|
const TCollection_AsciiString allowedChars =
|
||||||
|
"qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_";
|
||||||
|
bool isValidName = true;
|
||||||
|
int p=1; // replace not allowed chars with underscore
|
||||||
|
while (p <= aName.Length() &&
|
||||||
|
(p = aName.FirstLocationNotInSet(allowedChars, p, aName.Length())))
|
||||||
|
{
|
||||||
|
if ( p == 1 || p == aName.Length() || aName.Value(p-1) == '_')
|
||||||
|
aName.Remove( p, 1 ); // remove double _ and from the start and the end
|
||||||
|
else
|
||||||
|
aName.SetValue(p, '_');
|
||||||
|
isValidName = false;
|
||||||
|
}
|
||||||
|
if ( aName.IsIntegerValue() ) { // aName must not start with a digit
|
||||||
|
aName.Insert( 1, 'a' );
|
||||||
|
isValidName = false;
|
||||||
|
}
|
||||||
|
return isValidName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
/*!
|
/*!
|
||||||
* DumpPython
|
* DumpPython
|
||||||
@ -513,8 +640,8 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
|
|||||||
int beg, end = aSavedTrace.Length(), from = 1;
|
int beg, end = aSavedTrace.Length(), from = 1;
|
||||||
while ( from < end && ( beg = aSavedTrace.Location( aSmeshCall, from, end ))) {
|
while ( from < end && ( beg = aSavedTrace.Location( aSmeshCall, from, end ))) {
|
||||||
char charBefore = ( beg == 1 ) ? ' ' : aSavedTrace.Value( beg - 1 );
|
char charBefore = ( beg == 1 ) ? ' ' : aSavedTrace.Value( beg - 1 );
|
||||||
if ( isspace( charBefore ) || charBefore == '=' ) {
|
if ( isspace( charBefore ) || charBefore == '=' ) { // "smesh." is not a part of a long word
|
||||||
aSavedTrace.Insert( beg + aSmeshCall.Length() - 1, gen );
|
aSavedTrace.Insert( beg + aSmeshCall.Length() - 1, gen );// "smesh" -> "smeshgen"
|
||||||
end += gen.Length();
|
end += gen.Length();
|
||||||
}
|
}
|
||||||
from = beg + aSmeshCall.Length();
|
from = beg + aSmeshCall.Length();
|
||||||
@ -550,8 +677,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
|
|||||||
TColStd_SequenceOfAsciiString seqRemoved;
|
TColStd_SequenceOfAsciiString seqRemoved;
|
||||||
Resource_DataMapOfAsciiStringAsciiString mapRemoved;
|
Resource_DataMapOfAsciiStringAsciiString mapRemoved;
|
||||||
Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
|
Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length();
|
||||||
TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_"),
|
TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_");
|
||||||
allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_");
|
|
||||||
|
|
||||||
// Collect names of GEOM objects to exclude same names for SMESH objects
|
// Collect names of GEOM objects to exclude same names for SMESH objects
|
||||||
GEOM::string_array_var aGeomNames = geom->GetAllDumpNames();
|
GEOM::string_array_var aGeomNames = geom->GetAllDumpNames();
|
||||||
@ -573,21 +699,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
|
|||||||
// The Object is in Study
|
// The Object is in Study
|
||||||
aName = theObjectNames.Find(anEntry);
|
aName = theObjectNames.Find(anEntry);
|
||||||
// check validity of aName
|
// check validity of aName
|
||||||
bool isValidName = true;
|
bool isValidName = fixPythonName( aName );
|
||||||
int p=1; // replace not allowed chars with underscore
|
|
||||||
while (p <= aName.Length() &&
|
|
||||||
(p = aName.FirstLocationNotInSet(allowedChars, p, aName.Length())))
|
|
||||||
{
|
|
||||||
if ( p == 1 || p == aName.Length() || aName.Value(p-1) == '_')
|
|
||||||
aName.Remove( p, 1 ); // remove double _ and from the start and the end
|
|
||||||
else
|
|
||||||
aName.SetValue(p, '_');
|
|
||||||
isValidName = false;
|
|
||||||
}
|
|
||||||
if ( aName.IsIntegerValue() ) { // aName must not start with a digit
|
|
||||||
aName.Insert( 1, 'a' );
|
|
||||||
isValidName = false;
|
|
||||||
}
|
|
||||||
if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
|
if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) {
|
||||||
// diff objects have same name - make a new name by appending a digit
|
// diff objects have same name - make a new name by appending a digit
|
||||||
TCollection_AsciiString aName2;
|
TCollection_AsciiString aName2;
|
||||||
@ -679,6 +791,49 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl
|
|||||||
|
|
||||||
anUpdatedScript += "\n\n\tpass\n";
|
anUpdatedScript += "\n\n\tpass\n";
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
// put string literals describing patterns into separate functions
|
||||||
|
// -----------------------------------------------------------------
|
||||||
|
|
||||||
|
TCollection_AsciiString aLongString, aFunctionType;
|
||||||
|
int where = 1;
|
||||||
|
set< string > functionNameSet;
|
||||||
|
while ( SMESH::TPythonDump::CutoutLongString( anUpdatedScript, where, aLongString, aFunctionType ))
|
||||||
|
{
|
||||||
|
// make a python string literal
|
||||||
|
aLongString.Prepend(":\n\treturn '''\n");
|
||||||
|
aLongString += "\n\t'''\n\tpass\n";
|
||||||
|
|
||||||
|
TCollection_AsciiString functionName;
|
||||||
|
|
||||||
|
// check if the function returning this literal is already defined
|
||||||
|
int posAlready = anUpdatedScript.Location( aLongString, where, anUpdatedScript.Length() );
|
||||||
|
if ( posAlready ) // already defined
|
||||||
|
{
|
||||||
|
// find the function name
|
||||||
|
int functBeg = posAlready;
|
||||||
|
char* script = anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def fuction()"
|
||||||
|
while ( *script != ' ' ) {
|
||||||
|
script--;
|
||||||
|
functBeg--;
|
||||||
|
}
|
||||||
|
functBeg++; // do not take ' '
|
||||||
|
posAlready--; // do not take ':'
|
||||||
|
functionName = anUpdatedScript.SubString( functBeg, posAlready );
|
||||||
|
}
|
||||||
|
else // not defined yet
|
||||||
|
{
|
||||||
|
// find a unique function name
|
||||||
|
fixPythonName( aFunctionType );
|
||||||
|
Standard_Integer nb = 0;
|
||||||
|
do functionName = aFunctionType + "_" + ( nb++ ) + "()";
|
||||||
|
while ( !functionNameSet.insert( functionName.ToCString() ).second );
|
||||||
|
|
||||||
|
anUpdatedScript += helper + "\n\ndef " + functionName + aLongString; // define function
|
||||||
|
}
|
||||||
|
anUpdatedScript.InsertBefore( where, functionName ); // call function
|
||||||
|
}
|
||||||
|
|
||||||
aValidScript = true;
|
aValidScript = true;
|
||||||
|
|
||||||
return anUpdatedScript;
|
return anUpdatedScript;
|
||||||
|
@ -106,8 +106,21 @@ SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
|
|||||||
|
|
||||||
CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
|
CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
|
||||||
{
|
{
|
||||||
|
// remove some gabage from the end
|
||||||
|
TCollection_AsciiString patternDescription = (char*) theFileContents;
|
||||||
|
int pos = patternDescription.Length();
|
||||||
|
while (! isdigit( patternDescription.Value( pos )))
|
||||||
|
pos--;
|
||||||
|
if ( pos != patternDescription.Length() ) {
|
||||||
|
patternDescription.Trunc( pos );
|
||||||
|
}
|
||||||
|
|
||||||
// Update Python script
|
// Update Python script
|
||||||
TPythonDump() << "isDone = pattern.LoadFromFile(" << theFileContents << ")";
|
TPythonDump() << "isDone = pattern.LoadFromFile("
|
||||||
|
<< TPythonDump::LongStringStart("Pattern")
|
||||||
|
<< patternDescription
|
||||||
|
<< TPythonDump::LongStringEnd()
|
||||||
|
<< ")";
|
||||||
addErrorCode( "LoadFromFile" );
|
addErrorCode( "LoadFromFile" );
|
||||||
|
|
||||||
return myPattern.Load( theFileContents );
|
return myPattern.Load( theFileContents );
|
||||||
|
@ -110,6 +110,35 @@ namespace SMESH
|
|||||||
|
|
||||||
static char* SMESHGenName() { return "smeshgen"; }
|
static char* SMESHGenName() { return "smeshgen"; }
|
||||||
static char* MeshEditorName() { return "mesh_editor"; }
|
static char* MeshEditorName() { return "mesh_editor"; }
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Return marker of long string literal beginning
|
||||||
|
* \param type - a name of functionality producing the string literal
|
||||||
|
* \retval TCollection_AsciiString - the marker string to be written into
|
||||||
|
* a raw python script
|
||||||
|
*/
|
||||||
|
static TCollection_AsciiString LongStringStart(const char* type);
|
||||||
|
/*!
|
||||||
|
* \brief Return marker of long string literal end
|
||||||
|
* \retval TCollection_AsciiString - the marker string to be written into
|
||||||
|
* a raw python script
|
||||||
|
*/
|
||||||
|
static TCollection_AsciiString LongStringEnd();
|
||||||
|
/*!
|
||||||
|
* \brief Cut out a long string literal from a string
|
||||||
|
* \param theText - text possibly containing string literals
|
||||||
|
* \param theFrom - position in the text to search from
|
||||||
|
* \param theLongString - the retrieved literal
|
||||||
|
* \param theStringType - a name of functionality produced the literal
|
||||||
|
* \retval bool - true if a string literal found
|
||||||
|
*
|
||||||
|
* The literal is removed from theText; theFrom points position right after
|
||||||
|
* the removed literal
|
||||||
|
*/
|
||||||
|
static bool CutoutLongString( TCollection_AsciiString & theText,
|
||||||
|
int & theFrom,
|
||||||
|
TCollection_AsciiString & theLongString,
|
||||||
|
TCollection_AsciiString & theStringType);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user