0026377: Passing Handle objects as arguments to functions as non-const reference...
[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 under
8 // the terms of the GNU Lesser General Public License 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 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 OCCT_DEBUG
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 : ReadCompleteInfo
707 //purpose  : 
708 //=======================================================================
709 void DDF_IOStream::ReadCompleteInfo( Standard_IStream& /*theIStream*/, Handle(Storage_Data)& /*theData*/ )
710 {
711
712 }
713
714 //=======================================================================
715 //function : EndReadInfoSection
716 //purpose  : 
717 //=======================================================================
718
719 Storage_Error DDF_IOStream::EndReadInfoSection() 
720 { return FindTag("END_INFO_SECTION"); }
721
722
723 // ---------------- COMMENTS : WRITE
724
725
726 //=======================================================================
727 //function : BeginWriteCommentSection
728 //purpose  : 
729 //=======================================================================
730
731 Storage_Error DDF_IOStream::BeginWriteCommentSection() 
732 {
733   *myOStream << "BEGIN_COMMENT_SECTION\n";
734   if (myOStream->bad()) Storage_StreamWriteError::Raise();
735   return Storage_VSOk;
736 }
737
738 //=======================================================================
739 //function : WriteComment
740 //purpose  : 
741 //=======================================================================
742
743 void DDF_IOStream::WriteComment(const TColStd_SequenceOfExtendedString& aCom)
744 {
745  Standard_Integer i,aSize;
746
747  aSize = aCom.Length();
748  *myOStream << aSize << "\n";
749  if (myOStream->bad()) Storage_StreamWriteError::Raise();
750
751  for (i = 1; i <= aSize; i++) {
752    WriteExtendedLine(aCom.Value(i));
753    if (myOStream->bad()) Storage_StreamWriteError::Raise();
754  }
755 }
756
757 //=======================================================================
758 //function : EndWriteCommentSection
759 //purpose  : 
760 //=======================================================================
761
762 Storage_Error DDF_IOStream::EndWriteCommentSection() 
763 {
764   *myOStream << "END_COMMENT_SECTION\n";
765   if (myOStream->bad()) Storage_StreamWriteError::Raise();
766   return Storage_VSOk;
767 }
768
769
770 // ---------------- COMMENTS : READ
771
772
773 //=======================================================================
774 //function : BeginReadCommentSection
775 //purpose  : 
776 //=======================================================================
777
778 Storage_Error DDF_IOStream::BeginReadCommentSection() 
779 {
780   return FindTag("BEGIN_COMMENT_SECTION");
781 }
782
783 //=======================================================================
784 //function : ReadComment
785 //purpose  : 
786 //=======================================================================
787
788 void DDF_IOStream::ReadComment(TColStd_SequenceOfExtendedString& aCom)
789 {
790   TCollection_ExtendedString line;
791   Standard_Integer           len,i;
792
793   if (!(*myIStream >> len)) Storage_StreamTypeMismatchError::Raise("ReadComment");
794   
795   FlushEndOfLine();  
796
797   for (i = 1; i <= len && !IsEnd(); i++) {
798     ReadExtendedLine(line);
799     aCom.Append(line);
800     line.Clear();
801   }
802 }
803
804 //=======================================================================
805 //function : EndReadCommentSection
806 //purpose  : 
807 //=======================================================================
808
809 Storage_Error DDF_IOStream::EndReadCommentSection() 
810 { return FindTag("END_COMMENT_SECTION"); }
811
812
813 // --------------- TYPE : WRITE
814
815
816 //=======================================================================
817 //function : BeginWriteTypeSection
818 //purpose  : 
819 //=======================================================================
820
821 Storage_Error DDF_IOStream::BeginWriteTypeSection() 
822 {
823   *myOStream << "BEGIN_TYPE_SECTION\n";
824   if (myOStream->bad()) Storage_StreamWriteError::Raise();
825   return Storage_VSOk;
826 }
827
828 //=======================================================================
829 //function : SetTypeSectionSize
830 //purpose  : 
831 //=======================================================================
832
833 void DDF_IOStream::SetTypeSectionSize(const Standard_Integer aSize) 
834 {
835   *myOStream << aSize << "\n";
836   if (myOStream->bad()) Storage_StreamWriteError::Raise();
837 }
838
839 //=======================================================================
840 //function : WriteTypeInformations
841 //purpose  : 
842 //=======================================================================
843
844 void DDF_IOStream::WriteTypeInformations(const Standard_Integer typeNum,
845                                       const TCollection_AsciiString& typeName) 
846 {
847   *myOStream << typeNum << " " << typeName.ToCString() << "\n";
848   if (myOStream->bad()) Storage_StreamWriteError::Raise();
849 }
850
851 //=======================================================================
852 //function : EndWriteTypeSection
853 //purpose  : 
854 //=======================================================================
855
856 Storage_Error DDF_IOStream::EndWriteTypeSection() 
857 {
858   *myOStream << "END_TYPE_SECTION\n";
859   if (myOStream->bad()) Storage_StreamWriteError::Raise();
860   return Storage_VSOk;
861 }
862
863
864 // ------------------- TYPE : READ
865
866
867 //=======================================================================
868 //function : BeginReadTypeSection
869 //purpose  : 
870 //=======================================================================
871
872 Storage_Error DDF_IOStream::BeginReadTypeSection() 
873 { return FindTag("BEGIN_TYPE_SECTION"); }
874
875 //=======================================================================
876 //function : TypeSectionSize
877 //purpose  : 
878 //=======================================================================
879
880 Standard_Integer DDF_IOStream::TypeSectionSize() 
881 {
882   Standard_Integer i;
883
884   if (!(*myIStream >> i)) Storage_StreamTypeMismatchError::Raise("TypeSectionSize");
885
886   FlushEndOfLine();
887
888   return i;
889 }
890
891 //=======================================================================
892 //function : ReadTypeInformations
893 //purpose  : 
894 //=======================================================================
895
896 void DDF_IOStream::ReadTypeInformations(Standard_Integer& typeNum,
897                                     TCollection_AsciiString& typeName) 
898 {
899   if (!(*myIStream >> typeNum)) Storage_StreamTypeMismatchError::Raise("ReadTypeInformations 1");
900   if (!(*myIStream >> typeName)) Storage_StreamTypeMismatchError::Raise("ReadTypeInformations 2");
901   FlushEndOfLine();
902 }
903
904 //=======================================================================
905 //function : EndReadTypeSection
906 //purpose  : 
907 //=======================================================================
908
909 Storage_Error DDF_IOStream::EndReadTypeSection() 
910 {
911   return FindTag("END_TYPE_SECTION");
912 }
913
914
915 // -------------------- ROOT : WRITE
916
917
918 //=======================================================================
919 //function : BeginWriteRootSection
920 //purpose  : 
921 //=======================================================================
922
923 Storage_Error DDF_IOStream::BeginWriteRootSection() 
924 {
925   *myOStream << "BEGIN_ROOT_SECTION\n";
926   if (myOStream->bad()) Storage_StreamWriteError::Raise();
927   return Storage_VSOk;
928 }
929
930 //=======================================================================
931 //function : SetRootSectionSize
932 //purpose  : 
933 //=======================================================================
934
935 void DDF_IOStream::SetRootSectionSize(const Standard_Integer aSize) 
936 {
937   *myOStream << aSize << "\n";
938   if (myOStream->bad()) Storage_StreamWriteError::Raise();
939 }
940
941 //=======================================================================
942 //function : WriteRoot
943 //purpose  : 
944 //=======================================================================
945
946 void DDF_IOStream::WriteRoot(const TCollection_AsciiString& rootName, const Standard_Integer aRef, const TCollection_AsciiString& rootType) 
947 {
948   *myOStream << aRef << " " << rootName.ToCString() << " " << rootType.ToCString() << "\n";
949   if (myOStream->bad()) Storage_StreamWriteError::Raise();
950 }
951
952 //=======================================================================
953 //function : EndWriteRootSection
954 //purpose  : 
955 //=======================================================================
956
957 Storage_Error DDF_IOStream::EndWriteRootSection() 
958 {
959   *myOStream << "END_ROOT_SECTION\n";
960   if (myOStream->bad()) Storage_StreamWriteError::Raise();
961   return Storage_VSOk;
962 }
963
964
965 // ----------------------- ROOT : READ
966
967
968 //=======================================================================
969 //function : BeginReadRootSection
970 //purpose  : 
971 //=======================================================================
972
973 Storage_Error DDF_IOStream::BeginReadRootSection() 
974 { return FindTag("BEGIN_ROOT_SECTION"); }
975
976 //=======================================================================
977 //function : RootSectionSize
978 //purpose  : 
979 //=======================================================================
980
981 Standard_Integer DDF_IOStream::RootSectionSize() 
982 {
983   Standard_Integer i;
984
985   if (!(*myIStream >> i)) Storage_StreamTypeMismatchError::Raise("RootSectionSize");
986   
987   FlushEndOfLine();
988   
989   return i;
990 }
991
992 //=======================================================================
993 //function : ReadRoot
994 //purpose  : 
995 //=======================================================================
996
997 void DDF_IOStream::ReadRoot(TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType) 
998 {
999   if (!(*myIStream >> aRef)) Storage_StreamTypeMismatchError::Raise("ReadRoot");
1000   ReadWord(rootName);
1001   ReadWord(rootType);
1002 }
1003
1004 //=======================================================================
1005 //function : EndReadRootSection
1006 //purpose  : 
1007 //=======================================================================
1008
1009 Storage_Error DDF_IOStream::EndReadRootSection() 
1010 { return FindTag("END_ROOT_SECTION"); }
1011
1012
1013 // -------------------------- REF : WRITE
1014
1015
1016 //=======================================================================
1017 //function : BeginWriteRefSection
1018 //purpose  : 
1019 //=======================================================================
1020
1021 Storage_Error DDF_IOStream::BeginWriteRefSection() 
1022 {
1023   *myOStream << "BEGIN_REF_SECTION\n";
1024   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1025   return Storage_VSOk;
1026 }
1027
1028 //=======================================================================
1029 //function : SetRefSectionSize
1030 //purpose  : 
1031 //=======================================================================
1032
1033 void DDF_IOStream::SetRefSectionSize(const Standard_Integer aSize) 
1034 {
1035   *myOStream << aSize << "\n";
1036   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1037 }
1038
1039 //=======================================================================
1040 //function : WriteReferenceType
1041 //purpose  : 
1042 //=======================================================================
1043
1044 void DDF_IOStream::WriteReferenceType(const Standard_Integer reference,
1045                                   const Standard_Integer typeNum) 
1046 {
1047   *myOStream << reference << " " << typeNum << "\n";
1048   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1049 }
1050
1051 //=======================================================================
1052 //function : EndWriteRefSection
1053 //purpose  : 
1054 //=======================================================================
1055
1056 Storage_Error DDF_IOStream::EndWriteRefSection() 
1057 {
1058   *myOStream << "END_REF_SECTION\n";
1059   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1060   return Storage_VSOk;
1061 }
1062
1063
1064 // ----------------------- REF : READ
1065
1066
1067 //=======================================================================
1068 //function : BeginReadRefSection
1069 //purpose  : 
1070 //=======================================================================
1071
1072 Storage_Error DDF_IOStream::BeginReadRefSection() 
1073 { return FindTag("BEGIN_REF_SECTION"); }
1074
1075 //=======================================================================
1076 //function : RefSectionSize
1077 //purpose  : 
1078 //=======================================================================
1079
1080 Standard_Integer DDF_IOStream::RefSectionSize() 
1081 {
1082   Standard_Integer i;
1083
1084   if (!(*myIStream >> i)) Storage_StreamTypeMismatchError::Raise("RefSectionSize");
1085   FlushEndOfLine();
1086
1087   return i;
1088 }
1089
1090 //=======================================================================
1091 //function : ReadReferenceType
1092 //purpose  : 
1093 //=======================================================================
1094
1095 void DDF_IOStream::ReadReferenceType(Standard_Integer& reference,
1096                                  Standard_Integer& typeNum) 
1097 {
1098   if (!(*myIStream >> reference)) Storage_StreamTypeMismatchError::Raise("ReadReferenceType 1");
1099   if (!(*myIStream >> typeNum)) Storage_StreamTypeMismatchError::Raise("ReadReferenceType 2");
1100   FlushEndOfLine();
1101 }
1102
1103 //=======================================================================
1104 //function : EndReadRefSection
1105 //purpose  : 
1106 //=======================================================================
1107
1108 Storage_Error DDF_IOStream::EndReadRefSection() 
1109 {
1110   return FindTag("END_REF_SECTION");
1111 }
1112
1113
1114 // -------------------- DATA : WRITE
1115
1116
1117 //=======================================================================
1118 //function : BeginWriteDataSection
1119 //purpose  : 
1120 //=======================================================================
1121
1122 Storage_Error DDF_IOStream::BeginWriteDataSection() 
1123 {
1124   *myOStream << "BEGIN_DATA_SECTION";
1125   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1126   return Storage_VSOk;
1127 }
1128
1129 //=======================================================================
1130 //function : WritePersistentObjectHeader
1131 //purpose  : 
1132 //=======================================================================
1133
1134 void DDF_IOStream::WritePersistentObjectHeader(const Standard_Integer aRef,
1135                                            const Standard_Integer aType) 
1136 {
1137   *myOStream << "\n#" << aRef << "=%" << aType;
1138   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1139 }
1140
1141 //=======================================================================
1142 //function : BeginWritePersistentObjectData
1143 //purpose  : 
1144 //=======================================================================
1145
1146 void DDF_IOStream::BeginWritePersistentObjectData() 
1147 {
1148   *myOStream << "( ";
1149   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1150 }
1151
1152 //=======================================================================
1153 //function : BeginWriteObjectData
1154 //purpose  : 
1155 //=======================================================================
1156
1157 void DDF_IOStream::BeginWriteObjectData() 
1158 {
1159   *myOStream << "( ";
1160   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1161 }
1162
1163 //=======================================================================
1164 //function : EndWriteObjectData
1165 //purpose  : 
1166 //=======================================================================
1167
1168 void DDF_IOStream::EndWriteObjectData() 
1169 {
1170   *myOStream << ") ";
1171   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1172 }
1173
1174 //=======================================================================
1175 //function : EndWritePersistentObjectData
1176 //purpose  : 
1177 //=======================================================================
1178
1179 void DDF_IOStream::EndWritePersistentObjectData() 
1180 {
1181   *myOStream << ")";
1182   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1183 }
1184
1185 //=======================================================================
1186 //function : EndWriteDataSection
1187 //purpose  : 
1188 //=======================================================================
1189
1190 Storage_Error DDF_IOStream::EndWriteDataSection() 
1191 {
1192   *myOStream << "\nEND_DATA_SECTION\n";
1193   if (myOStream->bad()) Storage_StreamWriteError::Raise();
1194   return Storage_VSOk;
1195 }
1196
1197
1198 // ---------------------- DATA : READ
1199
1200
1201 //=======================================================================
1202 //function : BeginReadDataSection
1203 //purpose  : 
1204 //=======================================================================
1205
1206 Storage_Error DDF_IOStream::BeginReadDataSection() 
1207 { return FindTag("BEGIN_DATA_SECTION"); }
1208
1209 //=======================================================================
1210 //function : ReadPersistentObjectHeader
1211 //purpose  : 
1212 //=======================================================================
1213
1214 void DDF_IOStream::ReadPersistentObjectHeader(Standard_Integer& aRef,
1215                                           Standard_Integer& aType) 
1216 {
1217   char c;
1218
1219   myIStream->get(c);
1220
1221   while (c != '#') {
1222     if (IsEnd() || (c != ' ') || (c == '\n')) {
1223       Storage_StreamFormatError::Raise();
1224     }
1225     myIStream->get(c);
1226   }
1227
1228   if (!(*myIStream >> aRef)) Storage_StreamTypeMismatchError::Raise("ReadPersistentObjectHeader 1");
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   myIStream->get(c);
1239
1240   while (c != '%') {
1241     if (IsEnd() || (c != ' ') || (c == '\n')) {
1242       Storage_StreamFormatError::Raise();
1243     }
1244     myIStream->get(c);
1245   }
1246
1247   if (!(*myIStream >> aType)) Storage_StreamTypeMismatchError::Raise("ReadPersistentObjectHeader 2");
1248 }
1249
1250 //=======================================================================
1251 //function : BeginReadPersistentObjectData
1252 //purpose  : 
1253 //=======================================================================
1254
1255 void DDF_IOStream::BeginReadPersistentObjectData() 
1256 {
1257   char c;
1258   myIStream->get(c);
1259   while (c != '(') {
1260     if (IsEnd() || (c != ' ') || (c == '\n')) {
1261       Storage_StreamFormatError::Raise();
1262     }
1263     myIStream->get(c);
1264   }
1265 }
1266
1267 //=======================================================================
1268 //function : BeginReadObjectData
1269 //purpose  : 
1270 //=======================================================================
1271
1272 void DDF_IOStream::BeginReadObjectData() 
1273 {
1274   char c;
1275   myIStream->get(c);
1276   while (c != '(') {
1277     if (IsEnd() || (c != ' ') || (c == '\n')) {
1278       Storage_StreamFormatError::Raise("BeginReadObjectData");
1279     }
1280     myIStream->get(c);
1281   }
1282 }
1283
1284 //=======================================================================
1285 //function : EndReadObjectData
1286 //purpose  : 
1287 //=======================================================================
1288
1289 void DDF_IOStream::EndReadObjectData() 
1290 {
1291   char c;
1292   myIStream->get(c);
1293   while (c != ')') {
1294     if (IsEnd() || (c != ' ') || (c == '\n')) {
1295       Storage_StreamFormatError::Raise("EndReadObjectData");
1296     }
1297     myIStream->get(c);
1298   }
1299 }
1300
1301 //=======================================================================
1302 //function : EndReadPersistentObjectData
1303 //purpose  : 
1304 //=======================================================================
1305
1306 void DDF_IOStream::EndReadPersistentObjectData() 
1307 {
1308   char c;
1309
1310   myIStream->get(c);
1311   while (c != ')') {
1312     if (IsEnd() || (c != ' ') || (c == '\n')) {
1313       Storage_StreamFormatError::Raise("EndReadPersistentObjectData");
1314     }
1315     myIStream->get(c);
1316   }
1317
1318   myIStream->get(c);
1319   while (c != '\n') {
1320     if (IsEnd() || (c != ' ')) {
1321       Storage_StreamFormatError::Raise();
1322     }
1323     myIStream->get(c);
1324   }
1325 }
1326
1327 //=======================================================================
1328 //function : EndReadDataSection
1329 //purpose  : 
1330 //=======================================================================
1331
1332 Storage_Error DDF_IOStream::EndReadDataSection() 
1333 { return FindTag("END_DATA_SECTION"); }
1334
1335 //=======================================================================
1336 //function : IsGoodFileType
1337 //purpose  : 
1338 //=======================================================================
1339
1340 Storage_Error DDF_IOStream::IsGoodFileType(istream* anIStream)
1341 {
1342   DDF_IOStream      f;
1343   Storage_Error s;
1344
1345   s = f.Open(anIStream);
1346
1347   if (s == Storage_VSOk) {
1348     TCollection_AsciiString l;
1349     Standard_Integer        len = (Standard_Integer) strlen(DDF_IOStream::MagicNumber());
1350
1351     f.ReadChar(l,len);
1352
1353     f.Close();
1354
1355     if (strncmp(DDF_IOStream::MagicNumber(),l.ToCString(),len) != 0) {
1356 #ifdef OCCT_DEBUG
1357     cout<<"IsGoodFileType: format error"<<endl;
1358 #endif
1359       s = Storage_VSFormatError;
1360     }
1361   }
1362
1363   return s;
1364 }
1365