1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
16 #include <FSD_BinaryFile.hxx>
18 #include <OSD_OpenFile.hxx>
19 #include <Storage_BaseDriver.hxx>
20 #include <Storage_StreamExtCharParityError.hxx>
21 #include <Storage_StreamFormatError.hxx>
22 #include <Storage_StreamTypeMismatchError.hxx>
23 #include <Storage_StreamWriteError.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_ExtendedString.hxx>
26 #include <Standard_Assert.hxx>
28 const Standard_CString MAGICNUMBER = "BINFILE";
30 //=======================================================================
31 //function : FSD_BinaryFile
33 //=======================================================================
35 FSD_BinaryFile::FSD_BinaryFile() :
38 myHeader.testindian = -1;
41 myHeader.bcomment = -1;
42 myHeader.ecomment = -1;
53 //=======================================================================
54 //function : IsGoodFileType
55 //purpose : INFO SECTION
57 //=======================================================================
59 Storage_Error FSD_BinaryFile::IsGoodFileType(const TCollection_AsciiString& aName)
64 s = f.Open(aName,Storage_VSRead);
66 if (s == Storage_VSOk) {
67 TCollection_AsciiString l;
68 Standard_Size len = strlen(FSD_BinaryFile::MagicNumber());
74 if (strncmp(FSD_BinaryFile::MagicNumber(),l.ToCString(),len) != 0) {
75 s = Storage_VSFormatError;
82 //=======================================================================
85 //=======================================================================
87 Storage_Error FSD_BinaryFile::Open(const TCollection_AsciiString& aName,const Storage_OpenMode aMode)
89 Storage_Error result = Storage_VSOk;
93 if (OpenMode() == Storage_VSNone) {
94 if (aMode == Storage_VSRead) {
95 myStream = OSD_OpenFile(aName.ToCString(),"rb");
97 else if (aMode == Storage_VSWrite) {
98 myStream = OSD_OpenFile(aName.ToCString(),"wb");
100 else if (aMode == Storage_VSReadWrite) {
101 myStream = OSD_OpenFile(aName.ToCString(),"w+b");
104 if (myStream == 0L) {
105 result = Storage_VSOpenError;
112 result = Storage_VSAlreadyOpen;
118 //=======================================================================
121 //=======================================================================
123 Standard_Boolean FSD_BinaryFile::IsEnd()
125 return (feof(myStream) != 0);
128 //=======================================================================
131 //=======================================================================
133 Storage_Error FSD_BinaryFile::Close()
135 Storage_Error result = Storage_VSOk;
137 if (OpenMode() != Storage_VSNone) {
139 SetOpenMode(Storage_VSNone);
142 result = Storage_VSNotOpen;
148 //=======================================================================
149 //function : MagicNumber
150 //purpose : ------------------ PROTECTED
151 //=======================================================================
153 Standard_CString FSD_BinaryFile::MagicNumber()
158 //=======================================================================
159 //function : ReadChar
160 //purpose : read <rsize> character from the current position.
161 //=======================================================================
163 void FSD_BinaryFile::ReadChar(TCollection_AsciiString& buffer, const Standard_Size rsize)
166 Standard_Size ccount = 0;
170 while (!IsEnd() && (ccount < rsize)) {
171 ccount += fread(&c, sizeof(char),1, myStream);
176 //=======================================================================
177 //function : SkipObject
179 //=======================================================================
181 void FSD_BinaryFile::SkipObject()
186 //=======================================================================
187 //function : PutReference
188 //purpose : ---------------------- PUBLIC : PUT
189 //=======================================================================
191 Storage_BaseDriver& FSD_BinaryFile::PutReference(const Standard_Integer aValue)
193 #if OCCT_BINARY_FILE_DO_INVERSE
194 Standard_Integer t = InverseInt (aValue);
196 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise();
198 if (!fwrite(&aValue,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise();
203 //=======================================================================
204 //function : PutCharacter
206 //=======================================================================
208 Storage_BaseDriver& FSD_BinaryFile::PutCharacter(const Standard_Character aValue)
210 if (!fwrite(&aValue,sizeof(Standard_Character),1,myStream)) Storage_StreamWriteError::Raise();
214 //=======================================================================
215 //function : PutExtCharacter
217 //=======================================================================
219 Storage_BaseDriver& FSD_BinaryFile::PutExtCharacter(const Standard_ExtCharacter aValue)
221 #if OCCT_BINARY_FILE_DO_INVERSE
222 Standard_ExtCharacter t = InverseExtChar (aValue);
224 if (!fwrite(&t,sizeof(Standard_ExtCharacter),1,myStream)) Storage_StreamWriteError::Raise();
226 if (!fwrite(&aValue,sizeof(Standard_ExtCharacter),1,myStream)) Storage_StreamWriteError::Raise();
231 //=======================================================================
232 //function : PutInteger
234 //=======================================================================
236 Storage_BaseDriver& FSD_BinaryFile::PutInteger(const Standard_Integer aValue)
238 #if OCCT_BINARY_FILE_DO_INVERSE
239 Standard_Integer t = InverseInt (aValue);
241 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise();
243 if (!fwrite(&aValue,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise();
249 //=======================================================================
250 //function : PutBoolean
252 //=======================================================================
254 Storage_BaseDriver& FSD_BinaryFile::PutBoolean(const Standard_Boolean aValue)
256 #if OCCT_BINARY_FILE_DO_INVERSE
257 Standard_Integer t = InverseInt ((Standard_Integer) aValue);
259 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) Storage_StreamWriteError::Raise();
261 if (!fwrite(&aValue,sizeof(Standard_Boolean),1,myStream)) Storage_StreamWriteError::Raise();
266 //=======================================================================
269 //=======================================================================
271 Storage_BaseDriver& FSD_BinaryFile::PutReal(const Standard_Real aValue)
273 #if OCCT_BINARY_FILE_DO_INVERSE
274 Standard_Real t = InverseReal (aValue);
276 if (!fwrite(&t,sizeof(Standard_Real),1,myStream)) Storage_StreamWriteError::Raise();
278 if (!fwrite(&aValue,sizeof(Standard_Real),1,myStream)) Storage_StreamWriteError::Raise();
283 //=======================================================================
284 //function : PutShortReal
286 //=======================================================================
288 Storage_BaseDriver& FSD_BinaryFile::PutShortReal(const Standard_ShortReal aValue)
290 #if OCCT_BINARY_FILE_DO_INVERSE
291 Standard_ShortReal t = InverseShortReal (aValue);
293 if (!fwrite(&t,sizeof(Standard_ShortReal),1,myStream)) Storage_StreamWriteError::Raise();
295 if (!fwrite(&aValue,sizeof(Standard_ShortReal),1,myStream)) Storage_StreamWriteError::Raise();
300 //=======================================================================
301 //function : GetReference
302 //purpose : ----------------- PUBLIC : GET
303 //=======================================================================
305 Storage_BaseDriver& FSD_BinaryFile::GetReference(Standard_Integer& aValue)
307 if (!fread(&aValue,sizeof(Standard_Integer),1,myStream))
308 Storage_StreamTypeMismatchError::Raise();
309 #if OCCT_BINARY_FILE_DO_INVERSE
310 aValue = InverseInt (aValue);
315 //=======================================================================
316 //function : GetCharacter
318 //=======================================================================
320 Storage_BaseDriver& FSD_BinaryFile::GetCharacter(Standard_Character& aValue)
322 if (!fread(&aValue,sizeof(Standard_Character),1,myStream))
323 Storage_StreamTypeMismatchError::Raise();
327 //=======================================================================
328 //function : GetExtCharacter
330 //=======================================================================
332 Storage_BaseDriver& FSD_BinaryFile::GetExtCharacter(Standard_ExtCharacter& aValue)
334 if (!fread(&aValue,sizeof(Standard_ExtCharacter),1,myStream))
335 Storage_StreamTypeMismatchError::Raise();
336 #if OCCT_BINARY_FILE_DO_INVERSE
337 aValue = InverseExtChar (aValue);
342 //=======================================================================
343 //function : GetInteger
345 //=======================================================================
347 Storage_BaseDriver& FSD_BinaryFile::GetInteger(Standard_Integer& aValue)
349 if (!fread(&aValue,sizeof(Standard_Integer),1,myStream))
350 Storage_StreamTypeMismatchError::Raise();
351 #if OCCT_BINARY_FILE_DO_INVERSE
352 aValue = InverseInt (aValue);
357 //=======================================================================
358 //function : GetBoolean
360 //=======================================================================
362 Storage_BaseDriver& FSD_BinaryFile::GetBoolean(Standard_Boolean& aValue)
364 if (!fread(&aValue,sizeof(Standard_Boolean),1,myStream))
365 Storage_StreamTypeMismatchError::Raise();
366 #if OCCT_BINARY_FILE_DO_INVERSE
367 aValue = InverseInt ((Standard_Integer) aValue);
372 //=======================================================================
375 //=======================================================================
377 Storage_BaseDriver& FSD_BinaryFile::GetReal(Standard_Real& aValue)
379 if (!fread(&aValue,sizeof(Standard_Real),1,myStream))
380 Storage_StreamTypeMismatchError::Raise();
381 #if OCCT_BINARY_FILE_DO_INVERSE
382 aValue = InverseReal (aValue);
387 //=======================================================================
388 //function : GetShortReal
390 //=======================================================================
392 Storage_BaseDriver& FSD_BinaryFile::GetShortReal(Standard_ShortReal& aValue)
394 if (!fread(&aValue,sizeof(Standard_ShortReal),1,myStream))
395 Storage_StreamTypeMismatchError::Raise();
396 #if OCCT_BINARY_FILE_DO_INVERSE
397 aValue = InverseShortReal (aValue);
402 //=======================================================================
405 //=======================================================================
407 void FSD_BinaryFile::Destroy()
409 if (OpenMode() != Storage_VSNone) {
414 //=======================================================================
415 //function : BeginWriteInfoSection
416 //purpose : -------------------------- INFO : WRITE
417 //=======================================================================
419 Storage_Error FSD_BinaryFile::BeginWriteInfoSection()
423 Standard_Integer aResult;
426 aWrapUnion.ti2[0] = 1;
427 aWrapUnion.ti2[1] = 2;
428 aWrapUnion.ti2[2] = 3;
429 aWrapUnion.ti2[3] = 4;
431 myHeader.testindian = aWrapUnion.aResult;
433 if (!fwrite(FSD_BinaryFile::MagicNumber(),
434 strlen(FSD_BinaryFile::MagicNumber()),
437 Storage_StreamWriteError::Raise();
439 myHeader.binfo = ftell(myStream);
445 //=======================================================================
446 //function : WriteInfo
448 //=======================================================================
450 void FSD_BinaryFile::WriteInfo(const Standard_Integer nbObj,
451 const TCollection_AsciiString& dbVersion,
452 const TCollection_AsciiString& date,
453 const TCollection_AsciiString& schemaName,
454 const TCollection_AsciiString& schemaVersion,
455 const TCollection_ExtendedString& appName,
456 const TCollection_AsciiString& appVersion,
457 const TCollection_ExtendedString& dataType,
458 const TColStd_SequenceOfAsciiString& userInfo)
463 WriteString(dbVersion);
465 WriteString(schemaName);
466 WriteString(schemaVersion);
467 WriteExtendedString(appName);
468 WriteString(appVersion);
469 WriteExtendedString(dataType);
470 i = userInfo.Length();
473 for (i = 1; i <= userInfo.Length(); i++) {
474 WriteString(userInfo.Value(i));
478 //=======================================================================
479 //function : EndWriteInfoSection
481 //=======================================================================
483 Storage_Error FSD_BinaryFile::EndWriteInfoSection()
485 myHeader.einfo = ftell(myStream);
490 //=======================================================================
491 //function : BeginReadInfoSection
493 //=======================================================================
495 Storage_Error FSD_BinaryFile::BeginReadInfoSection()
497 Storage_Error s = Storage_VSOk;
498 TCollection_AsciiString l;
499 Standard_Size len = strlen(FSD_BinaryFile::MagicNumber());
503 if (strncmp(FSD_BinaryFile::MagicNumber(),l.ToCString(),len) != 0) {
504 s = Storage_VSFormatError;
513 //=======================================================================
514 //function : ReadInfo
515 //purpose : ------------------- INFO : READ
516 //=======================================================================
518 void FSD_BinaryFile::ReadInfo(Standard_Integer& nbObj,
519 TCollection_AsciiString& dbVersion,
520 TCollection_AsciiString& date,
521 TCollection_AsciiString& schemaName,
522 TCollection_AsciiString& schemaVersion,
523 TCollection_ExtendedString& appName,
524 TCollection_AsciiString& appVersion,
525 TCollection_ExtendedString& dataType,
526 TColStd_SequenceOfAsciiString& userInfo)
529 ReadString(dbVersion);
531 ReadString(schemaName);
532 ReadString(schemaVersion);
533 ReadExtendedString(appName);
534 ReadString(appVersion);
535 ReadExtendedString(dataType);
537 Standard_Integer i,len = 0;
540 TCollection_AsciiString line;
542 for (i = 1; i <= len && !IsEnd(); i++) {
544 userInfo.Append(line);
548 //=======================================================================
549 //function : EndReadInfoSection
550 //purpose : COMMENTS SECTION
552 //=======================================================================
554 Storage_Error FSD_BinaryFile::EndReadInfoSection()
556 if (!fseek(myStream,myHeader.einfo,SEEK_SET)) return Storage_VSOk;
557 else return Storage_VSSectionNotFound;
560 //=======================================================================
561 //function : BeginWriteCommentSection
562 //purpose : ---------------- COMMENTS : WRITE
563 //=======================================================================
565 Storage_Error FSD_BinaryFile::BeginWriteCommentSection()
567 myHeader.bcomment = ftell(myStream);
571 //=======================================================================
572 //function : WriteComment
574 //=======================================================================
576 void FSD_BinaryFile::WriteComment(const TColStd_SequenceOfExtendedString& aCom)
578 Standard_Integer i,aSize;
580 aSize = aCom.Length();
582 for (i = 1; i <= aSize; i++) {
583 WriteExtendedString(aCom.Value(i));
587 //=======================================================================
588 //function : EndWriteCommentSection
590 //=======================================================================
592 Storage_Error FSD_BinaryFile::EndWriteCommentSection()
594 myHeader.ecomment = ftell(myStream);
599 //=======================================================================
600 //function : BeginReadCommentSection
601 //purpose : ---------------- COMMENTS : READ
602 //=======================================================================
604 Storage_Error FSD_BinaryFile::BeginReadCommentSection()
606 if (!fseek(myStream,myHeader.bcomment,SEEK_SET)) return Storage_VSOk;
607 else return Storage_VSSectionNotFound;
610 //=======================================================================
611 //function : ReadComment
613 //=======================================================================
615 void FSD_BinaryFile::ReadComment(TColStd_SequenceOfExtendedString& aCom)
617 TCollection_ExtendedString line;
618 Standard_Integer len,i;
621 for (i = 1; i <= len && !IsEnd(); i++) {
622 ReadExtendedString(line);
627 //=======================================================================
628 //function : EndReadCommentSection
630 //=======================================================================
632 Storage_Error FSD_BinaryFile::EndReadCommentSection()
634 if (!fseek(myStream,myHeader.ecomment,SEEK_SET)) return Storage_VSOk;
635 else return Storage_VSSectionNotFound;
638 //=======================================================================
639 //function : BeginWriteTypeSection
640 //purpose : --------------- TYPE : WRITE
641 //=======================================================================
643 Storage_Error FSD_BinaryFile::BeginWriteTypeSection()
645 myHeader.btype = ftell(myStream);
650 //=======================================================================
651 //function : SetTypeSectionSize
653 //=======================================================================
655 void FSD_BinaryFile::SetTypeSectionSize(const Standard_Integer aSize)
660 //=======================================================================
661 //function : WriteTypeInformations
663 //=======================================================================
665 void FSD_BinaryFile::WriteTypeInformations(const Standard_Integer typeNum,
666 const TCollection_AsciiString& typeName)
669 WriteString(typeName);
672 //=======================================================================
673 //function : EndWriteTypeSection
675 //=======================================================================
677 Storage_Error FSD_BinaryFile::EndWriteTypeSection()
679 myHeader.etype = ftell(myStream);
684 //=======================================================================
685 //function : BeginReadTypeSection
686 //purpose : ------------------- TYPE : READ
687 //=======================================================================
689 Storage_Error FSD_BinaryFile::BeginReadTypeSection()
691 if (!fseek(myStream,myHeader.btype,SEEK_SET)) return Storage_VSOk;
692 else return Storage_VSSectionNotFound;
695 //=======================================================================
696 //function : TypeSectionSize
698 //=======================================================================
700 Standard_Integer FSD_BinaryFile::TypeSectionSize()
708 //=======================================================================
709 //function : ReadTypeInformations
711 //=======================================================================
713 void FSD_BinaryFile::ReadTypeInformations(Standard_Integer& typeNum,TCollection_AsciiString& typeName)
716 ReadString(typeName);
719 //=======================================================================
720 //function : EndReadTypeSection
721 //purpose : ROOT SECTION
723 //=======================================================================
725 Storage_Error FSD_BinaryFile::EndReadTypeSection()
727 if (!fseek(myStream,myHeader.etype,SEEK_SET)) return Storage_VSOk;
728 else return Storage_VSSectionNotFound;
731 //=======================================================================
732 //function : BeginWriteRootSection
733 //purpose : -------------------- ROOT : WRITE
734 //=======================================================================
736 Storage_Error FSD_BinaryFile::BeginWriteRootSection()
738 myHeader.broot = ftell(myStream);
743 //=======================================================================
744 //function : SetRootSectionSize
746 //=======================================================================
748 void FSD_BinaryFile::SetRootSectionSize(const Standard_Integer aSize)
753 //=======================================================================
754 //function : WriteRoot
756 //=======================================================================
758 void FSD_BinaryFile::WriteRoot(const TCollection_AsciiString& rootName, const Standard_Integer aRef, const TCollection_AsciiString& rootType)
761 WriteString(rootName);
762 WriteString(rootType);
765 //=======================================================================
766 //function : EndWriteRootSection
768 //=======================================================================
770 Storage_Error FSD_BinaryFile::EndWriteRootSection()
772 myHeader.eroot = ftell(myStream);
777 //=======================================================================
778 //function : BeginReadRootSection
779 //purpose : ----------------------- ROOT : READ
780 //=======================================================================
782 Storage_Error FSD_BinaryFile::BeginReadRootSection()
784 if (!fseek(myStream,myHeader.broot,SEEK_SET)) return Storage_VSOk;
785 else return Storage_VSSectionNotFound;
788 //=======================================================================
789 //function : RootSectionSize
791 //=======================================================================
793 Standard_Integer FSD_BinaryFile::RootSectionSize()
801 //=======================================================================
802 //function : ReadRoot
804 //=======================================================================
806 void FSD_BinaryFile::ReadRoot(TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType)
809 ReadString(rootName);
810 ReadString(rootType);
813 //=======================================================================
814 //function : EndReadRootSection
815 //purpose : REF SECTION
817 //=======================================================================
819 Storage_Error FSD_BinaryFile::EndReadRootSection()
821 if (!fseek(myStream,myHeader.eroot,SEEK_SET)) return Storage_VSOk;
822 else return Storage_VSSectionNotFound;
825 //=======================================================================
826 //function : BeginWriteRefSection
827 //purpose : -------------------------- REF : WRITE
828 //=======================================================================
830 Storage_Error FSD_BinaryFile::BeginWriteRefSection()
832 myHeader.bref = ftell(myStream);
837 //=======================================================================
838 //function : SetRefSectionSize
840 //=======================================================================
842 void FSD_BinaryFile::SetRefSectionSize(const Standard_Integer aSize)
847 //=======================================================================
848 //function : WriteReferenceType
850 //=======================================================================
852 void FSD_BinaryFile::WriteReferenceType(const Standard_Integer reference,const Standard_Integer typeNum)
854 PutReference(reference);
858 //=======================================================================
859 //function : EndWriteRefSection
861 //=======================================================================
863 Storage_Error FSD_BinaryFile::EndWriteRefSection()
865 myHeader.eref = ftell(myStream);
870 //=======================================================================
871 //function : BeginReadRefSection
872 //purpose : ----------------------- REF : READ
873 //=======================================================================
875 Storage_Error FSD_BinaryFile::BeginReadRefSection()
877 if (!fseek(myStream,myHeader.bref,SEEK_SET)) return Storage_VSOk;
878 else return Storage_VSSectionNotFound;
881 //=======================================================================
882 //function : RefSectionSize
884 //=======================================================================
886 Standard_Integer FSD_BinaryFile::RefSectionSize()
894 //=======================================================================
895 //function : ReadReferenceType
897 //=======================================================================
899 void FSD_BinaryFile::ReadReferenceType(Standard_Integer& reference,
900 Standard_Integer& typeNum)
902 GetReference(reference);
906 //=======================================================================
907 //function : EndReadRefSection
908 //purpose : DATA SECTION
910 //=======================================================================
912 Storage_Error FSD_BinaryFile::EndReadRefSection()
914 if (!fseek(myStream,myHeader.eref,SEEK_SET)) return Storage_VSOk;
915 else return Storage_VSSectionNotFound;
918 //=======================================================================
919 //function : BeginWriteDataSection
920 //purpose : -------------------- DATA : WRITE
921 //=======================================================================
923 Storage_Error FSD_BinaryFile::BeginWriteDataSection()
925 myHeader.bdata = ftell(myStream);
930 //=======================================================================
931 //function : WritePersistentObjectHeader
933 //=======================================================================
935 void FSD_BinaryFile::WritePersistentObjectHeader(const Standard_Integer aRef,
936 const Standard_Integer aType)
942 //=======================================================================
943 //function : BeginWritePersistentObjectData
945 //=======================================================================
947 void FSD_BinaryFile::BeginWritePersistentObjectData()
951 //=======================================================================
952 //function : BeginWriteObjectData
954 //=======================================================================
956 void FSD_BinaryFile::BeginWriteObjectData()
960 //=======================================================================
961 //function : EndWriteObjectData
963 //=======================================================================
965 void FSD_BinaryFile::EndWriteObjectData()
969 //=======================================================================
970 //function : EndWritePersistentObjectData
972 //=======================================================================
974 void FSD_BinaryFile::EndWritePersistentObjectData()
978 //=======================================================================
979 //function : EndWriteDataSection
981 //=======================================================================
983 Storage_Error FSD_BinaryFile::EndWriteDataSection()
985 myHeader.edata = ftell(myStream);
987 fseek(myStream,myHeader.binfo,SEEK_SET);
992 //=======================================================================
993 //function : BeginReadDataSection
994 //purpose : ---------------------- DATA : READ
995 //=======================================================================
997 Storage_Error FSD_BinaryFile::BeginReadDataSection()
999 if (!fseek(myStream,myHeader.bdata,SEEK_SET)) return Storage_VSOk;
1000 else return Storage_VSSectionNotFound;
1003 //=======================================================================
1004 //function : ReadPersistentObjectHeader
1006 //=======================================================================
1008 void FSD_BinaryFile::ReadPersistentObjectHeader(Standard_Integer& aRef,
1009 Standard_Integer& aType)
1015 //=======================================================================
1016 //function : BeginReadPersistentObjectData
1018 //=======================================================================
1020 void FSD_BinaryFile::BeginReadPersistentObjectData()
1024 //=======================================================================
1025 //function : BeginReadObjectData
1027 //=======================================================================
1029 void FSD_BinaryFile::BeginReadObjectData()
1033 //=======================================================================
1034 //function : EndReadObjectData
1036 //=======================================================================
1038 void FSD_BinaryFile::EndReadObjectData()
1042 //=======================================================================
1043 //function : EndReadPersistentObjectData
1045 //=======================================================================
1047 void FSD_BinaryFile::EndReadPersistentObjectData()
1051 //=======================================================================
1052 //function : EndReadDataSection
1054 //=======================================================================
1056 Storage_Error FSD_BinaryFile::EndReadDataSection()
1058 if (!fseek(myStream,myHeader.edata,SEEK_SET)) return Storage_VSOk;
1059 else return Storage_VSSectionNotFound;
1062 //=======================================================================
1063 //function : WriteString
1064 //purpose : write string at the current position.
1065 //=======================================================================
1067 void FSD_BinaryFile::WriteString(const TCollection_AsciiString& aString)
1069 Standard_Integer size;
1071 size = aString.Length();
1076 if (!fwrite(aString.ToCString(),aString.Length(),1,myStream)) Storage_StreamWriteError::Raise();
1080 //=======================================================================
1081 //function : ReadString
1082 //purpose : read string from the current position.
1083 //=======================================================================
1085 void FSD_BinaryFile::ReadString(TCollection_AsciiString& aString)
1087 Standard_Integer size = 0;
1091 Standard_Character *c = (Standard_Character *)Standard::Allocate((size+1) * sizeof(Standard_Character));
1092 if (!fread(c,size,1,myStream)) Storage_StreamWriteError::Raise();
1102 //=======================================================================
1103 //function : WriteExtendedString
1104 //purpose : write string at the current position.
1105 //=======================================================================
1107 void FSD_BinaryFile::WriteExtendedString(const TCollection_ExtendedString& aString)
1109 Standard_Integer size;
1111 size = aString.Length();
1116 Standard_ExtString anExtStr;
1117 #if OCCT_BINARY_FILE_DO_INVERSE
1118 TCollection_ExtendedString aCopy = aString;
1119 anExtStr = aCopy.ToExtString();
1121 Standard_PExtCharacter pChar;
1123 pChar=(Standard_PExtCharacter)anExtStr;
1125 for (Standard_Integer i=0; i < size; i++)
1126 pChar[i] = InverseExtChar (pChar[i]);
1128 anExtStr = aString.ToExtString();
1130 if (!fwrite(anExtStr,sizeof(Standard_ExtCharacter)*aString.Length(),1,myStream))
1131 Storage_StreamWriteError::Raise();
1135 //=======================================================================
1136 //function : ReadExtendedString
1137 //purpose : read string from the current position.
1138 //=======================================================================
1140 void FSD_BinaryFile::ReadExtendedString(TCollection_ExtendedString& aString)
1142 Standard_Integer size = 0;
1146 Standard_ExtCharacter *c = (Standard_ExtCharacter *)
1147 Standard::Allocate((size+1) * sizeof(Standard_ExtCharacter));
1148 if (!fread(c,size*sizeof(Standard_ExtCharacter),1,myStream))
1149 Storage_StreamWriteError::Raise();
1151 #if OCCT_BINARY_FILE_DO_INVERSE
1152 for (Standard_Integer i=0; i < size; i++)
1153 c[i] = InverseExtChar (c[i]);
1163 //=======================================================================
1164 //function : WriteHeader
1166 //=======================================================================
1168 void FSD_BinaryFile::WriteHeader()
1170 PutInteger(myHeader.testindian);
1171 PutInteger(myHeader.binfo);
1172 PutInteger(myHeader.einfo);
1173 PutInteger(myHeader.bcomment);
1174 PutInteger(myHeader.ecomment);
1175 PutInteger(myHeader.btype);
1176 PutInteger(myHeader.etype);
1177 PutInteger(myHeader.broot);
1178 PutInteger(myHeader.eroot);
1179 PutInteger(myHeader.bref);
1180 PutInteger(myHeader.eref);
1181 PutInteger(myHeader.bdata);
1182 PutInteger(myHeader.edata);
1185 //=======================================================================
1186 //function : ReadHeader
1188 //=======================================================================
1190 void FSD_BinaryFile::ReadHeader()
1192 GetInteger(myHeader.testindian);
1193 GetInteger(myHeader.binfo);
1194 GetInteger(myHeader.einfo);
1195 GetInteger(myHeader.bcomment);
1196 GetInteger(myHeader.ecomment);
1197 GetInteger(myHeader.btype);
1198 GetInteger(myHeader.etype);
1199 GetInteger(myHeader.broot);
1200 GetInteger(myHeader.eroot);
1201 GetInteger(myHeader.bref);
1202 GetInteger(myHeader.eref);
1203 GetInteger(myHeader.bdata);
1204 GetInteger(myHeader.edata);
1208 //=======================================================================
1210 //purpose : return position in the file. Return -1 upon error.
1211 //=======================================================================
1213 Storage_Position FSD_BinaryFile::Tell()
1215 return (Storage_Position) ftell(myStream);
1218 //=======================================================================
1219 //function : InverseReal
1220 //purpose : Inverses bytes in the real value
1221 //=======================================================================
1223 Standard_Real FSD_BinaryFile::InverseReal (const Standard_Real theValue)
1225 Standard_STATIC_ASSERT(sizeof(Standard_Real) == 2 * sizeof(Standard_Integer));
1227 Standard_Integer i[2];
1228 Standard_Real aValue;
1231 aWrapUnion.aValue = theValue;
1233 Standard_Integer aTemp = aWrapUnion.i[1];
1234 aWrapUnion.i[1] = InverseInt(aWrapUnion.i[0]);
1235 aWrapUnion.i[0] = InverseInt(aTemp);
1237 return aWrapUnion.aValue;
1240 //=======================================================================
1241 //function : InverseShortReal
1242 //purpose : Inverses bytes in the short real value
1243 //=======================================================================
1245 Standard_ShortReal FSD_BinaryFile::InverseShortReal (const Standard_ShortReal theValue)
1247 Standard_STATIC_ASSERT(sizeof(Standard_ShortReal) == sizeof(Standard_Integer));
1249 Standard_ShortReal aValue;
1250 Standard_Integer aResult;
1253 aWrapUnion.aValue = theValue;
1254 aWrapUnion.aResult = InverseInt (aWrapUnion.aResult);
1256 return aWrapUnion.aValue;
1259 //=======================================================================
1260 //function : InverseSize
1261 //purpose : Inverses bytes in size_t type instance
1262 //=======================================================================
1265 inline Standard_Size OCCT_InverseSizeSpecialized (const Standard_Size theValue, int);
1268 inline Standard_Size OCCT_InverseSizeSpecialized <4> (const Standard_Size theValue, int)
1270 return FSD_BinaryFile::InverseInt(static_cast<Standard_Integer>(theValue));
1274 inline Standard_Size OCCT_InverseSizeSpecialized <8> (const Standard_Size theValue, int)
1277 Standard_Integer i[2];
1278 Standard_Size aValue;
1281 aWrapUnion.aValue = theValue;
1283 Standard_Integer aTemp = aWrapUnion.i[1];
1284 aWrapUnion.i[1] = FSD_BinaryFile::InverseInt(aWrapUnion.i[0]);
1285 aWrapUnion.i[0] = FSD_BinaryFile::InverseInt(aTemp);
1287 return aWrapUnion.aValue;
1290 Standard_Size FSD_BinaryFile::InverseSize (const Standard_Size theValue)
1292 return OCCT_InverseSizeSpecialized <sizeof(Standard_Size)> (theValue, 0);