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