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