19f62f265333562813b76e643e839c989b648e27
[occt.git] / src / BinTools / BinTools.cxx
1 // Created on: 2004-05-18
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <BinTools.hxx>
18 #include <BinTools_ShapeSet.hxx>
19 #include <FSD_FileHeader.hxx>
20 #include <OSD_OpenFile.hxx>
21 #include <Storage_StreamTypeMismatchError.hxx>
22
23 //=======================================================================
24 //function : PutBool
25 //purpose  : 
26 //=======================================================================
27 Standard_OStream& BinTools::PutBool(Standard_OStream& OS, const Standard_Boolean aValue)
28 {
29   OS.put((Standard_Byte)(aValue ? 1 : 0));
30   return OS;
31 }
32
33
34 //=======================================================================
35 //function : PutInteger
36 //purpose  : 
37 //=======================================================================
38
39 Standard_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
55 Standard_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
70 Standard_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
84 Standard_IStream& BinTools::GetReal(Standard_IStream& IS, Standard_Real& aValue)
85 {
86   if(!IS.read ((char*)&aValue, sizeof(Standard_Real)))
87     throw Storage_StreamTypeMismatchError();
88 #if DO_INVERSE
89   aValue = InverseReal (aValue);
90 #endif
91   return IS;
92 }
93
94 //=======================================================================
95 //function : GetInteger
96 //purpose  : 
97 //=======================================================================
98
99 Standard_IStream& BinTools::GetInteger(Standard_IStream& IS, Standard_Integer& aValue)
100 {
101   if(!IS.read ((char*)&aValue, sizeof(Standard_Integer)))
102     throw Storage_StreamTypeMismatchError();;
103 #if DO_INVERSE
104   aValue = InverseInt (aValue);
105 #endif
106   return IS;
107 }
108
109 //=======================================================================
110 //function : GetExtChar
111 //purpose  : 
112 //=======================================================================
113
114 Standard_IStream& BinTools::GetExtChar(Standard_IStream& IS, Standard_ExtCharacter& theValue)
115 {
116   if(!IS.read ((char*)&theValue, sizeof(Standard_ExtCharacter)))
117     throw Storage_StreamTypeMismatchError();;
118 #if DO_INVERSE
119   theValue = InverseExtChar (theValue);
120 #endif
121   return IS;
122 }
123
124 //=======================================================================
125 //function : GetBool
126 //purpose  : 
127 //=======================================================================
128
129 Standard_IStream& BinTools::GetBool(Standard_IStream& IS, Standard_Boolean& aValue)
130 {
131   aValue = (IS.get() != 0);
132   return IS;
133 }
134
135 //=======================================================================
136 //function : Write
137 //purpose  : 
138 //=======================================================================
139
140 void 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
154 void 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
166 Standard_Boolean BinTools::Write (const TopoDS_Shape& theShape, const Standard_CString theFile)
167 {
168   std::ofstream aStream;
169   aStream.precision (15);
170   OSD_OpenStream (aStream, theFile, std::ios::out | std::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
184 Standard_Boolean BinTools::Read (TopoDS_Shape& theShape, const Standard_CString theFile)
185 {
186   std::filebuf aBuf;
187   OSD_OpenStream (aBuf, theFile, std::ios::in | std::ios::binary);
188   if (!aBuf.is_open())
189     return Standard_False;
190
191   Standard_IStream aStream (&aBuf);
192   Read (theShape, aStream);
193   return aStream.good();
194 }