0023850: TDataStd_ByteArray is too slow on storage on disk
[occt.git] / src / XmlMDataStd / XmlMDataStd_BooleanArrayDriver.cxx
CommitLineData
b311480e 1// Created on: 2007-05-29
2// Created by: Vlad Romashko
3// Copyright (c) 2007-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
7fd59977 20
21#include <XmlMDataStd_BooleanArrayDriver.ixx>
22#include <TDataStd_BooleanArray.hxx>
23#include <TColStd_HArray1OfByte.hxx>
f7b4312f 24#include <NCollection_LocalArray.hxx>
7fd59977 25#include <XmlObjMgt.hxx>
26
27IMPLEMENT_DOMSTRING (FirstIndexString, "first")
28IMPLEMENT_DOMSTRING (LastIndexString, "last")
29
30//=======================================================================
31//function : XmlMDataStd_BooleanArrayDriver
32//purpose : Constructor
33//=======================================================================
34XmlMDataStd_BooleanArrayDriver::XmlMDataStd_BooleanArrayDriver(const Handle(CDM_MessageDriver)& theMsgDriver)
35 : XmlMDF_ADriver (theMsgDriver, NULL)
36{
37
38}
39
40//=======================================================================
41//function : NewEmpty
42//purpose :
43//=======================================================================
44Handle(TDF_Attribute) XmlMDataStd_BooleanArrayDriver::NewEmpty() const
45{
46 return new TDataStd_BooleanArray();
47}
48
49//=======================================================================
50//function : Paste
51//purpose : persistent -> transient (retrieve)
52//=======================================================================
53Standard_Boolean XmlMDataStd_BooleanArrayDriver::Paste(const XmlObjMgt_Persistent& theSource,
54 const Handle(TDF_Attribute)& theTarget,
55 XmlObjMgt_RRelocationTable& ) const
56{
6e6cd5d9 57 Standard_Integer aFirstInd, aLastInd, aValue;
7fd59977 58 const XmlObjMgt_Element& anElement = theSource;
59
60 // Read the FirstIndex; if the attribute is absent initialize to 1
61 XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
62 if (aFirstIndex == NULL)
63 aFirstInd = 1;
64 else if (!aFirstIndex.GetInteger(aFirstInd))
65 {
66 TCollection_ExtendedString aMessageString =
67 TCollection_ExtendedString("Cannot retrieve the first index"
68 " for BooleanArray attribute as \"")
69 + aFirstIndex + "\"";
70 WriteMessage (aMessageString);
71 return Standard_False;
72 }
73
74 // Read the LastIndex; the attribute should be present
75 if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd))
76 {
77 TCollection_ExtendedString aMessageString =
78 TCollection_ExtendedString("Cannot retrieve the last index"
79 " for BooleanArray attribute as \"")
80 + aFirstIndex + "\"";
81 WriteMessage (aMessageString);
82 return Standard_False;
83 }
84
85 if (aFirstInd > aLastInd)
86 {
87 TCollection_ExtendedString aMessageString =
88 TCollection_ExtendedString("The last index is greater than the first index"
89 " for BooleanArray attribute \"");
90 WriteMessage (aMessageString);
91 return Standard_False;
92 }
93
94 Handle(TDataStd_BooleanArray) aBooleanArray = Handle(TDataStd_BooleanArray)::DownCast(theTarget);
95 aBooleanArray->Init(aFirstInd, aLastInd);
96 Standard_Integer length = aLastInd - aFirstInd + 1;
f7b4312f 97 Handle(TColStd_HArray1OfByte) hArr = new TColStd_HArray1OfByte(0, length >> 3);
98 TColStd_Array1OfByte& arr = hArr->ChangeArray1();
7fd59977 99
f7b4312f 100 Standard_Integer i = 0, upper = arr.Upper();
7fd59977 101 Standard_CString aValueStr = Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
102 for (; i <= upper; i++)
103 {
104 if (!XmlObjMgt::GetInteger(aValueStr, aValue))
105 {
106 TCollection_ExtendedString aMessageString =
107 TCollection_ExtendedString("Cannot retrieve integer member"
108 " for BooleanArray attribute as \"")
109 + aValueStr + "\"";
110 WriteMessage (aMessageString);
111 return Standard_False;
112 }
f7b4312f 113 arr.SetValue(i, (Standard_Byte) aValue);
7fd59977 114 }
f7b4312f 115 aBooleanArray->SetInternalArray(hArr);
7fd59977 116
117 return Standard_True;
118}
119
120//=======================================================================
121//function : Paste
122//purpose : transient -> persistent (store)
123//=======================================================================
124void XmlMDataStd_BooleanArrayDriver::Paste(const Handle(TDF_Attribute)& theSource,
125 XmlObjMgt_Persistent& theTarget,
126 XmlObjMgt_SRelocationTable& ) const
127{
128 Handle(TDataStd_BooleanArray) aBooleanArray = Handle(TDataStd_BooleanArray)::DownCast(theSource);
129
130 Standard_Integer aL = aBooleanArray->Lower();
131 Standard_Integer anU = aBooleanArray->Upper();
7fd59977 132
133 theTarget.Element().setAttribute(::FirstIndexString(), aL);
134 theTarget.Element().setAttribute(::LastIndexString(), anU);
135
f7b4312f 136 const Handle(TColStd_HArray1OfByte)& hArr = aBooleanArray->InternalArray();
137 const TColStd_Array1OfByte& arr = hArr->Array1();
138
139 // Allocation of 4 chars for each byte.
140 Standard_Integer iChar = 0;
141 NCollection_LocalArray<Standard_Character> str;
142 if (arr.Length())
143 str.Allocate(4 * arr.Length() + 1);
144
145 // Convert integers - compressed boolean values, to a string.
146 Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
7fd59977 147 for (; i <= upper; i++)
148 {
f7b4312f 149 const Standard_Byte& byte = arr.Value(i);
150 iChar += Sprintf(&(str[iChar]), "%d ", byte);
7fd59977 151 }
f7b4312f 152
153 if (arr.Length())
154 XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
7fd59977 155}