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