0027961: Visualization - remove unused and no more working OpenGl_AVIWriter
[occt.git] / src / DDF / DDF_IOStream.cxx
CommitLineData
b311480e 1// Created by: DAUTRY Philippe
2// Copyright (c) 1997-1999 Matra Datavision
973c2be1 3// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16// ----------------
7fd59977 17
18// Version: 0.0
b311480e 19//Version Date Purpose
7fd59977 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
44const Standard_CString MAGICNUMBER = "FSDFILE";
45
46
47//=======================================================================
48//function : DDF_IOStream
49//purpose :
50//=======================================================================
51
c24d4017 52DDF_IOStream::DDF_IOStream() :
53 myIStream(NULL),
54 myOStream(NULL)
7fd59977 55{}
56
57//=======================================================================
58//function : Open
59//purpose :
60//=======================================================================
61
62Storage_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);
91322f44 81 myIStream->imbue (std::locale::classic()); // always use C locale
7fd59977 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);
91322f44 93 myOStream->imbue (std::locale::classic()); // make sure to always use C locale
7fd59977 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
110Storage_Error DDF_IOStream::Open(istream* anIStream)
111{
112 myOStream = NULL;
113 SetOpenMode(Storage_VSRead);
114 myIStream = anIStream;
115 myIStream->precision(17);
91322f44 116 myIStream->imbue (std::locale::classic()); // use always C locale
7fd59977 117 SetName("DDF_IOStream");
118 return Storage_VSOk; // ou Storage_VSAlreadyOpen ?
119}
120
121//=======================================================================
122//function : Open
123//purpose : "Opens" an ostream.
124//=======================================================================
125
126Storage_Error DDF_IOStream::Open(ostream* anOStream)
127{
128 myIStream = NULL;
129 SetOpenMode(Storage_VSWrite);
130 myOStream = anOStream;
131 myOStream->precision(17);
91322f44 132 myOStream->imbue (std::locale::classic()); // use always C locale
7fd59977 133 SetName("DDF_IOStream");
134 return Storage_VSOk; // ou Storage_VSAlreadyOpen ?
135}
136
137//=======================================================================
138//function : IsEnd
139//purpose :
140//=======================================================================
141
142Standard_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
153Storage_Error DDF_IOStream::Close()
154{ return Storage_VSOk; }
155
156
157// ------------------ PROTECTED
158
159
160//=======================================================================
161//function : MagicNumber
162//purpose :
163//=======================================================================
164
487bf1ce 165Standard_CString DDF_IOStream::MagicNumber()
7fd59977 166{ return MAGICNUMBER; }
167
168//=======================================================================
169//function : FlushEndOfLine
170//purpose :
171//=======================================================================
172
173void 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
196void 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
225void 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
247void 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();
008aef40 259 i = (Standard_ExtCharacter)c;
7fd59977 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;
008aef40 267 j = (Standard_ExtCharacter)c;
7fd59977 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
282void 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
301void DDF_IOStream::ReadString(TCollection_AsciiString& buffer)
302{
1d47d8d0 303 char c = '\0';
7fd59977 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
327void DDF_IOStream::ReadWord(TCollection_AsciiString& buffer)
328{
1d47d8d0 329 char c = '\0';
7fd59977 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
353Storage_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
376void DDF_IOStream::SkipObject()
377{
378 FlushEndOfLine();
379}
380
381
382// ---------------------- PUBLIC : PUT
383
384
385//=======================================================================
386//function : PutReference
387//purpose :
388//=======================================================================
389
390Storage_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
402Storage_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
414Storage_BaseDriver& DDF_IOStream::PutExtCharacter(const Standard_ExtCharacter aValue)
415{
c885cfda 416 *myOStream << (short )aValue << " ";
7fd59977 417 if (myOStream->bad()) Storage_StreamWriteError::Raise("PutExtCharacter");
418 return *this;
419}
420
421//=======================================================================
422//function : PutInteger
423//purpose :
424//=======================================================================
425
426Storage_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
438Storage_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
450Storage_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
462Storage_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
478Storage_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
489Storage_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
500Storage_BaseDriver& DDF_IOStream::GetExtCharacter(Standard_ExtCharacter& aValue)
501{
c885cfda 502 short aChar = 0;
503 if (!(*myIStream >> aChar)) Storage_StreamTypeMismatchError::Raise("GetExtCharacter");
504 aValue = aChar;
7fd59977 505 return *this;
506}
507
508//=======================================================================
509//function : GetInteger
510//purpose :
511//=======================================================================
512
513Storage_BaseDriver& DDF_IOStream::GetInteger(Standard_Integer& aValue)
514{
515 if (!(*myIStream >> aValue)) Storage_StreamTypeMismatchError::Raise("GetInteger");
516 return *this;
517}
518
519//=======================================================================
520//function : GetBoolean
521//purpose :
522//=======================================================================
523
524Storage_BaseDriver& DDF_IOStream::GetBoolean(Standard_Boolean& aValue)
525{
526 if (!(*myIStream >> aValue)) Storage_StreamTypeMismatchError::Raise("GetBoolean");
527 return *this;
528}
529
530//=======================================================================
531//function : GetReal
532//purpose :
533//=======================================================================
534
535Storage_BaseDriver& DDF_IOStream::GetReal(Standard_Real& aValue)
536{
537 if (!(*myIStream >> aValue)) Storage_StreamTypeMismatchError::Raise("GetReal");
538 return *this;
539}
540
541//=======================================================================
542//function : GetShortReal
543//purpose :
544//=======================================================================
545
546Storage_BaseDriver& DDF_IOStream::GetShortReal(Standard_ShortReal& aValue)
547{
548 if (!(*myIStream >> aValue)) Storage_StreamTypeMismatchError::Raise("GetShortReal");
549 return *this;
550}
551
552// -------------------------- DESTROY
553
554//=======================================================================
555//function : Destroy
556//purpose :
557//=======================================================================
558
559void DDF_IOStream::Destroy()
560{
561 if (OpenMode() != Storage_VSNone) Close();
562}
563
564
565// -------------------------- INFO : WRITE
566
567
568//=======================================================================
569//function : BeginWriteInfoSection
570//purpose :
571//=======================================================================
572
573Storage_Error DDF_IOStream::BeginWriteInfoSection()
574{
575 *myOStream << DDF_IOStream::MagicNumber() << '\n';
576 *myOStream << "BEGIN_INFO_SECTION\n";
577 if (myOStream->bad()) Storage_StreamWriteError::Raise();
578
579 return Storage_VSOk;
580}
581
582//=======================================================================
583//function : WriteInfo
584//purpose :
585//=======================================================================
586
587void DDF_IOStream::WriteInfo(const Standard_Integer nbObj,
588 const TCollection_AsciiString& dbVersion,
589 const TCollection_AsciiString& date,
590 const TCollection_AsciiString& schemaName,
591 const TCollection_AsciiString& schemaVersion,
592 const TCollection_ExtendedString& appName,
593 const TCollection_AsciiString& appVersion,
594 const TCollection_ExtendedString& dataType,
595 const TColStd_SequenceOfAsciiString& userInfo)
596{
597 Standard_Integer i;
598// char *extBuffer;
599
600 *myOStream << nbObj;
601 *myOStream << "\n";
602 *myOStream << dbVersion.ToCString() << "\n";
603 *myOStream << date.ToCString() << "\n";
604 *myOStream << schemaName.ToCString() << "\n";
605 *myOStream << schemaVersion.ToCString() << "\n";
606 WriteExtendedLine(appName);
607 *myOStream << appVersion.ToCString() << "\n";
608 WriteExtendedLine(dataType);
609 *myOStream << userInfo.Length() << "\n";
610
611 if (myOStream->bad()) Storage_StreamWriteError::Raise();
612
613 for (i = 1; i <= userInfo.Length(); i++) {
614 *myOStream << userInfo.Value(i).ToCString() << "\n";
615 if (myOStream->bad()) Storage_StreamWriteError::Raise();
616 }
617}
618
619
620//=======================================================================
621//function : EndWriteInfoSection
622//purpose :
623//=======================================================================
624
625Storage_Error DDF_IOStream::EndWriteInfoSection()
626{
627 *myOStream << "END_INFO_SECTION\n";
628 if (myOStream->bad()) Storage_StreamWriteError::Raise();
629 return Storage_VSOk;
630}
631
632//=======================================================================
633//function : BeginReadInfoSection
634//purpose :
635//=======================================================================
636
637Storage_Error DDF_IOStream::BeginReadInfoSection()
638{
639 Storage_Error s;
640 TCollection_AsciiString l;
60be1f9b 641 Standard_Integer len = (Standard_Integer) strlen(DDF_IOStream::MagicNumber());
7fd59977 642
643 // Added because of Draw:
644 // It don't go to next line after reading its own header line information!
645 FlushEndOfLine();
646 ReadChar(l,len);
647
648 if (strncmp(DDF_IOStream::MagicNumber(),l.ToCString(),len) != 0) {
0797d9d3 649#ifdef OCCT_DEBUG
7fd59977 650 cout<<"BeginReadInfoSection: format error"<<endl;
651#endif
652 s = Storage_VSFormatError;
653 }
654 else {
655 s = FindTag("BEGIN_INFO_SECTION");
656 }
657
658 return s;
659}
660
661
662// ------------------- INFO : READ
663
664
665//=======================================================================
666//function : ReadInfo
667//purpose :
668//=======================================================================
669
670void DDF_IOStream::ReadInfo(Standard_Integer& nbObj,
671 TCollection_AsciiString& dbVersion,
672 TCollection_AsciiString& date,
673 TCollection_AsciiString& schemaName,
674 TCollection_AsciiString& schemaVersion,
675 TCollection_ExtendedString& appName,
676 TCollection_AsciiString& appVersion,
677 TCollection_ExtendedString& dataType,
678 TColStd_SequenceOfAsciiString& userInfo)
679{
680 if (!(*myIStream >> nbObj)) Storage_StreamTypeMismatchError::Raise("ReadInfo 1");
681
682 FlushEndOfLine();
683
684 ReadLine(dbVersion);
685 ReadLine(date);
686 ReadLine(schemaName);
687 ReadLine(schemaVersion);
688 ReadExtendedLine(appName);
689 ReadLine(appVersion);
690 ReadExtendedLine(dataType);
691
692 Standard_Integer i,len = 0;
693
694 if (!(*myIStream >> len)) Storage_StreamTypeMismatchError::Raise("ReadInfo 2");
695
696 FlushEndOfLine();
697
698 TCollection_AsciiString line;
699
700 for (i = 1; i <= len && !IsEnd(); i++) {
701 ReadLine(line);
702 userInfo.Append(line);
703 line.Clear();
704 }
705}
706
4ff92abe 707//=======================================================================
708//function : ReadCompleteInfo
709//purpose :
710//=======================================================================
711void DDF_IOStream::ReadCompleteInfo( Standard_IStream& /*theIStream*/, Handle(Storage_Data)& /*theData*/ )
712{
713
714}
715
7fd59977 716//=======================================================================
717//function : EndReadInfoSection
718//purpose :
719//=======================================================================
720
721Storage_Error DDF_IOStream::EndReadInfoSection()
722{ return FindTag("END_INFO_SECTION"); }
723
724
725// ---------------- COMMENTS : WRITE
726
727
728//=======================================================================
729//function : BeginWriteCommentSection
730//purpose :
731//=======================================================================
732
733Storage_Error DDF_IOStream::BeginWriteCommentSection()
734{
735 *myOStream << "BEGIN_COMMENT_SECTION\n";
736 if (myOStream->bad()) Storage_StreamWriteError::Raise();
737 return Storage_VSOk;
738}
739
740//=======================================================================
741//function : WriteComment
742//purpose :
743//=======================================================================
744
745void DDF_IOStream::WriteComment(const TColStd_SequenceOfExtendedString& aCom)
746{
747 Standard_Integer i,aSize;
748
749 aSize = aCom.Length();
750 *myOStream << aSize << "\n";
751 if (myOStream->bad()) Storage_StreamWriteError::Raise();
752
753 for (i = 1; i <= aSize; i++) {
754 WriteExtendedLine(aCom.Value(i));
755 if (myOStream->bad()) Storage_StreamWriteError::Raise();
756 }
757}
758
759//=======================================================================
760//function : EndWriteCommentSection
761//purpose :
762//=======================================================================
763
764Storage_Error DDF_IOStream::EndWriteCommentSection()
765{
766 *myOStream << "END_COMMENT_SECTION\n";
767 if (myOStream->bad()) Storage_StreamWriteError::Raise();
768 return Storage_VSOk;
769}
770
771
772// ---------------- COMMENTS : READ
773
774
775//=======================================================================
776//function : BeginReadCommentSection
777//purpose :
778//=======================================================================
779
780Storage_Error DDF_IOStream::BeginReadCommentSection()
781{
782 return FindTag("BEGIN_COMMENT_SECTION");
783}
784
785//=======================================================================
786//function : ReadComment
787//purpose :
788//=======================================================================
789
790void DDF_IOStream::ReadComment(TColStd_SequenceOfExtendedString& aCom)
791{
792 TCollection_ExtendedString line;
793 Standard_Integer len,i;
794
795 if (!(*myIStream >> len)) Storage_StreamTypeMismatchError::Raise("ReadComment");
796
797 FlushEndOfLine();
798
799 for (i = 1; i <= len && !IsEnd(); i++) {
800 ReadExtendedLine(line);
801 aCom.Append(line);
802 line.Clear();
803 }
804}
805
806//=======================================================================
807//function : EndReadCommentSection
808//purpose :
809//=======================================================================
810
811Storage_Error DDF_IOStream::EndReadCommentSection()
812{ return FindTag("END_COMMENT_SECTION"); }
813
814
815// --------------- TYPE : WRITE
816
817
818//=======================================================================
819//function : BeginWriteTypeSection
820//purpose :
821//=======================================================================
822
823Storage_Error DDF_IOStream::BeginWriteTypeSection()
824{
825 *myOStream << "BEGIN_TYPE_SECTION\n";
826 if (myOStream->bad()) Storage_StreamWriteError::Raise();
827 return Storage_VSOk;
828}
829
830//=======================================================================
831//function : SetTypeSectionSize
832//purpose :
833//=======================================================================
834
835void DDF_IOStream::SetTypeSectionSize(const Standard_Integer aSize)
836{
837 *myOStream << aSize << "\n";
838 if (myOStream->bad()) Storage_StreamWriteError::Raise();
839}
840
841//=======================================================================
842//function : WriteTypeInformations
843//purpose :
844//=======================================================================
845
846void DDF_IOStream::WriteTypeInformations(const Standard_Integer typeNum,
847 const TCollection_AsciiString& typeName)
848{
849 *myOStream << typeNum << " " << typeName.ToCString() << "\n";
850 if (myOStream->bad()) Storage_StreamWriteError::Raise();
851}
852
853//=======================================================================
854//function : EndWriteTypeSection
855//purpose :
856//=======================================================================
857
858Storage_Error DDF_IOStream::EndWriteTypeSection()
859{
860 *myOStream << "END_TYPE_SECTION\n";
861 if (myOStream->bad()) Storage_StreamWriteError::Raise();
862 return Storage_VSOk;
863}
864
865
866// ------------------- TYPE : READ
867
868
869//=======================================================================
870//function : BeginReadTypeSection
871//purpose :
872//=======================================================================
873
874Storage_Error DDF_IOStream::BeginReadTypeSection()
875{ return FindTag("BEGIN_TYPE_SECTION"); }
876
877//=======================================================================
878//function : TypeSectionSize
879//purpose :
880//=======================================================================
881
882Standard_Integer DDF_IOStream::TypeSectionSize()
883{
884 Standard_Integer i;
885
886 if (!(*myIStream >> i)) Storage_StreamTypeMismatchError::Raise("TypeSectionSize");
887
888 FlushEndOfLine();
889
890 return i;
891}
892
893//=======================================================================
894//function : ReadTypeInformations
895//purpose :
896//=======================================================================
897
898void DDF_IOStream::ReadTypeInformations(Standard_Integer& typeNum,
899 TCollection_AsciiString& typeName)
900{
901 if (!(*myIStream >> typeNum)) Storage_StreamTypeMismatchError::Raise("ReadTypeInformations 1");
902 if (!(*myIStream >> typeName)) Storage_StreamTypeMismatchError::Raise("ReadTypeInformations 2");
903 FlushEndOfLine();
904}
905
906//=======================================================================
907//function : EndReadTypeSection
908//purpose :
909//=======================================================================
910
911Storage_Error DDF_IOStream::EndReadTypeSection()
912{
913 return FindTag("END_TYPE_SECTION");
914}
915
916
917// -------------------- ROOT : WRITE
918
919
920//=======================================================================
921//function : BeginWriteRootSection
922//purpose :
923//=======================================================================
924
925Storage_Error DDF_IOStream::BeginWriteRootSection()
926{
927 *myOStream << "BEGIN_ROOT_SECTION\n";
928 if (myOStream->bad()) Storage_StreamWriteError::Raise();
929 return Storage_VSOk;
930}
931
932//=======================================================================
933//function : SetRootSectionSize
934//purpose :
935//=======================================================================
936
937void DDF_IOStream::SetRootSectionSize(const Standard_Integer aSize)
938{
939 *myOStream << aSize << "\n";
940 if (myOStream->bad()) Storage_StreamWriteError::Raise();
941}
942
943//=======================================================================
944//function : WriteRoot
945//purpose :
946//=======================================================================
947
948void DDF_IOStream::WriteRoot(const TCollection_AsciiString& rootName, const Standard_Integer aRef, const TCollection_AsciiString& rootType)
949{
950 *myOStream << aRef << " " << rootName.ToCString() << " " << rootType.ToCString() << "\n";
951 if (myOStream->bad()) Storage_StreamWriteError::Raise();
952}
953
954//=======================================================================
955//function : EndWriteRootSection
956//purpose :
957//=======================================================================
958
959Storage_Error DDF_IOStream::EndWriteRootSection()
960{
961 *myOStream << "END_ROOT_SECTION\n";
962 if (myOStream->bad()) Storage_StreamWriteError::Raise();
963 return Storage_VSOk;
964}
965
966
967// ----------------------- ROOT : READ
968
969
970//=======================================================================
971//function : BeginReadRootSection
972//purpose :
973//=======================================================================
974
975Storage_Error DDF_IOStream::BeginReadRootSection()
976{ return FindTag("BEGIN_ROOT_SECTION"); }
977
978//=======================================================================
979//function : RootSectionSize
980//purpose :
981//=======================================================================
982
983Standard_Integer DDF_IOStream::RootSectionSize()
984{
985 Standard_Integer i;
986
987 if (!(*myIStream >> i)) Storage_StreamTypeMismatchError::Raise("RootSectionSize");
988
989 FlushEndOfLine();
990
991 return i;
992}
993
994//=======================================================================
995//function : ReadRoot
996//purpose :
997//=======================================================================
998
999void DDF_IOStream::ReadRoot(TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType)
1000{
1001 if (!(*myIStream >> aRef)) Storage_StreamTypeMismatchError::Raise("ReadRoot");
1002 ReadWord(rootName);
1003 ReadWord(rootType);
1004}
1005
1006//=======================================================================
1007//function : EndReadRootSection
1008//purpose :
1009//=======================================================================
1010
1011Storage_Error DDF_IOStream::EndReadRootSection()
1012{ return FindTag("END_ROOT_SECTION"); }
1013
1014
1015// -------------------------- REF : WRITE
1016
1017
1018//=======================================================================
1019//function : BeginWriteRefSection
1020//purpose :
1021//=======================================================================
1022
1023Storage_Error DDF_IOStream::BeginWriteRefSection()
1024{
1025 *myOStream << "BEGIN_REF_SECTION\n";
1026 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1027 return Storage_VSOk;
1028}
1029
1030//=======================================================================
1031//function : SetRefSectionSize
1032//purpose :
1033//=======================================================================
1034
1035void DDF_IOStream::SetRefSectionSize(const Standard_Integer aSize)
1036{
1037 *myOStream << aSize << "\n";
1038 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1039}
1040
1041//=======================================================================
1042//function : WriteReferenceType
1043//purpose :
1044//=======================================================================
1045
1046void DDF_IOStream::WriteReferenceType(const Standard_Integer reference,
1047 const Standard_Integer typeNum)
1048{
1049 *myOStream << reference << " " << typeNum << "\n";
1050 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1051}
1052
1053//=======================================================================
1054//function : EndWriteRefSection
1055//purpose :
1056//=======================================================================
1057
1058Storage_Error DDF_IOStream::EndWriteRefSection()
1059{
1060 *myOStream << "END_REF_SECTION\n";
1061 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1062 return Storage_VSOk;
1063}
1064
1065
1066// ----------------------- REF : READ
1067
1068
1069//=======================================================================
1070//function : BeginReadRefSection
1071//purpose :
1072//=======================================================================
1073
1074Storage_Error DDF_IOStream::BeginReadRefSection()
1075{ return FindTag("BEGIN_REF_SECTION"); }
1076
1077//=======================================================================
1078//function : RefSectionSize
1079//purpose :
1080//=======================================================================
1081
1082Standard_Integer DDF_IOStream::RefSectionSize()
1083{
1084 Standard_Integer i;
1085
1086 if (!(*myIStream >> i)) Storage_StreamTypeMismatchError::Raise("RefSectionSize");
1087 FlushEndOfLine();
1088
1089 return i;
1090}
1091
1092//=======================================================================
1093//function : ReadReferenceType
1094//purpose :
1095//=======================================================================
1096
1097void DDF_IOStream::ReadReferenceType(Standard_Integer& reference,
1098 Standard_Integer& typeNum)
1099{
1100 if (!(*myIStream >> reference)) Storage_StreamTypeMismatchError::Raise("ReadReferenceType 1");
1101 if (!(*myIStream >> typeNum)) Storage_StreamTypeMismatchError::Raise("ReadReferenceType 2");
1102 FlushEndOfLine();
1103}
1104
1105//=======================================================================
1106//function : EndReadRefSection
1107//purpose :
1108//=======================================================================
1109
1110Storage_Error DDF_IOStream::EndReadRefSection()
1111{
1112 return FindTag("END_REF_SECTION");
1113}
1114
1115
1116// -------------------- DATA : WRITE
1117
1118
1119//=======================================================================
1120//function : BeginWriteDataSection
1121//purpose :
1122//=======================================================================
1123
1124Storage_Error DDF_IOStream::BeginWriteDataSection()
1125{
1126 *myOStream << "BEGIN_DATA_SECTION";
1127 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1128 return Storage_VSOk;
1129}
1130
1131//=======================================================================
1132//function : WritePersistentObjectHeader
1133//purpose :
1134//=======================================================================
1135
1136void DDF_IOStream::WritePersistentObjectHeader(const Standard_Integer aRef,
1137 const Standard_Integer aType)
1138{
1139 *myOStream << "\n#" << aRef << "=%" << aType;
1140 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1141}
1142
1143//=======================================================================
1144//function : BeginWritePersistentObjectData
1145//purpose :
1146//=======================================================================
1147
1148void DDF_IOStream::BeginWritePersistentObjectData()
1149{
1150 *myOStream << "( ";
1151 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1152}
1153
1154//=======================================================================
1155//function : BeginWriteObjectData
1156//purpose :
1157//=======================================================================
1158
1159void DDF_IOStream::BeginWriteObjectData()
1160{
1161 *myOStream << "( ";
1162 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1163}
1164
1165//=======================================================================
1166//function : EndWriteObjectData
1167//purpose :
1168//=======================================================================
1169
1170void DDF_IOStream::EndWriteObjectData()
1171{
1172 *myOStream << ") ";
1173 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1174}
1175
1176//=======================================================================
1177//function : EndWritePersistentObjectData
1178//purpose :
1179//=======================================================================
1180
1181void DDF_IOStream::EndWritePersistentObjectData()
1182{
1183 *myOStream << ")";
1184 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1185}
1186
1187//=======================================================================
1188//function : EndWriteDataSection
1189//purpose :
1190//=======================================================================
1191
1192Storage_Error DDF_IOStream::EndWriteDataSection()
1193{
1194 *myOStream << "\nEND_DATA_SECTION\n";
1195 if (myOStream->bad()) Storage_StreamWriteError::Raise();
1196 return Storage_VSOk;
1197}
1198
1199
1200// ---------------------- DATA : READ
1201
1202
1203//=======================================================================
1204//function : BeginReadDataSection
1205//purpose :
1206//=======================================================================
1207
1208Storage_Error DDF_IOStream::BeginReadDataSection()
1209{ return FindTag("BEGIN_DATA_SECTION"); }
1210
1211//=======================================================================
1212//function : ReadPersistentObjectHeader
1213//purpose :
1214//=======================================================================
1215
1216void DDF_IOStream::ReadPersistentObjectHeader(Standard_Integer& aRef,
1217 Standard_Integer& aType)
1218{
1219 char c;
1220
1221 myIStream->get(c);
1222
1223 while (c != '#') {
1224 if (IsEnd() || (c != ' ') || (c == '\n')) {
1225 Storage_StreamFormatError::Raise();
1226 }
1227 myIStream->get(c);
1228 }
1229
1230 if (!(*myIStream >> aRef)) Storage_StreamTypeMismatchError::Raise("ReadPersistentObjectHeader 1");
1231 myIStream->get(c);
1232
1233 while (c != '=') {
1234 if (IsEnd() || (c != ' ') || (c == '\n')) {
1235 Storage_StreamFormatError::Raise();
1236 }
1237 myIStream->get(c);
1238 }
1239
1240 myIStream->get(c);
1241
1242 while (c != '%') {
1243 if (IsEnd() || (c != ' ') || (c == '\n')) {
1244 Storage_StreamFormatError::Raise();
1245 }
1246 myIStream->get(c);
1247 }
1248
1249 if (!(*myIStream >> aType)) Storage_StreamTypeMismatchError::Raise("ReadPersistentObjectHeader 2");
1250}
1251
1252//=======================================================================
1253//function : BeginReadPersistentObjectData
1254//purpose :
1255//=======================================================================
1256
1257void DDF_IOStream::BeginReadPersistentObjectData()
1258{
1259 char c;
1260 myIStream->get(c);
1261 while (c != '(') {
1262 if (IsEnd() || (c != ' ') || (c == '\n')) {
1263 Storage_StreamFormatError::Raise();
1264 }
1265 myIStream->get(c);
1266 }
1267}
1268
1269//=======================================================================
1270//function : BeginReadObjectData
1271//purpose :
1272//=======================================================================
1273
1274void DDF_IOStream::BeginReadObjectData()
1275{
1276 char c;
1277 myIStream->get(c);
1278 while (c != '(') {
1279 if (IsEnd() || (c != ' ') || (c == '\n')) {
1280 Storage_StreamFormatError::Raise("BeginReadObjectData");
1281 }
1282 myIStream->get(c);
1283 }
1284}
1285
1286//=======================================================================
1287//function : EndReadObjectData
1288//purpose :
1289//=======================================================================
1290
1291void DDF_IOStream::EndReadObjectData()
1292{
1293 char c;
1294 myIStream->get(c);
1295 while (c != ')') {
1296 if (IsEnd() || (c != ' ') || (c == '\n')) {
1297 Storage_StreamFormatError::Raise("EndReadObjectData");
1298 }
1299 myIStream->get(c);
1300 }
1301}
1302
1303//=======================================================================
1304//function : EndReadPersistentObjectData
1305//purpose :
1306//=======================================================================
1307
1308void DDF_IOStream::EndReadPersistentObjectData()
1309{
1310 char c;
1311
1312 myIStream->get(c);
1313 while (c != ')') {
1314 if (IsEnd() || (c != ' ') || (c == '\n')) {
1315 Storage_StreamFormatError::Raise("EndReadPersistentObjectData");
1316 }
1317 myIStream->get(c);
1318 }
1319
1320 myIStream->get(c);
1321 while (c != '\n') {
1322 if (IsEnd() || (c != ' ')) {
1323 Storage_StreamFormatError::Raise();
1324 }
1325 myIStream->get(c);
1326 }
1327}
1328
1329//=======================================================================
1330//function : EndReadDataSection
1331//purpose :
1332//=======================================================================
1333
1334Storage_Error DDF_IOStream::EndReadDataSection()
1335{ return FindTag("END_DATA_SECTION"); }
1336
1337//=======================================================================
1338//function : IsGoodFileType
1339//purpose :
1340//=======================================================================
1341
1342Storage_Error DDF_IOStream::IsGoodFileType(istream* anIStream)
1343{
1344 DDF_IOStream f;
1345 Storage_Error s;
1346
1347 s = f.Open(anIStream);
1348
1349 if (s == Storage_VSOk) {
1350 TCollection_AsciiString l;
60be1f9b 1351 Standard_Integer len = (Standard_Integer) strlen(DDF_IOStream::MagicNumber());
7fd59977 1352
1353 f.ReadChar(l,len);
1354
1355 f.Close();
1356
1357 if (strncmp(DDF_IOStream::MagicNumber(),l.ToCString(),len) != 0) {
0797d9d3 1358#ifdef OCCT_DEBUG
7fd59977 1359 cout<<"IsGoodFileType: format error"<<endl;
1360#endif
1361 s = Storage_VSFormatError;
1362 }
1363 }
1364
1365 return s;
1366}
1367