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>
18 #include <BinMDataStd.hxx>
20 //=======================================================================
21 //function : BinLDrivers_DocumentSection
22 //purpose : Empty constructor
23 //=======================================================================
25 BinLDrivers_DocumentSection::BinLDrivers_DocumentSection ()
26 : myIsPostRead (Standard_False)
32 //=======================================================================
33 //function : BinLDrivers_DocumentSection
34 //purpose : Constructor
35 //=======================================================================
37 BinLDrivers_DocumentSection::BinLDrivers_DocumentSection
38 (const TCollection_AsciiString& theName,
39 const Standard_Boolean isPostRead)
41 myIsPostRead (isPostRead)
47 //=======================================================================
50 //=======================================================================
52 const TCollection_AsciiString& BinLDrivers_DocumentSection::Name () const
57 //=======================================================================
60 //=======================================================================
62 uint64_t BinLDrivers_DocumentSection::Offset () const
67 //=======================================================================
68 //function : SetOffset
70 //=======================================================================
72 void BinLDrivers_DocumentSection::SetOffset (const uint64_t theOffset)
74 myValue[0] = theOffset;
77 //=======================================================================
78 //function : IsPostRead
80 //=======================================================================
82 Standard_Boolean BinLDrivers_DocumentSection::IsPostRead () const
87 //=======================================================================
90 //=======================================================================
92 uint64_t BinLDrivers_DocumentSection::Length () const
97 //=======================================================================
98 //function : SetLength
100 //=======================================================================
102 void BinLDrivers_DocumentSection::SetLength (const uint64_t theLength)
104 myValue[1] = theLength;
107 //=======================================================================
108 //function : WriteTOC
110 //=======================================================================
112 void BinLDrivers_DocumentSection::WriteTOC (Standard_OStream& theStream)
116 if (myName.IsEmpty() == Standard_False) {
117 Standard_Integer * aBufSz = reinterpret_cast<Standard_Integer *> (&aBuf[0]);
118 const Standard_Size aBufSzSize = sizeof(aBuf) / sizeof(Standard_Integer);
119 aBufSz[aBufSzSize-1] = 0;
121 strncpy (&aBuf[sizeof(Standard_Integer)],
123 sizeof(aBuf)-sizeof(Standard_Integer)-1);
125 // Calculate the length of the buffer: Standard_Size + string.
126 // If the length is not multiple of Standard_Size, it is properly increased
127 const Standard_Size aLen = strlen (&aBuf[sizeof(Standard_Integer)]);
128 Standard_Size aBufSize =
129 (aLen/sizeof(Standard_Integer))*sizeof(Standard_Integer);
131 aBufSize += sizeof(Standard_Integer);
133 // Write the buffer: size + string
135 aBufSz[0] = InverseInt ((Standard_Integer)aBufSize);
137 aBufSz[0] = (Standard_Integer)aBufSize;
139 theStream.write (&aBuf[0], aBufSize + sizeof(Standard_Integer));
141 // Store the address of Offset word in the file
142 myValue[0] = (uint64_t) theStream.tellp();
145 // Write the placeholders of Offset and Length of the section that should
146 // be written afterwards
150 theStream.write (&aBuf[0], 3*sizeof(uint64_t));
154 //=======================================================================
157 //=======================================================================
159 void BinLDrivers_DocumentSection::Write (Standard_OStream& theStream,
160 const uint64_t theOffset)
162 const uint64_t aSectionEnd = (uint64_t) theStream.tellp();
163 theStream.seekp((std::streamsize)myValue[0]);
164 myValue[0] = theOffset;
165 myValue[1] = aSectionEnd - theOffset;
169 uint64_t(myIsPostRead ? 1 : 0)
172 aVal[0] = InverseUint64(aVal[0]);
173 aVal[1] = InverseUint64(aVal[1]);
174 aVal[2] = InverseUint64(aVal[2]);
177 theStream.write((char *)&aVal[0], 3*sizeof(uint64_t));
178 theStream.seekp((std::streamsize)aSectionEnd);
181 //=======================================================================
184 //=======================================================================
186 void BinLDrivers_DocumentSection::ReadTOC
187 (BinLDrivers_DocumentSection& theSection,
188 Standard_IStream& theStream,
189 const Standard_Integer theDocFormatVersion)
192 Standard_Integer aNameBufferSize;
193 theStream.read ((char *)&aNameBufferSize, sizeof(Standard_Integer));
195 aNameBufferSize = InverseSize(aNameBufferSize);
197 if (aNameBufferSize > 0) {
198 theStream.read ((char *)&aBuf[0], (Standard_Size)aNameBufferSize);
199 theSection.myName = (Standard_CString)&aBuf[0];
202 if (theDocFormatVersion <= 9)
204 // Old documents stored file position as 4-bytes values.
205 Standard_Integer aValInt[3];
206 theStream.read ((char *)&aValInt[0], 3*sizeof(Standard_Integer));
208 aValue[0] = InverseInt (aValInt[0]);
209 aValue[1] = InverseInt (aValInt[1]);
210 aValue[2] = InverseInt (aValInt[2]);
212 aValue[0] = aValInt[0];
213 aValue[1] = aValInt[1];
214 aValue[2] = aValInt[2];
219 theStream.read ((char *)&aValue[0], 3*sizeof(uint64_t));
221 aValue[0] = InverseUint64 (aValue[0]);
222 aValue[1] = InverseUint64 (aValue[1]);
223 aValue[2] = InverseUint64 (aValue[2]);
227 theSection.myValue[0] = aValue[0];
228 theSection.myValue[1] = aValue[1];
229 theSection.myIsPostRead = (aValue[2] != 0);