1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SALOMEDSImpl_AttributeStudyProperties.cxx
24 // Author : Sergey RUIN
27 #include "SALOMEDSImpl_AttributeStudyProperties.hxx"
30 const std::string& SALOMEDSImpl_AttributeStudyProperties::GetID()
32 static std::string SALOMEDSImpl_AttributeStudyPropertiesID ("128371A2-8F52-11d6-A8A3-0001021E8C7F");
33 return SALOMEDSImpl_AttributeStudyPropertiesID;
36 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_AttributeStudyProperties::Set(const DF_Label& label)
38 SALOMEDSImpl_AttributeStudyProperties* A = NULL;
39 if (!(A=(SALOMEDSImpl_AttributeStudyProperties*)label.FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID()))) {
40 A = new SALOMEDSImpl_AttributeStudyProperties();
41 label.AddAttribute(A);
46 SALOMEDSImpl_AttributeStudyProperties::SALOMEDSImpl_AttributeStudyProperties()
47 :SALOMEDSImpl_GenericAttribute("AttributeStudyProperties")
50 myLockChanged = false;
55 void SALOMEDSImpl_AttributeStudyProperties::Init()
64 myComponentVersions.clear();
67 void SALOMEDSImpl_AttributeStudyProperties::SetModification(const std::string& theUserName,
74 if (theMinute<0 || theMinute>60 || theHour<0 || theHour>24 ||
75 theDay<0 || theDay>31 || theMonth<0 || theMonth>12)
81 myUserName.push_back(theUserName);
82 myMinute.push_back(theMinute);
83 myHour.push_back(theHour);
84 myDay.push_back(theDay);
85 myMonth.push_back(theMonth);
86 myYear.push_back(theYear);
89 void SALOMEDSImpl_AttributeStudyProperties::GetModifications
90 (std::vector<std::string>& theUserNames,
91 std::vector<int>& theMinutes,
92 std::vector<int>& theHours,
93 std::vector<int>& theDays,
94 std::vector<int>& theMonths,
95 std::vector<int>& theYears) const
97 theUserNames = myUserName;
98 theMinutes = myMinute;
105 std::string SALOMEDSImpl_AttributeStudyProperties::GetCreatorName() const
107 return myUserName.empty() ? std::string("") : myUserName[0];
110 bool SALOMEDSImpl_AttributeStudyProperties::GetCreationDate
117 if (myMinute.size() != 0) {
118 theMinute = myMinute[0];
121 theMonth = myMonth[0];
128 void SALOMEDSImpl_AttributeStudyProperties::ChangeCreatorName(const std::string& theName)
130 if (myUserName.size() > 0) {
133 myUserName[0] = theName;
137 void SALOMEDSImpl_AttributeStudyProperties::SetCreationMode(const int theMode)
139 if (theMode == myMode) return;
145 int SALOMEDSImpl_AttributeStudyProperties::GetCreationMode() const
150 void SALOMEDSImpl_AttributeStudyProperties::SetModified(const int theModified)
152 myModified = theModified;
155 bool SALOMEDSImpl_AttributeStudyProperties::IsModified() const
157 return (myModified != 0);
160 int SALOMEDSImpl_AttributeStudyProperties::GetModified() const
165 void SALOMEDSImpl_AttributeStudyProperties::SetLocked(const bool theLocked)
168 if (myLocked != theLocked) {
169 myLockChanged = true;
170 myLocked = theLocked;
174 bool SALOMEDSImpl_AttributeStudyProperties::IsLocked() const
179 bool SALOMEDSImpl_AttributeStudyProperties::IsLockChanged(const bool theErase)
181 if (!myLockChanged) return false;
182 if (theErase) myLockChanged = false;
186 const std::string& SALOMEDSImpl_AttributeStudyProperties::ID() const
191 void SALOMEDSImpl_AttributeStudyProperties::Restore(DF_Attribute* with)
193 SALOMEDSImpl_AttributeStudyProperties* aProp =
194 dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(with);
197 std::vector<std::string> aNames;
198 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
199 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
200 for (int i = 0, len = aNames.size(); i < len; i++) {
201 myUserName.push_back(aNames[i]);
202 myMinute.push_back(aMinutes[i]);
203 myHour.push_back(aHours[i]);
204 myDay.push_back(aDays[i]);
205 myMonth.push_back(aMonths[i]);
206 myYear.push_back(aYears[i]);
208 myMode = aProp->GetCreationMode();
209 myComponentVersions = aProp->GetComponentsVersions();
210 // myModified = aProp->GetModified();
211 // myLocked = aProp->IsLocked();
214 DF_Attribute* SALOMEDSImpl_AttributeStudyProperties::NewEmpty() const
216 return new SALOMEDSImpl_AttributeStudyProperties();
219 void SALOMEDSImpl_AttributeStudyProperties::Paste(DF_Attribute* into)
221 SALOMEDSImpl_AttributeStudyProperties* aProp =
222 dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(into);
226 for(i = 0; i < myUserName.size(); i++) {
227 aProp->SetModification(myUserName[i],
228 myMinute[i], myHour[i],
229 myDay[i], myMonth[i], myYear[i]);
232 aProp->SetCreationMode(myMode);
233 aProp->SetComponentsVersions( myComponentVersions );
234 // aProp->SetModified(myModified);
235 // aProp->SetLocked(myLocked);
239 std::string SALOMEDSImpl_AttributeStudyProperties::Save()
241 std::vector<std::string> aNames;
242 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
243 GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
245 int aLength, anIndex, unitsSize = 0, commentSize = 0;;
246 for (aLength = 0, anIndex = aNames.size()-1; anIndex >= 0; anIndex--)
247 aLength += aNames[anIndex].size() + 1;
249 std::string units = GetUnits();
250 std::string comment = GetComment();
253 std::map<std::string, std::string> versions;
254 versionMap::const_iterator it;
255 for (aLength1 = 0, it = myComponentVersions.begin(); it != myComponentVersions.end(); ++it ) {
256 std::string vlist = "";
257 versionList vl = it->second;
258 versionList::const_iterator vlit;
259 for ( vlit = vl.begin(); vlit != vl.end(); ++vlit ) {
260 if ( vlist != "" ) vlist += ";";
263 versions[ it->first ] = vlist;
264 aLength1 += it->first.size() + vlist.size() + 2;
267 unitsSize = units.size();
268 commentSize = comment.size();
270 char* aProperty = new char[3 + aLength + 12 * aNames.size() + 1 + unitsSize + 1 + commentSize + 1 + aLength1 ];
272 char crMode = (char)GetCreationMode();
274 sprintf(aProperty,"%c%c", crMode, IsLocked()?'l':'u');
276 aLength = aNames.size();
278 for (anIndex = 0; anIndex < aLength; anIndex++) {
279 sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
280 (int)(aMinutes[anIndex]),
281 (int)(aHours[anIndex]),
282 (int)(aDays[anIndex]),
283 (int)(aMonths[anIndex]),
284 (int)(aYears[anIndex]),
285 (char*)(aNames[anIndex].c_str()));
286 a = strlen(aProperty);
290 //Write delimeter of the section to define end of the modifications section
293 //Write units if need
294 if(units.size() > 0) {
295 sprintf(&(aProperty[a]),"%s",units.c_str());
296 a = strlen(aProperty);
299 aProperty[a++] = 1; //delimeter of the units and comments
301 //Write comments if need
302 if(comment.size() > 0) {
303 sprintf(&(aProperty[a]),"%s",comment.c_str());
304 a = strlen(aProperty);
307 aProperty[a++] = 30; //delimeter of the component versions
309 std::map<std::string, std::string>::const_iterator versionsIt;
310 for ( versionsIt = versions.begin(); versionsIt != versions.end(); ++versionsIt ) {
311 sprintf(&(aProperty[a]),"%s=%s",
312 (char*)(versionsIt->first.c_str()),
313 (char*)(versionsIt->second.c_str()));
314 a = strlen(aProperty);
319 std::string prop(aProperty);
325 void SALOMEDSImpl_AttributeStudyProperties::SetUnits(const std::string& theUnits)
327 if(myUnits == theUnits)
336 std::string SALOMEDSImpl_AttributeStudyProperties::GetUnits() const
341 void SALOMEDSImpl_AttributeStudyProperties::SetComment(const std::string& theComment)
343 if(myComment == theComment)
349 myComment = theComment;
352 std::string SALOMEDSImpl_AttributeStudyProperties::GetComment() const
357 void SALOMEDSImpl_AttributeStudyProperties::SetComponentVersion(const std::string& theComponent, const std::string& theVersion)
359 if (!theComponent.empty()) {
362 if (myComponentVersions.find(theComponent) == myComponentVersions.end()) myComponentVersions[theComponent] = versionList();
363 if (myComponentVersions[theComponent].empty() || myComponentVersions[theComponent].back() != theVersion)
364 myComponentVersions[theComponent].push_back(theVersion);
368 std::vector<std::string> SALOMEDSImpl_AttributeStudyProperties::GetStoredComponents() const
370 std::vector<std::string> components;
371 versionMap::const_iterator it;
372 for (it = myComponentVersions.begin(); it != myComponentVersions.end(); ++it)
373 components.push_back(it->first);
377 std::string SALOMEDSImpl_AttributeStudyProperties::GetComponentVersion(const std::string& theComponent) const
379 versionList versions = GetComponentVersions(theComponent);
380 return versions.size() > 0 ? versions[0] : std::string("");
383 std::vector<std::string> SALOMEDSImpl_AttributeStudyProperties::GetComponentVersions(const std::string& theComponent) const
385 versionList versions;
386 versionMap::const_iterator it = myComponentVersions.find(theComponent);
387 if ( it != myComponentVersions.end() ) versions = it->second;
391 std::map< std::string, std::vector<std::string> > SALOMEDSImpl_AttributeStudyProperties::GetComponentsVersions() const
393 return myComponentVersions;
396 void SALOMEDSImpl_AttributeStudyProperties::SetComponentsVersions( const std::map< std::string, std::vector<std::string> >& theVersions )
398 myComponentVersions = theVersions;
401 void SALOMEDSImpl_AttributeStudyProperties::Load(const std::string& value)
403 char* aCopy = (char*)value.c_str();
405 int crMode = (int)aCopy[0];
406 SetCreationMode(crMode);
409 // number 13 below is minimal size of modification data record, which has form:
410 // mmhhddMMyyyyname1, where
411 // - mm: minute = 2 bytes
412 // - hh: hour = 2 bytes
413 // - dd: day = 2 bytes
414 // - MM: month = 2 bytes
415 // - yyyy: year = 4 bytes
416 // - name: user's name = arbitrary value, minimal length is 0 bytes
417 // - 1 : records delimiter = 1 byte
418 for (anIndex = 2; anIndex + 13 < value.size() ;) {
420 int aMinute, aHour, aDay, aMonth, aYear;
421 str[0] = aCopy[anIndex++];
422 str[1] = aCopy[anIndex++];
425 str[0] = aCopy[anIndex++];
426 str[1] = aCopy[anIndex++];
428 str[0] = aCopy[anIndex++];
429 str[1] = aCopy[anIndex++];
431 str[0] = aCopy[anIndex++];
432 str[1] = aCopy[anIndex++];
434 str[0] = aCopy[anIndex++];
435 str[1] = aCopy[anIndex++];
436 str[2] = aCopy[anIndex++];
437 str[3] = aCopy[anIndex++];
442 for(aNameSize = 0; aCopy[anIndex+aNameSize]!=1; aNameSize++);
443 char *aName = new char[aNameSize+1];
444 strncpy(aName, &(aCopy[anIndex]), aNameSize);
445 aName[aNameSize] = 0;
446 SetModification(aName,aMinute,aHour,aDay,aMonth,aYear);
448 anIndex += aNameSize + 1;
450 //Check end of the modifications section
451 if(anIndex < value.size() && aCopy[anIndex] == 30)
455 //Case when study contains units and comment properties
456 if( anIndex < value.size() ) {
457 anIndex++; //skip the delimeter of the sections: char(30)
459 for(unitsSize = 0; aCopy[anIndex+unitsSize] != 1; unitsSize++);
462 char *anUnits = new char[unitsSize+1];
463 strncpy(anUnits, &(aCopy[anIndex]), unitsSize);
464 anUnits[unitsSize] = 0;
468 anIndex += unitsSize + 1;
471 for(commentSize = 0; aCopy[anIndex+commentSize] != 0 && aCopy[anIndex+commentSize] != 30; commentSize++);
473 if(commentSize > 0) {
474 char *aComment = new char[commentSize+1];
475 strncpy(aComment, &(aCopy[anIndex]), commentSize);
476 aComment[commentSize] = 0;
477 SetComment(aComment);
478 delete [] (aComment);
480 anIndex += commentSize + 1;
483 //Case when study contains components versions
484 if( anIndex < value.size() ) {
485 while ( anIndex < value.size() && aCopy[anIndex] != 0 ) {
487 for(modSize = 0; aCopy[anIndex+modSize] != '='; modSize++);
489 for(verSize = 0; aCopy[anIndex+modSize+1+verSize] != 1; verSize++);
492 char *aModule = new char[modSize+1];
493 strncpy(aModule, &(aCopy[anIndex]), modSize);
494 aModule[modSize] = 0;
495 char *aVersions = new char[verSize+1];
497 strncpy(aVersions, &(aCopy[anIndex+modSize+1]), verSize);
498 aVersions[verSize] = 0;
500 std::string mVersions = aVersions;
501 int start = 0, idx = mVersions.find( ';', start );
502 while ( idx != std::string::npos ) {
503 SetComponentVersion( aModule, mVersions.substr( start, idx-start ) );
505 idx = mVersions.find( ';', start );
507 SetComponentVersion( aModule, mVersions.substr( start ) );
510 delete [] (aVersions);
511 anIndex += modSize + 1 + verSize + 1;
516 if (aCopy[1] == 'l') {