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