0023901: Memory leaks in tests.
[occt.git] / src / XmlMDataStd / XmlMDataStd_RealArrayDriver.cxx
CommitLineData
b311480e 1// Created on: 2001-08-24
2// Created by: Alexnder GRIGORIEV
3// Copyright (c) 2001-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
20//AGV 150202: Changed prototype XmlObjMgt::SetStringValue()
7fd59977 21
22#define OCC6010 // vro 01.06.2004
23
24# include <stdio.h>
25#include <XmlMDataStd_RealArrayDriver.ixx>
26#include <TDataStd_RealArray.hxx>
27#include <XmlObjMgt.hxx>
28#include <XmlMDataStd.hxx>
29
30IMPLEMENT_DOMSTRING (FirstIndexString, "first")
31IMPLEMENT_DOMSTRING (LastIndexString, "last")
32IMPLEMENT_DOMSTRING (IsDeltaOn, "delta")
33
34//=======================================================================
35//function : XmlMDataStd_RealArrayDriver
36//purpose : Constructor
37//=======================================================================
38
39XmlMDataStd_RealArrayDriver::XmlMDataStd_RealArrayDriver
40 (const Handle(CDM_MessageDriver)& theMsgDriver)
41 : XmlMDF_ADriver (theMsgDriver, NULL)
42{}
43
44//=======================================================================
45//function : NewEmpty
46//purpose :
47//=======================================================================
48Handle(TDF_Attribute) XmlMDataStd_RealArrayDriver::NewEmpty() const
49{
50 return (new TDataStd_RealArray());
51}
52
53//=======================================================================
54//function : Paste
55//purpose : persistent -> transient (retrieve)
56//=======================================================================
57Standard_Boolean XmlMDataStd_RealArrayDriver::Paste
58 (const XmlObjMgt_Persistent& theSource,
59 const Handle(TDF_Attribute)& theTarget,
60 XmlObjMgt_RRelocationTable& ) const
61{
62 Standard_Integer aFirstInd, aLastInd, ind;
63 Standard_Real aValue;
64 const XmlObjMgt_Element& anElement = theSource;
65
66 // Read the FirstIndex; if the attribute is absent initialize to 1
67 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
68 if (aFirstIndex == NULL)
69 aFirstInd = 1;
70 else if (!aFirstIndex.GetInteger(aFirstInd)) {
71 TCollection_ExtendedString aMessageString =
72 TCollection_ExtendedString("Cannot retrieve the first index"
73 " for RealArray attribute as \"")
74 + aFirstIndex + "\"";
75 WriteMessage (aMessageString);
76 return Standard_False;
77 }
78
79 // Read LastIndex; the attribute should be present
80 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) {
81 TCollection_ExtendedString aMessageString =
82 TCollection_ExtendedString("Cannot retrieve the last index"
83 " for RealArray attribute as \"")
84 + aFirstIndex + "\"";
85 WriteMessage (aMessageString);
86 return Standard_False;
87 }
88
89 Handle(TDataStd_RealArray) aRealArray =
90 Handle(TDataStd_RealArray)::DownCast(theTarget);
91 aRealArray->Init(aFirstInd, aLastInd);
92
93 // Check the type of LDOMString
94 const XmlObjMgt_DOMString& aString = XmlObjMgt::GetStringValue(anElement);
95 if (aString.Type() == LDOMBasicString::LDOM_Integer) {
96 if (aFirstInd == aLastInd) {
97 Standard_Integer anIntValue;
98 if (aString.GetInteger(anIntValue))
99 aRealArray -> SetValue (aFirstInd, Standard_Real(anIntValue));
100 } else {
101 TCollection_ExtendedString aMessageString =
102 TCollection_ExtendedString("Cannot retrieve array of real members"
103 " for RealArray attribute from Integer \"")
104 + aString + "\"";
105 WriteMessage (aMessageString);
106 return Standard_False;
107 }
108 } else {
109 Standard_CString aValueStr = Standard_CString(aString.GetString());
110 for (ind = aFirstInd; ind <= aLastInd; ind++)
111 {
112 if (!XmlObjMgt::GetReal(aValueStr, aValue)) {
113 TCollection_ExtendedString aMessageString =
114 TCollection_ExtendedString("Cannot retrieve real member"
115 " for RealArray attribute as \"")
116 + aValueStr + "\"";
117 WriteMessage (aMessageString);
118 return Standard_False;
119 }
120 aRealArray->SetValue(ind, aValue);
121 }
122 }
123#ifdef DEB
124 //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl;
125#endif
126 Standard_Boolean aDelta(Standard_False);
127
128 if(XmlMDataStd::DocumentVersion() > 2) {
129 Standard_Integer aDeltaValue;
130 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
131 {
132 TCollection_ExtendedString aMessageString =
133 TCollection_ExtendedString("Cannot retrieve the isDelta value"
134 " for RealArray attribute as \"")
135 + aDeltaValue + "\"";
136 WriteMessage (aMessageString);
137 return Standard_False;
138 }
139 else
140 aDelta = (Standard_Boolean)aDeltaValue;
141 }
142#ifdef DEB
143 else if(XmlMDataStd::DocumentVersion() == -1)
144 cout << "Current DocVersion field is not initialized. " <<endl;
145#endif
146 aRealArray->SetDelta(aDelta);
147
148 return Standard_True;
149}
150
151//=======================================================================
152//function : Paste
153//purpose : transient -> persistent (store)
154//=======================================================================
155void XmlMDataStd_RealArrayDriver::Paste (const Handle(TDF_Attribute)& theSource,
156 XmlObjMgt_Persistent& theTarget,
157 XmlObjMgt_SRelocationTable& ) const
158{
159 Handle(TDataStd_RealArray) aRealArray =
160 Handle(TDataStd_RealArray)::DownCast(theSource);
161
162 Standard_Integer aL = aRealArray->Lower(), anU = aRealArray->Upper();
163 TCollection_AsciiString aValueStr;
164
165 if (aL != 1) theTarget.Element().setAttribute(::FirstIndexString(), aL);
166 theTarget.Element().setAttribute(::LastIndexString(), anU);
167 theTarget.Element().setAttribute(::IsDeltaOn(), aRealArray->GetDelta());
168
169 Standard_Integer i = aL;
170 while (1) {
171#ifndef OCC6010
172 aValueStr += TCollection_AsciiString(aRealArray->Value(i));
173#else
174 char aValueChar[32];
91322f44 175 Sprintf(aValueChar, "%.15g", aRealArray->Value(i));
7fd59977 176 aValueStr += aValueChar;
177#endif
178 if (i >= anU) break;
179 aValueStr += ' ';
180 ++i;
181 }
182 // No occurrence of '&', '<' and other irregular XML characters
183 XmlObjMgt::SetStringValue (theTarget, aValueStr.ToCString(), Standard_True);
184}