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_HArrayOfCallBack.hxx>
21 #include <Storage_HeaderData.hxx>
22 #include <Storage_InternalData.hxx>
23 #include <Storage_RootData.hxx>
24 #include <Storage_StreamExtCharParityError.hxx>
25 #include <Storage_StreamFormatError.hxx>
26 #include <Storage_StreamTypeMismatchError.hxx>
27 #include <Storage_StreamWriteError.hxx>
28 #include <Storage_TypeData.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <Standard_Assert.hxx>
33 const Standard_CString MAGICNUMBER = "BINFILE";
35 //=======================================================================
36 //function : FSD_BinaryFile
38 //=======================================================================
40 FSD_BinaryFile::FSD_BinaryFile() :
43 myHeader.testindian = -1;
46 myHeader.bcomment = -1;
47 myHeader.ecomment = -1;
58 //=======================================================================
59 //function : IsGoodFileType
60 //purpose : INFO SECTION
62 //=======================================================================
64 Storage_Error FSD_BinaryFile::IsGoodFileType(const TCollection_AsciiString& aName)
69 s = f.Open(aName,Storage_VSRead);
71 if (s == Storage_VSOk) {
72 TCollection_AsciiString l;
73 Standard_Size len = strlen(FSD_BinaryFile::MagicNumber());
79 if (strncmp(FSD_BinaryFile::MagicNumber(),l.ToCString(),len) != 0) {
80 s = Storage_VSFormatError;
87 //=======================================================================
90 //=======================================================================
92 Storage_Error FSD_BinaryFile::Open(const TCollection_AsciiString& aName,const Storage_OpenMode aMode)
94 Storage_Error result = Storage_VSOk;
98 if (OpenMode() == Storage_VSNone) {
99 if (aMode == Storage_VSRead) {
100 myStream = OSD_OpenFile(aName.ToCString(),"rb");
102 else if (aMode == Storage_VSWrite) {
103 myStream = OSD_OpenFile(aName.ToCString(),"wb");
105 else if (aMode == Storage_VSReadWrite) {
106 myStream = OSD_OpenFile(aName.ToCString(),"w+b");
109 if (myStream == 0L) {
110 result = Storage_VSOpenError;
117 result = Storage_VSAlreadyOpen;
123 //=======================================================================
126 //=======================================================================
128 Standard_Boolean FSD_BinaryFile::IsEnd()
130 return (feof(myStream) != 0);
133 //=======================================================================
136 //=======================================================================
138 Storage_Error FSD_BinaryFile::Close()
140 Storage_Error result = Storage_VSOk;
142 if (OpenMode() != Storage_VSNone) {
144 SetOpenMode(Storage_VSNone);
147 result = Storage_VSNotOpen;
153 //=======================================================================
154 //function : MagicNumber
155 //purpose : ------------------ PROTECTED
156 //=======================================================================
158 Standard_CString FSD_BinaryFile::MagicNumber()
163 //=======================================================================
164 //function : ReadChar
165 //purpose : read <rsize> character from the current position.
166 //=======================================================================
168 void FSD_BinaryFile::ReadChar(TCollection_AsciiString& buffer, const Standard_Size rsize)
171 Standard_Size ccount = 0;
175 while (!IsEnd() && (ccount < rsize)) {
176 ccount += fread(&c, sizeof(char),1, myStream);
181 //=======================================================================
182 //function : SkipObject
184 //=======================================================================
186 void FSD_BinaryFile::SkipObject()
191 //=======================================================================
192 //function : PutReference
193 //purpose : ---------------------- PUBLIC : PUT
194 //=======================================================================
196 Storage_BaseDriver& FSD_BinaryFile::PutReference(const Standard_Integer aValue)
198 #if OCCT_BINARY_FILE_DO_INVERSE
199 Standard_Integer t = InverseInt (aValue);
201 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
203 if (!fwrite(&aValue,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
208 //=======================================================================
209 //function : PutCharacter
211 //=======================================================================
213 Storage_BaseDriver& FSD_BinaryFile::PutCharacter(const Standard_Character aValue)
215 if (!fwrite(&aValue,sizeof(Standard_Character),1,myStream)) throw Storage_StreamWriteError();
219 //=======================================================================
220 //function : PutExtCharacter
222 //=======================================================================
224 Storage_BaseDriver& FSD_BinaryFile::PutExtCharacter(const Standard_ExtCharacter aValue)
226 #if OCCT_BINARY_FILE_DO_INVERSE
227 Standard_ExtCharacter t = InverseExtChar (aValue);
229 if (!fwrite(&t,sizeof(Standard_ExtCharacter),1,myStream)) throw Storage_StreamWriteError();
231 if (!fwrite(&aValue,sizeof(Standard_ExtCharacter),1,myStream)) throw Storage_StreamWriteError();
236 //=======================================================================
237 //function : PutInteger
239 //=======================================================================
241 Storage_BaseDriver& FSD_BinaryFile::PutInteger(const Standard_Integer aValue)
243 #if OCCT_BINARY_FILE_DO_INVERSE
244 Standard_Integer t = InverseInt (aValue);
246 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
248 if (!fwrite(&aValue,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
254 //=======================================================================
255 //function : PutInteger
257 //=======================================================================
258 Standard_Integer FSD_BinaryFile::PutInteger (Standard_OStream& theOStream,
259 const Standard_Integer theValue,
260 const Standard_Boolean theOnlyCount)
262 #if OCCT_BINARY_FILE_DO_INVERSE
263 Standard_Integer t = InverseInt (theValue);
265 Standard_Integer t = theValue;
270 theOStream.write ((char*)&t, sizeof(Standard_Integer));
271 if (theOStream.fail())
273 throw Storage_StreamWriteError();
277 return sizeof(Standard_Integer);
280 //=======================================================================
281 //function : PutBoolean
283 //=======================================================================
285 Storage_BaseDriver& FSD_BinaryFile::PutBoolean(const Standard_Boolean aValue)
287 #if OCCT_BINARY_FILE_DO_INVERSE
288 Standard_Integer t = InverseInt ((Standard_Integer) aValue);
290 Standard_Integer t = aValue ? 1 : 0;
292 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
296 //=======================================================================
299 //=======================================================================
301 Storage_BaseDriver& FSD_BinaryFile::PutReal(const Standard_Real aValue)
303 #if OCCT_BINARY_FILE_DO_INVERSE
304 Standard_Real t = InverseReal (aValue);
306 if (!fwrite(&t,sizeof(Standard_Real),1,myStream)) throw Storage_StreamWriteError();
308 if (!fwrite(&aValue,sizeof(Standard_Real),1,myStream)) throw Storage_StreamWriteError();
313 //=======================================================================
314 //function : PutShortReal
316 //=======================================================================
318 Storage_BaseDriver& FSD_BinaryFile::PutShortReal(const Standard_ShortReal aValue)
320 #if OCCT_BINARY_FILE_DO_INVERSE
321 Standard_ShortReal t = InverseShortReal (aValue);
323 if (!fwrite(&t,sizeof(Standard_ShortReal),1,myStream)) throw Storage_StreamWriteError();
325 if (!fwrite(&aValue,sizeof(Standard_ShortReal),1,myStream)) throw Storage_StreamWriteError();
330 //=======================================================================
331 //function : GetReference
332 //purpose : ----------------- PUBLIC : GET
333 //=======================================================================
335 Storage_BaseDriver& FSD_BinaryFile::GetReference(Standard_Integer& aValue)
337 if (!fread(&aValue,sizeof(Standard_Integer),1,myStream))
338 throw Storage_StreamTypeMismatchError();
339 #if OCCT_BINARY_FILE_DO_INVERSE
340 aValue = InverseInt (aValue);
345 //=======================================================================
346 //function : GetReference
347 //purpose : ----------------- PUBLIC : GET
348 //=======================================================================
349 void FSD_BinaryFile::GetReference(Standard_IStream& theIStream, Standard_Integer& aValue)
351 theIStream.read ((char*)&aValue, sizeof(Standard_Integer));
353 if (theIStream.gcount() != sizeof(Standard_Integer))
355 throw Storage_StreamTypeMismatchError();
358 #if OCCT_BINARY_FILE_DO_INVERSE
359 aValue = InverseInt (aValue);
363 //=======================================================================
364 //function : GetCharacter
366 //=======================================================================
368 Storage_BaseDriver& FSD_BinaryFile::GetCharacter(Standard_Character& aValue)
370 if (!fread(&aValue,sizeof(Standard_Character),1,myStream))
371 throw Storage_StreamTypeMismatchError();
375 //=======================================================================
376 //function : GetExtCharacter
378 //=======================================================================
380 Storage_BaseDriver& FSD_BinaryFile::GetExtCharacter(Standard_ExtCharacter& aValue)
382 if (!fread(&aValue,sizeof(Standard_ExtCharacter),1,myStream))
383 throw Storage_StreamTypeMismatchError();
384 #if OCCT_BINARY_FILE_DO_INVERSE
385 aValue = InverseExtChar (aValue);
390 //=======================================================================
391 //function : GetInteger
393 //=======================================================================
395 Storage_BaseDriver& FSD_BinaryFile::GetInteger(Standard_Integer& aValue)
397 if (!fread(&aValue,sizeof(Standard_Integer),1,myStream))
398 throw Storage_StreamTypeMismatchError();
399 #if OCCT_BINARY_FILE_DO_INVERSE
400 aValue = InverseInt (aValue);
405 //=======================================================================
406 //function : GetInteger
408 //=======================================================================
409 void FSD_BinaryFile::GetInteger (Standard_IStream& theIStream, Standard_Integer& theValue)
412 theIStream.read ((char*)&theValue, sizeof(Standard_Integer));
414 if (theIStream.gcount() != sizeof(Standard_Integer))
416 throw Storage_StreamTypeMismatchError();
419 #if OCCT_BINARY_FILE_DO_INVERSE
420 theValue = InverseInt (theValue);
424 //=======================================================================
425 //function : GetBoolean
427 //=======================================================================
429 Storage_BaseDriver& FSD_BinaryFile::GetBoolean(Standard_Boolean& aValue)
431 Standard_Integer anInt = 0;
432 if (!fread(&anInt,sizeof(Standard_Integer),1,myStream))
433 throw Storage_StreamTypeMismatchError();
434 #if OCCT_BINARY_FILE_DO_INVERSE
435 anInt = InverseInt (anInt);
437 aValue = (anInt != 0);
441 //=======================================================================
444 //=======================================================================
446 Storage_BaseDriver& FSD_BinaryFile::GetReal(Standard_Real& aValue)
448 if (!fread(&aValue,sizeof(Standard_Real),1,myStream))
449 throw Storage_StreamTypeMismatchError();
450 #if OCCT_BINARY_FILE_DO_INVERSE
451 aValue = InverseReal (aValue);
456 //=======================================================================
457 //function : GetShortReal
459 //=======================================================================
461 Storage_BaseDriver& FSD_BinaryFile::GetShortReal(Standard_ShortReal& aValue)
463 if (!fread(&aValue,sizeof(Standard_ShortReal),1,myStream))
464 throw Storage_StreamTypeMismatchError();
465 #if OCCT_BINARY_FILE_DO_INVERSE
466 aValue = InverseShortReal (aValue);
471 //=======================================================================
474 //=======================================================================
476 void FSD_BinaryFile::Destroy()
478 if (OpenMode() != Storage_VSNone) {
483 //=======================================================================
484 //function : BeginWriteInfoSection
485 //purpose : -------------------------- INFO : WRITE
486 //=======================================================================
488 Storage_Error FSD_BinaryFile::BeginWriteInfoSection()
492 Standard_Integer aResult;
495 aWrapUnion.ti2[0] = 1;
496 aWrapUnion.ti2[1] = 2;
497 aWrapUnion.ti2[2] = 3;
498 aWrapUnion.ti2[3] = 4;
500 myHeader.testindian = aWrapUnion.aResult;
502 if (!fwrite(FSD_BinaryFile::MagicNumber(),
503 strlen(FSD_BinaryFile::MagicNumber()),
506 throw Storage_StreamWriteError();
508 myHeader.binfo = ftell(myStream);
514 //=======================================================================
515 //function : WriteInfo
517 //=======================================================================
519 void FSD_BinaryFile::WriteInfo(const Standard_Integer nbObj,
520 const TCollection_AsciiString& dbVersion,
521 const TCollection_AsciiString& date,
522 const TCollection_AsciiString& schemaName,
523 const TCollection_AsciiString& schemaVersion,
524 const TCollection_ExtendedString& appName,
525 const TCollection_AsciiString& appVersion,
526 const TCollection_ExtendedString& dataType,
527 const TColStd_SequenceOfAsciiString& userInfo)
532 WriteString(dbVersion);
534 WriteString(schemaName);
535 WriteString(schemaVersion);
536 WriteExtendedString(appName);
537 WriteString(appVersion);
538 WriteExtendedString(dataType);
539 i = userInfo.Length();
542 for (i = 1; i <= userInfo.Length(); i++) {
543 WriteString(userInfo.Value(i));
547 //=======================================================================
548 //function : WriteInfo
550 //=======================================================================
551 Standard_Integer FSD_BinaryFile::WriteInfo (Standard_OStream& theOStream,
552 const Standard_Integer theObjNb,
553 const TCollection_AsciiString& theStoreVer,
554 const TCollection_AsciiString& theCreationDate,
555 const TCollection_AsciiString& theSchemaName,
556 const TCollection_AsciiString& theSchemaVersion,
557 const TCollection_ExtendedString& theAppName,
558 const TCollection_AsciiString& theAppVer,
559 const TCollection_ExtendedString& theDataType,
560 const TColStd_SequenceOfAsciiString& theUserInfo,
561 const Standard_Boolean theOnlyCount)
563 Standard_Integer anInfoSize = 0;
565 anInfoSize += PutInteger (theOStream, theObjNb, theOnlyCount);
566 anInfoSize += WriteString(theOStream, theStoreVer, theOnlyCount);
567 anInfoSize += WriteString(theOStream, theCreationDate, theOnlyCount);
568 anInfoSize += WriteString(theOStream, theSchemaName, theOnlyCount);
569 anInfoSize += WriteString(theOStream, theSchemaVersion, theOnlyCount);
570 anInfoSize += WriteExtendedString(theOStream, theAppName, theOnlyCount);
571 anInfoSize += WriteString(theOStream, theAppVer, theOnlyCount);
572 anInfoSize += WriteExtendedString(theOStream, theDataType, theOnlyCount);
574 Standard_Integer i = theUserInfo.Length();
575 anInfoSize += PutInteger(theOStream, i, theOnlyCount);
577 for (i = 1; i <= theUserInfo.Length(); i++) {
578 anInfoSize += WriteString (theOStream, theUserInfo.Value(i), theOnlyCount);
584 //=======================================================================
585 //function : EndWriteInfoSection
587 //=======================================================================
589 Storage_Error FSD_BinaryFile::EndWriteInfoSection()
591 myHeader.einfo = ftell(myStream);
596 //=======================================================================
597 //function : EndWriteInfoSection
599 //=======================================================================
600 Storage_Error FSD_BinaryFile::EndWriteInfoSection(Standard_OStream& theOStream)
602 myHeader.einfo = (Standard_Integer)theOStream.tellp();
607 //=======================================================================
608 //function : BeginReadInfoSection
610 //=======================================================================
612 Storage_Error FSD_BinaryFile::BeginReadInfoSection()
614 Storage_Error s = Storage_VSOk;
615 TCollection_AsciiString l;
616 Standard_Size len = strlen(FSD_BinaryFile::MagicNumber());
620 if (strncmp(FSD_BinaryFile::MagicNumber(),l.ToCString(),len) != 0) {
621 s = Storage_VSFormatError;
630 //=======================================================================
631 //function : ReadInfo
632 //purpose : ------------------- INFO : READ
633 //=======================================================================
635 void FSD_BinaryFile::ReadInfo(Standard_Integer& nbObj,
636 TCollection_AsciiString& dbVersion,
637 TCollection_AsciiString& date,
638 TCollection_AsciiString& schemaName,
639 TCollection_AsciiString& schemaVersion,
640 TCollection_ExtendedString& appName,
641 TCollection_AsciiString& appVersion,
642 TCollection_ExtendedString& dataType,
643 TColStd_SequenceOfAsciiString& userInfo)
646 ReadString(dbVersion);
648 ReadString(schemaName);
649 ReadString(schemaVersion);
650 ReadExtendedString(appName);
651 ReadString(appVersion);
652 ReadExtendedString(dataType);
654 Standard_Integer i,len = 0;
657 TCollection_AsciiString line;
659 for (i = 1; i <= len && !IsEnd(); i++) {
661 userInfo.Append(line);
665 //=======================================================================
666 //function : ReadInfo
668 //=======================================================================
669 void FSD_BinaryFile::ReadCompleteInfo (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
671 FSD_FileHeader aHeaderPos;
672 ReadHeader(theIStream, aHeaderPos);
674 if (theData.IsNull())
676 theData = new Storage_Data();
679 Handle(Storage_InternalData) iData = theData->InternalData();
680 Handle(Storage_TypeData) tData = theData->TypeData();
681 Handle(Storage_RootData) rData = theData->RootData();
682 Handle(Storage_HeaderData) hData = theData->HeaderData();
684 ReadHeaderData (theIStream, hData);
686 Handle(Storage_HArrayOfCallBack) theCallBack;
688 while (theIStream.good() && !theIStream.eof())
690 Standard_Integer aPos = (Standard_Integer)theIStream.tellg();
692 if (aPos >= aHeaderPos.edata)
696 else if (aPos == aHeaderPos.bcomment)
698 TColStd_SequenceOfExtendedString mComment;
699 ReadComment (theIStream, mComment);
701 for (Standard_Integer i = 1; i <= mComment.Length(); i++)
703 hData->AddToComments (mComment.Value(i));
706 iData->ReadArray() = new Storage_HPArray(1, theData->NumberOfObjects());
708 else if (aPos == aHeaderPos.btype)
710 Standard_Integer aTypeSectionSize = TypeSectionSize (theIStream);
711 theCallBack = new Storage_HArrayOfCallBack (1, aTypeSectionSize);
713 TCollection_AsciiString aTypeName;
714 Standard_Integer aTypeNum;
716 for (Standard_Integer i = 1; i <= aTypeSectionSize; i++)
718 ReadTypeInformations (theIStream, aTypeNum, aTypeName);
719 tData->AddType (aTypeName,aTypeNum);
721 theCallBack->SetValue (aTypeNum, NULL);
724 else if (aPos == aHeaderPos.broot)
726 Standard_Integer aRootSectionSize = RootSectionSize(theIStream);
728 Standard_Integer aRef;
729 TCollection_AsciiString aRootName, aTypeName;
730 Handle(Storage_Root) aRoot;
731 Handle(Standard_Persistent) aPer;
733 for (Standard_Integer i = 1; i <= aRootSectionSize; i++)
735 ReadRoot (theIStream, aRootName, aRef, aTypeName);
737 aRoot = new Storage_Root(aRootName, aPer);
738 aRoot->SetReference(aRef);
739 aRoot->SetType(aTypeName);
740 rData->AddRoot(aRoot);
743 else if (aPos == aHeaderPos.bref)
745 Standard_Integer aRefSectionSize = RefSectionSize (theIStream);
747 Standard_Integer aTypeNum, aRef = 0;
749 for (Standard_Integer i = 1; i <= aRefSectionSize; i++)
751 ReadReferenceType (theIStream, aRef, aTypeNum);
753 iData->ReadArray()->ChangeValue(aRef) = theCallBack->Value(aTypeNum)->New();
755 if (!iData->ReadArray()->ChangeValue(aRef).IsNull())
757 iData->ReadArray()->ChangeValue(aRef)->TypeNum() = aTypeNum;
761 else if (aPos == aHeaderPos.bdata)
767 Handle(Storage_HSeqOfRoot) aRoots = rData->Roots();
768 for(Standard_Integer i = 1; i <= theData->NumberOfRoots(); i++)
770 const Handle(Storage_Root)& aCurRoot = aRoots->Value(i);
771 rData->UpdateRoot (aCurRoot->Name(), iData->ReadArray()->Value (aCurRoot->Reference()));
777 //=======================================================================
778 //function : EndReadInfoSection
779 //purpose : COMMENTS SECTION
781 //=======================================================================
783 Storage_Error FSD_BinaryFile::EndReadInfoSection()
785 if (!fseek(myStream,myHeader.einfo,SEEK_SET)) return Storage_VSOk;
786 else return Storage_VSSectionNotFound;
789 //=======================================================================
790 //function : BeginWriteCommentSection
791 //purpose : ---------------- COMMENTS : WRITE
792 //=======================================================================
794 Storage_Error FSD_BinaryFile::BeginWriteCommentSection()
796 myHeader.bcomment = ftell(myStream);
800 //=======================================================================
801 //function : BeginWriteCommentSection
803 //=======================================================================
804 Storage_Error FSD_BinaryFile::BeginWriteCommentSection(Standard_OStream& theOStream)
806 myHeader.bcomment = (Standard_Integer)theOStream.tellp();
810 //=======================================================================
811 //function : WriteComment
813 //=======================================================================
815 void FSD_BinaryFile::WriteComment(const TColStd_SequenceOfExtendedString& aCom)
817 Standard_Integer i,aSize;
819 aSize = aCom.Length();
821 for (i = 1; i <= aSize; i++) {
822 WriteExtendedString(aCom.Value(i));
826 //=======================================================================
827 //function : WriteComment
829 //=======================================================================
830 Standard_Integer FSD_BinaryFile::WriteComment (Standard_OStream& theOStream,
831 const TColStd_SequenceOfExtendedString& theComments,
832 const Standard_Boolean theOnlyCount)
834 Standard_Integer aCommentSize = 0;
836 Standard_Integer aSize = theComments.Length();
837 aCommentSize += PutInteger(theOStream, aSize, theOnlyCount);
839 for (Standard_Integer i = 1; i <= aSize; i++) {
840 aCommentSize += WriteExtendedString (theOStream, theComments.Value(i), theOnlyCount);
846 //=======================================================================
847 //function : EndWriteCommentSection
849 //=======================================================================
851 Storage_Error FSD_BinaryFile::EndWriteCommentSection()
853 myHeader.ecomment = ftell(myStream);
858 //=======================================================================
859 //function : EndWriteCommentSection
861 //=======================================================================
862 Storage_Error FSD_BinaryFile::EndWriteCommentSection (Standard_OStream& theOStream)
864 myHeader.ecomment = (Standard_Integer)theOStream.tellp();
869 //=======================================================================
870 //function : BeginReadCommentSection
871 //purpose : ---------------- COMMENTS : READ
872 //=======================================================================
874 Storage_Error FSD_BinaryFile::BeginReadCommentSection()
876 if (!fseek(myStream,myHeader.bcomment,SEEK_SET)) return Storage_VSOk;
877 else return Storage_VSSectionNotFound;
880 //=======================================================================
881 //function : ReadComment
883 //=======================================================================
885 void FSD_BinaryFile::ReadComment(TColStd_SequenceOfExtendedString& aCom)
887 TCollection_ExtendedString line;
888 Standard_Integer len,i;
891 for (i = 1; i <= len && !IsEnd(); i++) {
892 ReadExtendedString(line);
897 //=======================================================================
898 //function : ReadComment
900 //=======================================================================
901 void FSD_BinaryFile::ReadComment (Standard_IStream& theIStream, TColStd_SequenceOfExtendedString& aCom)
903 TCollection_ExtendedString line;
904 Standard_Integer len,i;
906 GetInteger(theIStream, len);
907 for (i = 1; i <= len && theIStream.good(); i++)
909 ReadExtendedString(theIStream, line);
914 //=======================================================================
915 //function : EndReadCommentSection
917 //=======================================================================
919 Storage_Error FSD_BinaryFile::EndReadCommentSection()
921 if (!fseek(myStream,myHeader.ecomment,SEEK_SET)) return Storage_VSOk;
922 else return Storage_VSSectionNotFound;
925 //=======================================================================
926 //function : BeginWriteTypeSection
927 //purpose : --------------- TYPE : WRITE
928 //=======================================================================
930 Storage_Error FSD_BinaryFile::BeginWriteTypeSection()
932 myHeader.btype = ftell(myStream);
937 //=======================================================================
938 //function : SetTypeSectionSize
940 //=======================================================================
942 void FSD_BinaryFile::SetTypeSectionSize(const Standard_Integer aSize)
947 //=======================================================================
948 //function : WriteTypeInformations
950 //=======================================================================
952 void FSD_BinaryFile::WriteTypeInformations(const Standard_Integer typeNum,
953 const TCollection_AsciiString& typeName)
956 WriteString(typeName);
959 //=======================================================================
960 //function : EndWriteTypeSection
962 //=======================================================================
964 Storage_Error FSD_BinaryFile::EndWriteTypeSection()
966 myHeader.etype = ftell(myStream);
971 //=======================================================================
972 //function : BeginReadTypeSection
973 //purpose : ------------------- TYPE : READ
974 //=======================================================================
976 Storage_Error FSD_BinaryFile::BeginReadTypeSection()
978 if (!fseek(myStream,myHeader.btype,SEEK_SET)) return Storage_VSOk;
979 else return Storage_VSSectionNotFound;
982 //=======================================================================
983 //function : TypeSectionSize
985 //=======================================================================
987 Standard_Integer FSD_BinaryFile::TypeSectionSize()
995 //=======================================================================
996 //function : TypeSectionSize
998 //=======================================================================
999 Standard_Integer FSD_BinaryFile::TypeSectionSize(Standard_IStream& theIStream)
1003 GetInteger(theIStream, i);
1007 //=======================================================================
1008 //function : ReadTypeInformations
1010 //=======================================================================
1012 void FSD_BinaryFile::ReadTypeInformations(Standard_Integer& typeNum,TCollection_AsciiString& typeName)
1014 GetInteger(typeNum);
1015 ReadString(typeName);
1018 //=======================================================================
1019 //function : ReadTypeInformations
1021 //=======================================================================
1022 void FSD_BinaryFile::ReadTypeInformations(Standard_IStream& theIStream, Standard_Integer& typeNum,TCollection_AsciiString& typeName)
1024 GetInteger(theIStream, typeNum);
1025 ReadString(theIStream, typeName);
1028 //=======================================================================
1029 //function : EndReadTypeSection
1030 //purpose : ROOT SECTION
1032 //=======================================================================
1034 Storage_Error FSD_BinaryFile::EndReadTypeSection()
1036 if (!fseek(myStream,myHeader.etype,SEEK_SET)) return Storage_VSOk;
1037 else return Storage_VSSectionNotFound;
1040 //=======================================================================
1041 //function : BeginWriteRootSection
1042 //purpose : -------------------- ROOT : WRITE
1043 //=======================================================================
1045 Storage_Error FSD_BinaryFile::BeginWriteRootSection()
1047 myHeader.broot = ftell(myStream);
1049 return Storage_VSOk;
1052 //=======================================================================
1053 //function : SetRootSectionSize
1055 //=======================================================================
1057 void FSD_BinaryFile::SetRootSectionSize(const Standard_Integer aSize)
1062 //=======================================================================
1063 //function : WriteRoot
1065 //=======================================================================
1067 void FSD_BinaryFile::WriteRoot(const TCollection_AsciiString& rootName, const Standard_Integer aRef, const TCollection_AsciiString& rootType)
1070 WriteString(rootName);
1071 WriteString(rootType);
1074 //=======================================================================
1075 //function : EndWriteRootSection
1077 //=======================================================================
1079 Storage_Error FSD_BinaryFile::EndWriteRootSection()
1081 myHeader.eroot = ftell(myStream);
1083 return Storage_VSOk;
1086 //=======================================================================
1087 //function : BeginReadRootSection
1088 //purpose : ----------------------- ROOT : READ
1089 //=======================================================================
1091 Storage_Error FSD_BinaryFile::BeginReadRootSection()
1093 if (!fseek(myStream,myHeader.broot,SEEK_SET)) return Storage_VSOk;
1094 else return Storage_VSSectionNotFound;
1097 //=======================================================================
1098 //function : RootSectionSize
1100 //=======================================================================
1102 Standard_Integer FSD_BinaryFile::RootSectionSize()
1110 //=======================================================================
1111 //function : RootSectionSize
1113 //=======================================================================
1114 Standard_Integer FSD_BinaryFile::RootSectionSize (Standard_IStream& theIStream)
1118 GetInteger(theIStream, i);
1122 //=======================================================================
1123 //function : ReadRoot
1125 //=======================================================================
1127 void FSD_BinaryFile::ReadRoot(TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType)
1130 ReadString(rootName);
1131 ReadString(rootType);
1134 //=======================================================================
1135 //function : ReadRoot
1137 //=======================================================================
1138 void FSD_BinaryFile::ReadRoot (Standard_IStream& theIStream, TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType)
1140 GetReference(theIStream, aRef);
1141 ReadString(theIStream, rootName);
1142 ReadString(theIStream, rootType);
1145 //=======================================================================
1146 //function : EndReadRootSection
1147 //purpose : REF SECTION
1149 //=======================================================================
1151 Storage_Error FSD_BinaryFile::EndReadRootSection()
1153 if (!fseek(myStream,myHeader.eroot,SEEK_SET)) return Storage_VSOk;
1154 else return Storage_VSSectionNotFound;
1157 //=======================================================================
1158 //function : BeginWriteRefSection
1159 //purpose : -------------------------- REF : WRITE
1160 //=======================================================================
1162 Storage_Error FSD_BinaryFile::BeginWriteRefSection()
1164 myHeader.bref = ftell(myStream);
1166 return Storage_VSOk;
1169 //=======================================================================
1170 //function : SetRefSectionSize
1172 //=======================================================================
1174 void FSD_BinaryFile::SetRefSectionSize(const Standard_Integer aSize)
1179 //=======================================================================
1180 //function : WriteReferenceType
1182 //=======================================================================
1184 void FSD_BinaryFile::WriteReferenceType(const Standard_Integer reference,const Standard_Integer typeNum)
1186 PutReference(reference);
1187 PutInteger(typeNum);
1190 //=======================================================================
1191 //function : EndWriteRefSection
1193 //=======================================================================
1195 Storage_Error FSD_BinaryFile::EndWriteRefSection()
1197 myHeader.eref = ftell(myStream);
1199 return Storage_VSOk;
1202 //=======================================================================
1203 //function : BeginReadRefSection
1204 //purpose : ----------------------- REF : READ
1205 //=======================================================================
1207 Storage_Error FSD_BinaryFile::BeginReadRefSection()
1209 if (!fseek(myStream,myHeader.bref,SEEK_SET)) return Storage_VSOk;
1210 else return Storage_VSSectionNotFound;
1213 //=======================================================================
1214 //function : RefSectionSize
1216 //=======================================================================
1218 Standard_Integer FSD_BinaryFile::RefSectionSize()
1226 //=======================================================================
1227 //function : RefSectionSize
1229 //=======================================================================
1230 Standard_Integer FSD_BinaryFile::RefSectionSize (Standard_IStream& theIStream)
1234 GetInteger(theIStream, i);
1238 //=======================================================================
1239 //function : ReadReferenceType
1241 //=======================================================================
1243 void FSD_BinaryFile::ReadReferenceType(Standard_Integer& reference,
1244 Standard_Integer& typeNum)
1246 GetReference(reference);
1247 GetInteger(typeNum);
1250 //=======================================================================
1251 //function : ReadReferenceType
1253 //=======================================================================
1254 void FSD_BinaryFile::ReadReferenceType (Standard_IStream& theIStream, Standard_Integer& reference, Standard_Integer& typeNum)
1256 GetReference (theIStream, reference);
1257 GetInteger (theIStream, typeNum);
1260 //=======================================================================
1261 //function : EndReadRefSection
1262 //purpose : DATA SECTION
1264 //=======================================================================
1266 Storage_Error FSD_BinaryFile::EndReadRefSection()
1268 if (!fseek(myStream,myHeader.eref,SEEK_SET)) return Storage_VSOk;
1269 else return Storage_VSSectionNotFound;
1272 //=======================================================================
1273 //function : BeginWriteDataSection
1274 //purpose : -------------------- DATA : WRITE
1275 //=======================================================================
1277 Storage_Error FSD_BinaryFile::BeginWriteDataSection()
1279 myHeader.bdata = ftell(myStream);
1281 return Storage_VSOk;
1284 //=======================================================================
1285 //function : WritePersistentObjectHeader
1287 //=======================================================================
1289 void FSD_BinaryFile::WritePersistentObjectHeader(const Standard_Integer aRef,
1290 const Standard_Integer aType)
1296 //=======================================================================
1297 //function : BeginWritePersistentObjectData
1299 //=======================================================================
1301 void FSD_BinaryFile::BeginWritePersistentObjectData()
1305 //=======================================================================
1306 //function : BeginWriteObjectData
1308 //=======================================================================
1310 void FSD_BinaryFile::BeginWriteObjectData()
1314 //=======================================================================
1315 //function : EndWriteObjectData
1317 //=======================================================================
1319 void FSD_BinaryFile::EndWriteObjectData()
1323 //=======================================================================
1324 //function : EndWritePersistentObjectData
1326 //=======================================================================
1328 void FSD_BinaryFile::EndWritePersistentObjectData()
1332 //=======================================================================
1333 //function : EndWriteDataSection
1335 //=======================================================================
1337 Storage_Error FSD_BinaryFile::EndWriteDataSection()
1339 myHeader.edata = ftell(myStream);
1341 fseek(myStream,myHeader.binfo,SEEK_SET);
1343 return Storage_VSOk;
1346 //=======================================================================
1347 //function : BeginReadDataSection
1348 //purpose : ---------------------- DATA : READ
1349 //=======================================================================
1351 Storage_Error FSD_BinaryFile::BeginReadDataSection()
1353 if (!fseek(myStream,myHeader.bdata,SEEK_SET)) return Storage_VSOk;
1354 else return Storage_VSSectionNotFound;
1357 //=======================================================================
1358 //function : ReadPersistentObjectHeader
1360 //=======================================================================
1362 void FSD_BinaryFile::ReadPersistentObjectHeader(Standard_Integer& aRef,
1363 Standard_Integer& aType)
1369 //=======================================================================
1370 //function : BeginReadPersistentObjectData
1372 //=======================================================================
1374 void FSD_BinaryFile::BeginReadPersistentObjectData()
1378 //=======================================================================
1379 //function : BeginReadObjectData
1381 //=======================================================================
1383 void FSD_BinaryFile::BeginReadObjectData()
1387 //=======================================================================
1388 //function : EndReadObjectData
1390 //=======================================================================
1392 void FSD_BinaryFile::EndReadObjectData()
1396 //=======================================================================
1397 //function : EndReadPersistentObjectData
1399 //=======================================================================
1401 void FSD_BinaryFile::EndReadPersistentObjectData()
1405 //=======================================================================
1406 //function : EndReadDataSection
1408 //=======================================================================
1410 Storage_Error FSD_BinaryFile::EndReadDataSection()
1412 if (!fseek(myStream,myHeader.edata,SEEK_SET)) return Storage_VSOk;
1413 else return Storage_VSSectionNotFound;
1416 //=======================================================================
1417 //function : WriteString
1418 //purpose : write string at the current position.
1419 //=======================================================================
1421 void FSD_BinaryFile::WriteString(const TCollection_AsciiString& aString)
1423 Standard_Integer size;
1425 size = aString.Length();
1430 if (!fwrite(aString.ToCString(),aString.Length(),1,myStream)) throw Storage_StreamWriteError();
1434 //=======================================================================
1435 //function : WriteString
1436 //purpose : write string at the current position.
1437 //=======================================================================
1438 Standard_Integer FSD_BinaryFile::WriteString (Standard_OStream& theOStream,
1439 const TCollection_AsciiString& theString,
1440 const Standard_Boolean theOnlyCount)
1442 Standard_Integer aNumAndStrLen, anAsciiStrLen;
1444 anAsciiStrLen = aNumAndStrLen = theString.Length();
1446 aNumAndStrLen += PutInteger (theOStream, anAsciiStrLen, theOnlyCount);
1448 if (anAsciiStrLen > 0 && !theOnlyCount)
1450 theOStream.write (theString.ToCString(), theString.Length());
1451 if (theOStream.fail())
1453 throw Storage_StreamWriteError();
1457 return aNumAndStrLen;
1460 //=======================================================================
1461 //function : ReadString
1462 //purpose : read string from the current position.
1463 //=======================================================================
1465 void FSD_BinaryFile::ReadString(TCollection_AsciiString& aString)
1467 Standard_Integer size = 0;
1471 Standard_Character *c = (Standard_Character *)Standard::Allocate((size+1) * sizeof(Standard_Character));
1472 if (!fread(c,size,1,myStream)) throw Storage_StreamWriteError();
1482 //=======================================================================
1483 //function : ReadString
1484 //purpose : read string from the current position.
1485 //=======================================================================
1486 void FSD_BinaryFile::ReadString (Standard_IStream& theIStream, TCollection_AsciiString& aString)
1488 Standard_Integer size = 0;
1490 GetInteger(theIStream, size);
1494 Standard_Character *c = (Standard_Character *)Standard::Allocate((size+1) * sizeof(Standard_Character));
1496 if (!theIStream.good())
1498 throw Storage_StreamReadError();
1501 theIStream.read (c, size);
1503 if (theIStream.gcount() != size)
1505 throw Storage_StreamReadError();
1520 //=======================================================================
1521 //function : WriteExtendedString
1522 //purpose : write string at the current position.
1523 //=======================================================================
1525 void FSD_BinaryFile::WriteExtendedString(const TCollection_ExtendedString& aString)
1527 Standard_Integer size;
1529 size = aString.Length();
1534 Standard_ExtString anExtStr;
1535 #if OCCT_BINARY_FILE_DO_INVERSE
1536 TCollection_ExtendedString aCopy = aString;
1537 anExtStr = aCopy.ToExtString();
1539 Standard_PExtCharacter pChar;
1541 pChar=(Standard_PExtCharacter)anExtStr;
1543 for (Standard_Integer i=0; i < size; i++)
1544 pChar[i] = InverseExtChar (pChar[i]);
1546 anExtStr = aString.ToExtString();
1548 if (!fwrite(anExtStr,sizeof(Standard_ExtCharacter)*aString.Length(),1,myStream))
1549 throw Storage_StreamWriteError();
1553 //=======================================================================
1554 //function : WriteExtendedString
1555 //purpose : write string at the current position.
1556 //=======================================================================
1557 Standard_Integer FSD_BinaryFile::WriteExtendedString (Standard_OStream& theOStream,
1558 const TCollection_ExtendedString& theString,
1559 const Standard_Boolean theOnlyCount)
1561 Standard_Integer aNumAndStrLen, anExtStrLen;
1562 anExtStrLen = theString.Length();
1564 aNumAndStrLen = anExtStrLen * sizeof(Standard_ExtCharacter);
1565 aNumAndStrLen += PutInteger (theOStream, anExtStrLen, theOnlyCount);
1567 if (anExtStrLen > 0 && !theOnlyCount)
1569 Standard_ExtString anExtStr;
1570 #if OCCT_BINARY_FILE_DO_INVERSE
1571 TCollection_ExtendedString aCopy = theString;
1572 anExtStr = aCopy.ToExtString();
1574 Standard_PExtCharacter pChar;
1576 pChar = (Standard_PExtCharacter)anExtStr;
1578 for (Standard_Integer i = 0; i < anExtStrLen; i++)
1580 pChar[i] = InverseExtChar (pChar[i]);
1583 anExtStr = theString.ToExtString();
1586 theOStream.write((char*)anExtStr, sizeof(Standard_ExtCharacter)*theString.Length());
1587 if (theOStream.fail())
1589 throw Storage_StreamWriteError();
1593 return aNumAndStrLen;
1596 //=======================================================================
1597 //function : ReadExtendedString
1598 //purpose : read string from the current position.
1599 //=======================================================================
1601 void FSD_BinaryFile::ReadExtendedString(TCollection_ExtendedString& aString)
1603 Standard_Integer size = 0;
1607 Standard_ExtCharacter *c = (Standard_ExtCharacter *)
1608 Standard::Allocate((size+1) * sizeof(Standard_ExtCharacter));
1609 if (!fread(c,size*sizeof(Standard_ExtCharacter),1,myStream))
1610 throw Storage_StreamWriteError();
1612 #if OCCT_BINARY_FILE_DO_INVERSE
1613 for (Standard_Integer i=0; i < size; i++)
1614 c[i] = InverseExtChar (c[i]);
1624 //=======================================================================
1625 //function : ReadExtendedString
1626 //purpose : read string from the current position.
1627 //=======================================================================
1628 void FSD_BinaryFile::ReadExtendedString (Standard_IStream& theIStream, TCollection_ExtendedString& aString)
1630 Standard_Integer size = 0;
1632 GetInteger (theIStream, size);
1636 Standard_ExtCharacter *c = (Standard_ExtCharacter *)Standard::Allocate((size+1) * sizeof(Standard_ExtCharacter));
1638 if (!theIStream.good())
1640 throw Storage_StreamReadError();
1643 const std::streamsize aNbBytes = std::streamsize(sizeof(Standard_ExtCharacter) * size);
1644 theIStream.read ((char *)c, aNbBytes);
1645 if (theIStream.gcount() != aNbBytes)
1647 throw Storage_StreamReadError();
1652 #if OCCT_BINARY_FILE_DO_INVERSE
1653 for (Standard_Integer i=0; i < size; i++)
1655 c[i] = InverseExtChar (c[i]);
1667 //=======================================================================
1668 //function : WriteHeader
1670 //=======================================================================
1672 void FSD_BinaryFile::WriteHeader()
1674 PutInteger(myHeader.testindian);
1675 PutInteger(myHeader.binfo);
1676 PutInteger(myHeader.einfo);
1677 PutInteger(myHeader.bcomment);
1678 PutInteger(myHeader.ecomment);
1679 PutInteger(myHeader.btype);
1680 PutInteger(myHeader.etype);
1681 PutInteger(myHeader.broot);
1682 PutInteger(myHeader.eroot);
1683 PutInteger(myHeader.bref);
1684 PutInteger(myHeader.eref);
1685 PutInteger(myHeader.bdata);
1686 PutInteger(myHeader.edata);
1689 //=======================================================================
1690 //function : WriteHeader
1692 //=======================================================================
1693 Standard_Integer FSD_BinaryFile::WriteHeader (Standard_OStream& theOStream,
1694 const FSD_FileHeader& theHeader,
1695 const Standard_Boolean theOnlyCount)
1697 Standard_Integer aHeaderSize = 0;
1699 aHeaderSize += PutInteger (theOStream, theHeader.testindian, theOnlyCount);
1700 aHeaderSize += PutInteger (theOStream, theHeader.binfo, theOnlyCount);
1701 aHeaderSize += PutInteger (theOStream, theHeader.einfo, theOnlyCount);
1702 aHeaderSize += PutInteger (theOStream, theHeader.bcomment, theOnlyCount);
1703 aHeaderSize += PutInteger (theOStream, theHeader.ecomment, theOnlyCount);
1704 aHeaderSize += PutInteger (theOStream, theHeader.btype, theOnlyCount);
1705 aHeaderSize += PutInteger (theOStream, theHeader.etype, theOnlyCount);
1706 aHeaderSize += PutInteger (theOStream, theHeader.broot, theOnlyCount);
1707 aHeaderSize += PutInteger (theOStream, theHeader.eroot, theOnlyCount);
1708 aHeaderSize += PutInteger (theOStream, theHeader.bref, theOnlyCount);
1709 aHeaderSize += PutInteger (theOStream, theHeader.eref, theOnlyCount);
1710 aHeaderSize += PutInteger (theOStream, theHeader.bdata, theOnlyCount);
1711 aHeaderSize += PutInteger (theOStream, theHeader.edata, theOnlyCount);
1716 //=======================================================================
1717 //function : ReadHeader
1719 //=======================================================================
1721 void FSD_BinaryFile::ReadHeader()
1723 GetInteger(myHeader.testindian);
1724 GetInteger(myHeader.binfo);
1725 GetInteger(myHeader.einfo);
1726 GetInteger(myHeader.bcomment);
1727 GetInteger(myHeader.ecomment);
1728 GetInteger(myHeader.btype);
1729 GetInteger(myHeader.etype);
1730 GetInteger(myHeader.broot);
1731 GetInteger(myHeader.eroot);
1732 GetInteger(myHeader.bref);
1733 GetInteger(myHeader.eref);
1734 GetInteger(myHeader.bdata);
1735 GetInteger(myHeader.edata);
1738 //=======================================================================
1739 //function : ReadHeader
1741 //=======================================================================
1743 void FSD_BinaryFile::ReadHeader(Standard_IStream& theIStream, FSD_FileHeader& theFileHeader)
1745 GetInteger (theIStream, theFileHeader.testindian);
1746 GetInteger (theIStream, theFileHeader.binfo);
1747 GetInteger (theIStream, theFileHeader.einfo);
1748 GetInteger (theIStream, theFileHeader.bcomment);
1749 GetInteger (theIStream, theFileHeader.ecomment);
1750 GetInteger (theIStream, theFileHeader.btype);
1751 GetInteger (theIStream, theFileHeader.etype);
1752 GetInteger (theIStream, theFileHeader.broot);
1753 GetInteger (theIStream, theFileHeader.eroot);
1754 GetInteger (theIStream, theFileHeader.bref);
1755 GetInteger (theIStream, theFileHeader.eref);
1756 GetInteger (theIStream, theFileHeader.bdata);
1757 GetInteger (theIStream, theFileHeader.edata);
1760 //=======================================================================
1761 //function : ReadHeaderData
1763 //=======================================================================
1764 void FSD_BinaryFile::ReadHeaderData( Standard_IStream& theIStream, const Handle(Storage_HeaderData)& theHeaderData )
1767 TCollection_AsciiString uinfo,mStorageVersion,mDate,mSchemaName,mSchemaVersion,mApplicationVersion;
1768 TCollection_ExtendedString mApplicationName,mDataType;
1769 TColStd_SequenceOfAsciiString mUserInfo;
1770 Standard_Integer mNBObj;
1772 FSD_BinaryFile::GetInteger (theIStream, mNBObj);
1773 FSD_BinaryFile::ReadString (theIStream, mStorageVersion);
1774 FSD_BinaryFile::ReadString (theIStream, mDate);
1775 FSD_BinaryFile::ReadString (theIStream, mSchemaName);
1776 FSD_BinaryFile::ReadString (theIStream, mSchemaVersion);
1777 FSD_BinaryFile::ReadExtendedString(theIStream, mApplicationName);
1778 FSD_BinaryFile::ReadString (theIStream, mApplicationVersion);
1779 FSD_BinaryFile::ReadExtendedString(theIStream, mDataType);
1781 Standard_Integer len = 0;
1782 TCollection_AsciiString line;
1784 FSD_BinaryFile::GetInteger(theIStream, len);
1786 for (Standard_Integer i = 1; i <= len && theIStream.good(); i++)
1788 FSD_BinaryFile::ReadString (theIStream, line);
1789 mUserInfo.Append(line);
1792 theHeaderData->SetNumberOfObjects(mNBObj);
1793 theHeaderData->SetStorageVersion(mStorageVersion);
1794 theHeaderData->SetCreationDate(mDate);
1795 theHeaderData->SetSchemaName(mSchemaName);
1796 theHeaderData->SetSchemaVersion(mSchemaVersion);
1797 theHeaderData->SetApplicationName(mApplicationName);
1798 theHeaderData->SetApplicationVersion(mApplicationVersion);
1799 theHeaderData->SetDataType(mDataType);
1801 for (Standard_Integer i = 1; i <= mUserInfo.Length(); i++) {
1802 theHeaderData->AddToUserInfo(mUserInfo.Value(i));
1806 //=======================================================================
1808 //purpose : return position in the file. Return -1 upon error.
1809 //=======================================================================
1811 Storage_Position FSD_BinaryFile::Tell()
1813 return (Storage_Position) ftell(myStream);
1816 //=======================================================================
1817 //function : InverseReal
1818 //purpose : Inverses bytes in the real value
1819 //=======================================================================
1821 Standard_Real FSD_BinaryFile::InverseReal (const Standard_Real theValue)
1823 Standard_STATIC_ASSERT(sizeof(Standard_Real) == 2 * sizeof(Standard_Integer));
1825 Standard_Integer i[2];
1826 Standard_Real aValue;
1829 aWrapUnion.aValue = theValue;
1831 Standard_Integer aTemp = aWrapUnion.i[1];
1832 aWrapUnion.i[1] = InverseInt(aWrapUnion.i[0]);
1833 aWrapUnion.i[0] = InverseInt(aTemp);
1835 return aWrapUnion.aValue;
1838 //=======================================================================
1839 //function : InverseShortReal
1840 //purpose : Inverses bytes in the short real value
1841 //=======================================================================
1843 Standard_ShortReal FSD_BinaryFile::InverseShortReal (const Standard_ShortReal theValue)
1845 Standard_STATIC_ASSERT(sizeof(Standard_ShortReal) == sizeof(Standard_Integer));
1847 Standard_ShortReal aValue;
1848 Standard_Integer aResult;
1851 aWrapUnion.aValue = theValue;
1852 aWrapUnion.aResult = InverseInt (aWrapUnion.aResult);
1854 return aWrapUnion.aValue;
1857 //=======================================================================
1858 //function : InverseSize
1859 //purpose : Inverses bytes in size_t type instance
1860 //=======================================================================
1863 inline uint64_t OCCT_InverseSizeSpecialized (const uint64_t theValue, int);
1866 inline uint64_t OCCT_InverseSizeSpecialized <4> (const uint64_t theValue, int)
1868 return FSD_BinaryFile::InverseInt(static_cast<Standard_Integer>(theValue));
1872 inline uint64_t OCCT_InverseSizeSpecialized <8> (const uint64_t theValue, int)
1875 Standard_Integer i[2];
1879 aWrapUnion.aValue = theValue;
1881 Standard_Integer aTemp = aWrapUnion.i[1];
1882 aWrapUnion.i[1] = FSD_BinaryFile::InverseInt(aWrapUnion.i[0]);
1883 aWrapUnion.i[0] = FSD_BinaryFile::InverseInt(aTemp);
1885 return aWrapUnion.aValue;
1888 Standard_Size FSD_BinaryFile::InverseSize (const Standard_Size theValue)
1890 return (Standard_Size) OCCT_InverseSizeSpecialized <sizeof(Standard_Size)> (theValue, 0);
1893 uint64_t FSD_BinaryFile::InverseUint64 (const uint64_t theValue)
1895 return OCCT_InverseSizeSpecialized <sizeof(uint64_t)> (theValue, 0);