1 // Created on: 2004-05-18
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2004-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.
17 #include <BinTools.hxx>
18 #include <BinTools_ShapeSet.hxx>
19 #include <FSD_FileHeader.hxx>
20 #include <OSD_OpenFile.hxx>
21 #include <Storage_StreamTypeMismatchError.hxx>
23 //=======================================================================
26 //=======================================================================
27 Standard_OStream& BinTools::PutBool(Standard_OStream& OS, const Standard_Boolean aValue)
29 OS.put((Standard_Byte)(aValue ? 1 : 0));
34 //=======================================================================
35 //function : PutInteger
37 //=======================================================================
39 Standard_OStream& BinTools::PutInteger(Standard_OStream& OS, const Standard_Integer aValue)
41 Standard_Integer anIntValue = aValue;
43 anIntValue = InverseInt (aValue);
45 OS.write((char*)&anIntValue, sizeof(Standard_Integer));
50 //=======================================================================
53 //=======================================================================
55 Standard_OStream& BinTools::PutReal(Standard_OStream& OS, const Standard_Real aValue)
57 Standard_Real aRValue = aValue;
59 aRValue = InverseReal (aValue);
61 OS.write((char*)&aRValue, sizeof(Standard_Real));
65 //=======================================================================
66 //function : PutExtChar
68 //=======================================================================
70 Standard_OStream& BinTools::PutExtChar(Standard_OStream& OS, const Standard_ExtCharacter aValue)
72 Standard_ExtCharacter aSValue = aValue;
74 aSValue = InverseExtChar (aValue);
76 OS.write((char*)&aSValue, sizeof(Standard_ExtCharacter));
79 //=======================================================================
82 //=======================================================================
84 Standard_IStream& BinTools::GetReal(Standard_IStream& IS, Standard_Real& aValue)
86 if(!IS.read ((char*)&aValue, sizeof(Standard_Real)))
87 throw Storage_StreamTypeMismatchError();
89 aValue = InverseReal (aValue);
94 //=======================================================================
95 //function : GetInteger
97 //=======================================================================
99 Standard_IStream& BinTools::GetInteger(Standard_IStream& IS, Standard_Integer& aValue)
101 if(!IS.read ((char*)&aValue, sizeof(Standard_Integer)))
102 throw Storage_StreamTypeMismatchError();;
104 aValue = InverseInt (aValue);
109 //=======================================================================
110 //function : GetExtChar
112 //=======================================================================
114 Standard_IStream& BinTools::GetExtChar(Standard_IStream& IS, Standard_ExtCharacter& theValue)
116 if(!IS.read ((char*)&theValue, sizeof(Standard_ExtCharacter)))
117 throw Storage_StreamTypeMismatchError();;
119 theValue = InverseExtChar (theValue);
124 //=======================================================================
127 //=======================================================================
129 Standard_IStream& BinTools::GetBool(Standard_IStream& IS, Standard_Boolean& aValue)
131 aValue = (IS.get() != 0);
135 //=======================================================================
138 //=======================================================================
140 void BinTools::Write (const TopoDS_Shape& theShape, Standard_OStream& theStream)
142 BinTools_ShapeSet aShapeSet(Standard_True);
143 aShapeSet.SetFormatNb (3);
144 aShapeSet.Add (theShape);
145 aShapeSet.Write (theStream);
146 aShapeSet.Write (theShape, theStream);
149 //=======================================================================
152 //=======================================================================
154 void BinTools::Read (TopoDS_Shape& theShape, Standard_IStream& theStream)
156 BinTools_ShapeSet aShapeSet(Standard_True);
157 aShapeSet.Read (theStream);
158 aShapeSet.Read (theShape, theStream, aShapeSet.NbShapes());
161 //=======================================================================
164 //=======================================================================
166 Standard_Boolean BinTools::Write (const TopoDS_Shape& theShape, const Standard_CString theFile)
169 aStream.precision (15);
170 OSD_OpenStream (aStream, theFile, ios::out | ios::binary);
172 return Standard_False;
174 Write (theShape, aStream);
176 return aStream.good();
179 //=======================================================================
182 //=======================================================================
184 Standard_Boolean BinTools::Read (TopoDS_Shape& theShape, const Standard_CString theFile)
187 OSD_OpenStream (aBuf, theFile, ios::in | ios::binary);
189 return Standard_False;
191 Standard_IStream aStream (&aBuf);
192 Read (theShape, aStream);
193 return aStream.good();