Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeStudyProperties.cxx
index a70e8a413d054096b6a3a4c0a0197c54f35455e1..a86ab765af529ad2c6e210457cafa2555cebe4ea 100644 (file)
@@ -1,30 +1,32 @@
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-// 
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
-// version 2.1 of the License.
-// 
-// This library 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 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library 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
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File   : SALOMEDSImpl_AttributeStudyProperties.cxx
 //  Author : Sergey RUIN
 //  Module : SALOME
-
-#include <SALOMEDSImpl_AttributeStudyProperties.hxx>
-
-using namespace std;
-
+//
+#include "SALOMEDSImpl_AttributeStudyProperties.hxx"
+#include <string.h>
+#include <stdlib.h>
 
 const std::string& SALOMEDSImpl_AttributeStudyProperties::GetID()
 {
@@ -47,6 +49,7 @@ SALOMEDSImpl_AttributeStudyProperties::SALOMEDSImpl_AttributeStudyProperties()
 {
   myLocked = false;
   myLockChanged = false;
+  myModified = false;
   Init();
 }
 
@@ -59,14 +62,15 @@ void SALOMEDSImpl_AttributeStudyProperties::Init()
   myMonth.clear();
   myYear.clear();
   myMode = 0; // none
+  myComponentVersions.clear();
 }
 
 void SALOMEDSImpl_AttributeStudyProperties::SetModification(const std::string& theUserName,
-                                                            const int            theMinute,
-                                                            const int            theHour,
-                                                            const int            theDay,
-                                                            const int            theMonth,
-                                                            const int            theYear)
+                                                            const int          theMinute,
+                                                            const int          theHour,
+                                                            const int          theDay,
+                                                            const int          theMonth,
+                                                            const int          theYear)
 {
   if (theMinute<0 || theMinute>60 || theHour<0 || theHour>24 ||
       theDay<0 || theDay>31 || theMonth<0 || theMonth>12)
@@ -84,12 +88,12 @@ void SALOMEDSImpl_AttributeStudyProperties::SetModification(const std::string& t
 }
 
 void SALOMEDSImpl_AttributeStudyProperties::GetModifications
-                  (vector<string>& theUserNames,
-                   vector<int>&    theMinutes,
-                   vector<int>&    theHours,
-                   vector<int>&    theDays,
-                   vector<int>&    theMonths,
-                   vector<int>&    theYears) const
+                  (std::vector<std::string>& theUserNames,
+                   std::vector<int>&    theMinutes,
+                   std::vector<int>&    theHours,
+                   std::vector<int>&    theDays,
+                   std::vector<int>&    theMonths,
+                   std::vector<int>&    theYears) const
 {
   theUserNames = myUserName;
   theMinutes = myMinute;
@@ -101,9 +105,7 @@ void SALOMEDSImpl_AttributeStudyProperties::GetModifications
 
 std::string SALOMEDSImpl_AttributeStudyProperties::GetCreatorName() const
 {
-  if (myUserName.size() == 0)
-    return std::string("");
-  return myUserName[0];
+  return myUserName.empty() ? std::string("") : myUserName[0];
 }
 
 bool SALOMEDSImpl_AttributeStudyProperties::GetCreationDate
@@ -135,6 +137,7 @@ void SALOMEDSImpl_AttributeStudyProperties::ChangeCreatorName(const std::string&
 
 void SALOMEDSImpl_AttributeStudyProperties::SetCreationMode(const int theMode)
 {
+  if (theMode == myMode) return;
   CheckLocked();
   Backup();
   myMode = theMode;
@@ -174,7 +177,8 @@ bool SALOMEDSImpl_AttributeStudyProperties::IsLocked() const
   return myLocked;
 }
 
-bool SALOMEDSImpl_AttributeStudyProperties::IsLockChanged(const bool theErase) {
+bool SALOMEDSImpl_AttributeStudyProperties::IsLockChanged(const bool theErase)
+{
   if (!myLockChanged) return false;
   if (theErase) myLockChanged = false;
   return true;
@@ -191,10 +195,10 @@ void SALOMEDSImpl_AttributeStudyProperties::Restore(DF_Attribute* with)
     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(with);
 
   Init();
-  vector<string> aNames;
-  vector<int> aMinutes, aHours, aDays, aMonths, aYears;
+  std::vector<std::string> aNames;
+  std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
-  for (int i = 0, len = aNames.size(); i < len; i++) {
+  for (size_t i = 0, len = aNames.size(); i < len; i++) {
     myUserName.push_back(aNames[i]);
     myMinute.push_back(aMinutes[i]);
     myHour.push_back(aHours[i]);
@@ -203,6 +207,7 @@ void SALOMEDSImpl_AttributeStudyProperties::Restore(DF_Attribute* with)
     myYear.push_back(aYears[i]);
   }
   myMode = aProp->GetCreationMode();
+  myComponentVersions = aProp->GetComponentsVersions();
 //  myModified = aProp->GetModified();
 //  myLocked = aProp->IsLocked();
 }
@@ -219,29 +224,51 @@ void SALOMEDSImpl_AttributeStudyProperties::Paste(DF_Attribute* into)
   aProp->Init();
 
   int i;
-  for(i = 0; i < myUserName.size(); i++) {
+  for(i = 0; i < (int)myUserName.size(); i++) {  //TODO: mismatch signed/unsigned
     aProp->SetModification(myUserName[i],
                            myMinute[i], myHour[i],
                            myDay[i], myMonth[i], myYear[i]);
   }
 
   aProp->SetCreationMode(myMode);
+  aProp->SetComponentsVersions( myComponentVersions );
 //  aProp->SetModified(myModified);
 //  aProp->SetLocked(myLocked);
 }
 
 
-string SALOMEDSImpl_AttributeStudyProperties::Save()
+std::string SALOMEDSImpl_AttributeStudyProperties::Save()
 {
-  vector<string> aNames;
-  vector<int> aMinutes, aHours, aDays, aMonths, aYears;
+  std::vector<std::string> aNames;
+  std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
   GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
 
-  int aLength, anIndex;
+  int aLength, anIndex, unitsSize = 0, commentSize = 0;;
   for (aLength = 0, anIndex = aNames.size()-1; anIndex >= 0; anIndex--)
     aLength += aNames[anIndex].size() + 1;
 
-  char* aProperty = new char[3 + aLength + 12 * aNames.size()];
+  std::string units = GetUnits();
+  std::string comment = GetComment();
+
+  size_t aLength1 = 0;
+  std::map<std::string, std::string> versions;
+  versionMap::const_iterator it;
+  for (aLength1 = 0, it = myComponentVersions.begin(); it != myComponentVersions.end(); ++it ) {
+    std::string vlist = "";
+    versionList vl = it->second;
+    versionList::const_iterator vlit;
+    for ( vlit = vl.begin(); vlit != vl.end(); ++vlit ) {
+      if ( vlist != "" ) vlist += ";";
+      vlist += *vlit;
+    }
+    versions[ it->first ] = vlist;
+    aLength1 += it->first.size() + vlist.size() + 2;
+  }
+  
+  unitsSize = units.size();
+  commentSize = comment.size();
+
+  char* aProperty = new char[3 + aLength + 12 * aNames.size() + 1 + unitsSize + 1 + commentSize + 1 + aLength1 ];
 
   char crMode = (char)GetCreationMode();
 
@@ -251,23 +278,128 @@ string SALOMEDSImpl_AttributeStudyProperties::Save()
   int a = 2;
   for (anIndex = 0; anIndex  < aLength; anIndex++) {
     sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
-           (int)(aMinutes[anIndex]),
-           (int)(aHours[anIndex]),
-           (int)(aDays[anIndex]),
-           (int)(aMonths[anIndex]),
-           (int)(aYears[anIndex]),
-           (char*)(aNames[anIndex].c_str()));
-    a = strlen(aProperty);
+            (int)(aMinutes[anIndex]),
+            (int)(aHours[anIndex]),
+            (int)(aDays[anIndex]),
+            (int)(aMonths[anIndex]),
+            (int)(aYears[anIndex]),
+            (char*)(aNames[anIndex].c_str()));
+    a = (int)strlen(aProperty); //!< TODO: conversion from size_t to int
     aProperty[a++] = 1;
   }
+
+  //Write delimiter of the section to define end of the modifications section
+  aProperty[a++] = 30;
+
+  //Write units if need
+  if(units.size() > 0) {
+    sprintf(&(aProperty[a]),"%s",units.c_str());
+    a = (int)strlen(aProperty); //!< TODO: conversion from size_t to int
+  }
+
+  aProperty[a++] = 1; //delimiter of the units and comments
+
+  //Write comments if need
+  if(comment.size() > 0) {
+    sprintf(&(aProperty[a]),"%s",comment.c_str());
+    a = (int)strlen(aProperty); //!< TODO: conversion from size_t to int
+  }
+  
+  aProperty[a++] = 30; //delimiter of the component versions
+
+  std::map<std::string, std::string>::const_iterator versionsIt;
+  for ( versionsIt = versions.begin(); versionsIt != versions.end(); ++versionsIt ) {
+    sprintf(&(aProperty[a]),"%s=%s",
+            (char*)(versionsIt->first.c_str()),
+            (char*)(versionsIt->second.c_str()));
+    a = (int)strlen(aProperty); //!< TODO: conversion from size_t to int
+    aProperty[a++] = 1;
+  }
+
   aProperty[a] = 0;
-  string prop(aProperty);
+  std::string prop(aProperty);
   delete aProperty;
 
   return prop;
 }
 
-void SALOMEDSImpl_AttributeStudyProperties::Load(const string& value)
+void SALOMEDSImpl_AttributeStudyProperties::SetUnits(const std::string& theUnits)
+{
+  if(myUnits == theUnits)
+    return;
+
+  CheckLocked();
+  Backup();
+
+  myUnits = theUnits;
+}
+
+std::string SALOMEDSImpl_AttributeStudyProperties::GetUnits() const
+{
+  return myUnits;
+}
+
+void SALOMEDSImpl_AttributeStudyProperties::SetComment(const std::string& theComment)
+{
+  if(myComment == theComment)
+    return;
+
+  CheckLocked();
+  Backup();
+
+  myComment = theComment;
+}
+
+std::string SALOMEDSImpl_AttributeStudyProperties::GetComment() const 
+{
+  return myComment;
+}
+
+void SALOMEDSImpl_AttributeStudyProperties::SetComponentVersion(const std::string& theComponent, const std::string& theVersion)
+{
+  if (!theComponent.empty()) {
+    CheckLocked();
+    Backup();
+    if (myComponentVersions.find(theComponent) == myComponentVersions.end()) myComponentVersions[theComponent] = versionList();
+    if (myComponentVersions[theComponent].empty() || myComponentVersions[theComponent].back() != theVersion)
+      myComponentVersions[theComponent].push_back(theVersion);
+  }
+}
+
+std::vector<std::string> SALOMEDSImpl_AttributeStudyProperties::GetStoredComponents() const
+{
+  std::vector<std::string> components;
+  versionMap::const_iterator it;  
+  for (it = myComponentVersions.begin(); it != myComponentVersions.end(); ++it)
+    components.push_back(it->first);
+  return components;
+}
+
+std::string SALOMEDSImpl_AttributeStudyProperties::GetComponentVersion(const std::string& theComponent) const
+{
+  versionList versions = GetComponentVersions(theComponent);
+  return versions.size() > 0 ? versions[0] : std::string("");
+}
+
+std::vector<std::string> SALOMEDSImpl_AttributeStudyProperties::GetComponentVersions(const std::string& theComponent) const
+{
+  versionList versions;
+  versionMap::const_iterator it = myComponentVersions.find(theComponent);
+  if ( it != myComponentVersions.end() ) versions = it->second;
+  return versions;
+}
+
+std::map< std::string, std::vector<std::string> > SALOMEDSImpl_AttributeStudyProperties::GetComponentsVersions() const
+{
+  return myComponentVersions;
+}
+
+void SALOMEDSImpl_AttributeStudyProperties::SetComponentsVersions( const std::map< std::string, std::vector<std::string> >& theVersions )
+{
+  myComponentVersions = theVersions;
+}
+
+void SALOMEDSImpl_AttributeStudyProperties::Load(const std::string& value)
 {
   char* aCopy = (char*)value.c_str();
 
@@ -275,7 +407,16 @@ void SALOMEDSImpl_AttributeStudyProperties::Load(const string& value)
   SetCreationMode(crMode);
 
   int anIndex;
-  for (anIndex = 2; anIndex + 2 < value.size() ;) {
+  // number 13 below is minimal size of modification data record, which has form:
+  // mmhhddMMyyyyname1, where
+  // - mm:   minute = 2 bytes
+  // - hh:   hour   = 2 bytes
+  // - dd:   day    = 2 bytes
+  // - MM:   month  = 2 bytes
+  // - yyyy: year   = 4 bytes
+  // - name: user's name = arbitrary value, minimal length is 0 bytes
+  // - 1   : records delimiter = 1 byte  
+  for (anIndex = 2; anIndex + 13 < (int)value.size() ;) {  //TODO: mismatch signed/unsigned
     char str[10];
     int aMinute, aHour, aDay, aMonth, aYear;
     str[0] = aCopy[anIndex++];
@@ -304,11 +445,77 @@ void SALOMEDSImpl_AttributeStudyProperties::Load(const string& value)
     strncpy(aName, &(aCopy[anIndex]), aNameSize);
     aName[aNameSize] = 0;
     SetModification(aName,aMinute,aHour,aDay,aMonth,aYear);
-    delete(aName);
+    delete [] (aName);
     anIndex += aNameSize + 1;
+    
+    //Check end of the modifications section
+    if(anIndex < (int)value.size() && aCopy[anIndex] == 30) //TODO: mismatch signed/unsigned
+      break;
+  }
+  
+  //Case when study contains units and comment properties
+  if( anIndex < (int)value.size() ) {   //TODO: mismatch signed/unsigned
+    anIndex++; //skip the delimiter of the sections: char(30)
+    int unitsSize;
+    for(unitsSize = 0; aCopy[anIndex+unitsSize] != 1; unitsSize++);
+
+    if(unitsSize > 0) {
+      char *anUnits = new char[unitsSize+1];
+      strncpy(anUnits, &(aCopy[anIndex]), unitsSize);
+      anUnits[unitsSize] = 0;
+      SetUnits(anUnits);
+      delete [] (anUnits);
+    }
+    anIndex += unitsSize + 1;
+
+    int commentSize;
+    for(commentSize = 0; aCopy[anIndex+commentSize] != 0 && aCopy[anIndex+commentSize] != 30; commentSize++);
+
+    if(commentSize > 0) {
+      char *aComment = new char[commentSize+1];
+      strncpy(aComment, &(aCopy[anIndex]), commentSize);
+      aComment[commentSize] = 0;
+      SetComment(aComment);
+      delete [] (aComment);
+    }
+    anIndex += commentSize + 1;
+  }
+
+  //Case when study contains components versions
+  if( anIndex < (int)value.size() ) { //TODO: mismatch signed/unsigned
+    while ( anIndex < (int)value.size() && aCopy[anIndex] != 0 ) { //TODO: mismatch signed/unsigned
+      int modSize;
+      for(modSize = 0; aCopy[anIndex+modSize] != '='; modSize++);
+      int verSize;
+      for(verSize = 0; aCopy[anIndex+modSize+1+verSize] != 1; verSize++);
+
+      if(modSize > 0) {
+        char *aModule = new char[modSize+1];
+        strncpy(aModule, &(aCopy[anIndex]), modSize);
+        aModule[modSize] = 0;
+        char *aVersions = new char[verSize+1];
+        if ( verSize > 0 )
+          strncpy(aVersions, &(aCopy[anIndex+modSize+1]), verSize);
+        aVersions[verSize] = 0;
+        
+        std::string mVersions = aVersions;
+        size_t start = 0, idx = mVersions.find( ';', start );
+        while ( idx != std::string::npos ) {
+          SetComponentVersion( aModule, mVersions.substr( start, idx-start ) );
+          start = idx + 1;
+          idx = mVersions.find( ';', start );
+        }
+        SetComponentVersion( aModule, mVersions.substr( start ) );
+
+        delete [] (aModule);
+        delete [] (aVersions);
+        anIndex += modSize + 1 + verSize + 1;
+      }
+    }
   }
+  
   if (aCopy[1] == 'l') {
     SetLocked(true);
   }
-  SetModified(0);
+  SetModified(0);  
 }