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