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 |
27 | Standard_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 | |
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 |
6b467e52 |
52 | //purpose : |
7fd59977 |
53 | //======================================================================= |
6b467e52 |
54 | Standard_OStream& BinTools::PutReal (Standard_OStream& theOS, |
55 | const Standard_Real& theValue) |
56 | { |
57 | #if DO_INVERSE |
58 | const Standard_Real aRValue = InverseReal (theValue); |
59 | theOS.write((char*)&aRValue, sizeof(Standard_Real)); |
60 | #else |
61 | theOS.write((char*)&theValue, sizeof(Standard_Real)); |
62 | #endif |
63 | return theOS; |
64 | } |
7fd59977 |
65 | |
6b467e52 |
66 | //======================================================================= |
67 | //function : PutShortReal |
68 | //purpose : |
69 | //======================================================================= |
70 | Standard_OStream& BinTools::PutShortReal (Standard_OStream& theOS, |
71 | const Standard_ShortReal& theValue) |
7fd59977 |
72 | { |
7fd59977 |
73 | #if DO_INVERSE |
6b467e52 |
74 | const Standard_ShortReal aValue = InverseShortReal (theValue); |
75 | theOS.write ((char*)&aValue, sizeof(Standard_ShortReal)); |
76 | #else |
77 | theOS.write ((char*)&theValue, sizeof(Standard_ShortReal)); |
7fd59977 |
78 | #endif |
6b467e52 |
79 | return theOS; |
7fd59977 |
80 | } |
81 | |
82 | //======================================================================= |
83 | //function : PutExtChar |
84 | //purpose : |
85 | //======================================================================= |
86 | |
87 | Standard_OStream& BinTools::PutExtChar(Standard_OStream& OS, const Standard_ExtCharacter aValue) |
88 | { |
89 | Standard_ExtCharacter aSValue = aValue; |
90 | #if DO_INVERSE |
91 | aSValue = InverseExtChar (aValue); |
92 | #endif |
93 | OS.write((char*)&aSValue, sizeof(Standard_ExtCharacter)); |
94 | return OS; |
95 | } |
6b467e52 |
96 | |
7fd59977 |
97 | //======================================================================= |
98 | //function : GetReal |
6b467e52 |
99 | //purpose : |
7fd59977 |
100 | //======================================================================= |
6b467e52 |
101 | Standard_IStream& BinTools::GetReal (Standard_IStream& theIS, |
102 | Standard_Real& theValue) |
103 | { |
104 | if (!theIS.read ((char*)&theValue, sizeof(Standard_Real))) |
105 | { |
106 | throw Storage_StreamTypeMismatchError(); |
107 | } |
108 | #if DO_INVERSE |
109 | theValue = InverseReal (theValue); |
110 | #endif |
111 | return theIS; |
112 | } |
7fd59977 |
113 | |
6b467e52 |
114 | //======================================================================= |
115 | //function : GetShortReal |
116 | //purpose : |
117 | //======================================================================= |
118 | Standard_IStream& BinTools::GetShortReal (Standard_IStream& theIS, |
119 | Standard_ShortReal& theValue) |
7fd59977 |
120 | { |
6b467e52 |
121 | if (!theIS.read ((char*)&theValue, sizeof(Standard_ShortReal))) |
122 | { |
9775fa61 |
123 | throw Storage_StreamTypeMismatchError(); |
6b467e52 |
124 | } |
7fd59977 |
125 | #if DO_INVERSE |
6b467e52 |
126 | theValue = InverseShortReal (theValue); |
7fd59977 |
127 | #endif |
6b467e52 |
128 | return theIS; |
7fd59977 |
129 | } |
130 | |
131 | //======================================================================= |
132 | //function : GetInteger |
133 | //purpose : |
134 | //======================================================================= |
135 | |
136 | Standard_IStream& BinTools::GetInteger(Standard_IStream& IS, Standard_Integer& aValue) |
137 | { |
138 | if(!IS.read ((char*)&aValue, sizeof(Standard_Integer))) |
9775fa61 |
139 | throw Storage_StreamTypeMismatchError();; |
7fd59977 |
140 | #if DO_INVERSE |
141 | aValue = InverseInt (aValue); |
142 | #endif |
143 | return IS; |
144 | } |
145 | |
146 | //======================================================================= |
147 | //function : GetExtChar |
148 | //purpose : |
149 | //======================================================================= |
150 | |
151 | Standard_IStream& BinTools::GetExtChar(Standard_IStream& IS, Standard_ExtCharacter& theValue) |
152 | { |
153 | if(!IS.read ((char*)&theValue, sizeof(Standard_ExtCharacter))) |
9775fa61 |
154 | throw Storage_StreamTypeMismatchError();; |
7fd59977 |
155 | #if DO_INVERSE |
156 | theValue = InverseExtChar (theValue); |
157 | #endif |
158 | return IS; |
159 | } |
160 | |
161 | //======================================================================= |
162 | //function : GetBool |
163 | //purpose : |
164 | //======================================================================= |
165 | |
166 | Standard_IStream& BinTools::GetBool(Standard_IStream& IS, Standard_Boolean& aValue) |
167 | { |
dde68833 |
168 | aValue = (IS.get() != 0); |
7fd59977 |
169 | return IS; |
170 | } |
745c1386 |
171 | |
172 | //======================================================================= |
173 | //function : Write |
174 | //purpose : |
175 | //======================================================================= |
176 | |
177 | void BinTools::Write (const TopoDS_Shape& theShape, Standard_OStream& theStream) |
178 | { |
179 | BinTools_ShapeSet aShapeSet(Standard_True); |
180 | aShapeSet.SetFormatNb (3); |
181 | aShapeSet.Add (theShape); |
182 | aShapeSet.Write (theStream); |
183 | aShapeSet.Write (theShape, theStream); |
184 | } |
185 | |
186 | //======================================================================= |
187 | //function : Read |
188 | //purpose : |
189 | //======================================================================= |
190 | |
191 | void BinTools::Read (TopoDS_Shape& theShape, Standard_IStream& theStream) |
192 | { |
193 | BinTools_ShapeSet aShapeSet(Standard_True); |
194 | aShapeSet.Read (theStream); |
195 | aShapeSet.Read (theShape, theStream, aShapeSet.NbShapes()); |
196 | } |
197 | |
198 | //======================================================================= |
199 | //function : Write |
200 | //purpose : |
201 | //======================================================================= |
202 | |
203 | Standard_Boolean BinTools::Write (const TopoDS_Shape& theShape, const Standard_CString theFile) |
204 | { |
04232180 |
205 | std::ofstream aStream; |
745c1386 |
206 | aStream.precision (15); |
04232180 |
207 | OSD_OpenStream (aStream, theFile, std::ios::out | std::ios::binary); |
745c1386 |
208 | if (!aStream.good()) |
209 | return Standard_False; |
210 | |
211 | Write (theShape, aStream); |
212 | aStream.close(); |
213 | return aStream.good(); |
214 | } |
215 | |
216 | //======================================================================= |
217 | //function : Read |
218 | //purpose : |
219 | //======================================================================= |
220 | |
221 | Standard_Boolean BinTools::Read (TopoDS_Shape& theShape, const Standard_CString theFile) |
222 | { |
04232180 |
223 | std::filebuf aBuf; |
224 | OSD_OpenStream (aBuf, theFile, std::ios::in | std::ios::binary); |
745c1386 |
225 | if (!aBuf.is_open()) |
226 | return Standard_False; |
227 | |
228 | Standard_IStream aStream (&aBuf); |
229 | Read (theShape, aStream); |
230 | return aStream.good(); |
231 | } |