0022627: Change OCCT memory management defaults
[occt.git] / src / BinLDrivers / BinLDrivers_DocumentSection.cxx
CommitLineData
7fd59977 1// File: BinLDrivers_DocumentSection.cxx
2// Created: 20.06.08 18:45:28
3// Author: Alexander GRIGORIEV
4// Copyright: Open Cascade 2008
5
6#include <BinLDrivers_DocumentSection.hxx>
7#include <FSD_FileHeader.hxx>
8
9//=======================================================================
10//function : BinLDrivers_DocumentSection
11//purpose : Empty constructor
12//=======================================================================
13
14BinLDrivers_DocumentSection::BinLDrivers_DocumentSection ()
15 : myIsPostRead (Standard_False)
16{
17 myValue[0] = 0;
18 myValue[1] = 0;
19}
20
21//=======================================================================
22//function : BinLDrivers_DocumentSection
23//purpose : Constructor
24//=======================================================================
25
26BinLDrivers_DocumentSection::BinLDrivers_DocumentSection
27 (const TCollection_AsciiString& theName,
28 const Standard_Boolean isPostRead)
29 : myName (theName),
30 myIsPostRead (isPostRead)
31{
32 myValue[0] = 0;
33 myValue[1] = 0;
34}
35
36//=======================================================================
37//function : Name
38//purpose :
39//=======================================================================
40
41const TCollection_AsciiString& BinLDrivers_DocumentSection::Name () const
42{
43 return myName;
44}
45
46//=======================================================================
47//function : Offset
48//purpose :
49//=======================================================================
50
51Standard_Size BinLDrivers_DocumentSection::Offset () const
52{
53 return myValue[0];
54}
55
56//=======================================================================
57//function : SetOffset
58//purpose :
59//=======================================================================
60
61void BinLDrivers_DocumentSection::SetOffset (const Standard_Size theOffset)
62{
63 myValue[0] = theOffset;
64}
65
66//=======================================================================
67//function : IsPostRead
68//purpose :
69//=======================================================================
70
71Standard_Boolean BinLDrivers_DocumentSection::IsPostRead () const
72{
73 return myIsPostRead;
74}
75
76//=======================================================================
77//function : Length
78//purpose :
79//=======================================================================
80
81Standard_Size BinLDrivers_DocumentSection::Length () const
82{
83 return myValue[1];
84}
85
86//=======================================================================
87//function : SetLength
88//purpose :
89//=======================================================================
90
91void BinLDrivers_DocumentSection::SetLength (const Standard_Size theLength)
92{
93 myValue[1] = theLength;
94}
95
96//=======================================================================
97//function : WriteTOC
98//purpose :
99//=======================================================================
100
101void BinLDrivers_DocumentSection::WriteTOC (Standard_OStream& theStream)
102{
103 char aBuf[512];
104
105 if (myName.IsEmpty() == Standard_False) {
106 Standard_Integer * aBufSz = reinterpret_cast<Standard_Integer *> (&aBuf[0]);
107 const Standard_Size aBufSzSize = sizeof(aBuf) / sizeof(Standard_Integer);
108 aBufSz[aBufSzSize-1] = 0;
109
110 strncpy (&aBuf[sizeof(Standard_Integer)],
111 myName.ToCString(),
112 sizeof(aBuf)-sizeof(Standard_Integer)-1);
113
114 // Calculate the length of the buffer: Standard_Size + string.
115 // If the length is not multiple of Standard_Size, it is properly increased
116 const Standard_Size aLen = strlen (&aBuf[sizeof(Standard_Integer)]);
117 Standard_Size aBufSize =
118 (aLen/sizeof(Standard_Integer))*sizeof(Standard_Integer);
119 if (aBufSize < aLen)
120 aBufSize += sizeof(Standard_Integer);
121
122 // Write the buffer: size + string
123#if DO_INVERSE
124 aBufSz[0] = InverseSize ((Standard_Integer)aBufSize);
125#else
126 aBufSz[0] = aBufSize;
127#endif
128 theStream.write (&aBuf[0], aBufSize + sizeof(Standard_Integer));
129
130 // Store the address of Offset word in the file
131 myValue[0] = (Standard_Size) theStream.tellp();
132 myValue[1] = 0;
133
134 // Write the placeholders of Offset and Length of the section that should
135 // be written afterwards
136 aBufSz[0] = 0;
137 aBufSz[1] = 0;
138 aBufSz[2] = 0;
139 theStream.write (&aBuf[0], 3*sizeof(Standard_Integer));
140 }
141}
142
143//=======================================================================
144//function : Write
145//purpose :
146//=======================================================================
147
148void BinLDrivers_DocumentSection::Write (Standard_OStream& theStream,
149 const Standard_Size theOffset)
150{
151 const Standard_Size aSectionEnd = (Standard_Size) theStream.tellp();
152 theStream.seekp(myValue[0]);
153 myValue[0] = theOffset;
154 myValue[1] = aSectionEnd - theOffset;
155 Standard_Integer aVal[3] = {
156 Standard_Integer(myValue[0]),
157 Standard_Integer(myValue[1]),
158 Standard_Integer(myIsPostRead)
159 };
160#if DO_INVERSE
161 aVal[0] = InverseSize(aVal[0]);
162 aVal[1] = InverseSize(aVal[1]);
163 aVal[2] = InverseSize(aVal[2]);
164#endif
165
166 theStream.write((char *)&aVal[0], 3*sizeof(Standard_Integer));
167 theStream.seekp(aSectionEnd);
168}
169
170//=======================================================================
171//function : ReadTOC
172//purpose :
173//=======================================================================
174
175void BinLDrivers_DocumentSection::ReadTOC
176 (BinLDrivers_DocumentSection& theSection,
177 Standard_IStream& theStream)
178{
179 char aBuf[512];
180 Standard_Integer aNameBufferSize;
181 theStream.read ((char *)&aNameBufferSize, sizeof(Standard_Integer));
182#if DO_INVERSE
183 aNameBufferSize = InverseSize(aNameBufferSize);
184#endif
185 if (aNameBufferSize > 0) {
186 theStream.read ((char *)&aBuf[0], (Standard_Size)aNameBufferSize);
187 theSection.myName = (Standard_CString)&aBuf[0];
188 Standard_Integer aValue[3];
189 theStream.read ((char *)&aValue[0], 3*sizeof(Standard_Integer));
190#if DO_INVERSE
191 aValue[0] = InverseSize (aValue[0]);
192 aValue[1] = InverseSize (aValue[1]);
193 aValue[2] = InverseSize (aValue[2]);
194#endif
195 theSection.myValue[0] = (Standard_Size)aValue[0];
196 theSection.myValue[1] = (Standard_Size)aValue[1];
197 theSection.myIsPostRead = (Standard_Boolean)aValue[2];
198 }
199}