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