Warnings on vc14 were eliminated
[occt.git] / src / BinTools / BinTools.cxx
CommitLineData
b311480e 1// Created on: 2004-05-18
2// Created by: Sergey ZARITCHNY
973c2be1 3// Copyright (c) 2004-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
42cf5bc1 16
17#include <BinTools.hxx>
745c1386 18#include <BinTools_ShapeSet.hxx>
7fd59977 19#include <FSD_FileHeader.hxx>
745c1386 20#include <OSD_OpenFile.hxx>
7fd59977 21#include <Storage_StreamTypeMismatchError.hxx>
22
23//=======================================================================
24//function : PutBool
25//purpose :
26//=======================================================================
7fd59977 27Standard_OStream& BinTools::PutBool(Standard_OStream& OS, const Standard_Boolean aValue)
28{
dde68833 29 OS.put((Standard_Byte)(aValue ? 1 : 0));
7fd59977 30 return OS;
31}
32
33
34//=======================================================================
35//function : PutInteger
36//purpose :
37//=======================================================================
38
39Standard_OStream& BinTools::PutInteger(Standard_OStream& OS, const Standard_Integer aValue)
40{
41 Standard_Integer anIntValue = aValue;
42#if DO_INVERSE
43 anIntValue = InverseInt (aValue);
44#endif
45 OS.write((char*)&anIntValue, sizeof(Standard_Integer));
46 return OS;
47}
48
49
50//=======================================================================
51//function : PutReal
52//purpose :
53//=======================================================================
54
55Standard_OStream& BinTools::PutReal(Standard_OStream& OS, const Standard_Real aValue)
56{
57 Standard_Real aRValue = aValue;
58#if DO_INVERSE
59 aRValue = InverseReal (aValue);
60#endif
61 OS.write((char*)&aRValue, sizeof(Standard_Real));
62 return OS;
63}
64
65//=======================================================================
66//function : PutExtChar
67//purpose :
68//=======================================================================
69
70Standard_OStream& BinTools::PutExtChar(Standard_OStream& OS, const Standard_ExtCharacter aValue)
71{
72 Standard_ExtCharacter aSValue = aValue;
73#if DO_INVERSE
74 aSValue = InverseExtChar (aValue);
75#endif
76 OS.write((char*)&aSValue, sizeof(Standard_ExtCharacter));
77 return OS;
78}
79//=======================================================================
80//function : GetReal
81//purpose :
82//=======================================================================
83
84Standard_IStream& BinTools::GetReal(Standard_IStream& IS, Standard_Real& aValue)
85{
86 if(!IS.read ((char*)&aValue, sizeof(Standard_Real)))
9775fa61 87 throw Storage_StreamTypeMismatchError();
7fd59977 88#if DO_INVERSE
89 aValue = InverseReal (aValue);
90#endif
91 return IS;
92}
93
94//=======================================================================
95//function : GetInteger
96//purpose :
97//=======================================================================
98
99Standard_IStream& BinTools::GetInteger(Standard_IStream& IS, Standard_Integer& aValue)
100{
101 if(!IS.read ((char*)&aValue, sizeof(Standard_Integer)))
9775fa61 102 throw Storage_StreamTypeMismatchError();;
7fd59977 103#if DO_INVERSE
104 aValue = InverseInt (aValue);
105#endif
106 return IS;
107}
108
109//=======================================================================
110//function : GetExtChar
111//purpose :
112//=======================================================================
113
114Standard_IStream& BinTools::GetExtChar(Standard_IStream& IS, Standard_ExtCharacter& theValue)
115{
116 if(!IS.read ((char*)&theValue, sizeof(Standard_ExtCharacter)))
9775fa61 117 throw Storage_StreamTypeMismatchError();;
7fd59977 118#if DO_INVERSE
119 theValue = InverseExtChar (theValue);
120#endif
121 return IS;
122}
123
124//=======================================================================
125//function : GetBool
126//purpose :
127//=======================================================================
128
129Standard_IStream& BinTools::GetBool(Standard_IStream& IS, Standard_Boolean& aValue)
130{
dde68833 131 aValue = (IS.get() != 0);
7fd59977 132 return IS;
133}
745c1386 134
135//=======================================================================
136//function : Write
137//purpose :
138//=======================================================================
139
140void BinTools::Write (const TopoDS_Shape& theShape, Standard_OStream& theStream)
141{
142 BinTools_ShapeSet aShapeSet(Standard_True);
143 aShapeSet.SetFormatNb (3);
144 aShapeSet.Add (theShape);
145 aShapeSet.Write (theStream);
146 aShapeSet.Write (theShape, theStream);
147}
148
149//=======================================================================
150//function : Read
151//purpose :
152//=======================================================================
153
154void BinTools::Read (TopoDS_Shape& theShape, Standard_IStream& theStream)
155{
156 BinTools_ShapeSet aShapeSet(Standard_True);
157 aShapeSet.Read (theStream);
158 aShapeSet.Read (theShape, theStream, aShapeSet.NbShapes());
159}
160
161//=======================================================================
162//function : Write
163//purpose :
164//=======================================================================
165
166Standard_Boolean BinTools::Write (const TopoDS_Shape& theShape, const Standard_CString theFile)
167{
168 ofstream aStream;
169 aStream.precision (15);
170 OSD_OpenStream (aStream, theFile, ios::out | ios::binary);
171 if (!aStream.good())
172 return Standard_False;
173
174 Write (theShape, aStream);
175 aStream.close();
176 return aStream.good();
177}
178
179//=======================================================================
180//function : Read
181//purpose :
182//=======================================================================
183
184Standard_Boolean BinTools::Read (TopoDS_Shape& theShape, const Standard_CString theFile)
185{
186 filebuf aBuf;
fb0b0531 187 OSD_OpenStream (aBuf, theFile, ios::in | ios::binary);
745c1386 188 if (!aBuf.is_open())
189 return Standard_False;
190
191 Standard_IStream aStream (&aBuf);
192 Read (theShape, aStream);
193 return aStream.good();
194}