0023947: Eliminate trivial compiler warnings in MSVC++ with warning level 4
[occt.git] / src / FSD / FSD_File.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_File.ixx>
20 #include <OSD.hxx>
21
22 const Standard_CString MAGICNUMBER = "FSDFILE";
23 const Standard_CString ENDOFNORMALEXTENDEDSECTION = "BEGIN_REF_SECTION";
24 const Standard_Integer SIZEOFNORMALEXTENDEDSECTION = 16;
25
26 //#define USEOSDREAL 1
27
28 //=======================================================================
29 //function : FSD_File
30 //purpose  : 
31 //=======================================================================
32
33 FSD_File::FSD_File()
34 {
35
36 }
37
38 //=======================================================================
39 //function : IsGoodFileType
40 //purpose  : INFO SECTION
41 //           write
42 //=======================================================================
43
44 Storage_Error FSD_File::IsGoodFileType(const TCollection_AsciiString& aName)
45 {
46   FSD_File      f;
47   Storage_Error s;
48
49   s = f.Open(aName,Storage_VSRead);
50
51   if (s == Storage_VSOk) {
52     TCollection_AsciiString l;
53     Standard_Size        len = strlen(FSD_File::MagicNumber());
54
55     f.ReadChar(l,len);
56
57     f.Close();
58
59     if (strncmp(FSD_File::MagicNumber(),l.ToCString(),len) != 0) {
60       s = Storage_VSFormatError;
61     }
62   }
63
64   return s;
65 }
66
67 //=======================================================================
68 //function : Open
69 //purpose  : 
70 //=======================================================================
71
72 Storage_Error FSD_File::Open(const TCollection_AsciiString& aName,const Storage_OpenMode aMode)
73 {
74   Storage_Error result = Storage_VSOk;
75
76   SetName(aName);
77
78   if (OpenMode() == Storage_VSNone) {
79     if (aMode == Storage_VSRead) {
80       myStream.open(aName.ToCString(),ios::in); // ios::nocreate is not portable
81     }
82     else if (aMode == Storage_VSWrite) {
83       myStream.open(aName.ToCString(),ios::out);
84     }
85     else if (aMode == Storage_VSReadWrite) {
86       myStream.open(aName.ToCString(),ios::in|ios::out);
87     }
88     
89     if (myStream.fail()) {
90       result = Storage_VSOpenError;
91     }
92     else {
93       myStream.precision(17);
94       myStream.imbue (std::locale::classic()); // use always C locale
95       SetOpenMode(aMode);
96     }
97   }
98   else {
99     result = Storage_VSAlreadyOpen;
100   }
101
102   return result;
103 }
104
105 //=======================================================================
106 //function : IsEnd
107 //purpose  : 
108 //=======================================================================
109
110 Standard_Boolean FSD_File::IsEnd()
111 {
112   return myStream.eof();
113 }
114
115 //=======================================================================
116 //function : Close
117 //purpose  : 
118 //=======================================================================
119
120 Storage_Error FSD_File::Close()
121 {
122   Storage_Error result = Storage_VSOk;
123
124   if (OpenMode() != Storage_VSNone) {
125     myStream.close();
126     SetOpenMode(Storage_VSNone);
127   }
128   else {
129     result = Storage_VSNotOpen;
130   }
131
132   return result;
133 }
134
135 //=======================================================================
136 //function : MagicNumber
137 //purpose  : ------------------ PROTECTED
138 //=======================================================================
139
140 const Standard_CString FSD_File::MagicNumber()
141 {
142   return MAGICNUMBER;
143 }
144
145 //=======================================================================
146 //function : FlushEndOfLine
147 //purpose  : 
148 //=======================================================================
149
150 void FSD_File::FlushEndOfLine()
151 {
152   TCollection_AsciiString aDummy;
153   ReadLine (aDummy); // flush is nothing more than to read till the line-break
154 /*  static char Buffer[8192];
155   char c;
156   Standard_Boolean IsEnd = Standard_False;
157
158   while (!IsEnd && !FSD_File::IsEnd()) {
159     Buffer[0] = '\0';
160     myStream.get(Buffer,8192,'\n');
161
162     if (myStream.get(c) && c != '\n') {
163     }
164     else {
165       IsEnd = Standard_True;
166     }
167   }*/
168 }
169
170 //=======================================================================
171 //function : ReadLine
172 //purpose  : read from the current position to the end of line.
173 //=======================================================================
174
175 void FSD_File::ReadLine(TCollection_AsciiString& buffer)
176 {
177   char Buffer[8193];
178   Standard_Boolean IsEnd = Standard_False;
179   
180   buffer.Clear();
181
182   while (!IsEnd && !FSD_File::IsEnd()) {
183     Buffer[0] = '\0';
184     myStream.getline(Buffer,8192,'\n');
185     
186 //    char c;
187 //    if (myStream.get(c) && c != '\n') {
188 //      buffer += Buffer;
189 //      buffer += c;
190 //    }
191 //    else {
192       buffer += Buffer;
193       IsEnd = Standard_True;
194 //    }
195   }
196 }
197
198 //=======================================================================
199 //function : WriteExtendedLine
200 //purpose  : write from the current position to the end of line.
201 //=======================================================================
202
203 void FSD_File::WriteExtendedLine(const TCollection_ExtendedString& buffer)
204 {
205   Standard_ExtString extBuffer;
206   Standard_Integer   i,c,d;
207
208   extBuffer = buffer.ToExtString();
209
210   for (i = 0; i < buffer.Length(); i++) {
211     c = (extBuffer[i] & 0x0000FF00 ) >> 8 ;
212     d = extBuffer[i] & 0x000000FF;
213
214     myStream << (char)c << (char)d;
215   }
216
217   myStream << (char)0 << "\n";
218 }
219
220 //=======================================================================
221 //function : ReadExtendedLine
222 //purpose  : 
223 //=======================================================================
224
225 void FSD_File::ReadExtendedLine(TCollection_ExtendedString& buffer)
226 {
227   char c = '\0';
228   Standard_ExtCharacter i = 0,j,count = 0;
229   Standard_Boolean fin = Standard_False;
230   Standard_CString tg = ENDOFNORMALEXTENDEDSECTION;
231  
232   buffer.Clear();
233
234   while (!fin && !IsEnd()) {
235     myStream.get(c);
236
237     if (c == tg[count]) count++;
238     else count = 0;
239     if (count < SIZEOFNORMALEXTENDEDSECTION) {
240       i = 0; j = 0;
241       i += (Standard_ExtCharacter)c;
242       if (c == '\0') fin = Standard_True;
243       i = (i << 8);
244       
245       myStream.get(c);
246       if (c == tg[count]) count++;
247       else count = 0;
248       if (count < SIZEOFNORMALEXTENDEDSECTION) {
249         j += (Standard_ExtCharacter)c;
250         if (c != '\n') {
251           fin = Standard_False;
252           i |= (0x00FF & j);
253           buffer += (Standard_ExtCharacter)i;
254         }
255       }
256       else {
257         Storage_StreamExtCharParityError::Raise();      
258       }
259     }
260     else {
261       Storage_StreamExtCharParityError::Raise();
262     }
263   }
264 }
265
266 //=======================================================================
267 //function : ReadChar
268 //purpose  : read <rsize> character from the current position.
269 //=======================================================================
270
271 void FSD_File::ReadChar(TCollection_AsciiString& buffer, const Standard_Size rsize)
272 {
273   char             c;
274   Standard_Size ccount = 0;
275
276   buffer.Clear();
277
278   while (!IsEnd() && (ccount < rsize)) {
279     myStream.get(c);
280     buffer += c;
281     ccount++;
282   }
283 }
284
285 //=======================================================================
286 //function : ReadString
287 //purpose  : read from the first none space character position to the end of line.
288 //=======================================================================
289
290 void FSD_File::ReadString(TCollection_AsciiString& buffer)
291 {
292   char Buffer[8193];
293   char *bpos;
294   Standard_Boolean IsEnd = Standard_False,isFirstTime = Standard_True;
295   
296   buffer.Clear();
297   
298   while (!IsEnd && !FSD_File::IsEnd()) {
299     Buffer[0] = '\0';
300     myStream.getline(Buffer,8192,'\n');
301     bpos = Buffer;
302
303     // LeftAdjust
304     //
305     if (isFirstTime) {
306       isFirstTime = Standard_False;
307       while (*bpos == '\n' || *bpos == ' ') bpos++;
308     }
309 //    char c;
310 //    if (myStream.get(c) && c != '\n') {
311 //      buffer += bpos;
312 //      buffer += c;
313 //    }
314 //    else {
315       buffer += bpos;
316       IsEnd = Standard_True;
317 //    }
318   }
319   
320 }
321
322 //=======================================================================
323 //function : ReadWord
324 //purpose  : read from the current position to the next white space or end of line.
325 //=======================================================================
326
327 void FSD_File::ReadWord(TCollection_AsciiString& buffer)
328 {
329   char c = '\0';
330   char b[8193],*tmpb;
331   Standard_Boolean IsEnd = Standard_False;
332   Standard_Integer i;
333
334   tmpb = b;
335   memset(b,'\0',8193);
336   buffer.Clear();
337
338   while (!IsEnd && !FSD_File::IsEnd()) {
339     myStream.get(c);
340     if ((c != ' ') && (c != '\n')) IsEnd = Standard_True;
341   }
342
343   IsEnd = Standard_False;
344   i = 0;
345
346   while (!IsEnd && !FSD_File::IsEnd()) {
347     if (i == 8192) {
348       buffer += b;
349       tmpb = b;
350       memset(b,'\0',8193);
351       i = 0;
352     }
353     *tmpb = c;
354     tmpb++; i++;
355     myStream.get(c);
356     if ((c == '\n') || (c == ' ')) IsEnd = Standard_True;
357   }
358
359   buffer += b;
360 }
361
362 //=======================================================================
363 //function : FindTag
364 //purpose  : 
365 //=======================================================================
366
367 Storage_Error FSD_File::FindTag(const Standard_CString aTag)
368 {
369   TCollection_AsciiString l;
370   
371   ReadString(l);
372
373   while ((strcmp(l.ToCString(),aTag) != 0) && !IsEnd()) {
374     ReadString(l);
375   }
376
377   if (IsEnd()) {
378     return Storage_VSSectionNotFound;
379   }
380   else {
381     return Storage_VSOk;
382   }
383 }
384
385 //=======================================================================
386 //function : SkipObject
387 //purpose  : 
388 //=======================================================================
389
390 void FSD_File::SkipObject()
391 {
392   FlushEndOfLine();
393 }
394
395 //=======================================================================
396 //function : PutReference
397 //purpose  : ---------------------- PUBLIC : PUT
398 //=======================================================================
399
400 Storage_BaseDriver& FSD_File::PutReference(const Standard_Integer aValue)
401 {
402   myStream << aValue << " ";
403   if (myStream.bad()) Storage_StreamWriteError::Raise();
404   return *this;
405 }
406
407 //=======================================================================
408 //function : PutCharacter
409 //purpose  : 
410 //=======================================================================
411
412 Storage_BaseDriver& FSD_File::PutCharacter(const Standard_Character aValue)
413 {
414   unsigned short i;
415
416   i = aValue;
417   myStream << i << " ";
418   if (myStream.bad()) Storage_StreamWriteError::Raise();
419   return *this;
420 }
421
422 //=======================================================================
423 //function : PutExtCharacter
424 //purpose  : 
425 //=======================================================================
426
427 Storage_BaseDriver& FSD_File::PutExtCharacter(const Standard_ExtCharacter aValue)
428 {
429   myStream << aValue << " ";
430   if (myStream.bad()) Storage_StreamWriteError::Raise();
431   return *this;
432 }
433
434 //=======================================================================
435 //function : PutInteger
436 //purpose  : 
437 //=======================================================================
438
439 Storage_BaseDriver& FSD_File::PutInteger(const Standard_Integer aValue)
440 {
441   myStream << aValue << " ";
442   if (myStream.bad()) Storage_StreamWriteError::Raise();
443   return *this;
444 }
445
446 //=======================================================================
447 //function : PutBoolean
448 //purpose  : 
449 //=======================================================================
450
451 Storage_BaseDriver& FSD_File::PutBoolean(const Standard_Boolean aValue)
452 {
453   myStream << ((Standard_Integer)aValue) << " ";
454   if (myStream.bad()) Storage_StreamWriteError::Raise();
455   return *this;
456 }
457
458 //=======================================================================
459 //function : PutReal
460 //purpose  : 
461 //=======================================================================
462
463 Storage_BaseDriver& FSD_File::PutReal(const Standard_Real aValue)
464 {
465   myStream << ((Standard_Real)aValue) << " ";
466   if (myStream.bad()) Storage_StreamWriteError::Raise();
467   return *this;
468 }
469
470 //=======================================================================
471 //function : PutShortReal
472 //purpose  : 
473 //=======================================================================
474
475 Storage_BaseDriver& FSD_File::PutShortReal(const Standard_ShortReal aValue)
476 {
477   myStream << aValue << " ";
478   if (myStream.bad()) Storage_StreamWriteError::Raise();
479   return *this;
480 }
481
482 //=======================================================================
483 //function : GetReference
484 //purpose  : ----------------- PUBLIC : GET
485 //=======================================================================
486
487 Storage_BaseDriver& FSD_File::GetReference(Standard_Integer& aValue)
488 {
489   if (!(myStream >> aValue)) Storage_StreamTypeMismatchError::Raise();
490
491   return *this;
492 }
493
494 //=======================================================================
495 //function : GetCharacter
496 //purpose  : 
497 //=======================================================================
498
499 Storage_BaseDriver& FSD_File::GetCharacter(Standard_Character& aValue)
500 {
501   unsigned short i = 0;
502   if (!(myStream >> i)) {
503     // SGI : donne une erreur mais a une bonne valeur pour les caracteres ecrits
504     //       signes (-80 fait ios::badbit, mais la variable i est initialisee)
505     //
506     if (i == 0) Storage_StreamTypeMismatchError::Raise();
507     myStream.clear(ios::goodbit); // .clear(0) is not portable
508   }
509   aValue = (char)i;
510
511   return *this;
512 }
513
514 //=======================================================================
515 //function : GetExtCharacter
516 //purpose  : 
517 //=======================================================================
518
519 Storage_BaseDriver& FSD_File::GetExtCharacter(Standard_ExtCharacter& aValue)
520 {
521   if (!(myStream >> aValue)) Storage_StreamTypeMismatchError::Raise();
522   
523   return *this;
524 }
525
526 //=======================================================================
527 //function : GetInteger
528 //purpose  : 
529 //=======================================================================
530
531 Storage_BaseDriver& FSD_File::GetInteger(Standard_Integer& aValue)
532 {
533   if (!(myStream >> aValue)) Storage_StreamTypeMismatchError::Raise();
534
535   return *this;
536 }
537
538 //=======================================================================
539 //function : GetBoolean
540 //purpose  : 
541 //=======================================================================
542
543 Storage_BaseDriver& FSD_File::GetBoolean(Standard_Boolean& aValue)
544 {
545   if (!(myStream >> aValue)) Storage_StreamTypeMismatchError::Raise();
546
547   return *this;
548 }
549
550 //=======================================================================
551 //function : GetReal
552 //purpose  : 
553 //=======================================================================
554
555 Storage_BaseDriver& FSD_File::GetReal(Standard_Real& aValue)
556 {
557 #ifdef USEOSDREAL
558   char realbuffer[100];
559
560   realbuffer[0] = '\0';
561   if (!(myStream >> realbuffer)) Storage_StreamTypeMismatchError::Raise();
562   if (!OSD::CStringToReal(realbuffer,aValue)) Storage_StreamTypeMismatchError::Raise();
563
564   return *this;
565 #else
566   if (!(myStream >> aValue)) Storage_StreamTypeMismatchError::Raise();
567
568   return *this;
569 #endif
570 }
571
572 //=======================================================================
573 //function : GetShortReal
574 //purpose  : 
575 //=======================================================================
576
577 Storage_BaseDriver& FSD_File::GetShortReal(Standard_ShortReal& aValue)
578 {
579 #ifdef USEOSDREAL
580   char realbuffer[100];
581   Standard_Real r = 0.0;
582
583   realbuffer[0] = '\0';
584   if (!(myStream >> realbuffer)) Storage_StreamTypeMismatchError::Raise();
585   if (!OSD::CStringToReal(realbuffer,r)) Storage_StreamTypeMismatchError::Raise();
586
587   aValue = r;
588
589   return *this;
590 #else
591   if (!(myStream >> aValue)) Storage_StreamTypeMismatchError::Raise();
592  return *this;
593 #endif
594 }
595
596 //=======================================================================
597 //function : Destroy
598 //purpose  : 
599 //=======================================================================
600
601 void FSD_File::Destroy()
602 {
603   if (OpenMode() != Storage_VSNone) {
604     Close();
605   }
606 }
607
608 //=======================================================================
609 //function : BeginWriteInfoSection
610 //purpose  : -------------------------- INFO : WRITE
611 //=======================================================================
612
613 Storage_Error FSD_File::BeginWriteInfoSection() 
614 {
615   myStream << FSD_File::MagicNumber() << '\n';
616   myStream << "BEGIN_INFO_SECTION\n";
617   if (myStream.bad()) Storage_StreamWriteError::Raise();
618
619   return Storage_VSOk;
620 }
621
622 //=======================================================================
623 //function : WriteInfo
624 //purpose  : 
625 //=======================================================================
626
627 void FSD_File::WriteInfo(const Standard_Integer nbObj,
628                          const TCollection_AsciiString& dbVersion,
629                          const TCollection_AsciiString& date,
630                          const TCollection_AsciiString& schemaName,
631                          const TCollection_AsciiString& schemaVersion,
632                          const TCollection_ExtendedString& appName,
633                          const TCollection_AsciiString& appVersion,
634                          const TCollection_ExtendedString& dataType,
635                          const TColStd_SequenceOfAsciiString& userInfo) 
636 {
637   Standard_Integer i;
638
639   myStream << nbObj;
640   myStream << "\n";
641   myStream << dbVersion.ToCString() << "\n";
642   myStream << date.ToCString() << "\n";
643   myStream << schemaName.ToCString() << "\n";
644   myStream << schemaVersion.ToCString() << "\n";
645   WriteExtendedLine(appName);
646   myStream << appVersion.ToCString() << "\n";
647   WriteExtendedLine(dataType);
648   myStream << userInfo.Length() << "\n";
649
650   if (myStream.bad()) Storage_StreamWriteError::Raise();
651
652   for (i = 1; i <= userInfo.Length(); i++) {
653     myStream << userInfo.Value(i).ToCString() << "\n";
654     if (myStream.bad()) Storage_StreamWriteError::Raise();
655   }
656 }
657
658 //=======================================================================
659 //function : EndWriteInfoSection
660 //purpose  : read
661 //=======================================================================
662
663 Storage_Error FSD_File::EndWriteInfoSection() 
664 {
665   myStream << "END_INFO_SECTION\n";
666   if (myStream.bad())  Storage_StreamWriteError::Raise();
667   return Storage_VSOk;
668 }
669
670 //=======================================================================
671 //function : BeginReadInfoSection
672 //purpose  : 
673 //=======================================================================
674
675 Storage_Error FSD_File::BeginReadInfoSection() 
676 {
677   Storage_Error s;
678   TCollection_AsciiString l;
679   Standard_Size        len = strlen(FSD_File::MagicNumber());
680
681   ReadChar(l,len);
682   
683   if (strncmp(FSD_File::MagicNumber(),l.ToCString(),len) != 0) {
684     s = Storage_VSFormatError;
685   }
686   else {
687     s = FindTag("BEGIN_INFO_SECTION");
688   }
689
690   return s;
691 }
692
693 //=======================================================================
694 //function : ReadInfo
695 //purpose  : ------------------- INFO : READ
696 //=======================================================================
697
698 void FSD_File::ReadInfo(Standard_Integer& nbObj,
699                         TCollection_AsciiString& dbVersion,
700                         TCollection_AsciiString& date,
701                         TCollection_AsciiString& schemaName,
702                         TCollection_AsciiString& schemaVersion,
703                         TCollection_ExtendedString& appName,
704                         TCollection_AsciiString& appVersion,
705                         TCollection_ExtendedString& dataType,
706                         TColStd_SequenceOfAsciiString& userInfo) 
707 {
708   if (!(myStream >> nbObj)) Storage_StreamTypeMismatchError::Raise();
709
710   FlushEndOfLine();
711
712   ReadLine(dbVersion);
713   ReadLine(date);
714   ReadLine(schemaName);
715   ReadLine(schemaVersion);
716   ReadExtendedLine(appName);
717   ReadLine(appVersion);
718   ReadExtendedLine(dataType);
719
720   Standard_Integer i,len = 0;
721
722   if (!(myStream >> len)) Storage_StreamTypeMismatchError::Raise();
723
724   FlushEndOfLine();
725
726   TCollection_AsciiString line;
727
728   for (i = 1; i <= len && !IsEnd(); i++) {
729     ReadLine(line);
730     userInfo.Append(line);
731     line.Clear();
732   }
733 }
734
735 //=======================================================================
736 //function : EndReadInfoSection
737 //purpose  : COMMENTS SECTION
738 //           write
739 //=======================================================================
740
741 Storage_Error FSD_File::EndReadInfoSection() 
742 {
743   return FindTag("END_INFO_SECTION");
744 }
745
746 //=======================================================================
747 //function : BeginWriteCommentSection
748 //purpose  : ---------------- COMMENTS : WRITE
749 //=======================================================================
750
751 Storage_Error FSD_File::BeginWriteCommentSection() 
752 {
753   myStream << "BEGIN_COMMENT_SECTION\n";
754   if (myStream.bad()) Storage_StreamWriteError::Raise();
755   return Storage_VSOk;
756 }
757
758 //=======================================================================
759 //function : WriteComment
760 //purpose  : 
761 //=======================================================================
762
763 void FSD_File::WriteComment(const TColStd_SequenceOfExtendedString& aCom)
764 {
765  Standard_Integer i,aSize;
766
767  aSize = aCom.Length();
768  myStream << aSize << "\n";
769  if (myStream.bad()) Storage_StreamWriteError::Raise();
770
771  for (i = 1; i <= aSize; i++) {
772    WriteExtendedLine(aCom.Value(i));
773    if (myStream.bad()) Storage_StreamWriteError::Raise();
774  }
775 }
776
777 //=======================================================================
778 //function : EndWriteCommentSection
779 //purpose  : read
780 //=======================================================================
781
782 Storage_Error FSD_File::EndWriteCommentSection() 
783 {
784   myStream << "END_COMMENT_SECTION\n";
785   if (myStream.bad()) Storage_StreamWriteError::Raise();
786   return Storage_VSOk;
787 }
788
789 //=======================================================================
790 //function : BeginReadCommentSection
791 //purpose  : ---------------- COMMENTS : READ
792 //=======================================================================
793
794 Storage_Error FSD_File::BeginReadCommentSection() 
795 {
796   return FindTag("BEGIN_COMMENT_SECTION");
797 }
798
799 //=======================================================================
800 //function : ReadComment
801 //purpose  : 
802 //=======================================================================
803
804 void FSD_File::ReadComment(TColStd_SequenceOfExtendedString& aCom)
805 {
806   TCollection_ExtendedString line;
807   Standard_Integer           len,i;
808
809   if (!(myStream >> len)) Storage_StreamTypeMismatchError::Raise();
810   
811   FlushEndOfLine();  
812
813   for (i = 1; i <= len && !IsEnd(); i++) {
814     ReadExtendedLine(line);
815     aCom.Append(line);
816     line.Clear();
817   }
818 }
819
820 //=======================================================================
821 //function : EndReadCommentSection
822 //purpose  : 
823 //=======================================================================
824
825 Storage_Error FSD_File::EndReadCommentSection() 
826 {
827   return FindTag("END_COMMENT_SECTION");
828 }
829
830 //=======================================================================
831 //function : BeginWriteTypeSection
832 //purpose  : --------------- TYPE : WRITE
833 //=======================================================================
834
835 Storage_Error FSD_File::BeginWriteTypeSection() 
836 {
837   myStream << "BEGIN_TYPE_SECTION\n";
838   if (myStream.bad()) Storage_StreamWriteError::Raise();
839   return Storage_VSOk;
840 }
841
842 //=======================================================================
843 //function : SetTypeSectionSize
844 //purpose  : 
845 //=======================================================================
846
847 void FSD_File::SetTypeSectionSize(const Standard_Integer aSize) 
848 {
849   myStream << aSize << "\n";
850   if (myStream.bad()) Storage_StreamWriteError::Raise();
851 }
852
853 //=======================================================================
854 //function : WriteTypeInformations
855 //purpose  : 
856 //=======================================================================
857
858 void FSD_File::WriteTypeInformations(const Standard_Integer typeNum,
859                                       const TCollection_AsciiString& typeName) 
860 {
861   myStream << typeNum << " " << typeName.ToCString() << "\n";
862   if (myStream.bad()) Storage_StreamWriteError::Raise();
863 }
864
865 //=======================================================================
866 //function : EndWriteTypeSection
867 //purpose  : read
868 //=======================================================================
869
870 Storage_Error FSD_File::EndWriteTypeSection() 
871 {
872   myStream << "END_TYPE_SECTION\n";
873   if (myStream.bad()) Storage_StreamWriteError::Raise();
874   return Storage_VSOk;
875 }
876
877 //=======================================================================
878 //function : BeginReadTypeSection
879 //purpose  : ------------------- TYPE : READ
880 //=======================================================================
881
882 Storage_Error FSD_File::BeginReadTypeSection() 
883 {
884   return FindTag("BEGIN_TYPE_SECTION");
885 }
886
887 //=======================================================================
888 //function : TypeSectionSize
889 //purpose  : 
890 //=======================================================================
891
892 Standard_Integer FSD_File::TypeSectionSize() 
893 {
894   Standard_Integer i;
895
896   if (!(myStream >> i)) Storage_StreamTypeMismatchError::Raise();
897
898   FlushEndOfLine();
899
900   return i;
901 }
902
903 //=======================================================================
904 //function : ReadTypeInformations
905 //purpose  : 
906 //=======================================================================
907
908 void FSD_File::ReadTypeInformations(Standard_Integer& typeNum,
909                                     TCollection_AsciiString& typeName) 
910 {
911   if (!(myStream >> typeNum)) Storage_StreamTypeMismatchError::Raise();
912   if (!(myStream >> typeName)) Storage_StreamTypeMismatchError::Raise();
913   FlushEndOfLine();
914 }
915
916 //=======================================================================
917 //function : EndReadTypeSection
918 //purpose  : ROOT SECTION
919 //           write
920 //=======================================================================
921
922 Storage_Error FSD_File::EndReadTypeSection() 
923 {
924   return FindTag("END_TYPE_SECTION");
925 }
926
927 //=======================================================================
928 //function : BeginWriteRootSection
929 //purpose  : -------------------- ROOT : WRITE
930 //=======================================================================
931
932 Storage_Error FSD_File::BeginWriteRootSection() 
933 {
934   myStream << "BEGIN_ROOT_SECTION\n";
935   if (myStream.bad()) Storage_StreamWriteError::Raise();
936   return Storage_VSOk;
937 }
938
939 //=======================================================================
940 //function : SetRootSectionSize
941 //purpose  : 
942 //=======================================================================
943
944 void FSD_File::SetRootSectionSize(const Standard_Integer aSize) 
945 {
946   myStream << aSize << "\n";
947   if (myStream.bad()) Storage_StreamWriteError::Raise();
948 }
949
950 //=======================================================================
951 //function : WriteRoot
952 //purpose  : 
953 //=======================================================================
954
955 void FSD_File::WriteRoot(const TCollection_AsciiString& rootName, const Standard_Integer aRef, const TCollection_AsciiString& rootType) 
956 {
957   myStream << aRef << " " << rootName.ToCString() << " " << rootType.ToCString() << "\n";
958   if (myStream.bad()) Storage_StreamWriteError::Raise();
959 }
960
961 //=======================================================================
962 //function : EndWriteRootSection
963 //purpose  : read
964 //=======================================================================
965
966 Storage_Error FSD_File::EndWriteRootSection() 
967 {
968   myStream << "END_ROOT_SECTION\n";
969   if (myStream.bad()) Storage_StreamWriteError::Raise();
970   return Storage_VSOk;
971 }
972
973 //=======================================================================
974 //function : BeginReadRootSection
975 //purpose  : ----------------------- ROOT : READ
976 //=======================================================================
977
978 Storage_Error FSD_File::BeginReadRootSection() 
979 {
980   return FindTag("BEGIN_ROOT_SECTION");
981 }
982
983 //=======================================================================
984 //function : RootSectionSize
985 //purpose  : 
986 //=======================================================================
987
988 Standard_Integer FSD_File::RootSectionSize() 
989 {
990   Standard_Integer i;
991
992   if (!(myStream >> i)) Storage_StreamTypeMismatchError::Raise();
993   
994   FlushEndOfLine();
995   
996   return i;
997 }
998
999 //=======================================================================
1000 //function : ReadRoot
1001 //purpose  : 
1002 //=======================================================================
1003
1004 void FSD_File::ReadRoot(TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType) 
1005 {
1006   if (!(myStream >> aRef)) Storage_StreamTypeMismatchError::Raise();
1007   ReadWord(rootName);
1008   ReadWord(rootType);
1009 }
1010
1011 //=======================================================================
1012 //function : EndReadRootSection
1013 //purpose  : REF SECTION
1014 //           write
1015 //=======================================================================
1016
1017 Storage_Error FSD_File::EndReadRootSection() 
1018 {
1019   return FindTag("END_ROOT_SECTION");
1020 }
1021
1022 //=======================================================================
1023 //function : BeginWriteRefSection
1024 //purpose  : -------------------------- REF : WRITE
1025 //=======================================================================
1026
1027 Storage_Error FSD_File::BeginWriteRefSection() 
1028 {
1029   myStream << "BEGIN_REF_SECTION\n";
1030   if (myStream.bad()) Storage_StreamWriteError::Raise();
1031   return Storage_VSOk;
1032 }
1033
1034 //=======================================================================
1035 //function : SetRefSectionSize
1036 //purpose  : 
1037 //=======================================================================
1038
1039 void FSD_File::SetRefSectionSize(const Standard_Integer aSize) 
1040 {
1041   myStream << aSize << "\n";
1042   if (myStream.bad()) Storage_StreamWriteError::Raise();
1043 }
1044
1045 //=======================================================================
1046 //function : WriteReferenceType
1047 //purpose  : 
1048 //=======================================================================
1049
1050 void FSD_File::WriteReferenceType(const Standard_Integer reference,
1051                                   const Standard_Integer typeNum) 
1052 {
1053   myStream << reference << " " << typeNum << "\n";
1054   if (myStream.bad()) Storage_StreamWriteError::Raise();
1055 }
1056
1057 //=======================================================================
1058 //function : EndWriteRefSection
1059 //purpose  : read
1060 //=======================================================================
1061
1062 Storage_Error FSD_File::EndWriteRefSection() 
1063 {
1064   myStream << "END_REF_SECTION\n";
1065   if (myStream.bad()) Storage_StreamWriteError::Raise();
1066   return Storage_VSOk;
1067 }
1068
1069 //=======================================================================
1070 //function : BeginReadRefSection
1071 //purpose  : ----------------------- REF : READ
1072 //=======================================================================
1073
1074 Storage_Error FSD_File::BeginReadRefSection() 
1075 {
1076   return FindTag("BEGIN_REF_SECTION");
1077 }
1078
1079 //=======================================================================
1080 //function : RefSectionSize
1081 //purpose  : 
1082 //=======================================================================
1083
1084 Standard_Integer FSD_File::RefSectionSize() 
1085 {
1086   Standard_Integer i;
1087
1088   if (!(myStream >> i)) Storage_StreamTypeMismatchError::Raise();
1089   FlushEndOfLine();
1090
1091   return i;
1092 }
1093
1094 //=======================================================================
1095 //function : ReadReferenceType
1096 //purpose  : 
1097 //=======================================================================
1098
1099 void FSD_File::ReadReferenceType(Standard_Integer& reference,
1100                                  Standard_Integer& typeNum) 
1101 {
1102   if (!(myStream >> reference)) Storage_StreamTypeMismatchError::Raise();
1103   if (!(myStream >> typeNum)) Storage_StreamTypeMismatchError::Raise();
1104   FlushEndOfLine();
1105 }
1106
1107 //=======================================================================
1108 //function : EndReadRefSection
1109 //purpose  : DATA SECTION
1110 //           write
1111 //=======================================================================
1112
1113 Storage_Error FSD_File::EndReadRefSection() 
1114 {
1115   return FindTag("END_REF_SECTION");
1116 }
1117
1118 //=======================================================================
1119 //function : BeginWriteDataSection
1120 //purpose  : -------------------- DATA : WRITE
1121 //=======================================================================
1122
1123 Storage_Error FSD_File::BeginWriteDataSection() 
1124 {
1125   myStream << "BEGIN_DATA_SECTION";
1126   if (myStream.bad()) Storage_StreamWriteError::Raise();
1127   return Storage_VSOk;
1128 }
1129
1130 //=======================================================================
1131 //function : WritePersistentObjectHeader
1132 //purpose  : 
1133 //=======================================================================
1134
1135 void FSD_File::WritePersistentObjectHeader(const Standard_Integer aRef,
1136                                            const Standard_Integer aType) 
1137 {
1138   myStream << "\n#" << aRef << "=%" << aType;
1139   if (myStream.bad()) Storage_StreamWriteError::Raise();
1140 }
1141
1142 //=======================================================================
1143 //function : BeginWritePersistentObjectData
1144 //purpose  : 
1145 //=======================================================================
1146
1147 void FSD_File::BeginWritePersistentObjectData() 
1148 {
1149   myStream << "( ";
1150   if (myStream.bad()) Storage_StreamWriteError::Raise();
1151 }
1152
1153 //=======================================================================
1154 //function : BeginWriteObjectData
1155 //purpose  : 
1156 //=======================================================================
1157
1158 void FSD_File::BeginWriteObjectData() 
1159 {
1160   myStream << "( ";
1161   if (myStream.bad()) Storage_StreamWriteError::Raise();
1162 }
1163
1164 //=======================================================================
1165 //function : EndWriteObjectData
1166 //purpose  : 
1167 //=======================================================================
1168
1169 void FSD_File::EndWriteObjectData() 
1170 {
1171   myStream << ") ";
1172   if (myStream.bad()) Storage_StreamWriteError::Raise();
1173 }
1174
1175 //=======================================================================
1176 //function : EndWritePersistentObjectData
1177 //purpose  : 
1178 //=======================================================================
1179
1180 void FSD_File::EndWritePersistentObjectData() 
1181 {
1182   myStream << ")";
1183   if (myStream.bad()) Storage_StreamWriteError::Raise();
1184 }
1185
1186 //=======================================================================
1187 //function : EndWriteDataSection
1188 //purpose  : read
1189 //=======================================================================
1190
1191 Storage_Error FSD_File::EndWriteDataSection() 
1192 {
1193   myStream << "\nEND_DATA_SECTION\n";
1194   if (myStream.bad()) Storage_StreamWriteError::Raise();
1195   return Storage_VSOk;
1196 }
1197
1198 //=======================================================================
1199 //function : BeginReadDataSection
1200 //purpose  : ---------------------- DATA : READ
1201 //=======================================================================
1202
1203 Storage_Error FSD_File::BeginReadDataSection() 
1204 {
1205   return FindTag("BEGIN_DATA_SECTION");
1206 }
1207
1208 //=======================================================================
1209 //function : ReadPersistentObjectHeader
1210 //purpose  : 
1211 //=======================================================================
1212
1213 void FSD_File::ReadPersistentObjectHeader(Standard_Integer& aRef,
1214                                           Standard_Integer& aType) 
1215 {
1216   char c;
1217
1218   myStream.get(c);
1219
1220   while (c != '#') {
1221     if (IsEnd() || (c != ' ') || (c == '\n')) {
1222       Storage_StreamFormatError::Raise();
1223     }
1224     myStream.get(c);
1225   }
1226
1227   if (!(myStream >> aRef)) Storage_StreamTypeMismatchError::Raise();
1228
1229   myStream.get(c);
1230
1231
1232    while (c != '=') {
1233     if (IsEnd() || (c != ' ') || (c == '\n')) {
1234       Storage_StreamFormatError::Raise();
1235     }
1236     myStream.get(c);
1237   }
1238
1239   myStream.get(c);
1240
1241   while (c != '%') {
1242     if (IsEnd() || (c != ' ') || (c == '\n')) {
1243       Storage_StreamFormatError::Raise();
1244     }
1245     myStream.get(c);
1246   }
1247
1248   if (!(myStream >> aType)) Storage_StreamTypeMismatchError::Raise();
1249 //  cout << "REF:" << aRef << " TYPE:"<< aType << endl;
1250 }
1251
1252 //=======================================================================
1253 //function : BeginReadPersistentObjectData
1254 //purpose  : 
1255 //=======================================================================
1256
1257 void FSD_File::BeginReadPersistentObjectData() 
1258 {
1259   char c;
1260   myStream.get(c);
1261   while (c != '(') {
1262     if (IsEnd() || (c != ' ') || (c == '\n')) {
1263       Storage_StreamFormatError::Raise();
1264     }
1265     myStream.get(c);
1266   }
1267
1268 //cout << "BeginReadPersistentObjectData" << endl;
1269 }
1270
1271 //=======================================================================
1272 //function : BeginReadObjectData
1273 //purpose  : 
1274 //=======================================================================
1275
1276 void FSD_File::BeginReadObjectData() 
1277 {
1278
1279   char c;
1280   myStream.get(c);
1281   while (c != '(') {
1282     if (IsEnd() || (c != ' ') || (c == '\n')) {
1283       Storage_StreamFormatError::Raise();
1284     }
1285     myStream.get(c);
1286   }
1287
1288 //  cout << "BeginReadObjectData" << endl;
1289 }
1290
1291 //=======================================================================
1292 //function : EndReadObjectData
1293 //purpose  : 
1294 //=======================================================================
1295
1296 void FSD_File::EndReadObjectData() 
1297 {
1298
1299   char c;
1300   myStream.get(c);
1301   while (c != ')') {
1302     if (IsEnd() || (c != ' ') || (c == '\n')) {
1303       Storage_StreamFormatError::Raise();
1304     }
1305     myStream.get(c);
1306   }
1307
1308 //  cout << "EndReadObjectData" << endl;
1309 }
1310
1311 //=======================================================================
1312 //function : EndReadPersistentObjectData
1313 //purpose  : 
1314 //=======================================================================
1315
1316 void FSD_File::EndReadPersistentObjectData() 
1317 {
1318
1319   char c;
1320
1321   myStream.get(c);
1322   while (c != ')') {
1323     if (IsEnd() || (c != ' ') || (c == '\n')) {
1324       Storage_StreamFormatError::Raise();
1325     }
1326     myStream.get(c);
1327   }
1328
1329   myStream.get(c);
1330   while (c != '\n') {
1331     if (IsEnd() || (c != ' ')) {
1332       Storage_StreamFormatError::Raise();
1333     }
1334     myStream.get(c);
1335   }
1336 //  cout << "EndReadPersistentObjectData" << endl;
1337 }
1338
1339 //=======================================================================
1340 //function : EndReadDataSection
1341 //purpose  : 
1342 //=======================================================================
1343
1344 Storage_Error FSD_File::EndReadDataSection() 
1345 {
1346   return FindTag("END_DATA_SECTION");
1347 }
1348
1349 //=======================================================================
1350 //function : Tell
1351 //purpose  : return position in the file. Return -1 upon error.
1352 //=======================================================================
1353
1354 Storage_Position FSD_File::Tell()
1355 {
1356   switch (OpenMode()) {
1357   case Storage_VSRead:
1358     return (Storage_Position) myStream.tellp();
1359   case Storage_VSWrite:
1360     return (Storage_Position) myStream.tellg();
1361   case Storage_VSReadWrite: {
1362     Storage_Position aPosR  = (Storage_Position) myStream.tellp();
1363     Storage_Position aPosW  = (Storage_Position) myStream.tellg();
1364     if (aPosR < aPosW)
1365       return aPosW;
1366     else
1367       return aPosR;
1368   }
1369   default: return -1;
1370   }
1371 }