0025317: Failure on attempt to save an Ocaf document with a long double array attribu...
[occt.git] / src / XmlMDataStd / XmlMDataStd_RealArrayDriver.cxx
CommitLineData
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
28IMPLEMENT_DOMSTRING (FirstIndexString, "first")
29IMPLEMENT_DOMSTRING (LastIndexString, "last")
30IMPLEMENT_DOMSTRING (IsDeltaOn, "delta")
31
32//=======================================================================
33//function : XmlMDataStd_RealArrayDriver
34//purpose : Constructor
35//=======================================================================
36
37XmlMDataStd_RealArrayDriver::XmlMDataStd_RealArrayDriver
38 (const Handle(CDM_MessageDriver)& theMsgDriver)
39 : XmlMDF_ADriver (theMsgDriver, NULL)
40{}
41
42//=======================================================================
43//function : NewEmpty
44//purpose :
45//=======================================================================
46Handle(TDF_Attribute) XmlMDataStd_RealArrayDriver::NewEmpty() const
47{
48 return (new TDataStd_RealArray());
49}
50
51//=======================================================================
52//function : Paste
53//purpose : persistent -> transient (retrieve)
54//=======================================================================
55Standard_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 }
121#ifdef DEB
122 //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl;
123#endif
124 Standard_Boolean aDelta(Standard_False);
125
126 if(XmlMDataStd::DocumentVersion() > 2) {
127 Standard_Integer aDeltaValue;
128 if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue))
129 {
130 TCollection_ExtendedString aMessageString =
131 TCollection_ExtendedString("Cannot retrieve the isDelta value"
132 " for RealArray attribute as \"")
133 + aDeltaValue + "\"";
134 WriteMessage (aMessageString);
135 return Standard_False;
136 }
137 else
138 aDelta = (Standard_Boolean)aDeltaValue;
139 }
140#ifdef DEB
141 else if(XmlMDataStd::DocumentVersion() == -1)
142 cout << "Current DocVersion field is not initialized. " <<endl;
143#endif
144 aRealArray->SetDelta(aDelta);
145
146 return Standard_True;
147}
148
149//=======================================================================
150//function : Paste
151//purpose : transient -> persistent (store)
152//=======================================================================
153void XmlMDataStd_RealArrayDriver::Paste (const Handle(TDF_Attribute)& theSource,
154 XmlObjMgt_Persistent& theTarget,
155 XmlObjMgt_SRelocationTable& ) const
156{
157 Handle(TDataStd_RealArray) aRealArray =
158 Handle(TDataStd_RealArray)::DownCast(theSource);
f7b4312f 159 const Handle(TColStd_HArray1OfReal)& hRealArray = aRealArray->Array();
160 const TColStd_Array1OfReal& realArray = hRealArray->Array1();
161 Standard_Integer aL = realArray.Lower(), anU = realArray.Upper();
7fd59977 162
163 if (aL != 1) theTarget.Element().setAttribute(::FirstIndexString(), aL);
164 theTarget.Element().setAttribute(::LastIndexString(), anU);
165 theTarget.Element().setAttribute(::IsDeltaOn(), aRealArray->GetDelta());
166
f7b4312f 167 // Allocation of 25 chars for each double value including the space:
168 // An example: -3.1512678732195273e+020
169 Standard_Integer iChar = 0;
170 NCollection_LocalArray<Standard_Character> str;
171 if (realArray.Length())
8e1e79f0 172 {
173 try
174 {
175 OCC_CATCH_SIGNALS
176 str.Allocate(25 * realArray.Length() + 1);
177 }
178 catch (Standard_OutOfMemory)
179 {
180 // If allocation of big space for the string of double array values failed,
181 // try to calculate the necessary space more accurate and allocate it.
182 // It may take some time... therefore it was not done initially and
183 // an attempt to use a simple 25 chars for a double value was used.
184 Standard_Character buf[25];
185 Standard_Integer i(aL), nbChars(0);
186 while (i <= anU)
187 {
188 nbChars += Sprintf(buf, "%.17g ", realArray.Value(i++)) + 1/*a space*/;
189 }
190 if (nbChars)
191 str.Allocate(nbChars);
192 }
193 }
194
7fd59977 195 Standard_Integer i = aL;
302f96fb 196 for (;;)
f7b4312f 197 {
302f96fb 198 iChar += Sprintf(&(str[iChar]), "%.17g ", realArray.Value(i));
f7b4312f 199 if (i >= anU)
200 break;
7fd59977 201 ++i;
202 }
f7b4312f 203
7fd59977 204 // No occurrence of '&', '<' and other irregular XML characters
f7b4312f 205 if (realArray.Length())
206 {
207 str[iChar - 1] = '\0';
208 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
209 }
7fd59977 210}