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