eb0fd5767c6c5230f6f378432fea2cbc79ab1a4a
[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 << (short )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   short aChar = 0;
543   if (!(myStream >> aChar)) Storage_StreamTypeMismatchError::Raise();
544   aValue = aChar;
545   return *this;
546 }
547
548 //=======================================================================
549 //function : GetInteger
550 //purpose  : 
551 //=======================================================================
552
553 Storage_BaseDriver& FSD_CmpFile::GetInteger(Standard_Integer& aValue)
554 {
555   if (!(myStream >> aValue)) Storage_StreamTypeMismatchError::Raise();
556
557   return *this;
558 }
559
560 //=======================================================================
561 //function : GetBoolean
562 //purpose  : 
563 //=======================================================================
564
565 Storage_BaseDriver& FSD_CmpFile::GetBoolean(Standard_Boolean& aValue)
566 {
567   if (!(myStream >> aValue)) Storage_StreamTypeMismatchError::Raise();
568
569   return *this;
570 }
571
572 //=======================================================================
573 //function : GetReal
574 //purpose  : 
575 //=======================================================================
576
577 Storage_BaseDriver& FSD_CmpFile::GetReal(Standard_Real& aValue)
578 {
579   char realbuffer[100];
580
581   realbuffer[0] = '\0';
582   if (!(myStream >> realbuffer)) {
583 #ifdef OCCT_DEBUG
584     cerr << "%%%ERROR: read error of double at offset " << myStream.tellg() << endl;
585     cerr << "\t buffer is" << realbuffer<< endl;
586 #endif
587     Storage_StreamTypeMismatchError::Raise();
588   }
589   if (!OSD::CStringToReal(realbuffer,aValue)) {
590 #ifdef OCCT_DEBUG
591     cerr << "%%%ERROR: read error of double at offset " << myStream.tellg() << endl;
592     cerr << "\t buffer is" << realbuffer<< endl;
593 #endif
594     Storage_StreamTypeMismatchError::Raise();
595   }
596
597   return *this;
598 }
599
600 //=======================================================================
601 //function : GetShortReal
602 //purpose  : 
603 //=======================================================================
604
605 Storage_BaseDriver& FSD_CmpFile::GetShortReal(Standard_ShortReal& aValue)
606 {
607   char realbuffer[100];
608   Standard_Real r = 0.0;
609
610   realbuffer[0] = '\0';
611   if (!(myStream >> realbuffer)) Storage_StreamTypeMismatchError::Raise();
612   if (!OSD::CStringToReal(realbuffer,r))
613     Storage_StreamTypeMismatchError::Raise();
614
615   aValue = (Standard_ShortReal)r;
616
617   return *this;
618 }
619
620 //=======================================================================
621 //function : Destroy
622 //purpose  : 
623 //=======================================================================
624
625 void FSD_CmpFile::Destroy()
626 {
627   if (OpenMode() != Storage_VSNone) {
628     Close();
629   }
630 }
631
632 //=======================================================================
633 //function : BeginWriteInfoSection
634 //purpose  : -------------------------- INFO : WRITE
635 //=======================================================================
636
637 Storage_Error FSD_CmpFile::BeginWriteInfoSection() 
638 {
639   myStream << FSD_CmpFile::MagicNumber() << '\n';
640   myStream << "BEGIN_INFO_SECTION\n";
641   if (myStream.bad()) Storage_StreamWriteError::Raise();
642
643   return Storage_VSOk;
644 }
645
646 //=======================================================================
647 //function : WriteInfo
648 //purpose  : 
649 //=======================================================================
650
651 void FSD_CmpFile::WriteInfo(const Standard_Integer nbObj,
652                          const TCollection_AsciiString& dbVersion,
653                          const TCollection_AsciiString& date,
654                          const TCollection_AsciiString& schemaName,
655                          const TCollection_AsciiString& schemaVersion,
656                          const TCollection_ExtendedString& appName,
657                          const TCollection_AsciiString& appVersion,
658                          const TCollection_ExtendedString& dataType,
659                          const TColStd_SequenceOfAsciiString& userInfo) 
660 {
661   Standard_Integer i;
662
663   myStream << nbObj;
664   myStream << "\n";
665   myStream << dbVersion.ToCString() << "\n";
666   myStream << date.ToCString() << "\n";
667   myStream << schemaName.ToCString() << "\n";
668   myStream << schemaVersion.ToCString() << "\n";
669   WriteExtendedLine(appName);
670   myStream << appVersion.ToCString() << "\n";
671   WriteExtendedLine(dataType);
672   myStream << userInfo.Length() << "\n";
673
674   if (myStream.bad()) Storage_StreamWriteError::Raise();
675
676   for (i = 1; i <= userInfo.Length(); i++) {
677     myStream << userInfo.Value(i).ToCString() << "\n";
678     if (myStream.bad()) Storage_StreamWriteError::Raise();
679   }
680 }
681
682 //=======================================================================
683 //function : EndWriteInfoSection
684 //purpose  : read
685 //=======================================================================
686
687 Storage_Error FSD_CmpFile::EndWriteInfoSection() 
688 {
689   myStream << "END_INFO_SECTION\n";
690   if (myStream.bad())  Storage_StreamWriteError::Raise();
691   return Storage_VSOk;
692 }
693
694 //=======================================================================
695 //function : BeginReadInfoSection
696 //purpose  : 
697 //=======================================================================
698
699 Storage_Error FSD_CmpFile::BeginReadInfoSection() 
700 {
701   Storage_Error s;
702   TCollection_AsciiString l;
703   Standard_Size        len = strlen(FSD_CmpFile::MagicNumber());
704
705   ReadChar(l,len);
706   
707   if (strncmp(FSD_CmpFile::MagicNumber(),l.ToCString(),len) != 0) {
708     s = Storage_VSFormatError;
709   }
710   else {
711     s = FindTag("BEGIN_INFO_SECTION");
712   }
713
714   return s;
715 }
716
717 //=======================================================================
718 //function : ReadInfo
719 //purpose  : ------------------- INFO : READ
720 //=======================================================================
721
722 void FSD_CmpFile::ReadInfo(Standard_Integer& nbObj,
723                         TCollection_AsciiString& dbVersion,
724                         TCollection_AsciiString& date,
725                         TCollection_AsciiString& schemaName,
726                         TCollection_AsciiString& schemaVersion,
727                         TCollection_ExtendedString& appName,
728                         TCollection_AsciiString& appVersion,
729                         TCollection_ExtendedString& dataType,
730                         TColStd_SequenceOfAsciiString& userInfo) 
731 {
732   if (!(myStream >> nbObj)) Storage_StreamTypeMismatchError::Raise();
733
734   FlushEndOfLine();
735
736   ReadLine(dbVersion);
737   ReadLine(date);
738   ReadLine(schemaName);
739   ReadLine(schemaVersion);
740   ReadExtendedLine(appName);
741   ReadLine(appVersion);
742   ReadExtendedLine(dataType);
743
744   Standard_Integer i,len = 0;
745
746   if (!(myStream >> len)) Storage_StreamTypeMismatchError::Raise();
747
748   FlushEndOfLine();
749
750   TCollection_AsciiString line;
751
752   for (i = 1; i <= len && !IsEnd(); i++) {
753     ReadLine(line);
754     userInfo.Append(line);
755     line.Clear();
756   }
757 }
758
759 //=======================================================================
760 //function : ReadCompleteInfo
761 //purpose  : 
762 //           
763 //=======================================================================
764 void FSD_CmpFile::ReadCompleteInfo( Standard_IStream& /*theIStream*/, Handle(Storage_Data)& /*theData*/)
765 {
766
767 }
768
769 //=======================================================================
770 //function : EndReadInfoSection
771 //purpose  : COMMENTS SECTION
772 //           write
773 //=======================================================================
774
775 Storage_Error FSD_CmpFile::EndReadInfoSection() 
776 {
777   return FindTag("END_INFO_SECTION");
778 }
779
780 //=======================================================================
781 //function : BeginWriteCommentSection
782 //purpose  : ---------------- COMMENTS : WRITE
783 //=======================================================================
784
785 Storage_Error FSD_CmpFile::BeginWriteCommentSection() 
786 {
787   myStream << "BEGIN_COMMENT_SECTION\n";
788   if (myStream.bad()) Storage_StreamWriteError::Raise();
789   return Storage_VSOk;
790 }
791
792 //=======================================================================
793 //function : WriteComment
794 //purpose  : 
795 //=======================================================================
796
797 void FSD_CmpFile::WriteComment(const TColStd_SequenceOfExtendedString& aCom)
798 {
799  Standard_Integer i,aSize;
800
801  aSize = aCom.Length();
802  myStream << aSize << "\n";
803  if (myStream.bad()) Storage_StreamWriteError::Raise();
804
805  for (i = 1; i <= aSize; i++) {
806    WriteExtendedLine(aCom.Value(i));
807    if (myStream.bad()) Storage_StreamWriteError::Raise();
808  }
809 }
810
811 //=======================================================================
812 //function : EndWriteCommentSection
813 //purpose  : read
814 //=======================================================================
815
816 Storage_Error FSD_CmpFile::EndWriteCommentSection() 
817 {
818   myStream << "END_COMMENT_SECTION\n";
819   if (myStream.bad()) Storage_StreamWriteError::Raise();
820   return Storage_VSOk;
821 }
822
823 //=======================================================================
824 //function : BeginReadCommentSection
825 //purpose  : ---------------- COMMENTS : READ
826 //=======================================================================
827
828 Storage_Error FSD_CmpFile::BeginReadCommentSection() 
829 {
830   return FindTag("BEGIN_COMMENT_SECTION");
831 }
832
833 //=======================================================================
834 //function : ReadComment
835 //purpose  : 
836 //=======================================================================
837
838 void FSD_CmpFile::ReadComment(TColStd_SequenceOfExtendedString& aCom)
839 {
840   TCollection_ExtendedString line;
841   Standard_Integer           len,i;
842
843   if (!(myStream >> len)) Storage_StreamTypeMismatchError::Raise();
844   
845   FlushEndOfLine();  
846
847   for (i = 1; i <= len && !IsEnd(); i++) {
848     ReadExtendedLine(line);
849     aCom.Append(line);
850     line.Clear();
851   }
852 }
853
854 //=======================================================================
855 //function : EndReadCommentSection
856 //purpose  : 
857 //=======================================================================
858
859 Storage_Error FSD_CmpFile::EndReadCommentSection() 
860 {
861   return FindTag("END_COMMENT_SECTION");
862 }
863
864 //=======================================================================
865 //function : BeginWriteTypeSection
866 //purpose  : --------------- TYPE : WRITE
867 //=======================================================================
868
869 Storage_Error FSD_CmpFile::BeginWriteTypeSection() 
870 {
871   myStream << "BEGIN_TYPE_SECTION\n";
872   if (myStream.bad()) Storage_StreamWriteError::Raise();
873   return Storage_VSOk;
874 }
875
876 //=======================================================================
877 //function : SetTypeSectionSize
878 //purpose  : 
879 //=======================================================================
880
881 void FSD_CmpFile::SetTypeSectionSize(const Standard_Integer aSize) 
882 {
883   myStream << aSize << "\n";
884   if (myStream.bad()) Storage_StreamWriteError::Raise();
885 }
886
887 //=======================================================================
888 //function : WriteTypeInformations
889 //purpose  : 
890 //=======================================================================
891
892 void FSD_CmpFile::WriteTypeInformations(const Standard_Integer typeNum,
893                                       const TCollection_AsciiString& typeName) 
894 {
895   myStream << typeNum << " " << typeName.ToCString() << "\n";
896   if (myStream.bad()) Storage_StreamWriteError::Raise();
897 }
898
899 //=======================================================================
900 //function : EndWriteTypeSection
901 //purpose  : read
902 //=======================================================================
903
904 Storage_Error FSD_CmpFile::EndWriteTypeSection() 
905 {
906   myStream << "END_TYPE_SECTION\n";
907   if (myStream.bad()) Storage_StreamWriteError::Raise();
908   return Storage_VSOk;
909 }
910
911 //=======================================================================
912 //function : BeginReadTypeSection
913 //purpose  : ------------------- TYPE : READ
914 //=======================================================================
915
916 Storage_Error FSD_CmpFile::BeginReadTypeSection() 
917 {
918   return FindTag("BEGIN_TYPE_SECTION");
919 }
920
921 //=======================================================================
922 //function : TypeSectionSize
923 //purpose  : 
924 //=======================================================================
925
926 Standard_Integer FSD_CmpFile::TypeSectionSize() 
927 {
928   Standard_Integer i;
929
930   if (!(myStream >> i)) Storage_StreamTypeMismatchError::Raise();
931
932   FlushEndOfLine();
933
934   return i;
935 }
936
937 //=======================================================================
938 //function : ReadTypeInformations
939 //purpose  : 
940 //=======================================================================
941
942 void FSD_CmpFile::ReadTypeInformations(Standard_Integer& typeNum,
943                                     TCollection_AsciiString& typeName) 
944 {
945   if (!(myStream >> typeNum)) Storage_StreamTypeMismatchError::Raise();
946   if (!(myStream >> typeName)) Storage_StreamTypeMismatchError::Raise();
947   FlushEndOfLine();
948 }
949
950 //=======================================================================
951 //function : EndReadTypeSection
952 //purpose  : ROOT SECTION
953 //           write
954 //=======================================================================
955
956 Storage_Error FSD_CmpFile::EndReadTypeSection() 
957 {
958   return FindTag("END_TYPE_SECTION");
959 }
960
961 //=======================================================================
962 //function : BeginWriteRootSection
963 //purpose  : -------------------- ROOT : WRITE
964 //=======================================================================
965
966 Storage_Error FSD_CmpFile::BeginWriteRootSection() 
967 {
968   myStream << "BEGIN_ROOT_SECTION\n";
969   if (myStream.bad()) Storage_StreamWriteError::Raise();
970   return Storage_VSOk;
971 }
972
973 //=======================================================================
974 //function : SetRootSectionSize
975 //purpose  : 
976 //=======================================================================
977
978 void FSD_CmpFile::SetRootSectionSize(const Standard_Integer aSize) 
979 {
980   myStream << aSize << "\n";
981   if (myStream.bad()) Storage_StreamWriteError::Raise();
982 }
983
984 //=======================================================================
985 //function : WriteRoot
986 //purpose  : 
987 //=======================================================================
988
989 void FSD_CmpFile::WriteRoot(const TCollection_AsciiString& rootName, const Standard_Integer aRef, const TCollection_AsciiString& rootType) 
990 {
991   myStream << aRef << " " << rootName.ToCString() << " " << rootType.ToCString() << "\n";
992   if (myStream.bad()) Storage_StreamWriteError::Raise();
993 }
994
995 //=======================================================================
996 //function : EndWriteRootSection
997 //purpose  : read
998 //=======================================================================
999
1000 Storage_Error FSD_CmpFile::EndWriteRootSection() 
1001 {
1002   myStream << "END_ROOT_SECTION\n";
1003   if (myStream.bad()) Storage_StreamWriteError::Raise();
1004   return Storage_VSOk;
1005 }
1006
1007 //=======================================================================
1008 //function : BeginReadRootSection
1009 //purpose  : ----------------------- ROOT : READ
1010 //=======================================================================
1011
1012 Storage_Error FSD_CmpFile::BeginReadRootSection() 
1013 {
1014   return FindTag("BEGIN_ROOT_SECTION");
1015 }
1016
1017 //=======================================================================
1018 //function : RootSectionSize
1019 //purpose  : 
1020 //=======================================================================
1021
1022 Standard_Integer FSD_CmpFile::RootSectionSize() 
1023 {
1024   Standard_Integer i;
1025
1026   if (!(myStream >> i)) Storage_StreamTypeMismatchError::Raise();
1027   
1028   FlushEndOfLine();
1029   
1030   return i;
1031 }
1032
1033 //=======================================================================
1034 //function : ReadRoot
1035 //purpose  : 
1036 //=======================================================================
1037
1038 void FSD_CmpFile::ReadRoot(TCollection_AsciiString& rootName, Standard_Integer& aRef,TCollection_AsciiString& rootType) 
1039 {
1040   if (!(myStream >> aRef)) Storage_StreamTypeMismatchError::Raise();
1041   ReadWord(rootName);
1042   ReadWord(rootType);
1043 }
1044
1045 //=======================================================================
1046 //function : EndReadRootSection
1047 //purpose  : REF SECTION
1048 //           write
1049 //=======================================================================
1050
1051 Storage_Error FSD_CmpFile::EndReadRootSection() 
1052 {
1053   return FindTag("END_ROOT_SECTION");
1054 }
1055
1056 //=======================================================================
1057 //function : BeginWriteRefSection
1058 //purpose  : -------------------------- REF : WRITE
1059 //=======================================================================
1060
1061 Storage_Error FSD_CmpFile::BeginWriteRefSection() 
1062 {
1063   myStream << "BEGIN_REF_SECTION\n";
1064   if (myStream.bad()) Storage_StreamWriteError::Raise();
1065   return Storage_VSOk;
1066 }
1067
1068 //=======================================================================
1069 //function : SetRefSectionSize
1070 //purpose  : 
1071 //=======================================================================
1072
1073 void FSD_CmpFile::SetRefSectionSize(const Standard_Integer aSize) 
1074 {
1075   myStream << aSize << "\n";
1076   if (myStream.bad()) Storage_StreamWriteError::Raise();
1077 }
1078
1079 //=======================================================================
1080 //function : WriteReferenceType
1081 //purpose  : 
1082 //=======================================================================
1083
1084 void FSD_CmpFile::WriteReferenceType(const Standard_Integer reference,
1085                                   const Standard_Integer typeNum) 
1086 {
1087   myStream << reference << " " << typeNum << "\n";
1088   if (myStream.bad()) Storage_StreamWriteError::Raise();
1089 }
1090
1091 //=======================================================================
1092 //function : EndWriteRefSection
1093 //purpose  : read
1094 //=======================================================================
1095
1096 Storage_Error FSD_CmpFile::EndWriteRefSection() 
1097 {
1098   myStream << "END_REF_SECTION\n";
1099   if (myStream.bad()) Storage_StreamWriteError::Raise();
1100   return Storage_VSOk;
1101 }
1102
1103 //=======================================================================
1104 //function : BeginReadRefSection
1105 //purpose  : ----------------------- REF : READ
1106 //=======================================================================
1107
1108 Storage_Error FSD_CmpFile::BeginReadRefSection() 
1109 {
1110   return FindTag("BEGIN_REF_SECTION");
1111 }
1112
1113 //=======================================================================
1114 //function : RefSectionSize
1115 //purpose  : 
1116 //=======================================================================
1117
1118 Standard_Integer FSD_CmpFile::RefSectionSize() 
1119 {
1120   Standard_Integer i;
1121
1122   if (!(myStream >> i)) Storage_StreamTypeMismatchError::Raise();
1123   FlushEndOfLine();
1124
1125   return i;
1126 }
1127
1128 //=======================================================================
1129 //function : ReadReferenceType
1130 //purpose  : 
1131 //=======================================================================
1132
1133 void FSD_CmpFile::ReadReferenceType(Standard_Integer& reference,
1134                                  Standard_Integer& typeNum) 
1135 {
1136   if (!(myStream >> reference)) Storage_StreamTypeMismatchError::Raise();
1137   if (!(myStream >> typeNum)) Storage_StreamTypeMismatchError::Raise();
1138   FlushEndOfLine();
1139 }
1140
1141 //=======================================================================
1142 //function : EndReadRefSection
1143 //purpose  : DATA SECTION
1144 //           write
1145 //=======================================================================
1146
1147 Storage_Error FSD_CmpFile::EndReadRefSection() 
1148 {
1149   return FindTag("END_REF_SECTION");
1150 }
1151
1152 //=======================================================================
1153 //function : BeginWriteDataSection
1154 //purpose  : -------------------- DATA : WRITE
1155 //=======================================================================
1156
1157 Storage_Error FSD_CmpFile::BeginWriteDataSection() 
1158 {
1159   myStream << "BEGIN_DATA_SECTION";
1160   if (myStream.bad()) Storage_StreamWriteError::Raise();
1161   return Storage_VSOk;
1162 }
1163
1164 //=======================================================================
1165 //function : WritePersistentObjectHeader
1166 //purpose  : 
1167 //=======================================================================
1168
1169 void FSD_CmpFile::WritePersistentObjectHeader(const Standard_Integer aRef,
1170                                            const Standard_Integer aType) 
1171 {
1172   myStream << "\n#" << aRef << "%" << aType << " ";
1173   if (myStream.bad()) Storage_StreamWriteError::Raise();
1174 }
1175
1176 //=======================================================================
1177 //function : BeginWritePersistentObjectData
1178 //purpose  : 
1179 //=======================================================================
1180
1181 void FSD_CmpFile::BeginWritePersistentObjectData() 
1182 {
1183   if (myStream.bad()) Storage_StreamWriteError::Raise();
1184 }
1185
1186 //=======================================================================
1187 //function : BeginWriteObjectData
1188 //purpose  : 
1189 //=======================================================================
1190
1191 void FSD_CmpFile::BeginWriteObjectData() 
1192 {
1193   if (myStream.bad()) Storage_StreamWriteError::Raise();
1194 }
1195
1196 //=======================================================================
1197 //function : EndWriteObjectData
1198 //purpose  : 
1199 //=======================================================================
1200
1201 void FSD_CmpFile::EndWriteObjectData() 
1202 {
1203   if (myStream.bad()) Storage_StreamWriteError::Raise();
1204 }
1205
1206 //=======================================================================
1207 //function : EndWritePersistentObjectData
1208 //purpose  : 
1209 //=======================================================================
1210
1211 void FSD_CmpFile::EndWritePersistentObjectData() 
1212 {
1213   if (myStream.bad()) Storage_StreamWriteError::Raise();
1214 }
1215
1216 //=======================================================================
1217 //function : EndWriteDataSection
1218 //purpose  : read
1219 //=======================================================================
1220
1221 Storage_Error FSD_CmpFile::EndWriteDataSection() 
1222 {
1223   myStream << "\nEND_DATA_SECTION\n";
1224   if (myStream.bad()) Storage_StreamWriteError::Raise();
1225   return Storage_VSOk;
1226 }
1227
1228 //=======================================================================
1229 //function : BeginReadDataSection
1230 //purpose  : ---------------------- DATA : READ
1231 //=======================================================================
1232
1233 Storage_Error FSD_CmpFile::BeginReadDataSection() 
1234 {
1235   return FindTag("BEGIN_DATA_SECTION");
1236 }
1237
1238 //=======================================================================
1239 //function : ReadPersistentObjectHeader
1240 //purpose  : 
1241 //=======================================================================
1242
1243 void FSD_CmpFile::ReadPersistentObjectHeader(Standard_Integer& aRef,
1244                                           Standard_Integer& aType) 
1245 {
1246   char c;
1247
1248   myStream.get(c);
1249
1250   while (c != '#') {
1251     if (IsEnd() || (c != ' ') || (c == '\r')|| (c == '\n')) {
1252       Storage_StreamFormatError::Raise();
1253     }
1254     myStream.get(c);
1255   }
1256
1257   if (!(myStream >> aRef)) Storage_StreamTypeMismatchError::Raise();
1258
1259   myStream.get(c);
1260
1261   while (c != '%') {
1262     if (IsEnd() || (c != ' ') || (c == '\r')|| (c == '\n')) {
1263       Storage_StreamFormatError::Raise();
1264     }
1265     myStream.get(c);
1266   }
1267
1268   if (!(myStream >> aType)) Storage_StreamTypeMismatchError::Raise();
1269 //  cout << "REF:" << aRef << " TYPE:"<< aType << endl;
1270 }
1271
1272 //=======================================================================
1273 //function : BeginReadPersistentObjectData
1274 //purpose  : 
1275 //=======================================================================
1276
1277 void FSD_CmpFile::BeginReadPersistentObjectData() 
1278 {
1279 //cout << "BeginReadPersistentObjectData" << endl;
1280 }
1281
1282 //=======================================================================
1283 //function : BeginReadObjectData
1284 //purpose  : 
1285 //=======================================================================
1286
1287 void FSD_CmpFile::BeginReadObjectData() 
1288 {
1289 //  cout << "BeginReadObjectData" << endl;
1290 }
1291
1292 //=======================================================================
1293 //function : EndReadObjectData
1294 //purpose  : 
1295 //=======================================================================
1296
1297 void FSD_CmpFile::EndReadObjectData() 
1298 {
1299 //  cout << "EndReadObjectData" << endl;
1300 }
1301
1302 //=======================================================================
1303 //function : EndReadPersistentObjectData
1304 //purpose  : 
1305 //=======================================================================
1306
1307 void FSD_CmpFile::EndReadPersistentObjectData() 
1308 {
1309   char c;
1310
1311   myStream.get(c);
1312   while (c != '\n' && (c != '\r')) {
1313     if (IsEnd() || (c != ' ')) {
1314       Storage_StreamFormatError::Raise();
1315     }
1316     myStream.get(c);
1317   }
1318  if (c == '\r') {
1319    myStream.get(c);
1320  }
1321 //  cout << "EndReadPersistentObjectData" << endl;
1322 }
1323
1324 //=======================================================================
1325 //function : EndReadDataSection
1326 //purpose  : 
1327 //=======================================================================
1328
1329 Storage_Error FSD_CmpFile::EndReadDataSection() 
1330 {
1331   return FindTag("END_DATA_SECTION");
1332 }
1333
1334 //=======================================================================
1335 //function : Tell
1336 //purpose  : return position in the file. Return -1 upon error.
1337 //=======================================================================
1338
1339 Storage_Position FSD_CmpFile::Tell()
1340 {
1341   switch (OpenMode()) {
1342   case Storage_VSRead:
1343     return (Storage_Position) myStream.tellp();
1344   case Storage_VSWrite:
1345     return (Storage_Position) myStream.tellg();
1346   case Storage_VSReadWrite: {
1347     Storage_Position aPosR  = (Storage_Position) myStream.tellp();
1348     Storage_Position aPosW  = (Storage_Position) myStream.tellg();
1349     if (aPosR < aPosW)
1350       return aPosW;
1351     else
1352       return aPosR;
1353   }
1354   default: return -1;
1355   }
1356 }