1 // Created on: 2002-10-29
2 // Created by: Michael SAZONOV
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <BinLDrivers_DocumentStorageDriver.ixx>
17 #include <BinLDrivers.hxx>
18 #include <BinLDrivers_Marker.hxx>
19 #include <BinMDF_ADriver.hxx>
20 #include <BinObjMgt_Persistent.hxx>
21 #include <CDM_Application.hxx>
22 #include <FSD_BinaryFile.hxx>
23 #include <FSD_FileHeader.hxx>
24 #include <PCDM_ReadWriter.hxx>
25 #include <Standard_ErrorHandler.hxx>
26 #include <Storage_Schema.hxx>
27 #include <TColStd_Array1OfInteger.hxx>
28 #include <TColStd_ListIteratorOfListOfInteger.hxx>
29 #include <TColStd_ListOfInteger.hxx>
30 #include <TCollection_AsciiString.hxx>
31 #include <TDF_AttributeIterator.hxx>
32 #include <TDF_ChildIterator.hxx>
33 #include <TDF_Data.hxx>
34 #include <TDF_Tool.hxx>
35 #include <TDocStd_Document.hxx>
37 #define SHAPESECTION_POS (Standard_CString)"SHAPE_SECTION_POS:"
39 //=======================================================================
40 //function : BinLDrivers_DocumentStorageDriver
41 //purpose : Constructor
42 //=======================================================================
44 BinLDrivers_DocumentStorageDriver::BinLDrivers_DocumentStorageDriver ()
48 //=======================================================================
49 //function : SchemaName
51 //=======================================================================
53 TCollection_ExtendedString BinLDrivers_DocumentStorageDriver::SchemaName() const
55 TCollection_ExtendedString schemaname;
59 //=======================================================================
62 //=======================================================================
64 void BinLDrivers_DocumentStorageDriver::Write
65 (const Handle(CDM_Document)& theDocument,
66 const TCollection_ExtendedString& theFileName)
68 SetIsError(Standard_False);
69 SetStoreStatus(PCDM_SS_OK);
71 myMsgDriver = theDocument -> Application() -> MessageDriver();
72 myMapUnsupported.Clear();
74 #if defined(_DEBUG) || defined(DEB)
75 TCollection_ExtendedString aMethStr ("BinLDrivers_DocumentStorageDriver, ");
77 TCollection_ExtendedString aMethStr;
79 TCollection_ExtendedString anErrorStr ("Error: ");
81 Handle(TDocStd_Document) aDoc =
82 Handle(TDocStd_Document)::DownCast(theDocument);
84 SetIsError(Standard_True);
85 SetStoreStatus(PCDM_SS_Doc_IsNull);
89 TCollection_AsciiString aFileName (theFileName);
91 // First pass: collect empty labels, assign IDs to the types
92 if (myDrivers.IsNull())
93 myDrivers = AttributeDrivers (myMsgDriver);
94 Handle(TDF_Data) aData = aDoc->GetData();
95 FirstPass (aData->Root());
97 // 1. Write info section (including types table)
98 WriteInfoSection(theDocument, aFileName);
102 SetStoreStatus(PCDM_SS_Info_Section_Error);
107 ofstream anOS ((const wchar_t*) theFileName.ToExtString(), ios::in | ios::binary | ios::ate);
108 #elif !defined(IRIX) // 10.10.2005
109 ofstream anOS (aFileName.ToCString(), ios::in | ios::binary | ios::ate);
111 ofstream anOS (aFileName.ToCString(), ios::ate);
112 //ofstream anOS (aFileName.ToCString(), ios::out| ios::binary | ios::ate);
114 #ifdef BINLDRIVERS_DEB
115 const Standard_Integer aP = (Standard_Integer) anOS.tellp();
116 cout << "POS = " << aP <<endl;
122 // 2. Write the Table of Contents of Sections
123 BinLDrivers_VectorOfDocumentSection::Iterator anIterS (mySections);
124 for (; anIterS.More(); anIterS.Next())
125 anIterS.ChangeValue().WriteTOC (anOS);
127 // Shapes Section is the last one, it indicates the end of the table.
128 BinLDrivers_DocumentSection aShapesSection (SHAPESECTION_POS,
130 aShapesSection.WriteTOC (anOS);
132 // 3. Write document contents
133 // (Storage data to the stream)
134 myRelocTable.Clear();
137 // Write Doc structure
138 WriteSubTree (aData->Root(), anOS); // Doc is written
140 // 4. Write Shapes section
141 WriteShapeSection(aShapesSection, anOS);
143 // Write application-defined sections
144 for (anIterS.Init (mySections); anIterS.More(); anIterS.Next()) {
145 BinLDrivers_DocumentSection& aSection = anIterS.ChangeValue();
146 const Standard_Size aSectionOffset = (Standard_Size) anOS.tellp();
147 WriteSection (aSection.Name(), theDocument, anOS);
148 aSection.Write (anOS, aSectionOffset);
151 // End of processing: close structures and check the status
152 myPAtt.Destroy(); // free buffer
153 myEmptyLabels.Clear();
154 myMapUnsupported.Clear();
156 if (!myRelocTable.Extent()) {
157 // No objects written
159 WriteMessage (aMethStr + "no objects written");
161 SetIsError(Standard_True);
162 SetStoreStatus(PCDM_SS_No_Obj);
165 myRelocTable.Clear();
169 // A problem with the stream
170 #if defined(_DEBUG) || defined(DEB)
171 WriteMessage (anErrorStr + aMethStr +
172 "Problem with the file stream, rdstate="
173 + (Standard_Integer )anOS.rdstate());
175 TCollection_ExtendedString aStr =
176 anErrorStr + aMethStr + "Problem writing the file ";
177 WriteMessage (aStr + theFileName);
179 SetIsError(Standard_True);
180 SetStoreStatus(PCDM_SS_WriteFailure);
186 //=======================================================================
187 //function : UnsupportedAttrMsg
189 //=======================================================================
191 void BinLDrivers_DocumentStorageDriver::UnsupportedAttrMsg
192 (const Handle(Standard_Type)& theType)
195 static TCollection_ExtendedString aMsg
196 ("BinDrivers_DocumentStorageDriver: warning: attribute driver for type ");
198 if (!myMapUnsupported.Contains(theType)) {
199 myMapUnsupported.Add(theType);
201 WriteMessage (aMsg + theType->Name() + " not found");
206 //=======================================================================
207 //function : WriteSubTree
209 //=======================================================================
211 void BinLDrivers_DocumentStorageDriver::WriteSubTree
212 (const TDF_Label& theLabel,
213 Standard_OStream& theOS)
216 if (!myEmptyLabels.IsEmpty() && myEmptyLabels.First() == theLabel) {
217 myEmptyLabels.RemoveFirst();
221 // Write label header: tag
222 Standard_Integer aTag = theLabel.Tag();
224 aTag = InverseInt (aTag);
226 theOS.write ((char*)&aTag, sizeof(Standard_Integer));
229 TDF_AttributeIterator itAtt (theLabel);
230 for ( ; itAtt.More() && theOS; itAtt.Next()) {
231 const Handle(TDF_Attribute)& tAtt = itAtt.Value();
232 const Handle(Standard_Type)& aType = tAtt->DynamicType();
233 // Get type ID and driver
234 Handle(BinMDF_ADriver) aDriver;
235 const Standard_Integer aTypeId = myDrivers->GetDriver (aType,aDriver);
237 // Add source to relocation table
238 const Standard_Integer anId = myRelocTable.Add (tAtt);
240 // Create and fill data item
241 myPAtt.SetTypeId (aTypeId);
243 aDriver->Paste (tAtt, myPAtt, myRelocTable);
245 // Write data to the stream -->!!!
250 UnsupportedAttrMsg (aType);
254 // Problem with the stream
258 // Write the end attributes list marker
259 BinLDrivers_Marker anEndAttr = BinLDrivers_ENDATTRLIST;
261 anEndAttr = (BinLDrivers_Marker) InverseInt (anEndAttr);
263 theOS.write ((char*)&anEndAttr, sizeof(anEndAttr));
265 // Process sub-labels
266 TDF_ChildIterator itChld (theLabel);
267 for ( ; itChld.More(); itChld.Next())
269 const TDF_Label& aChildLab = itChld.Value();
270 WriteSubTree (aChildLab, theOS);
273 // Write the end label marker
274 BinLDrivers_Marker anEndLabel = BinLDrivers_ENDLABEL;
276 anEndLabel = (BinLDrivers_Marker) InverseInt (anEndLabel);
278 theOS.write ((char*)&anEndLabel, sizeof(anEndLabel));
282 //=======================================================================
283 //function : AttributeDrivers
285 //=======================================================================
287 Handle(BinMDF_ADriverTable) BinLDrivers_DocumentStorageDriver::AttributeDrivers
288 (const Handle(CDM_MessageDriver)& theMessageDriver)
290 return BinLDrivers::AttributeDrivers (theMessageDriver);
293 //=======================================================================
294 //function : FirstPassSubTree
296 //=======================================================================
298 Standard_Boolean BinLDrivers_DocumentStorageDriver::FirstPassSubTree
300 TDF_LabelList& ListOfEmptyL)
302 // are there writable attributes on L ?
303 Standard_Boolean hasAttr = Standard_False;
304 TDF_AttributeIterator itAtt (L);
305 for ( ; itAtt.More(); itAtt.Next()) {
306 const Handle(Standard_Type)& aType = itAtt.Value()->DynamicType();
307 Handle(BinMDF_ADriver) aDriver;
308 // do not rely on a value returned by GetDriver here, because
309 // the IDs have not yet been assigned to the types
310 myDrivers->GetDriver (aType, aDriver);
311 if (!aDriver.IsNull()) {
312 hasAttr = Standard_True;
313 myTypesMap.Add (aType);
317 UnsupportedAttrMsg (aType);
321 // are there writable attributes on sub-labels ?
322 Standard_Boolean hasChildAttr = Standard_False;
323 TDF_LabelList emptyChildrenList;
324 TDF_ChildIterator itChld (L);
325 for ( ; itChld.More(); itChld.Next())
327 if (FirstPassSubTree (itChld.Value(), emptyChildrenList))
328 emptyChildrenList.Append( itChld.Value() );
330 hasChildAttr = Standard_True;
333 Standard_Boolean isEmpty = !(hasAttr || hasChildAttr);
336 ListOfEmptyL.Append( emptyChildrenList );
341 //=======================================================================
342 //function : FirstPass
344 //=======================================================================
346 void BinLDrivers_DocumentStorageDriver::FirstPass
347 (const TDF_Label& theRoot)
350 myEmptyLabels.Clear();
352 if (FirstPassSubTree( theRoot, myEmptyLabels))
353 myEmptyLabels.Append( theRoot );
355 myDrivers->AssignIds (myTypesMap);
358 //=======================================================================
359 //function : WriteInfoSection
360 //purpose : Write info secton using FSD_BinaryFile driver
361 //=======================================================================
363 #define START_TYPES "START_TYPES"
364 #define END_TYPES "END_TYPES"
366 void BinLDrivers_DocumentStorageDriver::WriteInfoSection
367 (const Handle(CDM_Document)& theDocument,
368 const TCollection_AsciiString& theFileName)
370 FSD_BinaryFile aFileDriver;
371 if (aFileDriver.Open( theFileName, Storage_VSWrite ) != Storage_VSOk) {
372 #if defined(DEB) || defined(_DEBUG)
373 WriteMessage ("BinDrivers_DocumentStorageDriver: error opening file");
375 WriteMessage (TCollection_ExtendedString("Error: Cannot open file ") +
378 SetIsError(Standard_True);
382 if (aFileDriver.BeginWriteInfoSection() == Storage_VSOk)
385 Handle(Storage_Data) theData = new Storage_Data;
386 PCDM_ReadWriter::WriteFileFormat( theData, theDocument );
387 PCDM_ReadWriter::Writer()->WriteReferenceCounter(theData,theDocument);
388 PCDM_ReadWriter::Writer()->WriteReferences(theData,theDocument,theFileName);
389 PCDM_ReadWriter::Writer()->WriteExtensions(theData,theDocument);
390 PCDM_ReadWriter::Writer()->WriteVersion(theData,theDocument);
392 // add the types table
393 theData->AddToUserInfo(START_TYPES);
395 for (i = 1; i <= myTypesMap.Extent(); i++) {
396 Handle(BinMDF_ADriver) aDriver = myDrivers->GetDriver(i);
397 if (!aDriver.IsNull()) {
398 const TCollection_AsciiString& aTypeName = aDriver->TypeName();
399 theData->AddToUserInfo(aTypeName);
402 theData->AddToUserInfo(END_TYPES);
404 // add document comments
405 TColStd_SequenceOfExtendedString aComments;
406 theDocument->Comments(aComments);
407 for (i = 1; i <= aComments.Length(); i++)
408 theData->AddToComments(aComments(i));
411 aFileDriver.WriteInfo
413 BinLDrivers::StorageVersion(),
414 Storage_Schema::ICreationDate(),
415 TCollection_AsciiString(SchemaName(),'?'),
417 theData->ApplicationName(),
418 theData->ApplicationVersion(),
423 // we write a complete header section: info and comments
424 aFileDriver.EndWriteInfoSection();
425 aFileDriver.BeginWriteCommentSection();
426 aFileDriver.WriteComment(theData->Comments());// <=== !!! szy - it was missed
427 aFileDriver.EndWriteCommentSection();
428 // here the location of info and comment sections is written
429 aFileDriver.EndWriteDataSection();
432 #if defined(DEB) || defined(_DEBUG)
433 WriteMessage("BinDrivers_DocumentStorageDriver: error writing header");
435 WriteMessage(TCollection_ExtendedString("Error: Problem writing header "
436 "into file ") + theFileName);
438 SetIsError(Standard_True);
440 #ifdef BINLDRIVERS_DEB
441 const Standard_Integer aP = (Standard_Integer) aFileDriver.Tell();
442 cout << "POS = " << aP <<endl;
447 //=======================================================================
448 //function : WriteMessage
449 //purpose : write theMessage to the MessageDriver of the
451 //=======================================================================
453 void BinLDrivers_DocumentStorageDriver::WriteMessage
454 (const TCollection_ExtendedString& theMsg)
456 if (!myMsgDriver.IsNull())
457 myMsgDriver->Write (theMsg.ToExtString());
460 //=======================================================================
461 //function : AddSection
463 //=======================================================================
465 void BinLDrivers_DocumentStorageDriver::AddSection
466 (const TCollection_AsciiString& theName,
467 const Standard_Boolean isPostRead)
469 mySections.Append (BinLDrivers_DocumentSection (theName, isPostRead));
472 //=======================================================================
473 //function : WriteSection
475 //=======================================================================
477 void BinLDrivers_DocumentStorageDriver::WriteSection
478 (const TCollection_AsciiString& /*theName*/,
479 const Handle(CDM_Document)& /*theDocument*/,
480 Standard_OStream& /*theOS*/)
482 // empty; should be redefined in subclasses
485 //=======================================================================
486 //function : WriteShapeSection
487 //purpose : defines WriteShapeSection
488 //=======================================================================
489 void BinLDrivers_DocumentStorageDriver::WriteShapeSection
490 (BinLDrivers_DocumentSection& theSection,
491 Standard_OStream& theOS)
493 const Standard_Size aShapesSectionOffset = (Standard_Size) theOS.tellp();
494 theSection.Write (theOS, aShapesSectionOffset);