1 // Copyright (c) 2017 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <StdObjMgt_Persistent.hxx>
15 #include <Standard_ErrorHandler.hxx>
16 #include <Standard_NoSuchObject.hxx>
17 #include <StdStorage_RootData.hxx>
18 #include <StdStorage_Root.hxx>
19 #include <Storage_BaseDriver.hxx>
20 #include <Storage_StreamTypeMismatchError.hxx>
21 #include <Storage_DataMapIteratorOfMapOfPers.hxx>
22 #include <TCollection_AsciiString.hxx>
24 IMPLEMENT_STANDARD_RTTIEXT(StdStorage_RootData, Standard_Transient)
26 StdStorage_RootData::StdStorage_RootData()
27 : myErrorStatus(Storage_VSOk)
31 Standard_Boolean StdStorage_RootData::Read(Storage_BaseDriver& theDriver)
33 // Check driver open mode
34 if (theDriver.OpenMode() != Storage_VSRead
35 && theDriver.OpenMode() != Storage_VSReadWrite)
37 myErrorStatus = Storage_VSModeError;
38 myErrorStatusExt = "OpenMode";
39 return Standard_False;
43 myErrorStatus = theDriver.BeginReadRootSection();
44 if (myErrorStatus != Storage_VSOk)
46 myErrorStatusExt = "BeginReadRootSection";
47 return Standard_False;
50 TCollection_AsciiString aRootName, aTypeName;
51 Standard_Integer aRef;
53 Standard_Integer len = theDriver.RootSectionSize();
54 for (Standard_Integer i = 1; i <= len; i++)
59 theDriver.ReadRoot(aRootName, aRef, aTypeName);
61 catch (Storage_StreamTypeMismatchError const&)
63 myErrorStatus = Storage_VSTypeMismatch;
64 myErrorStatusExt = "ReadRoot";
65 return Standard_False;
68 Handle(StdStorage_Root) aRoot = new StdStorage_Root(aRootName, aRef, aTypeName);
69 myObjects.Add(aRootName, aRoot);
72 myErrorStatus = theDriver.EndReadRootSection();
73 if (myErrorStatus != Storage_VSOk)
75 myErrorStatusExt = "EndReadRootSection";
76 return Standard_False;
82 Standard_Boolean StdStorage_RootData::Write(Storage_BaseDriver& theDriver)
84 // Check driver open mode
85 if (theDriver.OpenMode() != Storage_VSWrite
86 && theDriver.OpenMode() != Storage_VSReadWrite)
88 myErrorStatus = Storage_VSModeError;
89 myErrorStatusExt = "OpenMode";
90 return Standard_False;
94 myErrorStatus = theDriver.BeginWriteRootSection();
95 if (myErrorStatus != Storage_VSOk)
97 myErrorStatusExt = "BeginWriteRootSection";
98 return Standard_False;
101 theDriver.SetRootSectionSize(NumberOfRoots());
102 for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next())
104 const Handle(StdStorage_Root)& aRoot = anIt.Value();
108 theDriver.WriteRoot(aRoot->Name(), aRoot->Reference(), aRoot->Type());
110 catch (Storage_StreamTypeMismatchError const&)
112 myErrorStatus = Storage_VSTypeMismatch;
113 myErrorStatusExt = "ReadRoot";
114 return Standard_False;
118 myErrorStatus = theDriver.EndWriteRootSection();
119 if (myErrorStatus != Storage_VSOk)
121 myErrorStatusExt = "EndWriteRootSection";
122 return Standard_False;
125 return Standard_True;
128 Standard_Integer StdStorage_RootData::NumberOfRoots() const
130 return myObjects.Extent();
133 void StdStorage_RootData::AddRoot(const Handle(StdStorage_Root)& aRoot)
135 myObjects.Add(aRoot->Name(), aRoot);
136 aRoot->myRef = myObjects.Size();
139 Handle(StdStorage_HSequenceOfRoots) StdStorage_RootData::Roots() const
141 Handle(StdStorage_HSequenceOfRoots) anObjectsSeq = new StdStorage_HSequenceOfRoots;
142 StdStorage_DataMapIteratorOfMapOfRoots it(myObjects);
144 for (; it.More(); it.Next()) {
145 anObjectsSeq->Append(it.Value());
151 Handle(StdStorage_Root) StdStorage_RootData::Find(const TCollection_AsciiString& aName) const
153 Handle(StdStorage_Root) p;
154 if (myObjects.Contains(aName)) {
155 p = myObjects.FindFromKey(aName);
161 Standard_Boolean StdStorage_RootData::IsRoot(const TCollection_AsciiString& aName) const
163 return myObjects.Contains(aName);
166 void StdStorage_RootData::RemoveRoot(const TCollection_AsciiString& aName)
168 if (myObjects.Contains(aName)) {
169 myObjects.ChangeFromKey(aName)->myRef = 0;
170 myObjects.RemoveKey(aName);
171 Standard_Integer aRef = 1;
172 for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next(), ++aRef)
173 anIt.ChangeValue()->myRef = aRef;
177 void StdStorage_RootData::Clear()
179 for (StdStorage_MapOfRoots::Iterator anIt(myObjects); anIt.More(); anIt.Next())
180 anIt.ChangeValue()->myRef = 0;
185 Storage_Error StdStorage_RootData::ErrorStatus() const
187 return myErrorStatus;
190 void StdStorage_RootData::SetErrorStatus(const Storage_Error anError)
192 myErrorStatus = anError;
195 void StdStorage_RootData::ClearErrorStatus()
197 myErrorStatus = Storage_VSOk;
198 myErrorStatusExt.Clear();
201 TCollection_AsciiString StdStorage_RootData::ErrorStatusExtension() const
203 return myErrorStatusExt;
206 void StdStorage_RootData::SetErrorStatusExtension(const TCollection_AsciiString& anErrorExt)
208 myErrorStatusExt = anErrorExt;