b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
46216226 |
2 | // Copyright (c) 1999-2017 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
42cf5bc1 |
15 | #include <FSD_CmpFile.hxx> |
fb0b0531 |
16 | #include <OSD_OpenFile.hxx> |
42cf5bc1 |
17 | #include <Storage_StreamFormatError.hxx> |
42cf5bc1 |
18 | #include <Storage_StreamTypeMismatchError.hxx> |
42cf5bc1 |
19 | #include <Storage_StreamWriteError.hxx> |
7fd59977 |
20 | |
7fd59977 |
21 | const Standard_CString MAGICNUMBER = "CMPFILE"; |
7fd59977 |
22 | |
39c8dc70 |
23 | IMPLEMENT_STANDARD_RTTIEXT(FSD_CmpFile, FSD_File) |
24 | |
7fd59977 |
25 | //======================================================================= |
26 | //function : FSD_CmpFile |
27 | //purpose : |
28 | //======================================================================= |
29 | |
30 | FSD_CmpFile::FSD_CmpFile() |
46216226 |
31 | {} |
7fd59977 |
32 | |
33 | //======================================================================= |
34 | //function : IsGoodFileType |
35 | //purpose : INFO SECTION |
36 | // write |
37 | //======================================================================= |
38 | |
39 | Storage_Error FSD_CmpFile::IsGoodFileType(const TCollection_AsciiString& aName) |
40 | { |
41 | FSD_CmpFile f; |
42 | Storage_Error s; |
43 | |
46216226 |
44 | s = f.Open(aName, Storage_VSRead); |
7fd59977 |
45 | |
46 | if (s == Storage_VSOk) { |
47 | TCollection_AsciiString l; |
46216226 |
48 | Standard_Size len = strlen(FSD_CmpFile::MagicNumber()); |
7fd59977 |
49 | |
46216226 |
50 | f.ReadChar(l, len); |
7fd59977 |
51 | |
52 | f.Close(); |
53 | |
46216226 |
54 | if (strncmp(FSD_CmpFile::MagicNumber(), l.ToCString(), len) != 0) { |
7fd59977 |
55 | s = Storage_VSFormatError; |
56 | } |
57 | } |
58 | |
59 | return s; |
60 | } |
61 | |
62 | //======================================================================= |
63 | //function : Open |
64 | //purpose : |
65 | //======================================================================= |
46216226 |
66 | Storage_Error FSD_CmpFile::Open(const TCollection_AsciiString& aName, const Storage_OpenMode aMode) |
7fd59977 |
67 | { |
68 | Storage_Error result = Storage_VSOk; |
7fd59977 |
69 | SetName(aName); |
70 | |
71 | if (OpenMode() == Storage_VSNone) { |
fb0b0531 |
72 | std::ios_base::openmode anOpenMode = std::ios_base::openmode(0); |
73 | switch (aMode) |
74 | { |
46216226 |
75 | case Storage_VSNone: |
76 | { |
77 | break; |
78 | } |
79 | case Storage_VSRead: |
80 | { |
04232180 |
81 | // std::ios::nocreate is not portable |
46216226 |
82 | #if !defined(IRIX) && !defined(DECOSF1) |
04232180 |
83 | anOpenMode = std::ios::in | std::ios::binary; |
46216226 |
84 | #else |
04232180 |
85 | anOpenMode = std::ios::in; |
46216226 |
86 | #endif |
87 | break; |
88 | } |
89 | case Storage_VSWrite: |
90 | { |
91 | #if !defined(IRIX) && !defined(DECOSF1) |
04232180 |
92 | anOpenMode = std::ios::out | std::ios::binary; |
46216226 |
93 | #else |
04232180 |
94 | anOpenMode = std::ios::out; |
46216226 |
95 | #endif |
96 | break; |
97 | } |
98 | case Storage_VSReadWrite: |
99 | { |
100 | #if !defined(IRIX) && !defined(DECOSF1) |
04232180 |
101 | anOpenMode = std::ios::in | std::ios::out | std::ios::binary; |
46216226 |
102 | #else |
04232180 |
103 | anOpenMode = std::ios::in | std::ios::out; |
46216226 |
104 | #endif |
105 | break; |
106 | } |
7fd59977 |
107 | } |
fb0b0531 |
108 | if (anOpenMode != 0) |
109 | { |
46216226 |
110 | OSD_OpenStream(myStream, aName, anOpenMode); |
7fd59977 |
111 | } |
7fd59977 |
112 | if (myStream.fail()) { |
113 | result = Storage_VSOpenError; |
114 | } |
115 | else { |
116 | myStream.precision(17); |
46216226 |
117 | myStream.imbue(std::locale::classic()); // use always C locale |
7fd59977 |
118 | SetOpenMode(aMode); |
119 | } |
46216226 |
120 | } |
7fd59977 |
121 | else { |
122 | result = Storage_VSAlreadyOpen; |
123 | } |
124 | return result; |
46216226 |
125 | } |
7fd59977 |
126 | |
127 | //======================================================================= |
128 | //function : MagicNumber |
129 | //purpose : ------------------ PROTECTED |
130 | //======================================================================= |
131 | |
487bf1ce |
132 | Standard_CString FSD_CmpFile::MagicNumber() |
7fd59977 |
133 | { |
134 | return MAGICNUMBER; |
135 | } |
136 | |
7fd59977 |
137 | //======================================================================= |
138 | //function : ReadLine |
139 | //purpose : read from the current position to the end of line. |
140 | //======================================================================= |
141 | |
142 | void FSD_CmpFile::ReadLine(TCollection_AsciiString& buffer) |
143 | { |
7fd59977 |
144 | buffer.Clear(); |
46216226 |
145 | TCollection_AsciiString aBuf('\0'); |
146 | FSD_File::ReadLine(aBuf); |
147 | for (Standard_Integer lv = aBuf.Length(); lv >= 1 && (aBuf.Value(lv) == '\r' || (aBuf.Value(lv) == '\n')); lv--) |
148 | aBuf.SetValue(lv, '\0'); |
149 | buffer = aBuf; |
7fd59977 |
150 | } |
151 | |
152 | //======================================================================= |
153 | //function : WriteExtendedLine |
154 | //purpose : write from the current position to the end of line. |
155 | //======================================================================= |
156 | |
157 | void FSD_CmpFile::WriteExtendedLine(const TCollection_ExtendedString& buffer) |
158 | { |
159 | #if 0 |
160 | Standard_ExtString extBuffer; |
46216226 |
161 | Standard_Integer i, c, d; |
7fd59977 |
162 | |
163 | extBuffer = buffer.ToExtString(); |
164 | |
165 | for (i = 0; i < buffer.Length(); i++) { |
46216226 |
166 | c = (extBuffer[i] & 0x0000FF00) >> 8; |
7fd59977 |
167 | d = extBuffer[i] & 0x000000FF; |
168 | |
169 | myStream << (char)c << (char)d; |
170 | } |
171 | |
172 | myStream << (char)0 << "\n"; |
173 | #endif |
174 | Standard_ExtString extBuffer; |
175 | Standard_Integer i; |
176 | |
177 | extBuffer = buffer.ToExtString(); |
178 | PutInteger(buffer.Length()); |
179 | for (i = 0; i < buffer.Length(); i++) { |
180 | PutExtCharacter(extBuffer[i]); |
181 | } |
182 | |
183 | myStream << "\n"; |
184 | } |
185 | |
186 | //======================================================================= |
187 | //function : ReadExtendedLine |
188 | //purpose : |
189 | //======================================================================= |
190 | |
191 | void FSD_CmpFile::ReadExtendedLine(TCollection_ExtendedString& buffer) |
192 | { |
193 | Standard_ExtCharacter c; |
194 | Standard_Integer i; |
195 | |
196 | GetInteger(i); |
197 | |
198 | for (i = 0; i < buffer.Length(); i++) { |
199 | GetExtCharacter(c); |
200 | buffer += c; |
201 | } |
202 | |
203 | FlushEndOfLine(); |
7fd59977 |
204 | } |
205 | |
206 | //======================================================================= |
46216226 |
207 | //function : ReadString |
208 | //purpose : read from the first none space character position to the end of line. |
7fd59977 |
209 | //======================================================================= |
210 | |
46216226 |
211 | void FSD_CmpFile::ReadString(TCollection_AsciiString& buffer) |
7fd59977 |
212 | { |
7fd59977 |
213 | buffer.Clear(); |
46216226 |
214 | TCollection_AsciiString aBuf('\0'); |
215 | FSD_File::ReadString(aBuf); |
216 | for (Standard_Integer lv = aBuf.Length(); lv >= 1 && (aBuf.Value(lv) == '\r' || (aBuf.Value(lv) == '\n')); lv--) |
217 | aBuf.SetValue(lv, '\0'); |
218 | buffer = aBuf; |
7fd59977 |
219 | } |
220 | |
221 | //======================================================================= |
46216226 |
222 | //function : Destroy |
223 | //purpose : |
7fd59977 |
224 | //======================================================================= |
225 | |
46216226 |
226 | void FSD_CmpFile::Destroy() |
7fd59977 |
227 | { |
46216226 |
228 | if (OpenMode() != Storage_VSNone) { |
229 | Close(); |
7fd59977 |
230 | } |
231 | } |
232 | |
233 | //======================================================================= |
46216226 |
234 | //function : BeginWriteInfoSection |
235 | //purpose : -------------------------- INFO : WRITE |
7fd59977 |
236 | //======================================================================= |
237 | |
46216226 |
238 | Storage_Error FSD_CmpFile::BeginWriteInfoSection() |
7fd59977 |
239 | { |
46216226 |
240 | myStream << FSD_CmpFile::MagicNumber() << '\n'; |
241 | myStream << "BEGIN_INFO_SECTION\n"; |
242 | if (myStream.bad()) throw Storage_StreamWriteError(); |
7fd59977 |
243 | |
46216226 |
244 | return Storage_VSOk; |
7fd59977 |
245 | } |
246 | |
247 | //======================================================================= |
46216226 |
248 | //function : BeginReadInfoSection |
7fd59977 |
249 | //purpose : |
250 | //======================================================================= |
251 | |
46216226 |
252 | Storage_Error FSD_CmpFile::BeginReadInfoSection() |
7fd59977 |
253 | { |
46216226 |
254 | Storage_Error s; |
7fd59977 |
255 | TCollection_AsciiString l; |
46216226 |
256 | Standard_Size len = strlen(FSD_CmpFile::MagicNumber()); |
7fd59977 |
257 | |
46216226 |
258 | ReadChar(l, len); |
7fd59977 |
259 | |
46216226 |
260 | if (strncmp(FSD_CmpFile::MagicNumber(), l.ToCString(), len) != 0) { |
261 | s = Storage_VSFormatError; |
7fd59977 |
262 | } |
263 | else { |
46216226 |
264 | s = FindTag("BEGIN_INFO_SECTION"); |
7fd59977 |
265 | } |
7fd59977 |
266 | |
46216226 |
267 | return s; |
7fd59977 |
268 | } |
269 | |
270 | //======================================================================= |
46216226 |
271 | //function : WritePersistentObjectHeader |
7fd59977 |
272 | //purpose : |
273 | //======================================================================= |
274 | |
46216226 |
275 | void FSD_CmpFile::WritePersistentObjectHeader(const Standard_Integer aRef, |
276 | const Standard_Integer aType) |
7fd59977 |
277 | { |
46216226 |
278 | myStream << "\n#" << aRef << "%" << aType << " "; |
9775fa61 |
279 | if (myStream.bad()) throw Storage_StreamWriteError(); |
7fd59977 |
280 | } |
281 | |
282 | //======================================================================= |
46216226 |
283 | //function : BeginWritePersistentObjectData |
7fd59977 |
284 | //purpose : |
285 | //======================================================================= |
286 | |
46216226 |
287 | void FSD_CmpFile::BeginWritePersistentObjectData() |
7fd59977 |
288 | { |
9775fa61 |
289 | if (myStream.bad()) throw Storage_StreamWriteError(); |
7fd59977 |
290 | } |
291 | |
292 | //======================================================================= |
46216226 |
293 | //function : BeginWriteObjectData |
7fd59977 |
294 | //purpose : |
295 | //======================================================================= |
296 | |
46216226 |
297 | void FSD_CmpFile::BeginWriteObjectData() |
7fd59977 |
298 | { |
9775fa61 |
299 | if (myStream.bad()) throw Storage_StreamWriteError(); |
7fd59977 |
300 | } |
301 | |
302 | //======================================================================= |
46216226 |
303 | //function : EndWriteObjectData |
7fd59977 |
304 | //purpose : |
305 | //======================================================================= |
306 | |
46216226 |
307 | void FSD_CmpFile::EndWriteObjectData() |
7fd59977 |
308 | { |
9775fa61 |
309 | if (myStream.bad()) throw Storage_StreamWriteError(); |
7fd59977 |
310 | } |
311 | |
312 | //======================================================================= |
46216226 |
313 | //function : EndWritePersistentObjectData |
7fd59977 |
314 | //purpose : |
315 | //======================================================================= |
316 | |
46216226 |
317 | void FSD_CmpFile::EndWritePersistentObjectData() |
7fd59977 |
318 | { |
9775fa61 |
319 | if (myStream.bad()) throw Storage_StreamWriteError(); |
7fd59977 |
320 | } |
321 | |
322 | //======================================================================= |
46216226 |
323 | //function : ReadPersistentObjectHeader |
7fd59977 |
324 | //purpose : |
325 | //======================================================================= |
326 | |
46216226 |
327 | void FSD_CmpFile::ReadPersistentObjectHeader(Standard_Integer& aRef, |
328 | Standard_Integer& aType) |
7fd59977 |
329 | { |
472433e2 |
330 | char c = '\0'; |
7fd59977 |
331 | |
46216226 |
332 | myStream.get(c); |
7fd59977 |
333 | |
46216226 |
334 | while (c != '#') { |
335 | if (IsEnd() || (c != ' ') || (c == '\r') || (c == '\n')) { |
336 | throw Storage_StreamFormatError(); |
337 | } |
338 | myStream.get(c); |
7fd59977 |
339 | } |
7fd59977 |
340 | |
46216226 |
341 | if (!(myStream >> aRef)) throw Storage_StreamTypeMismatchError(); |
7fd59977 |
342 | |
46216226 |
343 | myStream.get(c); |
7fd59977 |
344 | |
46216226 |
345 | while (c != '%') { |
346 | if (IsEnd() || (c != ' ') || (c == '\r') || (c == '\n')) { |
347 | throw Storage_StreamFormatError(); |
348 | } |
349 | myStream.get(c); |
7fd59977 |
350 | } |
351 | |
46216226 |
352 | if (!(myStream >> aType)) throw Storage_StreamTypeMismatchError(); |
04232180 |
353 | // std::cout << "REF:" << aRef << " TYPE:"<< aType << std::endl; |
7fd59977 |
354 | } |
355 | |
356 | //======================================================================= |
46216226 |
357 | //function : BeginReadPersistentObjectData |
7fd59977 |
358 | //purpose : |
359 | //======================================================================= |
360 | |
46216226 |
361 | void FSD_CmpFile::BeginReadPersistentObjectData() |
7fd59977 |
362 | { |
04232180 |
363 | //std::cout << "BeginReadPersistentObjectData" << std::endl; |
7fd59977 |
364 | } |
365 | |
366 | //======================================================================= |
46216226 |
367 | //function : BeginReadObjectData |
7fd59977 |
368 | //purpose : |
369 | //======================================================================= |
370 | |
46216226 |
371 | void FSD_CmpFile::BeginReadObjectData() |
7fd59977 |
372 | { |
04232180 |
373 | // std::cout << "BeginReadObjectData" << std::endl; |
7fd59977 |
374 | } |
375 | |
376 | //======================================================================= |
46216226 |
377 | //function : EndReadObjectData |
7fd59977 |
378 | //purpose : |
379 | //======================================================================= |
380 | |
46216226 |
381 | void FSD_CmpFile::EndReadObjectData() |
7fd59977 |
382 | { |
04232180 |
383 | // std::cout << "EndReadObjectData" << std::endl; |
7fd59977 |
384 | } |
385 | |
386 | //======================================================================= |
46216226 |
387 | //function : EndReadPersistentObjectData |
7fd59977 |
388 | //purpose : |
389 | //======================================================================= |
390 | |
46216226 |
391 | void FSD_CmpFile::EndReadPersistentObjectData() |
7fd59977 |
392 | { |
472433e2 |
393 | char c = '\0'; |
7fd59977 |
394 | |
46216226 |
395 | myStream.get(c); |
396 | while (c != '\n' && (c != '\r')) { |
397 | if (IsEnd() || (c != ' ')) { |
398 | throw Storage_StreamFormatError(); |
399 | } |
400 | myStream.get(c); |
7fd59977 |
401 | } |
46216226 |
402 | if (c == '\r') { |
7fd59977 |
403 | myStream.get(c); |
404 | } |
04232180 |
405 | // std::cout << "EndReadPersistentObjectData" << std::endl; |
7fd59977 |
406 | } |