b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 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 | |
7fd59977 |
15 | #include <FSD_BinaryFile.ixx> |
16 | #include <OSD.hxx> |
17 | |
18 | const Standard_CString MAGICNUMBER = "BINFILE"; |
7fd59977 |
19 | |
20 | //======================================================================= |
21 | //function : FSD_BinaryFile |
22 | //purpose : |
23 | //======================================================================= |
24 | |
25 | FSD_BinaryFile::FSD_BinaryFile() : |
26 | myStream(0L) |
27 | { |
28 | myHeader.testindian = -1; |
29 | myHeader.binfo = -1; |
30 | myHeader.einfo = -1; |
31 | myHeader.bcomment = -1; |
32 | myHeader.ecomment = -1; |
33 | myHeader.btype = -1; |
34 | myHeader.etype = -1; |
35 | myHeader.broot = -1; |
36 | myHeader.eroot = -1; |
37 | myHeader.bref = -1; |
38 | myHeader.eref = -1; |
39 | myHeader.bdata = -1; |
40 | myHeader.edata = -1; |
41 | } |
42 | |
43 | //======================================================================= |
44 | //function : IsGoodFileType |
45 | //purpose : INFO SECTION |
46 | // write |
47 | //======================================================================= |
48 | |
49 | Storage_Error FSD_BinaryFile::IsGoodFileType(const TCollection_AsciiString& aName) |
50 | { |
51 | FSD_BinaryFile f; |
52 | Storage_Error s; |
53 | |
54 | s = f.Open(aName,Storage_VSRead); |
55 | |
56 | if (s == Storage_VSOk) { |
57 | TCollection_AsciiString l; |
fb8a7358 |
58 | Standard_Size len = strlen(FSD_BinaryFile::MagicNumber()); |
7fd59977 |
59 | |
60 | f.ReadChar(l,len); |
61 | |
62 | f.Close(); |
63 | |
64 | if (strncmp(FSD_BinaryFile::MagicNumber(),l.ToCString(),len) != 0) { |
65 | s = Storage_VSFormatError; |
66 | } |
67 | } |
68 | |
69 | return s; |
70 | } |
71 | |
72 | //======================================================================= |
73 | //function : Open |
74 | //purpose : |
75 | //======================================================================= |
76 | |
77 | Storage_Error FSD_BinaryFile::Open(const TCollection_AsciiString& aName,const Storage_OpenMode aMode) |
78 | { |
79 | Storage_Error result = Storage_VSOk; |
80 | |
81 | SetName(aName); |
82 | |
83 | if (OpenMode() == Storage_VSNone) { |
d9ff84e8 |
84 | #ifdef _WIN32 |
85 | TCollection_ExtendedString aWName(aName); |
86 | if (aMode == Storage_VSRead) { |
87 | myStream = _wfopen((const wchar_t*)aWName.ToExtString(),L"rb"); |
88 | } |
89 | else if (aMode == Storage_VSWrite) { |
90 | myStream = _wfopen((const wchar_t*)aWName.ToExtString(),L"wb"); |
91 | } |
92 | else if (aMode == Storage_VSReadWrite) { |
93 | myStream = _wfopen((const wchar_t*)aWName.ToExtString(),L"w+b"); |
94 | } |
95 | #else |
7fd59977 |
96 | if (aMode == Storage_VSRead) { |
97 | myStream = fopen(aName.ToCString(),"rb"); |
98 | } |
99 | else if (aMode == Storage_VSWrite) { |
100 | myStream = fopen(aName.ToCString(),"wb"); |
101 | } |
102 | else if (aMode == Storage_VSReadWrite) { |
103 | myStream = fopen(aName.ToCString(),"w+b"); |
104 | } |
d9ff84e8 |
105 | #endif |
7fd59977 |
106 | |
107 | if (myStream == 0L) { |
108 | result = Storage_VSOpenError; |
109 | } |
110 | else { |
111 | SetOpenMode(aMode); |
112 | } |
113 | } |
114 | else { |
115 | result = Storage_VSAlreadyOpen; |
116 | } |
117 | |
118 | return result; |
119 | } |
120 | |
121 | //======================================================================= |
122 | //function : IsEnd |
123 | //purpose : |
124 | //======================================================================= |
125 | |
126 | Standard_Boolean FSD_BinaryFile::IsEnd() |
127 | { |
128 | return (feof(myStream) != 0); |
129 | } |
130 | |
131 | //======================================================================= |
132 | //function : Close |
133 | //purpose : |
134 | //======================================================================= |
135 | |
136 | Storage_Error FSD_BinaryFile::Close() |
137 | { |
138 | Storage_Error result = Storage_VSOk; |
139 | |
140 | if (OpenMode() != Storage_VSNone) { |
141 | fclose(myStream); |
142 | SetOpenMode(Storage_VSNone); |
143 | } |
144 | else { |
145 | result = Storage_VSNotOpen; |
146 | } |
147 | |
148 | return result; |
149 | } |
150 | |
151 | //======================================================================= |
152 | //function : MagicNumber |
153 | //purpose : ------------------ PROTECTED |
154 | //======================================================================= |
155 | |
156 | const Standard_CString FSD_BinaryFile::MagicNumber() |
157 | { |
158 | return MAGICNUMBER; |
159 | } |
160 | |
161 | //======================================================================= |
162 | //function : ReadChar |
163 | //purpose : read <rsize> character from the current position. |
164 | //======================================================================= |
165 | |
fb8a7358 |
166 | void FSD_BinaryFile::ReadChar(TCollection_AsciiString& buffer, const Standard_Size rsize) |
7fd59977 |
167 | { |
168 | char c; |
fb8a7358 |
169 | Standard_Size ccount = 0; |
7fd59977 |
170 | |
171 | buffer.Clear(); |
172 | |
173 | while (!IsEnd() && (ccount < rsize)) { |
174 | fread(&c, sizeof(char),1, myStream); |
175 | buffer += c; |
176 | ccount++; |
177 | } |
178 | } |
179 | |
180 | //======================================================================= |
181 | //function : SkipObject |
182 | //purpose : |
183 | //======================================================================= |
184 | |
185 | void FSD_BinaryFile::SkipObject() |
186 | { |
187 | |
188 | } |
189 | |
190 | //======================================================================= |
191 | //function : PutReference |
192 | //purpose : ---------------------- PUBLIC : PUT |
193 | //======================================================================= |
194 | |
195 | Storage_BaseDriver& FSD_BinaryFile::PutReference(const Standard_Integer aValue) |
196 | { |
197 | #if DO_INVERSE |
198 | Standard_Integer t = InverseInt (aValue); |
199 | |
200 | if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise(); |
201 | #else |
202 | if (!fwrite(&aValue,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise(); |
203 | #endif |
204 | return *this; |
205 | } |
206 | |
207 | //======================================================================= |
208 | //function : PutCharacter |
209 | //purpose : |
210 | //======================================================================= |
211 | |
212 | Storage_BaseDriver& FSD_BinaryFile::PutCharacter(const Standard_Character aValue) |
213 | { |
214 | if (!fwrite(&aValue,sizeof(Standard_Character),1,myStream)) Storage_StreamWriteError::Raise(); |
215 | return *this; |
216 | } |
217 | |
218 | //======================================================================= |
219 | //function : PutExtCharacter |
220 | //purpose : |
221 | //======================================================================= |
222 | |
223 | Storage_BaseDriver& FSD_BinaryFile::PutExtCharacter(const Standard_ExtCharacter aValue) |
224 | { |
225 | #if DO_INVERSE |
226 | Standard_ExtCharacter t = InverseExtChar (aValue); |
227 | |
228 | if (!fwrite(&t,sizeof(Standard_ExtCharacter),1,myStream)) Storage_StreamWriteError::Raise(); |
229 | #else |
230 | if (!fwrite(&aValue,sizeof(Standard_ExtCharacter),1,myStream)) Storage_StreamWriteError::Raise(); |
231 | #endif |
232 | return *this; |
233 | } |
234 | |
235 | //======================================================================= |
236 | //function : PutInteger |
237 | //purpose : |
238 | //======================================================================= |
239 | |
240 | Storage_BaseDriver& FSD_BinaryFile::PutInteger(const Standard_Integer aValue) |
241 | { |
242 | #if DO_INVERSE |
243 | Standard_Integer t = InverseInt (aValue); |
244 | |
245 | if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise(); |
246 | #else |
247 | if (!fwrite(&aValue,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise(); |
248 | #endif |
249 | |
250 | return *this; |
251 | } |
252 | |
253 | //======================================================================= |
254 | //function : PutBoolean |
255 | //purpose : |
256 | //======================================================================= |
257 | |
258 | Storage_BaseDriver& FSD_BinaryFile::PutBoolean(const Standard_Boolean aValue) |
259 | { |
260 | #if DO_INVERSE |
261 | Standard_Integer t = InverseInt ((Standard_Integer) aValue); |
262 | |
263 | if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise(); |
264 | #else |
265 | if (!fwrite(&aValue,sizeof(Standard_Boolean),1,myStream)) Storage_StreamWriteError::Raise(); |
266 | #endif |
267 | return *this; |
268 | } |
269 | |
270 | //======================================================================= |
271 | //function : PutReal |
272 | //purpose : |
273 | //======================================================================= |
274 | |
275 | Storage_BaseDriver& FSD_BinaryFile::PutReal(const Standard_Real aValue) |
276 | { |
277 | #if DO_INVERSE |
278 | Standard_Real t = InverseReal (aValue); |
279 | |
280 | if (!fwrite(&t,sizeof(Standard_Real),1,myStream)) Storage_StreamWriteError::Raise(); |
281 | #else |
282 | if (!fwrite(&aValue,sizeof(Standard_Real),1,myStream)) Storage_StreamWriteError::Raise(); |
283 | #endif |
284 | return *this; |
285 | } |
286 | |
287 | //======================================================================= |
288 | //function : PutShortReal |
289 | //purpose : |
290 | //======================================================================= |
291 | |
292 | Storage_BaseDriver& FSD_BinaryFile::PutShortReal(const Standard_ShortReal aValue) |
293 | { |
294 | #if DO_INVERSE |
295 | Standard_ShortReal t = InverseShortReal (aValue); |
296 | |
297 | if (!fwrite(&t,sizeof(Standard_ShortReal),1,myStream)) Storage_StreamWriteError::Raise(); |
298 | #else |
299 | if (!fwrite(&aValue,sizeof(Standard_ShortReal),1,myStream)) Storage_StreamWriteError::Raise(); |
300 | #endif |
301 | return *this; |
302 | } |
303 | |
304 | //======================================================================= |
305 | //function : GetReference |
306 | //purpose : ----------------- PUBLIC : GET |
307 | //======================================================================= |
308 | |
309 | Storage_BaseDriver& FSD_BinaryFile::GetReference(Standard_Integer& aValue) |
310 | { |
311 | if (!fread(&aValue,sizeof(Standard_Integer),1,myStream)) |
312 | Storage_StreamTypeMismatchError::Raise(); |
313 | #if DO_INVERSE |
314 | aValue = InverseInt (aValue); |
315 | #endif |
316 | return *this; |
317 | } |
318 | |
319 | //======================================================================= |
320 | //function : GetCharacter |
321 | //purpose : |
322 | //======================================================================= |
323 | |
324 | Storage_BaseDriver& FSD_BinaryFile::GetCharacter(Standard_Character& aValue) |
325 | { |
326 | if (!fread(&aValue,sizeof(Standard_Character),1,myStream)) |
327 | Storage_StreamTypeMismatchError::Raise(); |
328 | return *this; |
329 | } |
330 | |
331 | //======================================================================= |
332 | //function : GetExtCharacter |
333 | //purpose : |
334 | //======================================================================= |
335 | |
336 | Storage_BaseDriver& FSD_BinaryFile::GetExtCharacter(Standard_ExtCharacter& aValue) |
337 | { |
338 | if (!fread(&aValue,sizeof(Standard_ExtCharacter),1,myStream)) |
339 | Storage_StreamTypeMismatchError::Raise(); |
340 | #if DO_INVERSE |
341 | aValue = InverseExtChar (aValue); |
342 | #endif |
343 | return *this; |
344 | } |
345 | |
346 | //======================================================================= |
347 | //function : GetInteger |
348 | //purpose : |
349 | //======================================================================= |
350 | |
351 | Storage_BaseDriver& FSD_BinaryFile::GetInteger(Standard_Integer& aValue) |
352 | { |
353 | if (!fread(&aValue,sizeof(Standard_Integer),1,myStream)) |
354 | Storage_StreamTypeMismatchError::Raise(); |
355 | #if DO_INVERSE |
356 | aValue = InverseInt (aValue); |
357 | #endif |
358 | return *this; |
359 | } |
360 | |
361 | //======================================================================= |
362 | //function : GetBoolean |
363 | //purpose : |
364 | //======================================================================= |
365 | |
366 | Storage_BaseDriver& FSD_BinaryFile::GetBoolean(Standard_Boolean& aValue) |
367 | { |
368 | if (!fread(&aValue,sizeof(Standard_Boolean),1,myStream)) |
369 | Storage_StreamTypeMismatchError::Raise(); |
370 | #if DO_INVERSE |
371 | aValue = InverseInt ((Standard_Integer) aValue); |
372 | #endif |
373 | return *this; |
374 | } |
375 | |
376 | //======================================================================= |
377 | //function : GetReal |
378 | //purpose : |
379 | //======================================================================= |
380 | |
381 | Storage_BaseDriver& FSD_BinaryFile::GetReal(Standard_Real& aValue) |
382 | { |
383 | if (!fread(&aValue,sizeof(Standard_Real),1,myStream)) |
384 | Storage_StreamTypeMismatchError::Raise(); |
385 | #if DO_INVERSE |
386 | aValue = InverseReal (aValue); |
387 | #endif |
388 | return *this; |
389 | } |
390 | |
391 | //======================================================================= |
392 | //function : GetShortReal |
393 | //purpose : |
394 | //======================================================================= |
395 | |
396 | Storage_BaseDriver& FSD_BinaryFile::GetShortReal(Standard_ShortReal& aValue) |
397 | { |
398 | if (!fread(&aValue,sizeof(Standard_ShortReal),1,myStream)) |
399 | Storage_StreamTypeMismatchError::Raise(); |
400 | #if DO_INVERSE |
401 | aValue = InverseShortReal (aValue); |
402 | #endif |
403 | return *this; |
404 | } |
405 | |
406 | //======================================================================= |
407 | //function : Destroy |
408 | //purpose : |
409 | //======================================================================= |
410 | |
411 | void FSD_BinaryFile::Destroy() |
412 | { |
413 | if (OpenMode() != Storage_VSNone) { |
414 | Close(); |
415 | } |
416 | } |
417 | |
418 | //======================================================================= |
419 | //function : BeginWriteInfoSection |
420 | //purpose : -------------------------- INFO : WRITE |
421 | //======================================================================= |
422 | |
423 | Storage_Error FSD_BinaryFile::BeginWriteInfoSection() |
424 | { |
425 | char ti[4]; |
426 | ti[0] = 1; |
427 | ti[1] = 2; |
428 | ti[2] = 3; |
429 | ti[3] = 4; |
430 | myHeader.testindian = *((int*)ti); |
431 | if (!fwrite(FSD_BinaryFile::MagicNumber(), |
432 | strlen(FSD_BinaryFile::MagicNumber()), |
433 | 1, |
434 | myStream)) |
435 | Storage_StreamWriteError::Raise(); |
436 | |
437 | myHeader.binfo = ftell(myStream); |
438 | WriteHeader(); |
439 | |
440 | return Storage_VSOk; |
441 | } |
442 | |
443 | //======================================================================= |
444 | //function : WriteInfo |
445 | //purpose : |
446 | //======================================================================= |
447 | |
448 | void FSD_BinaryFile::WriteInfo(const Standard_Integer nbObj, |
449 | const TCollection_AsciiString& dbVersion, |
450 | const TCollection_AsciiString& date, |
451 | const TCollection_AsciiString& schemaName, |
452 | const TCollection_AsciiString& schemaVersion, |
453 | const TCollection_ExtendedString& appName, |
454 | const TCollection_AsciiString& appVersion, |
455 | const TCollection_ExtendedString& dataType, |
456 | const TColStd_SequenceOfAsciiString& userInfo) |
457 | { |
458 | Standard_Integer i; |
459 | |
460 | PutInteger(nbObj); |
461 | WriteString(dbVersion); |
462 | WriteString(date); |
463 | WriteString(schemaName); |
464 | WriteString(schemaVersion); |
465 | WriteExtendedString(appName); |
466 | WriteString(appVersion); |
467 | WriteExtendedString(dataType); |
468 | i = userInfo.Length(); |
469 | |
470 | PutInteger(i); |
471 | for (i = 1; i <= userInfo.Length(); i++) { |
472 | WriteString(userInfo.Value(i)); |
473 | } |
474 | } |
475 | |
476 | //======================================================================= |
477 | //function : EndWriteInfoSection |
478 | //purpose : read |
479 | //======================================================================= |
480 | |
481 | Storage_Error FSD_BinaryFile::EndWriteInfoSection() |
482 | { |
483 | myHeader.einfo = ftell(myStream); |
484 | |
485 | return Storage_VSOk; |
486 | } |
487 | |
488 | //======================================================================= |
489 | //function : BeginReadInfoSection |
490 | //purpose : |
491 | //======================================================================= |
492 | |
493 | Storage_Error FSD_BinaryFile::BeginReadInfoSection() |
494 | { |
495 | Storage_Error s = Storage_VSOk; |
496 | TCollection_AsciiString l; |
fb8a7358 |
497 | Standard_Size len = strlen(FSD_BinaryFile::MagicNumber()); |
7fd59977 |
498 | |
499 | ReadChar(l,len); |
500 | |
501 | if (strncmp(FSD_BinaryFile::MagicNumber(),l.ToCString(),len) != 0) { |
502 | s = Storage_VSFormatError; |
503 | } |
504 | else { |
505 | ReadHeader(); |
506 | } |
507 | |
508 | return s; |
509 | } |
510 | |
511 | //======================================================================= |
512 | //function : ReadInfo |
513 | //purpose : ------------------- INFO : READ |
514 | //======================================================================= |
515 | |
516 | void FSD_BinaryFile::ReadInfo(Standard_Integer& nbObj, |
517 | TCollection_AsciiString& dbVersion, |
518 | TCollection_AsciiString& date, |
519 | TCollection_AsciiString& schemaName, |
520 | TCollection_AsciiString& schemaVersion, |
521 | TCollection_ExtendedString& appName, |
522 | TCollection_AsciiString& appVersion, |
523 | TCollection_ExtendedString& dataType, |
524 | TColStd_SequenceOfAsciiString& userInfo) |
525 | { |
526 | GetInteger(nbObj); |
527 | ReadString(dbVersion); |
528 | ReadString(date); |
529 | ReadString(schemaName); |
530 | ReadString(schemaVersion); |
531 | ReadExtendedString(appName); |
532 | ReadString(appVersion); |
533 | ReadExtendedString(dataType); |
534 | |
535 | Standard_Integer i,len = 0; |
536 | |
537 | GetInteger(len); |
538 | TCollection_AsciiString line; |
539 | |
540 | for (i = 1; i <= len && !IsEnd(); i++) { |
541 | ReadString(line); |
542 | userInfo.Append(line); |
543 | } |
544 | } |
545 | |
546 | //======================================================================= |
547 | //function : EndReadInfoSection |
548 | //purpose : COMMENTS SECTION |
549 | // write |
550 | //======================================================================= |
551 | |
552 | Storage_Error FSD_BinaryFile::EndReadInfoSection() |
553 | { |
554 | if (!fseek(myStream,myHeader.einfo,SEEK_SET)) return Storage_VSOk; |
555 | else return Storage_VSSectionNotFound; |
556 | } |
557 | |
558 | //======================================================================= |
559 | //function : BeginWriteCommentSection |
560 | //purpose : ---------------- COMMENTS : WRITE |
561 | //======================================================================= |
562 | |
563 | Storage_Error FSD_BinaryFile::BeginWriteCommentSection() |
564 | { |
565 | myHeader.bcomment = ftell(myStream); |
566 | return Storage_VSOk; |
567 | } |
568 | |
569 | //======================================================================= |
570 | //function : WriteComment |
571 | //purpose : |
572 | //======================================================================= |
573 | |
574 | void FSD_BinaryFile::WriteComment(const TColStd_SequenceOfExtendedString& aCom) |
575 | { |
576 | Standard_Integer i,aSize; |
577 | |
578 | aSize = aCom.Length(); |
579 | PutInteger(aSize); |
580 | for (i = 1; i <= aSize; i++) { |
581 | WriteExtendedString(aCom.Value(i)); |
582 | } |
583 | } |
584 | |
585 | //======================================================================= |
586 | //function : EndWriteCommentSection |
587 | //purpose : read |
588 | //======================================================================= |
589 | |
590 | Storage_Error FSD_BinaryFile::EndWriteCommentSection() |
591 | { |
592 | myHeader.ecomment = ftell(myStream); |
593 | |
594 | return Storage_VSOk; |
595 | } |
596 | |
597 | //======================================================================= |
598 | //function : BeginReadCommentSection |
599 | //purpose : ---------------- COMMENTS : READ |
600 | //======================================================================= |
601 | |
602 | Storage_Error FSD_BinaryFile::BeginReadCommentSection() |
603 | { |
604 | if (!fseek(myStream,myHeader.bcomment,SEEK_SET)) return Storage_VSOk; |
605 | else return Storage_VSSectionNotFound; |
606 | } |
607 | |
608 | //======================================================================= |
609 | //function : ReadComment |
610 | //purpose : |
611 | //======================================================================= |
612 | |
613 | void FSD_BinaryFile::ReadComment(TColStd_SequenceOfExtendedString& aCom) |
614 | { |
615 | TCollection_ExtendedString line; |
616 | Standard_Integer len,i; |
617 | |
618 | GetInteger(len); |
619 | for (i = 1; i <= len && !IsEnd(); i++) { |
620 | ReadExtendedString(line); |
621 | aCom.Append(line); |
622 | } |
623 | } |
624 | |
625 | //======================================================================= |
626 | //function : EndReadCommentSection |
627 | //purpose : |
628 | //======================================================================= |
629 | |
630 | Storage_Error FSD_BinaryFile::EndReadCommentSection() |
631 | { |
632 | if (!fseek(myStream,myHeader.ecomment,SEEK_SET)) return Storage_VSOk; |
633 | else return Storage_VSSectionNotFound; |
634 | } |
635 | |
636 | //======================================================================= |
637 | //function : BeginWriteTypeSection |
638 | //purpose : --------------- TYPE : WRITE |
639 | //======================================================================= |
640 | |
641 | Storage_Error FSD_BinaryFile::BeginWriteTypeSection() |
642 | { |
643 | myHeader.btype = ftell(myStream); |
644 | |
645 | return Storage_VSOk; |
646 | } |
647 | |
648 | //======================================================================= |
649 | //function : SetTypeSectionSize |
650 | //purpose : |
651 | //======================================================================= |
652 | |
653 | void FSD_BinaryFile::SetTypeSectionSize(const Standard_Integer aSize) |
654 | { |
655 | PutInteger(aSize); |
656 | } |
657 | |
658 | //======================================================================= |
659 | //function : WriteTypeInformations |
660 | //purpose : |
661 | //======================================================================= |
662 | |
663 | void FSD_BinaryFile::WriteTypeInformations(const Standard_Integer typeNum, |
664 | const TCollection_AsciiString& typeName) |
665 | { |
666 | PutInteger(typeNum); |
667 | WriteString(typeName); |
668 | } |
669 | |
670 | //======================================================================= |
671 | //function : EndWriteTypeSection |
672 | //purpose : read |
673 | //======================================================================= |
674 | |
675 | Storage_Error FSD_BinaryFile::EndWriteTypeSection() |
676 | { |
677 | myHeader.etype = ftell(myStream); |
678 | |
679 | return Storage_VSOk; |
680 | } |
681 | |
682 | //======================================================================= |
683 | //function : BeginReadTypeSection |
684 | //purpose : ------------------- TYPE : READ |
685 | //======================================================================= |
686 | |
687 | Storage_Error FSD_BinaryFile::BeginReadTypeSection() |
688 | { |
689 | if (!fseek(myStream,myHeader.btype,SEEK_SET)) return Storage_VSOk; |
690 | else return Storage_VSSectionNotFound; |
691 | } |
692 | |
693 | //======================================================================= |
694 | //function : TypeSectionSize |
695 | //purpose : |
696 | //======================================================================= |
697 | |
698 | Standard_Integer FSD_BinaryFile::TypeSectionSize() |
699 | { |
700 | Standard_Integer i; |
701 | |
702 | GetInteger(i); |
703 | return i; |
704 | } |
705 | |
706 | //======================================================================= |
707 | //function : ReadTypeInformations |
708 | //purpose : |
709 | //======================================================================= |
710 | |
711 | void FSD_BinaryFile::ReadTypeInformations(Standard_Integer& typeNum,TCollection_AsciiString& typeName) |
712 | { |
713 | GetInteger(typeNum); |
714 | ReadString(typeName); |
715 | } |
716 | |
717 | //======================================================================= |
718 | //function : EndReadTypeSection |
719 | //purpose : ROOT SECTION |
720 | // write |
721 | //======================================================================= |
722 | |
723 | Storage_Error FSD_BinaryFile::EndReadTypeSection() |
724 | { |
725 | if (!fseek(myStream,myHeader.etype,SEEK_SET)) return Storage_VSOk; |
726 | else return Storage_VSSectionNotFound; |
727 | } |
728 | |
729 | //======================================================================= |
730 | //function : BeginWriteRootSection |
731 | //purpose : -------------------- ROOT : WRITE |
732 | //======================================================================= |
733 | |
734 | Storage_Error FSD_BinaryFile::BeginWriteRootSection() |
735 | { |
736 | myHeader.broot = ftell(myStream); |
737 | |
738 | return Storage_VSOk; |
739 | } |
740 | |
741 | //======================================================================= |
742 | //function : SetRootSectionSize |
743 | //purpose : |
744 | //======================================================================= |
745 | |
746 | void FSD_BinaryFile::SetRootSectionSize(const Standard_Integer aSize) |
747 | { |
748 | PutInteger(aSize); |
749 | } |
750 | |
751 | //======================================================================= |
752 | //function : WriteRoot |
753 | //purpose : |
754 | //======================================================================= |
755 | |
756 | void FSD_BinaryFile::WriteRoot(const TCollection_AsciiString& rootName, const Standard_Integer aRef, const TCollection_AsciiString& rootType) |
757 | { |
758 | PutReference(aRef); |
759 | WriteString(rootName); |
760 | WriteString(rootType); |
761 | } |
762 | |
763 | //======================================================================= |
764 | //function : EndWriteRootSection |
765 | //purpose : read |
766 | //======================================================================= |
767 | |
768 | Storage_Error FSD_BinaryFile::EndWriteRootSection() |
769 | { |
770 | myHeader.eroot = ftell(myStream); |
771 | |
772 | return Storage_VSOk; |
773 | } |
774 | |
775 | //======================================================================= |
776 | //function : BeginReadRootSection |
777 | //purpose : ----------------------- ROOT : READ |
778 | //======================================================================= |
779 | |
780 | Storage_Error FSD_BinaryFile::BeginReadRootSection() |
781 | { |
782 | if (!fseek(myStream,myHeader.broot,SEEK_SET)) return Storage_VSOk; |
783 | else return Storage_VSSectionNotFound; |
784 | } |
785 | |
786 | //======================================================================= |
787 | //function : RootSectionSize |
788 | //purpose : |
789 | //======================================================================= |
790 | |
791 | Standard_Integer FSD_BinaryFile::RootSectionSize() |
792 | { |
793 | Standard_Integer i; |
794 | |
795 | GetInteger(i); |
796 | return i; |
797 | } |
798 | |
799 | //======================================================================= |
800 | //function : ReadRoot |
801 | //purpose : |
802 | //======================================================================= |
803 | |
804 | void FSD_BinaryFile::ReadRoot(TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType) |
805 | { |
806 | GetReference(aRef); |
807 | ReadString(rootName); |
808 | ReadString(rootType); |
809 | } |
810 | |
811 | //======================================================================= |
812 | //function : EndReadRootSection |
813 | //purpose : REF SECTION |
814 | // write |
815 | //======================================================================= |
816 | |
817 | Storage_Error FSD_BinaryFile::EndReadRootSection() |
818 | { |
819 | if (!fseek(myStream,myHeader.eroot,SEEK_SET)) return Storage_VSOk; |
820 | else return Storage_VSSectionNotFound; |
821 | } |
822 | |
823 | //======================================================================= |
824 | //function : BeginWriteRefSection |
825 | //purpose : -------------------------- REF : WRITE |
826 | //======================================================================= |
827 | |
828 | Storage_Error FSD_BinaryFile::BeginWriteRefSection() |
829 | { |
830 | myHeader.bref = ftell(myStream); |
831 | |
832 | return Storage_VSOk; |
833 | } |
834 | |
835 | //======================================================================= |
836 | //function : SetRefSectionSize |
837 | //purpose : |
838 | //======================================================================= |
839 | |
840 | void FSD_BinaryFile::SetRefSectionSize(const Standard_Integer aSize) |
841 | { |
842 | PutInteger(aSize); |
843 | } |
844 | |
845 | //======================================================================= |
846 | //function : WriteReferenceType |
847 | //purpose : |
848 | //======================================================================= |
849 | |
850 | void FSD_BinaryFile::WriteReferenceType(const Standard_Integer reference,const Standard_Integer typeNum) |
851 | { |
852 | PutReference(reference); |
853 | PutInteger(typeNum); |
854 | } |
855 | |
856 | //======================================================================= |
857 | //function : EndWriteRefSection |
858 | //purpose : read |
859 | //======================================================================= |
860 | |
861 | Storage_Error FSD_BinaryFile::EndWriteRefSection() |
862 | { |
863 | myHeader.eref = ftell(myStream); |
864 | |
865 | return Storage_VSOk; |
866 | } |
867 | |
868 | //======================================================================= |
869 | //function : BeginReadRefSection |
870 | //purpose : ----------------------- REF : READ |
871 | //======================================================================= |
872 | |
873 | Storage_Error FSD_BinaryFile::BeginReadRefSection() |
874 | { |
875 | if (!fseek(myStream,myHeader.bref,SEEK_SET)) return Storage_VSOk; |
876 | else return Storage_VSSectionNotFound; |
877 | } |
878 | |
879 | //======================================================================= |
880 | //function : RefSectionSize |
881 | //purpose : |
882 | //======================================================================= |
883 | |
884 | Standard_Integer FSD_BinaryFile::RefSectionSize() |
885 | { |
886 | Standard_Integer i; |
887 | |
888 | GetInteger(i); |
889 | return i; |
890 | } |
891 | |
892 | //======================================================================= |
893 | //function : ReadReferenceType |
894 | //purpose : |
895 | //======================================================================= |
896 | |
897 | void FSD_BinaryFile::ReadReferenceType(Standard_Integer& reference, |
898 | Standard_Integer& typeNum) |
899 | { |
900 | GetReference(reference); |
901 | GetInteger(typeNum); |
902 | } |
903 | |
904 | //======================================================================= |
905 | //function : EndReadRefSection |
906 | //purpose : DATA SECTION |
907 | // write |
908 | //======================================================================= |
909 | |
910 | Storage_Error FSD_BinaryFile::EndReadRefSection() |
911 | { |
912 | if (!fseek(myStream,myHeader.eref,SEEK_SET)) return Storage_VSOk; |
913 | else return Storage_VSSectionNotFound; |
914 | } |
915 | |
916 | //======================================================================= |
917 | //function : BeginWriteDataSection |
918 | //purpose : -------------------- DATA : WRITE |
919 | //======================================================================= |
920 | |
921 | Storage_Error FSD_BinaryFile::BeginWriteDataSection() |
922 | { |
923 | myHeader.bdata = ftell(myStream); |
924 | |
925 | return Storage_VSOk; |
926 | } |
927 | |
928 | //======================================================================= |
929 | //function : WritePersistentObjectHeader |
930 | //purpose : |
931 | //======================================================================= |
932 | |
933 | void FSD_BinaryFile::WritePersistentObjectHeader(const Standard_Integer aRef, |
934 | const Standard_Integer aType) |
935 | { |
936 | PutReference(aRef); |
937 | PutInteger(aType); |
938 | } |
939 | |
940 | //======================================================================= |
941 | //function : BeginWritePersistentObjectData |
942 | //purpose : |
943 | //======================================================================= |
944 | |
945 | void FSD_BinaryFile::BeginWritePersistentObjectData() |
946 | { |
947 | } |
948 | |
949 | //======================================================================= |
950 | //function : BeginWriteObjectData |
951 | //purpose : |
952 | //======================================================================= |
953 | |
954 | void FSD_BinaryFile::BeginWriteObjectData() |
955 | { |
956 | } |
957 | |
958 | //======================================================================= |
959 | //function : EndWriteObjectData |
960 | //purpose : |
961 | //======================================================================= |
962 | |
963 | void FSD_BinaryFile::EndWriteObjectData() |
964 | { |
965 | } |
966 | |
967 | //======================================================================= |
968 | //function : EndWritePersistentObjectData |
969 | //purpose : |
970 | //======================================================================= |
971 | |
972 | void FSD_BinaryFile::EndWritePersistentObjectData() |
973 | { |
974 | } |
975 | |
976 | //======================================================================= |
977 | //function : EndWriteDataSection |
978 | //purpose : read |
979 | //======================================================================= |
980 | |
981 | Storage_Error FSD_BinaryFile::EndWriteDataSection() |
982 | { |
983 | myHeader.edata = ftell(myStream); |
984 | |
985 | fseek(myStream,myHeader.binfo,SEEK_SET); |
986 | WriteHeader(); |
987 | return Storage_VSOk; |
988 | } |
989 | |
990 | //======================================================================= |
991 | //function : BeginReadDataSection |
992 | //purpose : ---------------------- DATA : READ |
993 | //======================================================================= |
994 | |
995 | Storage_Error FSD_BinaryFile::BeginReadDataSection() |
996 | { |
997 | if (!fseek(myStream,myHeader.bdata,SEEK_SET)) return Storage_VSOk; |
998 | else return Storage_VSSectionNotFound; |
999 | } |
1000 | |
1001 | //======================================================================= |
1002 | //function : ReadPersistentObjectHeader |
1003 | //purpose : |
1004 | //======================================================================= |
1005 | |
1006 | void FSD_BinaryFile::ReadPersistentObjectHeader(Standard_Integer& aRef, |
1007 | Standard_Integer& aType) |
1008 | { |
1009 | GetReference(aRef); |
1010 | GetInteger(aType); |
1011 | } |
1012 | |
1013 | //======================================================================= |
1014 | //function : BeginReadPersistentObjectData |
1015 | //purpose : |
1016 | //======================================================================= |
1017 | |
1018 | void FSD_BinaryFile::BeginReadPersistentObjectData() |
1019 | { |
1020 | } |
1021 | |
1022 | //======================================================================= |
1023 | //function : BeginReadObjectData |
1024 | //purpose : |
1025 | //======================================================================= |
1026 | |
1027 | void FSD_BinaryFile::BeginReadObjectData() |
1028 | { |
1029 | } |
1030 | |
1031 | //======================================================================= |
1032 | //function : EndReadObjectData |
1033 | //purpose : |
1034 | //======================================================================= |
1035 | |
1036 | void FSD_BinaryFile::EndReadObjectData() |
1037 | { |
1038 | } |
1039 | |
1040 | //======================================================================= |
1041 | //function : EndReadPersistentObjectData |
1042 | //purpose : |
1043 | //======================================================================= |
1044 | |
1045 | void FSD_BinaryFile::EndReadPersistentObjectData() |
1046 | { |
1047 | } |
1048 | |
1049 | //======================================================================= |
1050 | //function : EndReadDataSection |
1051 | //purpose : |
1052 | //======================================================================= |
1053 | |
1054 | Storage_Error FSD_BinaryFile::EndReadDataSection() |
1055 | { |
1056 | if (!fseek(myStream,myHeader.edata,SEEK_SET)) return Storage_VSOk; |
1057 | else return Storage_VSSectionNotFound; |
1058 | } |
1059 | |
1060 | //======================================================================= |
1061 | //function : WriteString |
1062 | //purpose : write string at the current position. |
1063 | //======================================================================= |
1064 | |
1065 | void FSD_BinaryFile::WriteString(const TCollection_AsciiString& aString) |
1066 | { |
1067 | Standard_Integer size; |
1068 | |
1069 | size = aString.Length(); |
1070 | |
1071 | PutInteger(size); |
1072 | |
1073 | if (size > 0) { |
1074 | if (!fwrite(aString.ToCString(),aString.Length(),1,myStream)) Storage_StreamWriteError::Raise(); |
1075 | } |
1076 | } |
1077 | |
1078 | //======================================================================= |
1079 | //function : ReadString |
1080 | //purpose : read string from the current position. |
1081 | //======================================================================= |
1082 | |
1083 | void FSD_BinaryFile::ReadString(TCollection_AsciiString& aString) |
1084 | { |
1085 | Standard_Integer size = 0; |
1086 | |
1087 | GetInteger(size); |
1088 | if (size > 0) { |
1089 | Standard_Character *c = (Standard_Character *)Standard::Allocate((size+1) * sizeof(Standard_Character)); |
1090 | if (!fread(c,size,1,myStream)) Storage_StreamWriteError::Raise(); |
1091 | c[size] = '\0'; |
1092 | aString = c; |
547702a1 |
1093 | Standard::Free(c); |
7fd59977 |
1094 | } |
1095 | else { |
1096 | aString.Clear(); |
1097 | } |
1098 | } |
1099 | |
1100 | //======================================================================= |
1101 | //function : WriteExtendedString |
1102 | //purpose : write string at the current position. |
1103 | //======================================================================= |
1104 | |
1105 | void FSD_BinaryFile::WriteExtendedString(const TCollection_ExtendedString& aString) |
1106 | { |
1107 | Standard_Integer size; |
1108 | |
1109 | size = aString.Length(); |
1110 | |
1111 | PutInteger(size); |
1112 | |
1113 | if (size > 0) { |
1114 | Standard_ExtString anExtStr; |
1115 | #if DO_INVERSE |
1116 | TCollection_ExtendedString aCopy = aString; |
1117 | anExtStr = aCopy.ToExtString(); |
1118 | |
1119 | Standard_PExtCharacter pChar; |
1120 | // |
1121 | pChar=(Standard_PExtCharacter)anExtStr; |
1122 | |
1123 | for (Standard_Integer i=0; i < size; i++) |
1124 | pChar[i] = InverseExtChar (pChar[i]); |
1125 | #else |
1126 | anExtStr = aString.ToExtString(); |
1127 | #endif |
1128 | if (!fwrite(anExtStr,sizeof(Standard_ExtCharacter)*aString.Length(),1,myStream)) |
1129 | Storage_StreamWriteError::Raise(); |
1130 | } |
1131 | } |
1132 | |
1133 | //======================================================================= |
1134 | //function : ReadExtendedString |
1135 | //purpose : read string from the current position. |
1136 | //======================================================================= |
1137 | |
1138 | void FSD_BinaryFile::ReadExtendedString(TCollection_ExtendedString& aString) |
1139 | { |
1140 | Standard_Integer size = 0; |
1141 | |
1142 | GetInteger(size); |
1143 | if (size > 0) { |
1144 | Standard_ExtCharacter *c = (Standard_ExtCharacter *) |
1145 | Standard::Allocate((size+1) * sizeof(Standard_ExtCharacter)); |
1146 | if (!fread(c,size*sizeof(Standard_ExtCharacter),1,myStream)) |
1147 | Storage_StreamWriteError::Raise(); |
1148 | c[size] = '\0'; |
1149 | #if DO_INVERSE |
1150 | for (Standard_Integer i=0; i < size; i++) |
1151 | c[i] = InverseExtChar (c[i]); |
1152 | #endif |
1153 | aString = c; |
547702a1 |
1154 | Standard::Free(c); |
7fd59977 |
1155 | } |
1156 | else { |
1157 | aString.Clear(); |
1158 | } |
1159 | } |
1160 | |
1161 | //======================================================================= |
1162 | //function : WriteHeader |
1163 | //purpose : |
1164 | //======================================================================= |
1165 | |
1166 | void FSD_BinaryFile::WriteHeader() |
1167 | { |
1168 | PutInteger(myHeader.testindian); |
1169 | PutInteger(myHeader.binfo); |
1170 | PutInteger(myHeader.einfo); |
1171 | PutInteger(myHeader.bcomment); |
1172 | PutInteger(myHeader.ecomment); |
1173 | PutInteger(myHeader.btype); |
1174 | PutInteger(myHeader.etype); |
1175 | PutInteger(myHeader.broot); |
1176 | PutInteger(myHeader.eroot); |
1177 | PutInteger(myHeader.bref); |
1178 | PutInteger(myHeader.eref); |
1179 | PutInteger(myHeader.bdata); |
1180 | PutInteger(myHeader.edata); |
1181 | } |
1182 | |
1183 | //======================================================================= |
1184 | //function : ReadHeader |
1185 | //purpose : |
1186 | //======================================================================= |
1187 | |
1188 | void FSD_BinaryFile::ReadHeader() |
1189 | { |
1190 | GetInteger(myHeader.testindian); |
1191 | GetInteger(myHeader.binfo); |
1192 | GetInteger(myHeader.einfo); |
1193 | GetInteger(myHeader.bcomment); |
1194 | GetInteger(myHeader.ecomment); |
1195 | GetInteger(myHeader.btype); |
1196 | GetInteger(myHeader.etype); |
1197 | GetInteger(myHeader.broot); |
1198 | GetInteger(myHeader.eroot); |
1199 | GetInteger(myHeader.bref); |
1200 | GetInteger(myHeader.eref); |
1201 | GetInteger(myHeader.bdata); |
1202 | GetInteger(myHeader.edata); |
1203 | } |
1204 | |
1205 | |
1206 | //======================================================================= |
1207 | //function : Tell |
1208 | //purpose : return position in the file. Return -1 upon error. |
1209 | //======================================================================= |
1210 | |
1211 | Storage_Position FSD_BinaryFile::Tell() |
1212 | { |
1213 | return (Storage_Position) ftell(myStream); |
1214 | } |