Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeStudyProperties.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDSImpl_AttributeStudyProperties.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "SALOMEDSImpl_AttributeStudyProperties.hxx"
28 #include <string.h>
29
30 const std::string& SALOMEDSImpl_AttributeStudyProperties::GetID()
31 {
32   static std::string SALOMEDSImpl_AttributeStudyPropertiesID ("128371A2-8F52-11d6-A8A3-0001021E8C7F");
33   return SALOMEDSImpl_AttributeStudyPropertiesID;
34 }
35
36 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_AttributeStudyProperties::Set(const DF_Label& label)
37 {
38   SALOMEDSImpl_AttributeStudyProperties* A = NULL;
39   if (!(A=(SALOMEDSImpl_AttributeStudyProperties*)label.FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID()))) {
40     A = new SALOMEDSImpl_AttributeStudyProperties();
41     label.AddAttribute(A);
42   }
43   return A;
44 }
45
46 SALOMEDSImpl_AttributeStudyProperties::SALOMEDSImpl_AttributeStudyProperties()
47 :SALOMEDSImpl_GenericAttribute("AttributeStudyProperties")
48 {
49   myLocked = false;
50   myLockChanged = false;
51   Init();
52 }
53
54 void SALOMEDSImpl_AttributeStudyProperties::Init()
55 {
56   myUserName.clear();
57   myMinute.clear();
58   myHour.clear();
59   myDay.clear();
60   myMonth.clear();
61   myYear.clear();
62   myMode = 0; // none
63 }
64
65 void SALOMEDSImpl_AttributeStudyProperties::SetModification(const std::string& theUserName,
66                                                             const int            theMinute,
67                                                             const int            theHour,
68                                                             const int            theDay,
69                                                             const int            theMonth,
70                                                             const int            theYear)
71 {
72   if (theMinute<0 || theMinute>60 || theHour<0 || theHour>24 ||
73       theDay<0 || theDay>31 || theMonth<0 || theMonth>12)
74     return;
75
76   CheckLocked();
77   Backup();
78
79   myUserName.push_back(theUserName);
80   myMinute.push_back(theMinute);
81   myHour.push_back(theHour);
82   myDay.push_back(theDay);
83   myMonth.push_back(theMonth);
84   myYear.push_back(theYear);
85 }
86
87 void SALOMEDSImpl_AttributeStudyProperties::GetModifications
88                   (std::vector<std::string>& theUserNames,
89                    std::vector<int>&    theMinutes,
90                    std::vector<int>&    theHours,
91                    std::vector<int>&    theDays,
92                    std::vector<int>&    theMonths,
93                    std::vector<int>&    theYears) const
94 {
95   theUserNames = myUserName;
96   theMinutes = myMinute;
97   theHours = myHour;
98   theDays = myDay;
99   theMonths = myMonth;
100   theYears = myYear;
101 }
102
103 std::string SALOMEDSImpl_AttributeStudyProperties::GetCreatorName() const
104 {
105   if (myUserName.size() == 0)
106     return std::string("");
107   return myUserName[0];
108 }
109
110 bool SALOMEDSImpl_AttributeStudyProperties::GetCreationDate
111                               (int&           theMinute,
112                                int&           theHour,
113                                int&           theDay,
114                                int&           theMonth,
115                                int&           theYear) const
116 {
117   if (myMinute.size() != 0) {
118     theMinute = myMinute[0];
119     theHour = myHour[0];
120     theDay = myDay[0];
121     theMonth = myMonth[0];
122     theYear = myYear[0];
123     return true;
124   }
125   return false;
126 }
127
128 void SALOMEDSImpl_AttributeStudyProperties::ChangeCreatorName(const std::string& theName)
129 {
130   if (myUserName.size() > 0) {
131     CheckLocked();
132     Backup();
133     myUserName[0] = theName;
134   }
135 }
136
137 void SALOMEDSImpl_AttributeStudyProperties::SetCreationMode(const int theMode)
138 {
139   CheckLocked();
140   Backup();
141   myMode = theMode;
142 }
143
144 int SALOMEDSImpl_AttributeStudyProperties::GetCreationMode() const
145 {
146   return myMode;
147 }
148
149 void SALOMEDSImpl_AttributeStudyProperties::SetModified(const int theModified)
150 {
151   myModified = theModified;
152 }
153
154 bool SALOMEDSImpl_AttributeStudyProperties::IsModified() const
155 {
156   return (myModified != 0);
157 }
158
159 int SALOMEDSImpl_AttributeStudyProperties::GetModified() const
160 {
161   return myModified;
162 }
163
164 void SALOMEDSImpl_AttributeStudyProperties::SetLocked(const bool theLocked)
165 {
166 //  Backup();
167   if (myLocked != theLocked) {
168     myLockChanged = true;
169     myLocked = theLocked;
170   }
171 }
172
173 bool SALOMEDSImpl_AttributeStudyProperties::IsLocked() const
174 {
175   return myLocked;
176 }
177
178 bool SALOMEDSImpl_AttributeStudyProperties::IsLockChanged(const bool theErase) {
179   if (!myLockChanged) return false;
180   if (theErase) myLockChanged = false;
181   return true;
182 }
183
184 const std::string& SALOMEDSImpl_AttributeStudyProperties::ID() const
185 {
186   return GetID();
187 }
188
189 void SALOMEDSImpl_AttributeStudyProperties::Restore(DF_Attribute* with)
190 {
191   SALOMEDSImpl_AttributeStudyProperties* aProp =
192     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(with);
193
194   Init();
195   std::vector<std::string> aNames;
196   std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
197   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
198   for (int i = 0, len = aNames.size(); i < len; i++) {
199     myUserName.push_back(aNames[i]);
200     myMinute.push_back(aMinutes[i]);
201     myHour.push_back(aHours[i]);
202     myDay.push_back(aDays[i]);
203     myMonth.push_back(aMonths[i]);
204     myYear.push_back(aYears[i]);
205   }
206   myMode = aProp->GetCreationMode();
207 //  myModified = aProp->GetModified();
208 //  myLocked = aProp->IsLocked();
209 }
210
211 DF_Attribute* SALOMEDSImpl_AttributeStudyProperties::NewEmpty() const
212 {
213   return new SALOMEDSImpl_AttributeStudyProperties();
214 }
215
216 void SALOMEDSImpl_AttributeStudyProperties::Paste(DF_Attribute* into)
217 {
218   SALOMEDSImpl_AttributeStudyProperties* aProp =
219     dynamic_cast<SALOMEDSImpl_AttributeStudyProperties*>(into);
220   aProp->Init();
221
222   int i;
223   for(i = 0; i < myUserName.size(); i++) {
224     aProp->SetModification(myUserName[i],
225                            myMinute[i], myHour[i],
226                            myDay[i], myMonth[i], myYear[i]);
227   }
228
229   aProp->SetCreationMode(myMode);
230 //  aProp->SetModified(myModified);
231 //  aProp->SetLocked(myLocked);
232 }
233
234
235 std::string SALOMEDSImpl_AttributeStudyProperties::Save()
236 {
237   std::vector<std::string> aNames;
238   std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
239   GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
240
241   int aLength, anIndex, unitsSize = 0, commentSize = 0;;
242   for (aLength = 0, anIndex = aNames.size()-1; anIndex >= 0; anIndex--)
243     aLength += aNames[anIndex].size() + 1;
244
245   std::string units = GetUnits();
246   std::string comment = GetComment();
247   
248   unitsSize = units.size();
249   commentSize = comment.size();
250
251   char* aProperty = new char[3 + aLength + 12 * aNames.size() + 1 + unitsSize + 1 + commentSize];
252
253   char crMode = (char)GetCreationMode();
254
255   sprintf(aProperty,"%c%c", crMode, IsLocked()?'l':'u');
256
257   aLength = aNames.size();
258   int a = 2;
259   for (anIndex = 0; anIndex  < aLength; anIndex++) {
260     sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
261             (int)(aMinutes[anIndex]),
262             (int)(aHours[anIndex]),
263             (int)(aDays[anIndex]),
264             (int)(aMonths[anIndex]),
265             (int)(aYears[anIndex]),
266             (char*)(aNames[anIndex].c_str()));
267     a = strlen(aProperty);
268     aProperty[a++] = 1;
269   }
270
271   //Write delimeter of the section to define end of the modifications section
272   aProperty[a++] = 30;
273
274   //Write units if need
275   if(units.size() > 0) {
276     sprintf(&(aProperty[a]),"%s",units.c_str());
277     a = strlen(aProperty);
278   }
279
280   aProperty[a++] = 1; //delimeter of the units and comments
281
282   //Write comments if need
283   if(comment.size() > 0) {
284     sprintf(&(aProperty[a]),"%s",comment.c_str());
285     a = strlen(aProperty);
286     a++;
287   }
288   
289   aProperty[a] = 0;
290   std::string prop(aProperty);
291   delete aProperty;
292
293   return prop;
294 }
295
296 void SALOMEDSImpl_AttributeStudyProperties::SetUnits(const std::string& theUnits) {
297   if(myUnits == theUnits)
298     return;
299   myUnits = theUnits;
300 }
301
302 std::string SALOMEDSImpl_AttributeStudyProperties::GetUnits() {
303   return myUnits;
304 }
305
306 void SALOMEDSImpl_AttributeStudyProperties::SetComment(const std::string& theComment) {
307   if(myComment == theComment)
308     return;
309   myComment = theComment;
310 }
311
312 std::string SALOMEDSImpl_AttributeStudyProperties::GetComment() {
313   return myComment;
314 }
315
316
317 void SALOMEDSImpl_AttributeStudyProperties::Load(const std::string& value)
318 {
319   char* aCopy = (char*)value.c_str();
320
321   int crMode = (int)aCopy[0];
322   SetCreationMode(crMode);
323
324   int anIndex;
325   for (anIndex = 2; anIndex + 2 < value.size() ;) {
326     char str[10];
327     int aMinute, aHour, aDay, aMonth, aYear;
328     str[0] = aCopy[anIndex++];
329     str[1] = aCopy[anIndex++];
330     str[2] = 0;
331     aMinute = atoi(str);
332     str[0] = aCopy[anIndex++];
333     str[1] = aCopy[anIndex++];
334     aHour =  atoi(str);
335     str[0] = aCopy[anIndex++];
336     str[1] = aCopy[anIndex++];
337     aDay =  atoi(str);
338     str[0] = aCopy[anIndex++];
339     str[1] = aCopy[anIndex++];
340     aMonth =  atoi(str);
341     str[0] = aCopy[anIndex++];
342     str[1] = aCopy[anIndex++];
343     str[2] = aCopy[anIndex++];
344     str[3] = aCopy[anIndex++];
345     str[4] = 0;
346     aYear = atoi(str);
347
348     int aNameSize;
349     for(aNameSize = 0; aCopy[anIndex+aNameSize]!=1; aNameSize++);
350     char *aName = new char[aNameSize+1];
351     strncpy(aName, &(aCopy[anIndex]), aNameSize);
352     aName[aNameSize] = 0;
353     SetModification(aName,aMinute,aHour,aDay,aMonth,aYear);
354     delete [] (aName);
355     anIndex += aNameSize + 1;
356     
357     //Check end of the modifications section
358     if(anIndex < value.size() && aCopy[anIndex] == 30)
359       break;
360   }
361   
362   //Case then study contains units and comment properties
363   if( anIndex < value.size() ) {
364     anIndex++; //skip the delimeter of the sections: char(30)
365     int unitsSize;
366     for(unitsSize = 0; aCopy[anIndex+unitsSize] != 1; unitsSize++);
367
368     if(unitsSize > 0) {
369       char *anUnits = new char[unitsSize+1];
370       strncpy(anUnits, &(aCopy[anIndex]), unitsSize);
371       anUnits[unitsSize] = 0;
372       SetUnits(anUnits);
373       delete [] (anUnits);
374     }
375     anIndex += unitsSize + 1;
376
377     int commentSize;
378     for(commentSize = 0; aCopy[anIndex+commentSize] != 0; commentSize++);
379
380     if(commentSize > 0) {
381       char *aComment = new char[commentSize+1];
382       strncpy(aComment, &(aCopy[anIndex]), commentSize);
383       aComment[commentSize] = 0;
384       SetComment(aComment);
385       delete [] (aComment);
386     }
387     anIndex += commentSize;
388   }
389   
390   if (aCopy[1] == 'l') {
391     SetLocked(true);
392   }
393   SetModified(0);  
394 }