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