1 // Created on: 2008-06-20
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <BinLDrivers_DocumentSection.hxx>
17 #include <FSD_FileHeader.hxx>
19 //=======================================================================
20 //function : BinLDrivers_DocumentSection
21 //purpose : Empty constructor
22 //=======================================================================
24 BinLDrivers_DocumentSection::BinLDrivers_DocumentSection ()
25 : myIsPostRead (Standard_False)
31 //=======================================================================
32 //function : BinLDrivers_DocumentSection
33 //purpose : Constructor
34 //=======================================================================
36 BinLDrivers_DocumentSection::BinLDrivers_DocumentSection
37 (const TCollection_AsciiString& theName,
38 const Standard_Boolean isPostRead)
40 myIsPostRead (isPostRead)
46 //=======================================================================
49 //=======================================================================
51 const TCollection_AsciiString& BinLDrivers_DocumentSection::Name () const
56 //=======================================================================
59 //=======================================================================
61 Standard_Size BinLDrivers_DocumentSection::Offset () const
66 //=======================================================================
67 //function : SetOffset
69 //=======================================================================
71 void BinLDrivers_DocumentSection::SetOffset (const Standard_Size theOffset)
73 myValue[0] = theOffset;
76 //=======================================================================
77 //function : IsPostRead
79 //=======================================================================
81 Standard_Boolean BinLDrivers_DocumentSection::IsPostRead () const
86 //=======================================================================
89 //=======================================================================
91 Standard_Size BinLDrivers_DocumentSection::Length () const
96 //=======================================================================
97 //function : SetLength
99 //=======================================================================
101 void BinLDrivers_DocumentSection::SetLength (const Standard_Size theLength)
103 myValue[1] = theLength;
106 //=======================================================================
107 //function : WriteTOC
109 //=======================================================================
111 void BinLDrivers_DocumentSection::WriteTOC (Standard_OStream& theStream)
115 if (myName.IsEmpty() == Standard_False) {
116 Standard_Integer * aBufSz = reinterpret_cast<Standard_Integer *> (&aBuf[0]);
117 const Standard_Size aBufSzSize = sizeof(aBuf) / sizeof(Standard_Integer);
118 aBufSz[aBufSzSize-1] = 0;
120 strncpy (&aBuf[sizeof(Standard_Integer)],
122 sizeof(aBuf)-sizeof(Standard_Integer)-1);
124 // Calculate the length of the buffer: Standard_Size + string.
125 // If the length is not multiple of Standard_Size, it is properly increased
126 const Standard_Size aLen = strlen (&aBuf[sizeof(Standard_Integer)]);
127 Standard_Size aBufSize =
128 (aLen/sizeof(Standard_Integer))*sizeof(Standard_Integer);
130 aBufSize += sizeof(Standard_Integer);
132 // Write the buffer: size + string
134 aBufSz[0] = InverseSize ((Standard_Integer)aBufSize);
136 aBufSz[0] = (Standard_Integer)aBufSize;
138 theStream.write (&aBuf[0], aBufSize + sizeof(Standard_Integer));
140 // Store the address of Offset word in the file
141 myValue[0] = (Standard_Size) theStream.tellp();
144 // Write the placeholders of Offset and Length of the section that should
145 // be written afterwards
149 theStream.write (&aBuf[0], 3*sizeof(Standard_Integer));
153 //=======================================================================
156 //=======================================================================
158 void BinLDrivers_DocumentSection::Write (Standard_OStream& theStream,
159 const Standard_Size theOffset)
161 const Standard_Size aSectionEnd = (Standard_Size) theStream.tellp();
162 theStream.seekp(myValue[0]);
163 myValue[0] = theOffset;
164 myValue[1] = aSectionEnd - theOffset;
165 Standard_Integer aVal[3] = {
166 Standard_Integer(myValue[0]),
167 Standard_Integer(myValue[1]),
168 Standard_Integer(myIsPostRead)
171 aVal[0] = InverseSize(aVal[0]);
172 aVal[1] = InverseSize(aVal[1]);
173 aVal[2] = InverseSize(aVal[2]);
176 theStream.write((char *)&aVal[0], 3*sizeof(Standard_Integer));
177 theStream.seekp(aSectionEnd);
180 //=======================================================================
183 //=======================================================================
185 void BinLDrivers_DocumentSection::ReadTOC
186 (BinLDrivers_DocumentSection& theSection,
187 Standard_IStream& theStream)
190 Standard_Integer aNameBufferSize;
191 theStream.read ((char *)&aNameBufferSize, sizeof(Standard_Integer));
193 aNameBufferSize = InverseSize(aNameBufferSize);
195 if (aNameBufferSize > 0) {
196 theStream.read ((char *)&aBuf[0], (Standard_Size)aNameBufferSize);
197 theSection.myName = (Standard_CString)&aBuf[0];
198 Standard_Integer aValue[3];
199 theStream.read ((char *)&aValue[0], 3*sizeof(Standard_Integer));
201 aValue[0] = InverseSize (aValue[0]);
202 aValue[1] = InverseSize (aValue[1]);
203 aValue[2] = InverseSize (aValue[2]);
205 theSection.myValue[0] = (Standard_Size)aValue[0];
206 theSection.myValue[1] = (Standard_Size)aValue[1];
207 theSection.myIsPostRead = (Standard_Boolean)aValue[2];