Salome HOME
0021133: EDF 1605 ALL: Space in dump files instead of tabulations
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeStudyProperties.cxx
1 //  Copyright (C) 2007-2010  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;
242   for (aLength = 0, anIndex = aNames.size()-1; anIndex >= 0; anIndex--)
243     aLength += aNames[anIndex].size() + 1;
244
245   char* aProperty = new char[3 + aLength + 12 * aNames.size()];
246
247   char crMode = (char)GetCreationMode();
248
249   sprintf(aProperty,"%c%c", crMode, IsLocked()?'l':'u');
250
251   aLength = aNames.size();
252   int a = 2;
253   for (anIndex = 0; anIndex  < aLength; anIndex++) {
254     sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
255             (int)(aMinutes[anIndex]),
256             (int)(aHours[anIndex]),
257             (int)(aDays[anIndex]),
258             (int)(aMonths[anIndex]),
259             (int)(aYears[anIndex]),
260             (char*)(aNames[anIndex].c_str()));
261     a = strlen(aProperty);
262     aProperty[a++] = 1;
263   }
264   aProperty[a] = 0;
265   std::string prop(aProperty);
266   delete aProperty;
267
268   return prop;
269 }
270
271 void SALOMEDSImpl_AttributeStudyProperties::Load(const std::string& value)
272 {
273   char* aCopy = (char*)value.c_str();
274
275   int crMode = (int)aCopy[0];
276   SetCreationMode(crMode);
277
278   int anIndex;
279   for (anIndex = 2; anIndex + 2 < value.size() ;) {
280     char str[10];
281     int aMinute, aHour, aDay, aMonth, aYear;
282     str[0] = aCopy[anIndex++];
283     str[1] = aCopy[anIndex++];
284     str[2] = 0;
285     aMinute = atoi(str);
286     str[0] = aCopy[anIndex++];
287     str[1] = aCopy[anIndex++];
288     aHour =  atoi(str);
289     str[0] = aCopy[anIndex++];
290     str[1] = aCopy[anIndex++];
291     aDay =  atoi(str);
292     str[0] = aCopy[anIndex++];
293     str[1] = aCopy[anIndex++];
294     aMonth =  atoi(str);
295     str[0] = aCopy[anIndex++];
296     str[1] = aCopy[anIndex++];
297     str[2] = aCopy[anIndex++];
298     str[3] = aCopy[anIndex++];
299     str[4] = 0;
300     aYear = atoi(str);
301
302     int aNameSize;
303     for(aNameSize = 0; aCopy[anIndex+aNameSize]!=1; aNameSize++);
304     char *aName = new char[aNameSize+1];
305     strncpy(aName, &(aCopy[anIndex]), aNameSize);
306     aName[aNameSize] = 0;
307     SetModification(aName,aMinute,aHour,aDay,aMonth,aYear);
308     delete [] (aName);
309     anIndex += aNameSize + 1;
310   }
311   if (aCopy[1] == 'l') {
312     SetLocked(true);
313   }
314   SetModified(0);
315 }