0025133: TKOpenGl - Crash on closing a view containing presentations with capping
[occt.git] / src / VrmlData / VrmlData_WorldInfo.cxx
1 // Created on: 2007-08-01
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <VrmlData_WorldInfo.hxx>
17 #include <VrmlData_Scene.hxx>
18 #include <VrmlData_InBuffer.hxx>
19
20 #ifdef WNT
21 #define _CRT_SECURE_NO_DEPRECATE
22 #pragma warning (disable:4996)
23 #endif
24
25 IMPLEMENT_STANDARD_HANDLE  (VrmlData_WorldInfo, VrmlData_Node)
26 IMPLEMENT_STANDARD_RTTIEXT (VrmlData_WorldInfo, VrmlData_Node)
27
28 //=======================================================================
29 //function : VrmlData_WorldInfo::VrmlData_WorldInfo
30 //purpose  : Constructor
31 //=======================================================================
32
33 VrmlData_WorldInfo::VrmlData_WorldInfo (const VrmlData_Scene&  theScene,
34                                         const char             * theName,
35                                         const char             * theTitle)
36   : VrmlData_Node (theScene, theName),
37     myInfo        (theScene.Allocator())
38 {
39   SetTitle (theTitle);
40 }
41
42 //=======================================================================
43 //function : SetTitle
44 //purpose  : Set or modify the title.
45 //=======================================================================
46
47 void VrmlData_WorldInfo::SetTitle (const char * theString)
48 {
49   if (theString == 0L)
50     myTitle = 0L;
51   else {
52     const size_t len = strlen (theString) + 1;
53     if (len == 1)
54       myTitle = 0L;
55     else {
56       myTitle = static_cast <const char *>(Scene().Allocator()->Allocate(len));
57       memcpy (const_cast<char *> (myTitle), theString, len);
58     }
59   }
60 }
61
62 //=======================================================================
63 //function : AddInfo
64 //purpose  : Add a string to the list of info strings.
65 //=======================================================================
66
67 void VrmlData_WorldInfo::AddInfo (const char * theString)
68 {
69   if (theString != 0L)
70     if (* theString != '\0') {
71       const size_t len = strlen (theString) + 1;
72       char * aStr = static_cast <char *>(Scene().Allocator()->Allocate(len));
73       memcpy (aStr, theString, len);
74       myInfo.Append (aStr);
75     }
76 }
77
78 //=======================================================================
79 //function : Clone
80 //purpose  : Create a copy of this node
81 //=======================================================================
82
83 Handle(VrmlData_Node) VrmlData_WorldInfo::Clone
84                                 (const Handle(VrmlData_Node)& theOther) const
85 {
86   Handle(VrmlData_WorldInfo) aResult =
87     Handle(VrmlData_WorldInfo)::DownCast (VrmlData_Node::Clone(theOther));
88   if (aResult.IsNull())
89     aResult =
90       new VrmlData_WorldInfo (theOther.IsNull() ? Scene() : theOther->Scene(),
91                              Name());
92
93   if (&aResult->Scene() == &Scene()) {
94     aResult->myTitle = myTitle;
95     aResult->myInfo  = myInfo;
96   } else {
97     aResult->SetTitle (myTitle);
98     NCollection_List <const char *>::Iterator anIter (myInfo);
99     for (; anIter.More(); anIter.Next())
100       aResult->AddInfo (anIter.Value());
101   }
102   return aResult;
103 }
104
105 //=======================================================================
106 //function : Read
107 //purpose  : Read the Node from input stream.
108 //=======================================================================
109
110 VrmlData_ErrorStatus VrmlData_WorldInfo::Read (VrmlData_InBuffer& theBuffer)
111 {
112   VrmlData_ErrorStatus aStatus;
113   while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {
114
115     if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "title")) {
116       TCollection_AsciiString aTitleString;
117       if (OK (aStatus, ReadString (theBuffer, aTitleString)))
118         SetTitle (aTitleString.ToCString());
119
120     } else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "info")) {
121       NCollection_List<TCollection_AsciiString> lstInfo;
122       if (OK (aStatus, ReadMultiString (theBuffer, lstInfo))) {
123         NCollection_List<TCollection_AsciiString>::Iterator anIter (lstInfo);
124         for (; anIter.More(); anIter.Next())
125           AddInfo (anIter.Value().ToCString());
126       }
127     } else
128       break;
129   }
130
131   // Read the terminating (closing) brace
132   if (OK(aStatus))
133     aStatus = readBrace (theBuffer);
134   return aStatus;
135 }
136
137 //=======================================================================
138 //function : Write
139 //purpose  : Write the Node to the Scene output.
140 //=======================================================================
141
142 VrmlData_ErrorStatus VrmlData_WorldInfo::Write (const char * thePrefix) const
143 {
144   VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
145   const VrmlData_Scene& aScene = Scene();
146   static char header[] = "WorldInfo {";
147   if (aScene.IsDummyWrite() == Standard_False &&
148       OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent())))
149   {
150     char buf[4096];
151     if (myTitle) {
152       Sprintf (buf, "title \"%s\"", myTitle);
153       aStatus = aScene.WriteLine (buf);
154     }
155
156     if (myInfo.IsEmpty() == Standard_False && OK(aStatus)) {
157       if (OK (aStatus, aScene.WriteLine ("info [", 0L, GlobalIndent()))) {
158         NCollection_List<const char *>::Iterator anIter (myInfo);
159         while (anIter.More()) {
160           Sprintf (buf, "\"%s\"", anIter.Value());
161           anIter.Next();
162           if (anIter.More())
163             aStatus = aScene.WriteLine (buf, ",");
164           else
165             aStatus = aScene.WriteLine (buf);
166         }
167       }
168       aStatus = aScene.WriteLine ("]", 0L, -GlobalIndent());
169     }
170
171     aStatus = WriteClosing();
172   }
173   return aStatus;
174 }
175
176 //=======================================================================
177 //function : IsDefault
178 //purpose  : 
179 //=======================================================================
180
181 Standard_Boolean VrmlData_WorldInfo::IsDefault() const
182 {
183   return (myTitle == 0L && myInfo.IsEmpty());
184 }