From 6ea5655a46ab95dadce14fc509ca100ec4b2098f Mon Sep 17 00:00:00 2001 From: asl Date: Thu, 26 Nov 2009 14:05:52 +0000 Subject: [PATCH] save/load debug --- src/Notebook/SALOME_Notebook.cxx | 221 +++++++++++++++++-------- src/Notebook/SALOME_Notebook.hxx | 13 +- src/Notebook/SALOME_NotebookDriver.cxx | 2 - src/Notebook/SALOME_Parameter.cxx | 68 +++++--- src/Notebook/SALOME_Parameter.hxx | 4 +- 5 files changed, 210 insertions(+), 98 deletions(-) diff --git a/src/Notebook/SALOME_Notebook.cxx b/src/Notebook/SALOME_Notebook.cxx index eb3e4465e..cf041504c 100644 --- a/src/Notebook/SALOME_Notebook.cxx +++ b/src/Notebook/SALOME_Notebook.cxx @@ -62,6 +62,69 @@ std::string arg( const std::string& theStr, const std::string& theArg1, const st return aRes; } +std::vector split( const std::string& theData, const std::string& theSeparator, bool theIsKeepEmpty ) +{ + std::vector aParts; + int aPrev = 0, anInd = 0; + while( true ) + { + anInd = theData.find( theSeparator, aPrev ); + if( anInd < 0 ) + break; + + std::string aPart = theData.substr( aPrev, anInd - aPrev ); + if( aPart.length() > 0 || theIsKeepEmpty ) + aParts.push_back( aPart ); + + aPrev = anInd + 1; + } + return aParts; +} + +void save( FILE* theFile, const std::string& theData ) +{ + short len = theData.length(); + fwrite( &len, 1, sizeof( short ), theFile ); + fwrite( theData.c_str(), 1, len, theFile ); +} + +void save( FILE* theFile, int theData ) +{ + fwrite( &theData, 1, sizeof( int ), theFile ); +} + +bool load( FILE* theFile, std::string& theData ) +{ + short len; + int rlen = fread( &len, 1, sizeof( short ), theFile ); + if( rlen < sizeof( short ) ) + return false; + + char buf[1024]; + rlen = fread( buf, 1, len, theFile ); + if( rlen < len ) + return false; + + buf[len] = 0; + theData = buf; + //printf( "load: %s\n", theData.c_str() ); + return true; +} + +bool load( FILE* theFile, int& theData ) +{ + int rlen = fread( &theData, 1, sizeof( int ), theFile ); + if( rlen < sizeof( int ) ) + return false; + + //printf( "load: %i\n", theData ); + return true; +} + + + + + @@ -272,7 +335,8 @@ bool SALOME_Notebook::Substitute( SubstitutionInfo& theSubstitution ) { std::list aDeps = myDependencies[anOldKey]; myDependencies.erase( anOldKey ); - myDependencies[aNewKey] = aDeps; + if( aDeps.size() > 0 ) + myDependencies[aNewKey] = aDeps; myParameters.erase( anOldName ); myParameters[aNewName] = aParamPtr; @@ -515,7 +579,7 @@ SALOME_Parameter* SALOME_Notebook::GetParameterPtr( const char* theParamName ) c return it==myParameters.end() ? 0 : it->second; } -void SALOME_Notebook::AddParameter( SALOME_Parameter* theParam ) +void SALOME_Notebook::AddParameter( SALOME_Parameter* theParam, bool theAddDependencies ) { //Utils_Locker lock( &myMutex ); @@ -526,15 +590,15 @@ void SALOME_Notebook::AddParameter( SALOME_Parameter* theParam ) //printf( "Add param: %s\n", anEntry.c_str() ); std::map< std::string, SALOME_Parameter* >::const_iterator it = myParameters.find( anEntry ); - if( it!=myParameters.end() ) - { - delete it->second; - ClearDependencies( GetKey( anEntry ), SALOME::All ); - } + bool exists = it!=myParameters.end(); + if( exists ) + ThrowError( arg( "The parameter '%1' already exists", anEntry ) ); try { - AddDependencies( theParam ); + myParameters[anEntry] = theParam; + if( theAddDependencies && !exists ) + AddDependencies( theParam ); } catch( const SALOME::NotebookError& ex ) { @@ -550,7 +614,6 @@ void SALOME_Notebook::AddDependencies( SALOME_Parameter* theParam ) //printf( "Dependencies search\n" ); std::string anEntry = theParam->GetEntry(); std::string aParamKey = GetKey( anEntry ); - myParameters[anEntry] = theParam; SALOME_StringList aDeps = theParam->Dependencies(); SALOME_StringList::const_iterator dit = aDeps.begin(), dlast = aDeps.end(); for( ; dit!=dlast; dit++ ) @@ -682,83 +745,106 @@ CORBA::Boolean SALOME_Notebook::Save( const char* theFileName ) if( !aFile ) return false; - fprintf( aFile, "\n\nnotebook\n" ); + save( aFile, "notebook" ); //1. Save dependencies - fprintf( aFile, "Dependencies\n" ); + save( aFile, "dependencies" ); std::map< std::string, std::list >::const_iterator dit = myDependencies.begin(), dlast = myDependencies.end(); for( ; dit!=dlast; dit++ ) { - fprintf( aFile, "%s -> ", dit->first.c_str() ); + save( aFile, dit->first ); std::list::const_iterator it = dit->second.begin(), last = dit->second.end(); for( ; it!=last; it++ ) - fprintf( aFile, "%s ", it->c_str() ); - fprintf( aFile, "\n" ); + save( aFile, *it ); + save( aFile, "end" ); } //2. Save parameters - fprintf( aFile, "Parameters\n" ); - std::map< std::string, SALOME_Parameter* >::const_iterator pit = myParameters.begin(), plast = myParameters.end(); + save( aFile, "parameters" ); + std::list aNames = GetParameters(); + std::list::const_iterator pit = aNames.begin(), plast = aNames.end(); for( ; pit!=plast; pit++ ) - { - fprintf( aFile, pit->second->Save().c_str() ); - fprintf( aFile, "\n" ); - } + myParameters[*pit]->Save( aFile ); //3. Save update list - fprintf( aFile, "Update\n" ); + save( aFile, "recompute list" ); std::list< KeyHelper >::const_iterator uit = myToUpdate.begin(), ulast = myToUpdate.end(); for( ; uit!=ulast; uit++ ) - { - fprintf( aFile, uit->key().c_str() ); - fprintf( aFile, "\n" ); - } + save( aFile, uit->key() ); //4. Save substitutions list + save( aFile, "substitutions" ); std::list::const_iterator sit = mySubstitutions.begin(), slast = mySubstitutions.end(); + for( ; sit!=slast; sit++ ) + Save( aFile, *sit ); fclose( aFile ); return true; } +bool SALOME_Notebook::Save( FILE* theFile, const SubstitutionInfo& theSubstitution ) const +{ + save( theFile, theSubstitution.myName ); + save( theFile, theSubstitution.myExpr ); + save( theFile, theSubstitution.myIsRename ); + std::list< KeyHelper >::const_iterator it = theSubstitution.myObjects.begin(), last = theSubstitution.myObjects.end(); + for( ; it!=last; it++ ) + save( theFile, it->key() ); + save( theFile, "end" ); + return true; +} + +SALOME_Notebook::SubstitutionInfo SALOME_Notebook::Load( FILE* theFile, const std::string& theFirstLine ) const +{ + printf( "load: %i\n", (int)this ); + SubstitutionInfo aSubstitution; + aSubstitution.myName = theFirstLine; + load( theFile, aSubstitution.myExpr ); + int isRename; load( theFile, isRename ); + aSubstitution.myIsRename = isRename; + + std::string aLine; + while( true ) + { + load( theFile, aLine ); + if( aLine == "end" ) + break; + aSubstitution.myObjects.push_back( KeyHelper( aLine, this ) ); + } + return aSubstitution; +} + namespace State { - typedef enum { Start, Title, Dependencies, Parameters, Update } LoadState; + typedef enum { Start, Title, Dependencies, Parameters, Recompute, Substitute } LoadState; } CORBA::Boolean SALOME_Notebook::Load( const char* theFileName ) { - printf( "SALOME_Notebook::Load\n" ); - State::LoadState aState = State::Start; FILE* aFile = fopen( theFileName, "r" ); if( !aFile ) return false; - const int BUF_SIZE = 256; - char aBuf[BUF_SIZE]; bool ok = true; + std::string aLine; try { - while( ok && fgets( aBuf, BUF_SIZE, aFile ) != NULL ) + while( true ) { - int aLen = strlen( aBuf ); - if( aLen > 0 ) - aBuf[aLen - 1] = 0; + if( !load( aFile, aLine ) ) + break; - std::string aLine = aBuf; - printf( "Line: '%s'\n", aBuf ); - if( aState == State::Start ) - { - if( aLine == "notebook" ) - aState = State::Title; - } - else if( aLine == "Dependencies" ) + if( aState == State::Start && aLine == "notebook" ) + aState = State::Title; + else if( aLine == "dependencies" ) aState = State::Dependencies; - else if( aLine == "Parameters" ) + else if( aLine == "parameters" ) aState = State::Parameters; - else if( aLine == "Update" ) - aState = State::Update; + else if( aLine == "recompute list" ) + aState = State::Recompute; + else if( aLine == "substitutions" ) + aState = State::Substitute; else switch( aState ) { @@ -768,16 +854,26 @@ CORBA::Boolean SALOME_Notebook::Load( const char* theFileName ) break; case State::Dependencies: - ParseDependencies( aLine ); + ParseDependencies( aFile, aLine ); break; case State::Parameters: - AddParameter( SALOME_Parameter::Load( aLine ) ); + { + SALOME_Parameter* aParam = SALOME_Parameter::Load( this, aFile, aLine ); + if( aParam ) + AddParameter( aParam, false ); + else + return false; break; + } - case State::Update: + case State::Recompute: myToUpdate.push_back( KeyHelper( aLine, this ) ); break; + + case State::Substitute: + mySubstitutions.push_back( Load( aFile, aLine ) ); + break; } } } @@ -791,34 +887,16 @@ CORBA::Boolean SALOME_Notebook::Load( const char* theFileName ) return ok; } -std::vector SALOME_Notebook::Split( const std::string& theData, const std::string& theSeparator, bool theIsKeepEmpty ) +void SALOME_Notebook::ParseDependencies( FILE* theFile, const std::string& theFirstLine ) { - std::vector aParts; - int aPrev = 0, anInd = 0; + std::string aKey = theFirstLine, aLine; while( true ) { - anInd = theData.find( theSeparator, aPrev ); - if( anInd < 0 ) + load( theFile, aLine ); + if( aLine == "end" ) break; - - std::string aPart = theData.substr( aPrev, anInd - aPrev ); - if( aPart.length() > 0 || theIsKeepEmpty ) - aParts.push_back( aPart ); - - aPrev = anInd + 1; + AddDependency( aKey, aLine ); } - return aParts; -} - -void SALOME_Notebook::ParseDependencies( const std::string& theData ) -{ - std::vector aParts = Split( theData, " ", false ); - if( aParts.size() < 2 || aParts.at( 1 ) != "->" ) - ThrowError( arg( "Incorrect parts format: %1", theData ) ); - - std::string aKey = aParts[0]; - for( int i = 2, n = aParts.size(); i < n; i++ ) - AddDependency( aKey, aParts[i] ); } char* SALOME_Notebook::DumpPython() @@ -908,6 +986,7 @@ void SALOME_Notebook::Sort( std::list< KeyHelper >& theList ) const char* SALOME_Notebook::Dump() { + printf( "dump: %i\n", (int)this ); std::string aStr; //1. Dependencies diff --git a/src/Notebook/SALOME_Notebook.hxx b/src/Notebook/SALOME_Notebook.hxx index 90e131020..2fe292350 100644 --- a/src/Notebook/SALOME_Notebook.hxx +++ b/src/Notebook/SALOME_Notebook.hxx @@ -39,6 +39,11 @@ class SALOME_Parameter; std::string arg( const std::string& theStr, const std::string& theArg1 ); std::string arg( const std::string& theStr, const std::string& theArg1, const std::string& theArg2 ); +std::vector split( const std::string& theData, const std::string& theSeparator, bool theIsKeepEmpty ); +void save( FILE* theFile, const std::string& theData ); +void save( FILE* theFile, int theData ); +bool load( FILE* theFile, std::string& theData ); +bool load( FILE* theFile, int& theData ); @@ -76,17 +81,15 @@ public: int GetNewId(); bool HasDependency( const std::string& theObjKey, const std::string& theRefKey ) const; - static std::vector Split( const std::string& theData, const std::string& theSeparator, bool theIsKeepEmpty ); - private: - void AddParameter( SALOME_Parameter* theParam ); + void AddParameter( SALOME_Parameter* theParam, bool theAddDependencies = true ); void AddDependencies( SALOME_Parameter* theParam ); void AddDependency( const std::string& theObjKey, const std::string& theRefKey ); void ClearDependencies( const std::string& theObjKey, SALOME::DependenciesType theType ); bool CheckParamName( const std::string& theParamName, std::string& theMsg ) const; SALOME::StringArray* GenerateList( const std::list& theList ) const; std::string GetComponent( const std::string& theKey, std::string& theEntry ) const; - void ParseDependencies( const std::string& theData ); + void ParseDependencies( FILE* theFile, const std::string& theFirstLine ); std::string GetKey( SALOME::ParameterizedObject_ptr theObj ) const; std::string GetKey( const std::string& theComponent, const std::string& theParamName ) const; std::string GetKey( const std::string& theParamName ) const; @@ -122,6 +125,8 @@ private: bool Substitute( SubstitutionInfo& theSubstitution ); SubstitutionInfo CreateSubstitution( const std::string& theName, const std::string& theExpr, bool theIsRename ) const; void AddSubstitution( const std::string& theName, const std::string& theExpr, bool theIsRename ); + bool Save( FILE* theFile, const SubstitutionInfo& theSubstitution ) const; + SubstitutionInfo Load( FILE* theFile, const std::string& theFirstLine ) const; private: std::map< std::string, std::list > myDependencies; diff --git a/src/Notebook/SALOME_NotebookDriver.cxx b/src/Notebook/SALOME_NotebookDriver.cxx index c89f93670..c63fdd979 100644 --- a/src/Notebook/SALOME_NotebookDriver.cxx +++ b/src/Notebook/SALOME_NotebookDriver.cxx @@ -60,8 +60,6 @@ SALOMEDS::TMPFile* SALOME_NotebookDriver::SaveASCII( SALOMEDS::SComponent_ptr th CORBA::Boolean SALOME_NotebookDriver::Load( SALOMEDS::SComponent_ptr theComponent, const SALOMEDS::TMPFile& theStream, const char* theURL, bool isMultiFile ) { - printf( "SALOME_NotebookDriver::Load\n" ); - // Get a temporary directory for a file std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir(); diff --git a/src/Notebook/SALOME_Parameter.cxx b/src/Notebook/SALOME_Parameter.cxx index 3065d50ca..c62b79700 100644 --- a/src/Notebook/SALOME_Parameter.cxx +++ b/src/Notebook/SALOME_Parameter.cxx @@ -242,32 +242,62 @@ SALOME_StringList SALOME_Parameter::Dependencies() const return myIsCalculable ? myExpr.parser()->parameters() : SALOME_StringList(); } -std::string SALOME_Parameter::Save() const +void SALOME_Parameter::Save( FILE* theFile ) const { - char buf[256]; - sprintf( buf, "%s %i %i %i?", myName.c_str(), myId, (int)myIsAnonymous, (int)myIsCalculable ); - std::string aRes = buf; + save( theFile, myName ); + save( theFile, myIsAnonymous ); + save( theFile, myIsCalculable ); if( myIsCalculable ) - aRes += myExpr.expression(); + save( theFile, myExpr.expression() ); else { - sprintf( buf, "%i %s", myResult.type(), myResult.toString().c_str() ); - aRes += buf; + save( theFile, (int)myResult.type() ); + save( theFile, myResult.toString() ); } - return aRes; } -SALOME_Parameter* SALOME_Parameter::Load( const std::string& theData ) +SALOME_Parameter* SALOME_Parameter::Load( SALOME_Notebook* theNotebook, FILE* theFile, const std::string& theFirstLine ) { - std::vector aParts = SALOME_Notebook::Split( theData, "?", false ); - if( aParts.size() != 2 ) - return 0; + std::string aName = theFirstLine; + int isAnonymous, isCalculable; + load( theFile, isAnonymous ); + load( theFile, isCalculable ); + if( isCalculable ) + { + std::string anExpr; + load( theFile, anExpr ); + return isAnonymous ? new SALOME_Parameter( theNotebook, anExpr ) : new SALOME_Parameter( theNotebook, aName, anExpr, true ); + } + else + { + int aResType; + std::string aValue; + load( theFile, aResType ); + load( theFile, aValue ); + switch( aResType ) + { + case SALOME_EvalVariant_Boolean: + return new SALOME_Parameter( theNotebook, aName, aValue=="true" ); + + case SALOME_EvalVariant_Int: + case SALOME_EvalVariant_UInt: + { + int value; + if( sscanf( aValue.c_str(), "%i", &value ) == 1 ) + return new SALOME_Parameter( theNotebook, aName, value ); + } - char aName[256]; - int anAnonym, aCalc, anId; - if( sscanf( aParts[0].c_str(), "%s %i %i %i", aName, &anId, &anAnonym, &aCalc ) < 4 ) - return 0; + case SALOME_EvalVariant_Double: + { + double value; + if( sscanf( aValue.c_str(), "%lf", &value ) == 1 ) + return new SALOME_Parameter( theNotebook, aName, value ); + } + case SALOME_EvalVariant_String: + return new SALOME_Parameter( theNotebook, aName, aValue, false ); + } + } return 0; } @@ -293,15 +323,15 @@ char* SALOME_Parameter::GetExpression( CORBA::Boolean theForceConvert ) void SALOME_Parameter::Substitute( const std::string& theName, const SALOME_EvalExpr& theExpr ) { - if( !IsCalculable() ) - return; - if( myName == theName ) { myName = theExpr.expression(); return; } + if( !IsCalculable() ) + return; + myExpr.substitute( theName, theExpr ); if( IsAnonymous() ) { diff --git a/src/Notebook/SALOME_Parameter.hxx b/src/Notebook/SALOME_Parameter.hxx index 90ded0a93..71165b6a3 100644 --- a/src/Notebook/SALOME_Parameter.hxx +++ b/src/Notebook/SALOME_Parameter.hxx @@ -73,8 +73,8 @@ public: virtual char* GetExpression( CORBA::Boolean theForceConvert ); - std::string Save() const; - static SALOME_Parameter* Load( const std::string& theData ); + void Save( FILE* theFile ) const; + static SALOME_Parameter* Load( SALOME_Notebook* theNotebook, FILE* theFile, const std::string& theFirstLine ); bool IsAnonymous() const; bool IsCalculable() const; -- 2.39.2