0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / FSD / FSD_BinaryFile.cxx
CommitLineData
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
42cf5bc1 15
16#include <FSD_BinaryFile.hxx>
7fd59977 17#include <OSD.hxx>
94708556 18#include <OSD_OpenFile.hxx>
42cf5bc1 19#include <Storage_BaseDriver.hxx>
4ff92abe 20#include <Storage_HArrayOfCallBack.hxx>
21#include <Storage_HeaderData.hxx>
22#include <Storage_InternalData.hxx>
23#include <Storage_RootData.hxx>
42cf5bc1 24#include <Storage_StreamExtCharParityError.hxx>
25#include <Storage_StreamFormatError.hxx>
26#include <Storage_StreamTypeMismatchError.hxx>
27#include <Storage_StreamWriteError.hxx>
4ff92abe 28#include <Storage_TypeData.hxx>
42cf5bc1 29#include <TCollection_AsciiString.hxx>
30#include <TCollection_ExtendedString.hxx>
10a4116e 31#include <Standard_Assert.hxx>
7fd59977 32
33const Standard_CString MAGICNUMBER = "BINFILE";
7fd59977 34
39c8dc70 35IMPLEMENT_STANDARD_RTTIEXT(FSD_BinaryFile,Storage_BaseDriver)
36
7fd59977 37//=======================================================================
38//function : FSD_BinaryFile
39//purpose :
40//=======================================================================
41
42FSD_BinaryFile::FSD_BinaryFile() :
43myStream(0L)
44{
45 myHeader.testindian = -1;
46 myHeader.binfo = -1;
47 myHeader.einfo = -1;
48 myHeader.bcomment = -1;
49 myHeader.ecomment = -1;
50 myHeader.btype = -1;
51 myHeader.etype = -1;
52 myHeader.broot = -1;
53 myHeader.eroot = -1;
54 myHeader.bref = -1;
55 myHeader.eref = -1;
56 myHeader.bdata = -1;
57 myHeader.edata = -1;
58}
59
60//=======================================================================
61//function : IsGoodFileType
62//purpose : INFO SECTION
63// write
64//=======================================================================
65
66Storage_Error FSD_BinaryFile::IsGoodFileType(const TCollection_AsciiString& aName)
67{
68 FSD_BinaryFile f;
69 Storage_Error s;
70
71 s = f.Open(aName,Storage_VSRead);
72
73 if (s == Storage_VSOk) {
74 TCollection_AsciiString l;
fb8a7358 75 Standard_Size len = strlen(FSD_BinaryFile::MagicNumber());
7fd59977 76
77 f.ReadChar(l,len);
78
79 f.Close();
80
81 if (strncmp(FSD_BinaryFile::MagicNumber(),l.ToCString(),len) != 0) {
82 s = Storage_VSFormatError;
83 }
84 }
85
86 return s;
87}
88
89//=======================================================================
90//function : Open
91//purpose :
92//=======================================================================
93
94Storage_Error FSD_BinaryFile::Open(const TCollection_AsciiString& aName,const Storage_OpenMode aMode)
95{
96 Storage_Error result = Storage_VSOk;
97
98 SetName(aName);
99
100 if (OpenMode() == Storage_VSNone) {
d9ff84e8 101 if (aMode == Storage_VSRead) {
94708556 102 myStream = OSD_OpenFile(aName.ToCString(),"rb");
d9ff84e8 103 }
104 else if (aMode == Storage_VSWrite) {
94708556 105 myStream = OSD_OpenFile(aName.ToCString(),"wb");
d9ff84e8 106 }
107 else if (aMode == Storage_VSReadWrite) {
94708556 108 myStream = OSD_OpenFile(aName.ToCString(),"w+b");
d9ff84e8 109 }
7fd59977 110
111 if (myStream == 0L) {
112 result = Storage_VSOpenError;
113 }
114 else {
115 SetOpenMode(aMode);
116 }
117 }
118 else {
119 result = Storage_VSAlreadyOpen;
120 }
121
122 return result;
123}
124
125//=======================================================================
126//function : IsEnd
127//purpose :
128//=======================================================================
129
130Standard_Boolean FSD_BinaryFile::IsEnd()
131{
132 return (feof(myStream) != 0);
133}
134
135//=======================================================================
136//function : Close
137//purpose :
138//=======================================================================
139
140Storage_Error FSD_BinaryFile::Close()
141{
142 Storage_Error result = Storage_VSOk;
143
144 if (OpenMode() != Storage_VSNone) {
145 fclose(myStream);
146 SetOpenMode(Storage_VSNone);
147 }
148 else {
149 result = Storage_VSNotOpen;
150 }
151
152 return result;
153}
154
155//=======================================================================
156//function : MagicNumber
157//purpose : ------------------ PROTECTED
158//=======================================================================
159
487bf1ce 160Standard_CString FSD_BinaryFile::MagicNumber()
7fd59977 161{
162 return MAGICNUMBER;
163}
164
165//=======================================================================
166//function : ReadChar
167//purpose : read <rsize> character from the current position.
168//=======================================================================
169
fb8a7358 170void FSD_BinaryFile::ReadChar(TCollection_AsciiString& buffer, const Standard_Size rsize)
7fd59977 171{
172 char c;
fb8a7358 173 Standard_Size ccount = 0;
7fd59977 174
175 buffer.Clear();
176
177 while (!IsEnd() && (ccount < rsize)) {
98160038 178 ccount += fread(&c, sizeof(char),1, myStream);
7fd59977 179 buffer += c;
7fd59977 180 }
181}
182
183//=======================================================================
184//function : SkipObject
185//purpose :
186//=======================================================================
187
188void FSD_BinaryFile::SkipObject()
189{
190
191}
192
193//=======================================================================
194//function : PutReference
195//purpose : ---------------------- PUBLIC : PUT
196//=======================================================================
197
198Storage_BaseDriver& FSD_BinaryFile::PutReference(const Standard_Integer aValue)
199{
10a4116e 200#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 201 Standard_Integer t = InverseInt (aValue);
202
9775fa61 203 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
7fd59977 204#else
9775fa61 205 if (!fwrite(&aValue,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
7fd59977 206#endif
207 return *this;
208}
209
210//=======================================================================
211//function : PutCharacter
212//purpose :
213//=======================================================================
214
215Storage_BaseDriver& FSD_BinaryFile::PutCharacter(const Standard_Character aValue)
216{
9775fa61 217 if (!fwrite(&aValue,sizeof(Standard_Character),1,myStream)) throw Storage_StreamWriteError();
7fd59977 218 return *this;
219}
220
221//=======================================================================
222//function : PutExtCharacter
223//purpose :
224//=======================================================================
225
226Storage_BaseDriver& FSD_BinaryFile::PutExtCharacter(const Standard_ExtCharacter aValue)
227{
10a4116e 228#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 229 Standard_ExtCharacter t = InverseExtChar (aValue);
230
9775fa61 231 if (!fwrite(&t,sizeof(Standard_ExtCharacter),1,myStream)) throw Storage_StreamWriteError();
7fd59977 232#else
9775fa61 233 if (!fwrite(&aValue,sizeof(Standard_ExtCharacter),1,myStream)) throw Storage_StreamWriteError();
7fd59977 234#endif
235 return *this;
236}
237
238//=======================================================================
239//function : PutInteger
240//purpose :
241//=======================================================================
242
243Storage_BaseDriver& FSD_BinaryFile::PutInteger(const Standard_Integer aValue)
244{
10a4116e 245#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 246 Standard_Integer t = InverseInt (aValue);
247
9775fa61 248 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
7fd59977 249#else
9775fa61 250 if (!fwrite(&aValue,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
7fd59977 251#endif
252
253 return *this;
254}
255
4ff92abe 256//=======================================================================
257//function : PutInteger
258//purpose :
259//=======================================================================
260Standard_Integer FSD_BinaryFile::PutInteger (Standard_OStream& theOStream,
261 const Standard_Integer theValue,
262 const Standard_Boolean theOnlyCount)
263{
264#if OCCT_BINARY_FILE_DO_INVERSE
265 Standard_Integer t = InverseInt (theValue);
266#else
267 Standard_Integer t = theValue;
268#endif
269
270 if (!theOnlyCount)
271 {
272 theOStream.write ((char*)&t, sizeof(Standard_Integer));
273 if (theOStream.fail())
274 {
9775fa61 275 throw Storage_StreamWriteError();
4ff92abe 276 }
277 }
278
279 return sizeof(Standard_Integer);
280}
281
7fd59977 282//=======================================================================
283//function : PutBoolean
284//purpose :
285//=======================================================================
286
287Storage_BaseDriver& FSD_BinaryFile::PutBoolean(const Standard_Boolean aValue)
288{
10a4116e 289#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 290 Standard_Integer t = InverseInt ((Standard_Integer) aValue);
7fd59977 291#else
dde68833 292 Standard_Integer t = aValue ? 1 : 0;
7fd59977 293#endif
9775fa61 294 if (!fwrite(&t,sizeof(Standard_Integer),1,myStream)) throw Storage_StreamWriteError();
7fd59977 295 return *this;
296}
297
298//=======================================================================
299//function : PutReal
300//purpose :
301//=======================================================================
302
303Storage_BaseDriver& FSD_BinaryFile::PutReal(const Standard_Real aValue)
304{
10a4116e 305#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 306 Standard_Real t = InverseReal (aValue);
307
9775fa61 308 if (!fwrite(&t,sizeof(Standard_Real),1,myStream)) throw Storage_StreamWriteError();
7fd59977 309#else
9775fa61 310 if (!fwrite(&aValue,sizeof(Standard_Real),1,myStream)) throw Storage_StreamWriteError();
7fd59977 311#endif
312 return *this;
313}
314
315//=======================================================================
316//function : PutShortReal
317//purpose :
318//=======================================================================
319
320Storage_BaseDriver& FSD_BinaryFile::PutShortReal(const Standard_ShortReal aValue)
321{
10a4116e 322#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 323 Standard_ShortReal t = InverseShortReal (aValue);
324
9775fa61 325 if (!fwrite(&t,sizeof(Standard_ShortReal),1,myStream)) throw Storage_StreamWriteError();
7fd59977 326#else
9775fa61 327 if (!fwrite(&aValue,sizeof(Standard_ShortReal),1,myStream)) throw Storage_StreamWriteError();
7fd59977 328#endif
329 return *this;
330}
331
332//=======================================================================
333//function : GetReference
334//purpose : ----------------- PUBLIC : GET
335//=======================================================================
336
337Storage_BaseDriver& FSD_BinaryFile::GetReference(Standard_Integer& aValue)
338{
339 if (!fread(&aValue,sizeof(Standard_Integer),1,myStream))
9775fa61 340 throw Storage_StreamTypeMismatchError();
10a4116e 341#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 342 aValue = InverseInt (aValue);
343#endif
344 return *this;
345}
346
4ff92abe 347//=======================================================================
348//function : GetReference
349//purpose : ----------------- PUBLIC : GET
350//=======================================================================
351void FSD_BinaryFile::GetReference(Standard_IStream& theIStream, Standard_Integer& aValue)
352{
353 theIStream.read ((char*)&aValue, sizeof(Standard_Integer));
354
355 if (theIStream.gcount() != sizeof(Standard_Integer))
356 {
9775fa61 357 throw Storage_StreamTypeMismatchError();
4ff92abe 358 }
359
360#if OCCT_BINARY_FILE_DO_INVERSE
361 aValue = InverseInt (aValue);
362#endif
363}
364
7fd59977 365//=======================================================================
366//function : GetCharacter
367//purpose :
368//=======================================================================
369
370Storage_BaseDriver& FSD_BinaryFile::GetCharacter(Standard_Character& aValue)
371{
372 if (!fread(&aValue,sizeof(Standard_Character),1,myStream))
9775fa61 373 throw Storage_StreamTypeMismatchError();
7fd59977 374 return *this;
375}
376
377//=======================================================================
378//function : GetExtCharacter
379//purpose :
380//=======================================================================
381
382Storage_BaseDriver& FSD_BinaryFile::GetExtCharacter(Standard_ExtCharacter& aValue)
383{
384 if (!fread(&aValue,sizeof(Standard_ExtCharacter),1,myStream))
9775fa61 385 throw Storage_StreamTypeMismatchError();
10a4116e 386#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 387 aValue = InverseExtChar (aValue);
388#endif
389 return *this;
390}
391
392//=======================================================================
393//function : GetInteger
394//purpose :
395//=======================================================================
396
397Storage_BaseDriver& FSD_BinaryFile::GetInteger(Standard_Integer& aValue)
398{
399 if (!fread(&aValue,sizeof(Standard_Integer),1,myStream))
9775fa61 400 throw Storage_StreamTypeMismatchError();
10a4116e 401#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 402 aValue = InverseInt (aValue);
403#endif
404 return *this;
405}
406
4ff92abe 407//=======================================================================
408//function : GetInteger
409//purpose :
410//=======================================================================
411void FSD_BinaryFile::GetInteger (Standard_IStream& theIStream, Standard_Integer& theValue)
412{
413
414 theIStream.read ((char*)&theValue, sizeof(Standard_Integer));
415
416 if (theIStream.gcount() != sizeof(Standard_Integer))
417 {
9775fa61 418 throw Storage_StreamTypeMismatchError();
4ff92abe 419 }
420
421#if OCCT_BINARY_FILE_DO_INVERSE
422 theValue = InverseInt (theValue);
423#endif
424}
425
7fd59977 426//=======================================================================
427//function : GetBoolean
428//purpose :
429//=======================================================================
430
431Storage_BaseDriver& FSD_BinaryFile::GetBoolean(Standard_Boolean& aValue)
432{
dde68833 433 Standard_Integer anInt = 0;
434 if (!fread(&anInt,sizeof(Standard_Integer),1,myStream))
9775fa61 435 throw Storage_StreamTypeMismatchError();
10a4116e 436#if OCCT_BINARY_FILE_DO_INVERSE
dde68833 437 anInt = InverseInt (anInt);
7fd59977 438#endif
dde68833 439 aValue = (anInt != 0);
7fd59977 440 return *this;
441}
442
443//=======================================================================
444//function : GetReal
445//purpose :
446//=======================================================================
447
448Storage_BaseDriver& FSD_BinaryFile::GetReal(Standard_Real& aValue)
449{
450 if (!fread(&aValue,sizeof(Standard_Real),1,myStream))
9775fa61 451 throw Storage_StreamTypeMismatchError();
10a4116e 452#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 453 aValue = InverseReal (aValue);
454#endif
455 return *this;
456}
457
458//=======================================================================
459//function : GetShortReal
460//purpose :
461//=======================================================================
462
463Storage_BaseDriver& FSD_BinaryFile::GetShortReal(Standard_ShortReal& aValue)
464{
465 if (!fread(&aValue,sizeof(Standard_ShortReal),1,myStream))
9775fa61 466 throw Storage_StreamTypeMismatchError();
10a4116e 467#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 468 aValue = InverseShortReal (aValue);
469#endif
470 return *this;
471}
472
473//=======================================================================
474//function : Destroy
475//purpose :
476//=======================================================================
477
478void FSD_BinaryFile::Destroy()
479{
480 if (OpenMode() != Storage_VSNone) {
481 Close();
482 }
483}
484
485//=======================================================================
486//function : BeginWriteInfoSection
487//purpose : -------------------------- INFO : WRITE
488//=======================================================================
489
490Storage_Error FSD_BinaryFile::BeginWriteInfoSection()
491{
10a4116e 492 union {
493 char ti2[4];
494 Standard_Integer aResult;
495 } aWrapUnion;
496
497 aWrapUnion.ti2[0] = 1;
498 aWrapUnion.ti2[1] = 2;
499 aWrapUnion.ti2[2] = 3;
500 aWrapUnion.ti2[3] = 4;
501
502 myHeader.testindian = aWrapUnion.aResult;
503
7fd59977 504 if (!fwrite(FSD_BinaryFile::MagicNumber(),
505 strlen(FSD_BinaryFile::MagicNumber()),
506 1,
507 myStream))
9775fa61 508 throw Storage_StreamWriteError();
7fd59977 509
57c5e9e8 510 myHeader.binfo = (Standard_Integer )ftell(myStream);
7fd59977 511 WriteHeader();
512
513 return Storage_VSOk;
514}
515
516//=======================================================================
517//function : WriteInfo
518//purpose :
519//=======================================================================
520
521void FSD_BinaryFile::WriteInfo(const Standard_Integer nbObj,
522 const TCollection_AsciiString& dbVersion,
523 const TCollection_AsciiString& date,
524 const TCollection_AsciiString& schemaName,
525 const TCollection_AsciiString& schemaVersion,
526 const TCollection_ExtendedString& appName,
527 const TCollection_AsciiString& appVersion,
528 const TCollection_ExtendedString& dataType,
529 const TColStd_SequenceOfAsciiString& userInfo)
530{
531 Standard_Integer i;
532
533 PutInteger(nbObj);
534 WriteString(dbVersion);
535 WriteString(date);
536 WriteString(schemaName);
537 WriteString(schemaVersion);
538 WriteExtendedString(appName);
539 WriteString(appVersion);
540 WriteExtendedString(dataType);
541 i = userInfo.Length();
542
543 PutInteger(i);
544 for (i = 1; i <= userInfo.Length(); i++) {
545 WriteString(userInfo.Value(i));
546 }
547}
548
4ff92abe 549//=======================================================================
550//function : WriteInfo
551//purpose :
552//=======================================================================
553Standard_Integer FSD_BinaryFile::WriteInfo (Standard_OStream& theOStream,
554 const Standard_Integer theObjNb,
555 const TCollection_AsciiString& theStoreVer,
556 const TCollection_AsciiString& theCreationDate,
557 const TCollection_AsciiString& theSchemaName,
558 const TCollection_AsciiString& theSchemaVersion,
559 const TCollection_ExtendedString& theAppName,
560 const TCollection_AsciiString& theAppVer,
561 const TCollection_ExtendedString& theDataType,
562 const TColStd_SequenceOfAsciiString& theUserInfo,
563 const Standard_Boolean theOnlyCount)
564{
565 Standard_Integer anInfoSize = 0;
566
567 anInfoSize += PutInteger (theOStream, theObjNb, theOnlyCount);
568 anInfoSize += WriteString(theOStream, theStoreVer, theOnlyCount);
569 anInfoSize += WriteString(theOStream, theCreationDate, theOnlyCount);
570 anInfoSize += WriteString(theOStream, theSchemaName, theOnlyCount);
571 anInfoSize += WriteString(theOStream, theSchemaVersion, theOnlyCount);
572 anInfoSize += WriteExtendedString(theOStream, theAppName, theOnlyCount);
573 anInfoSize += WriteString(theOStream, theAppVer, theOnlyCount);
574 anInfoSize += WriteExtendedString(theOStream, theDataType, theOnlyCount);
575
576 Standard_Integer i = theUserInfo.Length();
577 anInfoSize += PutInteger(theOStream, i, theOnlyCount);
578
579 for (i = 1; i <= theUserInfo.Length(); i++) {
580 anInfoSize += WriteString (theOStream, theUserInfo.Value(i), theOnlyCount);
581 }
582
583 return anInfoSize;
584}
585
7fd59977 586//=======================================================================
587//function : EndWriteInfoSection
588//purpose : read
589//=======================================================================
590
591Storage_Error FSD_BinaryFile::EndWriteInfoSection()
592{
57c5e9e8 593 myHeader.einfo = (Standard_Integer )ftell(myStream);
7fd59977 594 return Storage_VSOk;
595}
596
4ff92abe 597//=======================================================================
598//function : EndWriteInfoSection
599//purpose : read
600//=======================================================================
601Storage_Error FSD_BinaryFile::EndWriteInfoSection(Standard_OStream& theOStream)
602{
603 myHeader.einfo = (Standard_Integer)theOStream.tellp();
4ff92abe 604 return Storage_VSOk;
605}
606
7fd59977 607//=======================================================================
608//function : BeginReadInfoSection
609//purpose :
610//=======================================================================
611
612Storage_Error FSD_BinaryFile::BeginReadInfoSection()
613{
614 Storage_Error s = Storage_VSOk;
615 TCollection_AsciiString l;
fb8a7358 616 Standard_Size len = strlen(FSD_BinaryFile::MagicNumber());
7fd59977 617
618 ReadChar(l,len);
619
620 if (strncmp(FSD_BinaryFile::MagicNumber(),l.ToCString(),len) != 0) {
621 s = Storage_VSFormatError;
622 }
623 else {
624 ReadHeader();
625 }
626
627 return s;
628}
629
630//=======================================================================
631//function : ReadInfo
632//purpose : ------------------- INFO : READ
633//=======================================================================
634
635void 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)
644{
645 GetInteger(nbObj);
646 ReadString(dbVersion);
647 ReadString(date);
648 ReadString(schemaName);
649 ReadString(schemaVersion);
650 ReadExtendedString(appName);
651 ReadString(appVersion);
652 ReadExtendedString(dataType);
653
654 Standard_Integer i,len = 0;
655
656 GetInteger(len);
657 TCollection_AsciiString line;
658
659 for (i = 1; i <= len && !IsEnd(); i++) {
660 ReadString(line);
661 userInfo.Append(line);
662 }
663}
664
4ff92abe 665//=======================================================================
666//function : ReadInfo
667//purpose :
668//=======================================================================
669void FSD_BinaryFile::ReadCompleteInfo (Standard_IStream& theIStream, Handle(Storage_Data)& theData)
670{
671 FSD_FileHeader aHeaderPos;
672 ReadHeader(theIStream, aHeaderPos);
673
674 if (theData.IsNull())
675 {
676 theData = new Storage_Data();
677 }
678
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();
683
684 ReadHeaderData (theIStream, hData);
685
686 Handle(Storage_HArrayOfCallBack) theCallBack;
687
688 while (theIStream.good() && !theIStream.eof())
689 {
690 Standard_Integer aPos = (Standard_Integer)theIStream.tellg();
691
692 if (aPos >= aHeaderPos.edata)
693 {
694 break;
695 }
696 else if (aPos == aHeaderPos.bcomment)
697 {
698 TColStd_SequenceOfExtendedString mComment;
699 ReadComment (theIStream, mComment);
700
701 for (Standard_Integer i = 1; i <= mComment.Length(); i++)
702 {
703 hData->AddToComments (mComment.Value(i));
704 }
705
706 iData->ReadArray() = new Storage_HPArray(1, theData->NumberOfObjects());
707 }
708 else if (aPos == aHeaderPos.btype)
709 {
710 Standard_Integer aTypeSectionSize = TypeSectionSize (theIStream);
711 theCallBack = new Storage_HArrayOfCallBack (1, aTypeSectionSize);
712
713 TCollection_AsciiString aTypeName;
714 Standard_Integer aTypeNum;
715
716 for (Standard_Integer i = 1; i <= aTypeSectionSize; i++)
717 {
718 ReadTypeInformations (theIStream, aTypeNum, aTypeName);
719 tData->AddType (aTypeName,aTypeNum);
720
721 theCallBack->SetValue (aTypeNum, NULL);
722 }
723 }
724 else if (aPos == aHeaderPos.broot)
725 {
726 Standard_Integer aRootSectionSize = RootSectionSize(theIStream);
727
728 Standard_Integer aRef;
729 TCollection_AsciiString aRootName, aTypeName;
730 Handle(Storage_Root) aRoot;
731 Handle(Standard_Persistent) aPer;
732
733 for (Standard_Integer i = 1; i <= aRootSectionSize; i++)
734 {
735 ReadRoot (theIStream, aRootName, aRef, aTypeName);
736
737 aRoot = new Storage_Root(aRootName, aPer);
738 aRoot->SetReference(aRef);
739 aRoot->SetType(aTypeName);
740 rData->AddRoot(aRoot);
741 }
742 }
743 else if (aPos == aHeaderPos.bref)
744 {
745 Standard_Integer aRefSectionSize = RefSectionSize (theIStream);
746
747 Standard_Integer aTypeNum, aRef = 0;
748
749 for (Standard_Integer i = 1; i <= aRefSectionSize; i++)
750 {
751 ReadReferenceType (theIStream, aRef, aTypeNum);
752
753 iData->ReadArray()->ChangeValue(aRef) = theCallBack->Value(aTypeNum)->New();
754
755 if (!iData->ReadArray()->ChangeValue(aRef).IsNull())
756 {
757 iData->ReadArray()->ChangeValue(aRef)->TypeNum() = aTypeNum;
758 }
759 }
760 }
761 else if (aPos == aHeaderPos.bdata)
762 {
763 //
764 }
765 }
766
767 Handle(Storage_HSeqOfRoot) aRoots = rData->Roots();
768 for(Standard_Integer i = 1; i <= theData->NumberOfRoots(); i++)
769 {
770 const Handle(Storage_Root)& aCurRoot = aRoots->Value(i);
771 rData->UpdateRoot (aCurRoot->Name(), iData->ReadArray()->Value (aCurRoot->Reference()));
772 }
773
774 iData->Clear();
775}
776
7fd59977 777//=======================================================================
778//function : EndReadInfoSection
779//purpose : COMMENTS SECTION
780// write
781//=======================================================================
782
783Storage_Error FSD_BinaryFile::EndReadInfoSection()
784{
785 if (!fseek(myStream,myHeader.einfo,SEEK_SET)) return Storage_VSOk;
786 else return Storage_VSSectionNotFound;
787}
788
789//=======================================================================
790//function : BeginWriteCommentSection
791//purpose : ---------------- COMMENTS : WRITE
792//=======================================================================
793
794Storage_Error FSD_BinaryFile::BeginWriteCommentSection()
795{
57c5e9e8 796 myHeader.bcomment = (Standard_Integer )ftell(myStream);
7fd59977 797 return Storage_VSOk;
798}
799
4ff92abe 800//=======================================================================
801//function : BeginWriteCommentSection
802//purpose :
803//=======================================================================
804Storage_Error FSD_BinaryFile::BeginWriteCommentSection(Standard_OStream& theOStream)
805{
806 myHeader.bcomment = (Standard_Integer)theOStream.tellp();
807 return Storage_VSOk;
808}
809
7fd59977 810//=======================================================================
811//function : WriteComment
812//purpose :
813//=======================================================================
814
815void FSD_BinaryFile::WriteComment(const TColStd_SequenceOfExtendedString& aCom)
816{
817 Standard_Integer i,aSize;
818
819 aSize = aCom.Length();
820 PutInteger(aSize);
821 for (i = 1; i <= aSize; i++) {
822 WriteExtendedString(aCom.Value(i));
823 }
824}
825
4ff92abe 826//=======================================================================
827//function : WriteComment
828//purpose :
829//=======================================================================
830Standard_Integer FSD_BinaryFile::WriteComment (Standard_OStream& theOStream,
831 const TColStd_SequenceOfExtendedString& theComments,
832 const Standard_Boolean theOnlyCount)
833{
834 Standard_Integer aCommentSize = 0;
835
836 Standard_Integer aSize = theComments.Length();
837 aCommentSize += PutInteger(theOStream, aSize, theOnlyCount);
838
839 for (Standard_Integer i = 1; i <= aSize; i++) {
840 aCommentSize += WriteExtendedString (theOStream, theComments.Value(i), theOnlyCount);
841 }
842
843 return aCommentSize;
844}
845
7fd59977 846//=======================================================================
847//function : EndWriteCommentSection
848//purpose : read
849//=======================================================================
850
851Storage_Error FSD_BinaryFile::EndWriteCommentSection()
852{
57c5e9e8 853 myHeader.ecomment = (Standard_Integer )ftell(myStream);
7fd59977 854 return Storage_VSOk;
855}
856
4ff92abe 857//=======================================================================
858//function : EndWriteCommentSection
859//purpose : read
860//=======================================================================
861Storage_Error FSD_BinaryFile::EndWriteCommentSection (Standard_OStream& theOStream)
862{
863 myHeader.ecomment = (Standard_Integer)theOStream.tellp();
864
865 return Storage_VSOk;
866}
867
7fd59977 868//=======================================================================
869//function : BeginReadCommentSection
870//purpose : ---------------- COMMENTS : READ
871//=======================================================================
872
873Storage_Error FSD_BinaryFile::BeginReadCommentSection()
874{
875 if (!fseek(myStream,myHeader.bcomment,SEEK_SET)) return Storage_VSOk;
876 else return Storage_VSSectionNotFound;
877}
878
879//=======================================================================
880//function : ReadComment
881//purpose :
882//=======================================================================
883
884void FSD_BinaryFile::ReadComment(TColStd_SequenceOfExtendedString& aCom)
885{
886 TCollection_ExtendedString line;
887 Standard_Integer len,i;
888
889 GetInteger(len);
890 for (i = 1; i <= len && !IsEnd(); i++) {
891 ReadExtendedString(line);
892 aCom.Append(line);
893 }
894}
895
4ff92abe 896//=======================================================================
897//function : ReadComment
898//purpose :
899//=======================================================================
900void FSD_BinaryFile::ReadComment (Standard_IStream& theIStream, TColStd_SequenceOfExtendedString& aCom)
901{
902 TCollection_ExtendedString line;
903 Standard_Integer len,i;
904
905 GetInteger(theIStream, len);
906 for (i = 1; i <= len && theIStream.good(); i++)
907 {
908 ReadExtendedString(theIStream, line);
909 aCom.Append(line);
910 }
911}
912
7fd59977 913//=======================================================================
914//function : EndReadCommentSection
915//purpose :
916//=======================================================================
917
918Storage_Error FSD_BinaryFile::EndReadCommentSection()
919{
920 if (!fseek(myStream,myHeader.ecomment,SEEK_SET)) return Storage_VSOk;
921 else return Storage_VSSectionNotFound;
922}
923
924//=======================================================================
925//function : BeginWriteTypeSection
926//purpose : --------------- TYPE : WRITE
927//=======================================================================
928
929Storage_Error FSD_BinaryFile::BeginWriteTypeSection()
930{
57c5e9e8 931 myHeader.btype = (Standard_Integer )ftell(myStream);
7fd59977 932 return Storage_VSOk;
933}
934
935//=======================================================================
936//function : SetTypeSectionSize
937//purpose :
938//=======================================================================
939
940void FSD_BinaryFile::SetTypeSectionSize(const Standard_Integer aSize)
941{
942 PutInteger(aSize);
943}
944
945//=======================================================================
946//function : WriteTypeInformations
947//purpose :
948//=======================================================================
949
950void FSD_BinaryFile::WriteTypeInformations(const Standard_Integer typeNum,
951 const TCollection_AsciiString& typeName)
952{
953 PutInteger(typeNum);
954 WriteString(typeName);
955}
956
957//=======================================================================
958//function : EndWriteTypeSection
959//purpose : read
960//=======================================================================
961
962Storage_Error FSD_BinaryFile::EndWriteTypeSection()
963{
57c5e9e8 964 myHeader.etype = (Standard_Integer )ftell(myStream);
7fd59977 965 return Storage_VSOk;
966}
967
968//=======================================================================
969//function : BeginReadTypeSection
970//purpose : ------------------- TYPE : READ
971//=======================================================================
972
973Storage_Error FSD_BinaryFile::BeginReadTypeSection()
974{
975 if (!fseek(myStream,myHeader.btype,SEEK_SET)) return Storage_VSOk;
976 else return Storage_VSSectionNotFound;
977}
978
979//=======================================================================
980//function : TypeSectionSize
981//purpose :
982//=======================================================================
983
984Standard_Integer FSD_BinaryFile::TypeSectionSize()
985{
986 Standard_Integer i;
987
988 GetInteger(i);
989 return i;
990}
991
4ff92abe 992//=======================================================================
993//function : TypeSectionSize
994//purpose :
995//=======================================================================
996Standard_Integer FSD_BinaryFile::TypeSectionSize(Standard_IStream& theIStream)
997{
998 Standard_Integer i;
999
1000 GetInteger(theIStream, i);
1001 return i;
1002}
1003
7fd59977 1004//=======================================================================
1005//function : ReadTypeInformations
1006//purpose :
1007//=======================================================================
1008
1009void FSD_BinaryFile::ReadTypeInformations(Standard_Integer& typeNum,TCollection_AsciiString& typeName)
1010{
1011 GetInteger(typeNum);
1012 ReadString(typeName);
1013}
1014
4ff92abe 1015//=======================================================================
1016//function : ReadTypeInformations
1017//purpose :
1018//=======================================================================
1019void FSD_BinaryFile::ReadTypeInformations(Standard_IStream& theIStream, Standard_Integer& typeNum,TCollection_AsciiString& typeName)
1020{
1021 GetInteger(theIStream, typeNum);
1022 ReadString(theIStream, typeName);
1023}
1024
7fd59977 1025//=======================================================================
1026//function : EndReadTypeSection
1027//purpose : ROOT SECTION
1028// write
1029//=======================================================================
1030
1031Storage_Error FSD_BinaryFile::EndReadTypeSection()
1032{
1033 if (!fseek(myStream,myHeader.etype,SEEK_SET)) return Storage_VSOk;
1034 else return Storage_VSSectionNotFound;
1035}
1036
1037//=======================================================================
1038//function : BeginWriteRootSection
1039//purpose : -------------------- ROOT : WRITE
1040//=======================================================================
1041
1042Storage_Error FSD_BinaryFile::BeginWriteRootSection()
1043{
57c5e9e8 1044 myHeader.broot = (Standard_Integer )ftell(myStream);
7fd59977 1045 return Storage_VSOk;
1046}
1047
1048//=======================================================================
1049//function : SetRootSectionSize
1050//purpose :
1051//=======================================================================
1052
1053void FSD_BinaryFile::SetRootSectionSize(const Standard_Integer aSize)
1054{
1055 PutInteger(aSize);
1056}
1057
1058//=======================================================================
1059//function : WriteRoot
1060//purpose :
1061//=======================================================================
1062
1063void FSD_BinaryFile::WriteRoot(const TCollection_AsciiString& rootName, const Standard_Integer aRef, const TCollection_AsciiString& rootType)
1064{
1065 PutReference(aRef);
1066 WriteString(rootName);
1067 WriteString(rootType);
1068}
1069
1070//=======================================================================
1071//function : EndWriteRootSection
1072//purpose : read
1073//=======================================================================
1074
1075Storage_Error FSD_BinaryFile::EndWriteRootSection()
1076{
57c5e9e8 1077 myHeader.eroot = (Standard_Integer )ftell(myStream);
7fd59977 1078 return Storage_VSOk;
1079}
1080
1081//=======================================================================
1082//function : BeginReadRootSection
1083//purpose : ----------------------- ROOT : READ
1084//=======================================================================
1085
1086Storage_Error FSD_BinaryFile::BeginReadRootSection()
1087{
1088 if (!fseek(myStream,myHeader.broot,SEEK_SET)) return Storage_VSOk;
1089 else return Storage_VSSectionNotFound;
1090}
1091
1092//=======================================================================
1093//function : RootSectionSize
1094//purpose :
1095//=======================================================================
1096
1097Standard_Integer FSD_BinaryFile::RootSectionSize()
1098{
1099 Standard_Integer i;
1100
1101 GetInteger(i);
1102 return i;
1103}
1104
4ff92abe 1105//=======================================================================
1106//function : RootSectionSize
1107//purpose :
1108//=======================================================================
1109Standard_Integer FSD_BinaryFile::RootSectionSize (Standard_IStream& theIStream)
1110{
1111 Standard_Integer i;
1112
1113 GetInteger(theIStream, i);
1114 return i;
1115}
1116
7fd59977 1117//=======================================================================
1118//function : ReadRoot
1119//purpose :
1120//=======================================================================
1121
1122void FSD_BinaryFile::ReadRoot(TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType)
1123{
1124 GetReference(aRef);
1125 ReadString(rootName);
1126 ReadString(rootType);
1127}
1128
4ff92abe 1129//=======================================================================
1130//function : ReadRoot
1131//purpose :
1132//=======================================================================
1133void FSD_BinaryFile::ReadRoot (Standard_IStream& theIStream, TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType)
1134{
1135 GetReference(theIStream, aRef);
1136 ReadString(theIStream, rootName);
1137 ReadString(theIStream, rootType);
1138}
1139
7fd59977 1140//=======================================================================
1141//function : EndReadRootSection
1142//purpose : REF SECTION
1143// write
1144//=======================================================================
1145
1146Storage_Error FSD_BinaryFile::EndReadRootSection()
1147{
1148 if (!fseek(myStream,myHeader.eroot,SEEK_SET)) return Storage_VSOk;
1149 else return Storage_VSSectionNotFound;
1150}
1151
1152//=======================================================================
1153//function : BeginWriteRefSection
1154//purpose : -------------------------- REF : WRITE
1155//=======================================================================
1156
1157Storage_Error FSD_BinaryFile::BeginWriteRefSection()
1158{
57c5e9e8 1159 myHeader.bref = (Standard_Integer )ftell(myStream);
7fd59977 1160 return Storage_VSOk;
1161}
1162
1163//=======================================================================
1164//function : SetRefSectionSize
1165//purpose :
1166//=======================================================================
1167
1168void FSD_BinaryFile::SetRefSectionSize(const Standard_Integer aSize)
1169{
1170 PutInteger(aSize);
1171}
1172
1173//=======================================================================
1174//function : WriteReferenceType
1175//purpose :
1176//=======================================================================
1177
1178void FSD_BinaryFile::WriteReferenceType(const Standard_Integer reference,const Standard_Integer typeNum)
1179{
1180 PutReference(reference);
1181 PutInteger(typeNum);
1182}
1183
1184//=======================================================================
1185//function : EndWriteRefSection
1186//purpose : read
1187//=======================================================================
1188
1189Storage_Error FSD_BinaryFile::EndWriteRefSection()
1190{
57c5e9e8 1191 myHeader.eref = (Standard_Integer )ftell(myStream);
7fd59977 1192 return Storage_VSOk;
1193}
1194
1195//=======================================================================
1196//function : BeginReadRefSection
1197//purpose : ----------------------- REF : READ
1198//=======================================================================
1199
1200Storage_Error FSD_BinaryFile::BeginReadRefSection()
1201{
1202 if (!fseek(myStream,myHeader.bref,SEEK_SET)) return Storage_VSOk;
1203 else return Storage_VSSectionNotFound;
1204}
1205
1206//=======================================================================
1207//function : RefSectionSize
1208//purpose :
1209//=======================================================================
1210
1211Standard_Integer FSD_BinaryFile::RefSectionSize()
1212{
1213 Standard_Integer i;
1214
1215 GetInteger(i);
1216 return i;
1217}
1218
4ff92abe 1219//=======================================================================
1220//function : RefSectionSize
1221//purpose :
1222//=======================================================================
1223Standard_Integer FSD_BinaryFile::RefSectionSize (Standard_IStream& theIStream)
1224{
1225 Standard_Integer i;
1226
1227 GetInteger(theIStream, i);
1228 return i;
1229}
1230
7fd59977 1231//=======================================================================
1232//function : ReadReferenceType
1233//purpose :
1234//=======================================================================
1235
1236void FSD_BinaryFile::ReadReferenceType(Standard_Integer& reference,
1237 Standard_Integer& typeNum)
1238{
1239 GetReference(reference);
1240 GetInteger(typeNum);
1241}
1242
4ff92abe 1243//=======================================================================
1244//function : ReadReferenceType
1245//purpose :
1246//=======================================================================
1247void FSD_BinaryFile::ReadReferenceType (Standard_IStream& theIStream, Standard_Integer& reference, Standard_Integer& typeNum)
1248{
1249 GetReference (theIStream, reference);
1250 GetInteger (theIStream, typeNum);
1251}
1252
7fd59977 1253//=======================================================================
1254//function : EndReadRefSection
1255//purpose : DATA SECTION
1256// write
1257//=======================================================================
1258
1259Storage_Error FSD_BinaryFile::EndReadRefSection()
1260{
1261 if (!fseek(myStream,myHeader.eref,SEEK_SET)) return Storage_VSOk;
1262 else return Storage_VSSectionNotFound;
1263}
1264
1265//=======================================================================
1266//function : BeginWriteDataSection
1267//purpose : -------------------- DATA : WRITE
1268//=======================================================================
1269
1270Storage_Error FSD_BinaryFile::BeginWriteDataSection()
1271{
57c5e9e8 1272 myHeader.bdata = (Standard_Integer )ftell(myStream);
7fd59977 1273 return Storage_VSOk;
1274}
1275
1276//=======================================================================
1277//function : WritePersistentObjectHeader
1278//purpose :
1279//=======================================================================
1280
1281void FSD_BinaryFile::WritePersistentObjectHeader(const Standard_Integer aRef,
1282 const Standard_Integer aType)
1283{
1284 PutReference(aRef);
1285 PutInteger(aType);
1286}
1287
1288//=======================================================================
1289//function : BeginWritePersistentObjectData
1290//purpose :
1291//=======================================================================
1292
1293void FSD_BinaryFile::BeginWritePersistentObjectData()
1294{
1295}
1296
1297//=======================================================================
1298//function : BeginWriteObjectData
1299//purpose :
1300//=======================================================================
1301
1302void FSD_BinaryFile::BeginWriteObjectData()
1303{
1304}
1305
1306//=======================================================================
1307//function : EndWriteObjectData
1308//purpose :
1309//=======================================================================
1310
1311void FSD_BinaryFile::EndWriteObjectData()
1312{
1313}
1314
1315//=======================================================================
1316//function : EndWritePersistentObjectData
1317//purpose :
1318//=======================================================================
1319
1320void FSD_BinaryFile::EndWritePersistentObjectData()
1321{
1322}
1323
1324//=======================================================================
1325//function : EndWriteDataSection
1326//purpose : read
1327//=======================================================================
1328
1329Storage_Error FSD_BinaryFile::EndWriteDataSection()
1330{
57c5e9e8 1331 myHeader.edata = (Standard_Integer )ftell(myStream);
7fd59977 1332
1333 fseek(myStream,myHeader.binfo,SEEK_SET);
1334 WriteHeader();
1335 return Storage_VSOk;
1336}
1337
1338//=======================================================================
1339//function : BeginReadDataSection
1340//purpose : ---------------------- DATA : READ
1341//=======================================================================
1342
1343Storage_Error FSD_BinaryFile::BeginReadDataSection()
1344{
1345 if (!fseek(myStream,myHeader.bdata,SEEK_SET)) return Storage_VSOk;
1346 else return Storage_VSSectionNotFound;
1347}
1348
1349//=======================================================================
1350//function : ReadPersistentObjectHeader
1351//purpose :
1352//=======================================================================
1353
1354void FSD_BinaryFile::ReadPersistentObjectHeader(Standard_Integer& aRef,
1355 Standard_Integer& aType)
1356{
1357 GetReference(aRef);
1358 GetInteger(aType);
1359}
1360
1361//=======================================================================
1362//function : BeginReadPersistentObjectData
1363//purpose :
1364//=======================================================================
1365
1366void FSD_BinaryFile::BeginReadPersistentObjectData()
1367{
1368}
1369
1370//=======================================================================
1371//function : BeginReadObjectData
1372//purpose :
1373//=======================================================================
1374
1375void FSD_BinaryFile::BeginReadObjectData()
1376{
1377}
1378
1379//=======================================================================
1380//function : EndReadObjectData
1381//purpose :
1382//=======================================================================
1383
1384void FSD_BinaryFile::EndReadObjectData()
1385{
1386}
1387
1388//=======================================================================
1389//function : EndReadPersistentObjectData
1390//purpose :
1391//=======================================================================
1392
1393void FSD_BinaryFile::EndReadPersistentObjectData()
1394{
1395}
1396
1397//=======================================================================
1398//function : EndReadDataSection
1399//purpose :
1400//=======================================================================
1401
1402Storage_Error FSD_BinaryFile::EndReadDataSection()
1403{
1404 if (!fseek(myStream,myHeader.edata,SEEK_SET)) return Storage_VSOk;
1405 else return Storage_VSSectionNotFound;
1406}
1407
1408//=======================================================================
1409//function : WriteString
1410//purpose : write string at the current position.
1411//=======================================================================
1412
1413void FSD_BinaryFile::WriteString(const TCollection_AsciiString& aString)
1414{
1415 Standard_Integer size;
1416
1417 size = aString.Length();
1418
1419 PutInteger(size);
1420
1421 if (size > 0) {
9775fa61 1422 if (!fwrite(aString.ToCString(),aString.Length(),1,myStream)) throw Storage_StreamWriteError();
7fd59977 1423 }
1424}
1425
4ff92abe 1426//=======================================================================
1427//function : WriteString
1428//purpose : write string at the current position.
1429//=======================================================================
1430Standard_Integer FSD_BinaryFile::WriteString (Standard_OStream& theOStream,
1431 const TCollection_AsciiString& theString,
1432 const Standard_Boolean theOnlyCount)
1433{
1434 Standard_Integer aNumAndStrLen, anAsciiStrLen;
1435
1436 anAsciiStrLen = aNumAndStrLen = theString.Length();
1437
1438 aNumAndStrLen += PutInteger (theOStream, anAsciiStrLen, theOnlyCount);
1439
1440 if (anAsciiStrLen > 0 && !theOnlyCount)
1441 {
1442 theOStream.write (theString.ToCString(), theString.Length());
1443 if (theOStream.fail())
1444 {
9775fa61 1445 throw Storage_StreamWriteError();
4ff92abe 1446 }
1447 }
1448
1449 return aNumAndStrLen;
1450}
1451
7fd59977 1452//=======================================================================
1453//function : ReadString
1454//purpose : read string from the current position.
1455//=======================================================================
1456
1457void FSD_BinaryFile::ReadString(TCollection_AsciiString& aString)
1458{
1459 Standard_Integer size = 0;
1460
1461 GetInteger(size);
1462 if (size > 0) {
1463 Standard_Character *c = (Standard_Character *)Standard::Allocate((size+1) * sizeof(Standard_Character));
9775fa61 1464 if (!fread(c,size,1,myStream)) throw Storage_StreamWriteError();
7fd59977 1465 c[size] = '\0';
1466 aString = c;
547702a1 1467 Standard::Free(c);
7fd59977 1468 }
1469 else {
1470 aString.Clear();
1471 }
1472}
1473
4ff92abe 1474//=======================================================================
1475//function : ReadString
1476//purpose : read string from the current position.
1477//=======================================================================
1478void FSD_BinaryFile::ReadString (Standard_IStream& theIStream, TCollection_AsciiString& aString)
1479{
1480 Standard_Integer size = 0;
1481
1482 GetInteger(theIStream, size);
1483
1484 if (size > 0)
1485 {
1486 Standard_Character *c = (Standard_Character *)Standard::Allocate((size+1) * sizeof(Standard_Character));
1487
1488 if (!theIStream.good())
1489 {
9775fa61 1490 throw Storage_StreamReadError();
4ff92abe 1491 }
1492
1493 theIStream.read (c, size);
1494
1495 if (theIStream.gcount() != size)
1496 {
9775fa61 1497 throw Storage_StreamReadError();
4ff92abe 1498 }
1499
1500 c[size] = '\0';
1501
1502 aString = c;
1503
1504 Standard::Free(c);
1505 }
1506 else
1507 {
1508 aString.Clear();
1509 }
1510}
1511
7fd59977 1512//=======================================================================
1513//function : WriteExtendedString
1514//purpose : write string at the current position.
1515//=======================================================================
1516
1517void FSD_BinaryFile::WriteExtendedString(const TCollection_ExtendedString& aString)
1518{
1519 Standard_Integer size;
1520
1521 size = aString.Length();
1522
1523 PutInteger(size);
1524
1525 if (size > 0) {
1526 Standard_ExtString anExtStr;
10a4116e 1527#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 1528 TCollection_ExtendedString aCopy = aString;
1529 anExtStr = aCopy.ToExtString();
1530
1531 Standard_PExtCharacter pChar;
1532 //
1533 pChar=(Standard_PExtCharacter)anExtStr;
1534
1535 for (Standard_Integer i=0; i < size; i++)
1536 pChar[i] = InverseExtChar (pChar[i]);
1537#else
1538 anExtStr = aString.ToExtString();
1539#endif
1540 if (!fwrite(anExtStr,sizeof(Standard_ExtCharacter)*aString.Length(),1,myStream))
9775fa61 1541 throw Storage_StreamWriteError();
7fd59977 1542 }
1543}
1544
4ff92abe 1545//=======================================================================
1546//function : WriteExtendedString
1547//purpose : write string at the current position.
1548//=======================================================================
1549Standard_Integer FSD_BinaryFile::WriteExtendedString (Standard_OStream& theOStream,
1550 const TCollection_ExtendedString& theString,
1551 const Standard_Boolean theOnlyCount)
1552{
1553 Standard_Integer aNumAndStrLen, anExtStrLen;
1554 anExtStrLen = theString.Length();
1555
1556 aNumAndStrLen = anExtStrLen * sizeof(Standard_ExtCharacter);
1557 aNumAndStrLen += PutInteger (theOStream, anExtStrLen, theOnlyCount);
1558
1559 if (anExtStrLen > 0 && !theOnlyCount)
1560 {
1561 Standard_ExtString anExtStr;
1562#if OCCT_BINARY_FILE_DO_INVERSE
1563 TCollection_ExtendedString aCopy = theString;
1564 anExtStr = aCopy.ToExtString();
1565
1566 Standard_PExtCharacter pChar;
1567 //
1568 pChar = (Standard_PExtCharacter)anExtStr;
1569
1570 for (Standard_Integer i = 0; i < anExtStrLen; i++)
1571 {
1572 pChar[i] = InverseExtChar (pChar[i]);
1573 }
1574#else
1575 anExtStr = theString.ToExtString();
1576#endif
1577
1578 theOStream.write((char*)anExtStr, sizeof(Standard_ExtCharacter)*theString.Length());
1579 if (theOStream.fail())
1580 {
9775fa61 1581 throw Storage_StreamWriteError();
4ff92abe 1582 }
1583 }
1584
1585 return aNumAndStrLen;
1586}
1587
7fd59977 1588//=======================================================================
1589//function : ReadExtendedString
1590//purpose : read string from the current position.
1591//=======================================================================
1592
1593void FSD_BinaryFile::ReadExtendedString(TCollection_ExtendedString& aString)
1594{
1595 Standard_Integer size = 0;
1596
1597 GetInteger(size);
1598 if (size > 0) {
1599 Standard_ExtCharacter *c = (Standard_ExtCharacter *)
1600 Standard::Allocate((size+1) * sizeof(Standard_ExtCharacter));
1601 if (!fread(c,size*sizeof(Standard_ExtCharacter),1,myStream))
9775fa61 1602 throw Storage_StreamWriteError();
7fd59977 1603 c[size] = '\0';
10a4116e 1604#if OCCT_BINARY_FILE_DO_INVERSE
7fd59977 1605 for (Standard_Integer i=0; i < size; i++)
1606 c[i] = InverseExtChar (c[i]);
1607#endif
1608 aString = c;
547702a1 1609 Standard::Free(c);
7fd59977 1610 }
1611 else {
1612 aString.Clear();
1613 }
1614}
1615
4ff92abe 1616//=======================================================================
1617//function : ReadExtendedString
1618//purpose : read string from the current position.
1619//=======================================================================
1620void FSD_BinaryFile::ReadExtendedString (Standard_IStream& theIStream, TCollection_ExtendedString& aString)
1621{
1622 Standard_Integer size = 0;
1623
1624 GetInteger (theIStream, size);
1625
1626 if (size > 0)
1627 {
1628 Standard_ExtCharacter *c = (Standard_ExtCharacter *)Standard::Allocate((size+1) * sizeof(Standard_ExtCharacter));
1629
1630 if (!theIStream.good())
1631 {
9775fa61 1632 throw Storage_StreamReadError();
4ff92abe 1633 }
1634
402cfabc 1635 const std::streamsize aNbBytes = std::streamsize(sizeof(Standard_ExtCharacter) * size);
1636 theIStream.read ((char *)c, aNbBytes);
1637 if (theIStream.gcount() != aNbBytes)
4ff92abe 1638 {
9775fa61 1639 throw Storage_StreamReadError();
4ff92abe 1640 }
402cfabc 1641
4ff92abe 1642 c[size] = '\0';
1643
1644#if OCCT_BINARY_FILE_DO_INVERSE
1645 for (Standard_Integer i=0; i < size; i++)
1646 {
1647 c[i] = InverseExtChar (c[i]);
1648 }
1649#endif
1650 aString = c;
1651 Standard::Free(c);
1652 }
1653 else
1654 {
1655 aString.Clear();
1656 }
1657}
1658
7fd59977 1659//=======================================================================
1660//function : WriteHeader
1661//purpose :
1662//=======================================================================
1663
1664void FSD_BinaryFile::WriteHeader()
1665{
1666 PutInteger(myHeader.testindian);
1667 PutInteger(myHeader.binfo);
1668 PutInteger(myHeader.einfo);
1669 PutInteger(myHeader.bcomment);
1670 PutInteger(myHeader.ecomment);
1671 PutInteger(myHeader.btype);
1672 PutInteger(myHeader.etype);
1673 PutInteger(myHeader.broot);
1674 PutInteger(myHeader.eroot);
1675 PutInteger(myHeader.bref);
1676 PutInteger(myHeader.eref);
1677 PutInteger(myHeader.bdata);
1678 PutInteger(myHeader.edata);
1679}
1680
4ff92abe 1681//=======================================================================
1682//function : WriteHeader
1683//purpose :
1684//=======================================================================
1685Standard_Integer FSD_BinaryFile::WriteHeader (Standard_OStream& theOStream,
1686 const FSD_FileHeader& theHeader,
1687 const Standard_Boolean theOnlyCount)
1688{
1689 Standard_Integer aHeaderSize = 0;
1690
1691 aHeaderSize += PutInteger (theOStream, theHeader.testindian, theOnlyCount);
1692 aHeaderSize += PutInteger (theOStream, theHeader.binfo, theOnlyCount);
1693 aHeaderSize += PutInteger (theOStream, theHeader.einfo, theOnlyCount);
1694 aHeaderSize += PutInteger (theOStream, theHeader.bcomment, theOnlyCount);
1695 aHeaderSize += PutInteger (theOStream, theHeader.ecomment, theOnlyCount);
1696 aHeaderSize += PutInteger (theOStream, theHeader.btype, theOnlyCount);
1697 aHeaderSize += PutInteger (theOStream, theHeader.etype, theOnlyCount);
1698 aHeaderSize += PutInteger (theOStream, theHeader.broot, theOnlyCount);
1699 aHeaderSize += PutInteger (theOStream, theHeader.eroot, theOnlyCount);
1700 aHeaderSize += PutInteger (theOStream, theHeader.bref, theOnlyCount);
1701 aHeaderSize += PutInteger (theOStream, theHeader.eref, theOnlyCount);
1702 aHeaderSize += PutInteger (theOStream, theHeader.bdata, theOnlyCount);
1703 aHeaderSize += PutInteger (theOStream, theHeader.edata, theOnlyCount);
1704
1705 return aHeaderSize;
1706}
1707
7fd59977 1708//=======================================================================
1709//function : ReadHeader
1710//purpose :
1711//=======================================================================
1712
1713void FSD_BinaryFile::ReadHeader()
1714{
1715 GetInteger(myHeader.testindian);
1716 GetInteger(myHeader.binfo);
1717 GetInteger(myHeader.einfo);
1718 GetInteger(myHeader.bcomment);
1719 GetInteger(myHeader.ecomment);
1720 GetInteger(myHeader.btype);
1721 GetInteger(myHeader.etype);
1722 GetInteger(myHeader.broot);
1723 GetInteger(myHeader.eroot);
1724 GetInteger(myHeader.bref);
1725 GetInteger(myHeader.eref);
1726 GetInteger(myHeader.bdata);
1727 GetInteger(myHeader.edata);
1728}
1729
4ff92abe 1730//=======================================================================
1731//function : ReadHeader
1732//purpose :
1733//=======================================================================
1734
1735void FSD_BinaryFile::ReadHeader(Standard_IStream& theIStream, FSD_FileHeader& theFileHeader)
1736{
1737 GetInteger (theIStream, theFileHeader.testindian);
1738 GetInteger (theIStream, theFileHeader.binfo);
1739 GetInteger (theIStream, theFileHeader.einfo);
1740 GetInteger (theIStream, theFileHeader.bcomment);
1741 GetInteger (theIStream, theFileHeader.ecomment);
1742 GetInteger (theIStream, theFileHeader.btype);
1743 GetInteger (theIStream, theFileHeader.etype);
1744 GetInteger (theIStream, theFileHeader.broot);
1745 GetInteger (theIStream, theFileHeader.eroot);
1746 GetInteger (theIStream, theFileHeader.bref);
1747 GetInteger (theIStream, theFileHeader.eref);
1748 GetInteger (theIStream, theFileHeader.bdata);
1749 GetInteger (theIStream, theFileHeader.edata);
1750}
1751
1752//=======================================================================
1753//function : ReadHeaderData
1754//purpose :
1755//=======================================================================
1756void FSD_BinaryFile::ReadHeaderData( Standard_IStream& theIStream, const Handle(Storage_HeaderData)& theHeaderData )
1757{
1758 // read info
1759 TCollection_AsciiString uinfo,mStorageVersion,mDate,mSchemaName,mSchemaVersion,mApplicationVersion;
1760 TCollection_ExtendedString mApplicationName,mDataType;
1761 TColStd_SequenceOfAsciiString mUserInfo;
1762 Standard_Integer mNBObj;
1763
1764 FSD_BinaryFile::GetInteger (theIStream, mNBObj);
1765 FSD_BinaryFile::ReadString (theIStream, mStorageVersion);
1766 FSD_BinaryFile::ReadString (theIStream, mDate);
1767 FSD_BinaryFile::ReadString (theIStream, mSchemaName);
1768 FSD_BinaryFile::ReadString (theIStream, mSchemaVersion);
1769 FSD_BinaryFile::ReadExtendedString(theIStream, mApplicationName);
1770 FSD_BinaryFile::ReadString (theIStream, mApplicationVersion);
1771 FSD_BinaryFile::ReadExtendedString(theIStream, mDataType);
1772
1773 Standard_Integer len = 0;
1774 TCollection_AsciiString line;
1775
1776 FSD_BinaryFile::GetInteger(theIStream, len);
1777
1778 for (Standard_Integer i = 1; i <= len && theIStream.good(); i++)
1779 {
1780 FSD_BinaryFile::ReadString (theIStream, line);
1781 mUserInfo.Append(line);
1782 }
1783
1784 theHeaderData->SetNumberOfObjects(mNBObj);
1785 theHeaderData->SetStorageVersion(mStorageVersion);
1786 theHeaderData->SetCreationDate(mDate);
1787 theHeaderData->SetSchemaName(mSchemaName);
1788 theHeaderData->SetSchemaVersion(mSchemaVersion);
1789 theHeaderData->SetApplicationName(mApplicationName);
1790 theHeaderData->SetApplicationVersion(mApplicationVersion);
1791 theHeaderData->SetDataType(mDataType);
1792
1793 for (Standard_Integer i = 1; i <= mUserInfo.Length(); i++) {
1794 theHeaderData->AddToUserInfo(mUserInfo.Value(i));
1795 }
1796}
7fd59977 1797
1798//=======================================================================
1799//function : Tell
1800//purpose : return position in the file. Return -1 upon error.
1801//=======================================================================
1802
1803Storage_Position FSD_BinaryFile::Tell()
1804{
1805 return (Storage_Position) ftell(myStream);
1806}
10a4116e 1807
1808//=======================================================================
1809//function : InverseReal
1810//purpose : Inverses bytes in the real value
1811//=======================================================================
1812
1813Standard_Real FSD_BinaryFile::InverseReal (const Standard_Real theValue)
1814{
1815 Standard_STATIC_ASSERT(sizeof(Standard_Real) == 2 * sizeof(Standard_Integer));
1816 union {
1817 Standard_Integer i[2];
1818 Standard_Real aValue;
1819 } aWrapUnion;
1820
1821 aWrapUnion.aValue = theValue;
1822
1823 Standard_Integer aTemp = aWrapUnion.i[1];
1824 aWrapUnion.i[1] = InverseInt(aWrapUnion.i[0]);
1825 aWrapUnion.i[0] = InverseInt(aTemp);
1826
1827 return aWrapUnion.aValue;
1828}
1829
1830//=======================================================================
1831//function : InverseShortReal
1832//purpose : Inverses bytes in the short real value
1833//=======================================================================
1834
1835Standard_ShortReal FSD_BinaryFile::InverseShortReal (const Standard_ShortReal theValue)
1836{
1837 Standard_STATIC_ASSERT(sizeof(Standard_ShortReal) == sizeof(Standard_Integer));
1838 union {
1839 Standard_ShortReal aValue;
1840 Standard_Integer aResult;
1841 } aWrapUnion;
1842
1843 aWrapUnion.aValue = theValue;
1844 aWrapUnion.aResult = InverseInt (aWrapUnion.aResult);
1845
1846 return aWrapUnion.aValue;
1847}
1848
1849//=======================================================================
1850//function : InverseSize
1851//purpose : Inverses bytes in size_t type instance
1852//=======================================================================
1853
1854template<int size>
41fbbba8 1855inline uint64_t OCCT_InverseSizeSpecialized (const uint64_t theValue, int);
10a4116e 1856
1857template<>
41fbbba8 1858inline uint64_t OCCT_InverseSizeSpecialized <4> (const uint64_t theValue, int)
10a4116e 1859{
1860 return FSD_BinaryFile::InverseInt(static_cast<Standard_Integer>(theValue));
1861}
1862
1863template<>
41fbbba8 1864inline uint64_t OCCT_InverseSizeSpecialized <8> (const uint64_t theValue, int)
10a4116e 1865{
1866 union {
1867 Standard_Integer i[2];
41fbbba8 1868 uint64_t aValue;
10a4116e 1869 } aWrapUnion;
1870
1871 aWrapUnion.aValue = theValue;
1872
1873 Standard_Integer aTemp = aWrapUnion.i[1];
1874 aWrapUnion.i[1] = FSD_BinaryFile::InverseInt(aWrapUnion.i[0]);
1875 aWrapUnion.i[0] = FSD_BinaryFile::InverseInt(aTemp);
1876
1877 return aWrapUnion.aValue;
1878}
1879
1880Standard_Size FSD_BinaryFile::InverseSize (const Standard_Size theValue)
1881{
41fbbba8 1882 return (Standard_Size) OCCT_InverseSizeSpecialized <sizeof(Standard_Size)> (theValue, 0);
1883}
1884
1885uint64_t FSD_BinaryFile::InverseUint64 (const uint64_t theValue)
1886{
1887 return OCCT_InverseSizeSpecialized <sizeof(uint64_t)> (theValue, 0);
10a4116e 1888}