b311480e |
1 | // Created on: 2001-08-24 |
2 | // Created by: Alexnder GRIGORIEV |
973c2be1 |
3 | // Copyright (c) 2001-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
b311480e |
15 | |
16 | //AGV 150202: Changed prototype XmlObjMgt::SetStringValue() |
7fd59977 |
17 | |
7fd59977 |
18 | # include <stdio.h> |
19 | #include <XmlMDataStd_RealArrayDriver.ixx> |
20 | #include <TDataStd_RealArray.hxx> |
21 | #include <XmlObjMgt.hxx> |
22 | #include <XmlMDataStd.hxx> |
f7b4312f |
23 | #include <TColStd_HArray1OfReal.hxx> |
24 | #include <NCollection_LocalArray.hxx> |
8e1e79f0 |
25 | #include <Standard_ErrorHandler.hxx> |
26 | #include <Standard_OutOfMemory.hxx> |
7fd59977 |
27 | |
28 | IMPLEMENT_DOMSTRING (FirstIndexString, "first") |
29 | IMPLEMENT_DOMSTRING (LastIndexString, "last") |
30 | IMPLEMENT_DOMSTRING (IsDeltaOn, "delta") |
31 | |
32 | //======================================================================= |
33 | //function : XmlMDataStd_RealArrayDriver |
34 | //purpose : Constructor |
35 | //======================================================================= |
36 | |
37 | XmlMDataStd_RealArrayDriver::XmlMDataStd_RealArrayDriver |
38 | (const Handle(CDM_MessageDriver)& theMsgDriver) |
39 | : XmlMDF_ADriver (theMsgDriver, NULL) |
40 | {} |
41 | |
42 | //======================================================================= |
43 | //function : NewEmpty |
44 | //purpose : |
45 | //======================================================================= |
46 | Handle(TDF_Attribute) XmlMDataStd_RealArrayDriver::NewEmpty() const |
47 | { |
48 | return (new TDataStd_RealArray()); |
49 | } |
50 | |
51 | //======================================================================= |
52 | //function : Paste |
53 | //purpose : persistent -> transient (retrieve) |
54 | //======================================================================= |
55 | Standard_Boolean XmlMDataStd_RealArrayDriver::Paste |
56 | (const XmlObjMgt_Persistent& theSource, |
57 | const Handle(TDF_Attribute)& theTarget, |
58 | XmlObjMgt_RRelocationTable& ) const |
59 | { |
60 | Standard_Integer aFirstInd, aLastInd, ind; |
61 | Standard_Real aValue; |
62 | const XmlObjMgt_Element& anElement = theSource; |
63 | |
64 | // Read the FirstIndex; if the attribute is absent initialize to 1 |
65 | XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString()); |
66 | if (aFirstIndex == NULL) |
67 | aFirstInd = 1; |
68 | else if (!aFirstIndex.GetInteger(aFirstInd)) { |
69 | TCollection_ExtendedString aMessageString = |
70 | TCollection_ExtendedString("Cannot retrieve the first index" |
71 | " for RealArray attribute as \"") |
72 | + aFirstIndex + "\""; |
73 | WriteMessage (aMessageString); |
74 | return Standard_False; |
75 | } |
76 | |
77 | // Read LastIndex; the attribute should be present |
78 | if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) { |
79 | TCollection_ExtendedString aMessageString = |
80 | TCollection_ExtendedString("Cannot retrieve the last index" |
81 | " for RealArray attribute as \"") |
82 | + aFirstIndex + "\""; |
83 | WriteMessage (aMessageString); |
84 | return Standard_False; |
85 | } |
86 | |
87 | Handle(TDataStd_RealArray) aRealArray = |
88 | Handle(TDataStd_RealArray)::DownCast(theTarget); |
89 | aRealArray->Init(aFirstInd, aLastInd); |
90 | |
91 | // Check the type of LDOMString |
92 | const XmlObjMgt_DOMString& aString = XmlObjMgt::GetStringValue(anElement); |
93 | if (aString.Type() == LDOMBasicString::LDOM_Integer) { |
94 | if (aFirstInd == aLastInd) { |
95 | Standard_Integer anIntValue; |
96 | if (aString.GetInteger(anIntValue)) |
97 | aRealArray -> SetValue (aFirstInd, Standard_Real(anIntValue)); |
98 | } else { |
99 | TCollection_ExtendedString aMessageString = |
100 | TCollection_ExtendedString("Cannot retrieve array of real members" |
101 | " for RealArray attribute from Integer \"") |
102 | + aString + "\""; |
103 | WriteMessage (aMessageString); |
104 | return Standard_False; |
105 | } |
106 | } else { |
107 | Standard_CString aValueStr = Standard_CString(aString.GetString()); |
108 | for (ind = aFirstInd; ind <= aLastInd; ind++) |
109 | { |
110 | if (!XmlObjMgt::GetReal(aValueStr, aValue)) { |
111 | TCollection_ExtendedString aMessageString = |
112 | TCollection_ExtendedString("Cannot retrieve real member" |
113 | " for RealArray attribute as \"") |
114 | + aValueStr + "\""; |
115 | WriteMessage (aMessageString); |
116 | return Standard_False; |
117 | } |
118 | aRealArray->SetValue(ind, aValue); |
119 | } |
120 | } |
7fd59977 |
121 | Standard_Boolean aDelta(Standard_False); |
122 | |
123 | if(XmlMDataStd::DocumentVersion() > 2) { |
124 | Standard_Integer aDeltaValue; |
125 | if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) |
126 | { |
127 | TCollection_ExtendedString aMessageString = |
128 | TCollection_ExtendedString("Cannot retrieve the isDelta value" |
129 | " for RealArray attribute as \"") |
130 | + aDeltaValue + "\""; |
131 | WriteMessage (aMessageString); |
132 | return Standard_False; |
133 | } |
134 | else |
135 | aDelta = (Standard_Boolean)aDeltaValue; |
136 | } |
63c629aa |
137 | #ifdef XMLMDATASTD_DEB |
7fd59977 |
138 | else if(XmlMDataStd::DocumentVersion() == -1) |
139 | cout << "Current DocVersion field is not initialized. " <<endl; |
140 | #endif |
141 | aRealArray->SetDelta(aDelta); |
142 | |
143 | return Standard_True; |
144 | } |
145 | |
146 | //======================================================================= |
147 | //function : Paste |
148 | //purpose : transient -> persistent (store) |
149 | //======================================================================= |
150 | void XmlMDataStd_RealArrayDriver::Paste (const Handle(TDF_Attribute)& theSource, |
151 | XmlObjMgt_Persistent& theTarget, |
152 | XmlObjMgt_SRelocationTable& ) const |
153 | { |
154 | Handle(TDataStd_RealArray) aRealArray = |
155 | Handle(TDataStd_RealArray)::DownCast(theSource); |
f7b4312f |
156 | const Handle(TColStd_HArray1OfReal)& hRealArray = aRealArray->Array(); |
157 | const TColStd_Array1OfReal& realArray = hRealArray->Array1(); |
158 | Standard_Integer aL = realArray.Lower(), anU = realArray.Upper(); |
7fd59977 |
159 | |
160 | if (aL != 1) theTarget.Element().setAttribute(::FirstIndexString(), aL); |
161 | theTarget.Element().setAttribute(::LastIndexString(), anU); |
162 | theTarget.Element().setAttribute(::IsDeltaOn(), aRealArray->GetDelta()); |
163 | |
f7b4312f |
164 | // Allocation of 25 chars for each double value including the space: |
165 | // An example: -3.1512678732195273e+020 |
166 | Standard_Integer iChar = 0; |
167 | NCollection_LocalArray<Standard_Character> str; |
168 | if (realArray.Length()) |
8e1e79f0 |
169 | { |
170 | try |
171 | { |
172 | OCC_CATCH_SIGNALS |
173 | str.Allocate(25 * realArray.Length() + 1); |
174 | } |
175 | catch (Standard_OutOfMemory) |
176 | { |
177 | // If allocation of big space for the string of double array values failed, |
178 | // try to calculate the necessary space more accurate and allocate it. |
179 | // It may take some time... therefore it was not done initially and |
180 | // an attempt to use a simple 25 chars for a double value was used. |
181 | Standard_Character buf[25]; |
182 | Standard_Integer i(aL), nbChars(0); |
183 | while (i <= anU) |
184 | { |
185 | nbChars += Sprintf(buf, "%.17g ", realArray.Value(i++)) + 1/*a space*/; |
186 | } |
187 | if (nbChars) |
188 | str.Allocate(nbChars); |
189 | } |
190 | } |
191 | |
7fd59977 |
192 | Standard_Integer i = aL; |
302f96fb |
193 | for (;;) |
f7b4312f |
194 | { |
302f96fb |
195 | iChar += Sprintf(&(str[iChar]), "%.17g ", realArray.Value(i)); |
f7b4312f |
196 | if (i >= anU) |
197 | break; |
7fd59977 |
198 | ++i; |
199 | } |
f7b4312f |
200 | |
7fd59977 |
201 | // No occurrence of '&', '<' and other irregular XML characters |
f7b4312f |
202 | if (realArray.Length()) |
203 | { |
204 | str[iChar - 1] = '\0'; |
205 | XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True); |
206 | } |
7fd59977 |
207 | } |