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