1 // Created on: 2000-08-21
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-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
17 #include <Message.hxx>
18 #include <Message_Messenger.hxx>
19 #include <Resource_Manager.hxx>
20 #include <ShapeProcess_Context.hxx>
21 #include <Standard_ErrorHandler.hxx>
22 #include <Standard_Failure.hxx>
23 #include <Standard_Type.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_HAsciiString.hxx>
26
27 #include <sys/stat.h>
IMPLEMENT_STANDARD_RTTIEXT(ShapeProcess_Context,Standard_Transient)28 IMPLEMENT_STANDARD_RTTIEXT(ShapeProcess_Context,Standard_Transient)
29
30 //=======================================================================
31 //function : ShapeProcess_Context
32 //purpose :
33 //=======================================================================
34 ShapeProcess_Context::ShapeProcess_Context()
35 {
36 myMessenger = Message::DefaultMessenger();
37 myTraceLev = 1;
38 }
39
40 //=======================================================================
41 //function : ShapeProcess_Context
42 //purpose :
43 //=======================================================================
44
ShapeProcess_Context(const Standard_CString file,const Standard_CString scope)45 ShapeProcess_Context::ShapeProcess_Context (const Standard_CString file,
46 const Standard_CString scope)
47 {
48 Init ( file, scope );
49 myMessenger = Message::DefaultMessenger();
50 myTraceLev = 1;
51 }
52
53 //=======================================================================
54 //function : Init
55 //purpose :
56 //=======================================================================
57
Init(const Standard_CString file,const Standard_CString scope)58 Standard_Boolean ShapeProcess_Context::Init (const Standard_CString file,
59 const Standard_CString scope)
60 {
61 myScope.Nullify();
62 myRC = LoadResourceManager ( file );
63 if ( scope && scope[0] ) {
64 SetScope ( scope );
65 }
66 return Standard_True; // myRC->Length() >0; NOT IMPLEMENTED
67 }
68
69 //=======================================================================
70 //function : LoadResourceManager
71 //purpose :
72 //=======================================================================
73
Handle(Resource_Manager)74 Handle(Resource_Manager) ShapeProcess_Context::LoadResourceManager (const Standard_CString name)
75 {
76 // Optimisation of loading resource file: file is load only once
77 // and reloaded only if file date has changed
78 static Handle(Resource_Manager) sRC;
79 static Standard_Time sMtime, sUMtime;
80 static TCollection_AsciiString sName;
81
82 struct stat buf;
83 Standard_Time aMtime(0), aUMtime(0);
84 TCollection_AsciiString aPath,aUserPath;
85 Resource_Manager::GetResourcePath(aPath,name,Standard_False);
86 Resource_Manager::GetResourcePath(aUserPath,name,Standard_True);
87 if ( !aPath.IsEmpty() )
88 {
89 stat( aPath.ToCString(), &buf );
90 aMtime = (Standard_Time)buf.st_mtime;
91 }
92 if ( !aUserPath.IsEmpty() )
93 {
94 stat( aUserPath.ToCString(), &buf );
95 aUMtime = (Standard_Time)buf.st_mtime;
96 }
97
98 Standard_Boolean isFileModified = Standard_False;
99 if ( !sRC.IsNull() ) {
100 if ( sName.IsEqual ( name ) ) {
101 if ( sMtime != aMtime )
102 {
103 sMtime = aMtime;
104 isFileModified = Standard_True;
105 }
106 if ( sUMtime != aUMtime )
107 {
108 sUMtime = aUMtime;
109 isFileModified = Standard_True;
110 }
111 if (isFileModified)
112 sRC.Nullify();
113 }
114 else
115 sRC.Nullify();
116 }
117 if ( sRC.IsNull() ) {
118 #ifdef OCCT_DEBUG
119 std::cout << "Info: ShapeProcess_Context: Reload Resource_Manager: "
120 << sName.ToCString() << " -> " << name << std::endl;
121 #endif
122 sRC = new Resource_Manager ( name );
123 if (!isFileModified)
124 {
125 sName = name;
126 sMtime = aMtime;
127 sUMtime = aUMtime;
128 }
129 }
130 return sRC;
131 }
132
133 //=======================================================================
134 //function : ResourceManager
135 //purpose :
136 //=======================================================================
137
Handle(Resource_Manager)138 const Handle(Resource_Manager) &ShapeProcess_Context::ResourceManager () const
139 {
140 return myRC;
141 }
142
143 //=======================================================================
144 //function : SetScope
145 //purpose :
146 //=======================================================================
147
SetScope(const Standard_CString scope)148 void ShapeProcess_Context::SetScope (const Standard_CString scope)
149 {
150 if ( myScope.IsNull() ) myScope = new TColStd_HSequenceOfHAsciiString;
151 Handle(TCollection_HAsciiString) str;
152 if ( myScope->Length() >0 ) {
153 str = new TCollection_HAsciiString ( myScope->Value ( myScope->Length() ) );
154 str->AssignCat ( "." );
155 str->AssignCat ( scope );
156 }
157 else str = new TCollection_HAsciiString ( scope );
158 myScope->Append ( str );
159 }
160
161 //=======================================================================
162 //function : UnSetScope
163 //purpose :
164 //=======================================================================
165
UnSetScope()166 void ShapeProcess_Context::UnSetScope ()
167 {
168 if ( ! myScope.IsNull() && myScope->Length() >0 )
169 myScope->Remove ( myScope->Length() );
170 }
171
172 //=======================================================================
173 //function : IsParamSet
174 //purpose :
175 //=======================================================================
176
MakeName(const Handle (TColStd_HSequenceOfHAsciiString)& scope,const Standard_CString param)177 static Handle(TCollection_HAsciiString) MakeName (const Handle(TColStd_HSequenceOfHAsciiString) &scope,
178 const Standard_CString param)
179 {
180 Handle(TCollection_HAsciiString) str;
181 if ( ! scope.IsNull() && scope->Length() >0 ) {
182 str = new TCollection_HAsciiString ( scope->Value ( scope->Length() )->String() );
183 str->AssignCat (".");
184 str->AssignCat ( param );
185 }
186 else str = new TCollection_HAsciiString ( param );
187 return str;
188 }
189
IsParamSet(const Standard_CString param) const190 Standard_Boolean ShapeProcess_Context::IsParamSet (const Standard_CString param) const
191 {
192 return ! myRC.IsNull() && myRC->Find ( MakeName ( myScope, param )->ToCString() );
193 }
194
195 //=======================================================================
196 //function : GetString
197 //purpose :
198 //=======================================================================
199
GetString(const Standard_CString param,TCollection_AsciiString & str) const200 Standard_Boolean ShapeProcess_Context::GetString (const Standard_CString param,
201 TCollection_AsciiString &str) const
202 {
203 if ( myRC.IsNull() ) return Standard_False;
204 Handle(TCollection_HAsciiString) pname = MakeName ( myScope, param );
205 if ( ! myRC->Find ( pname->ToCString() ) ) {
206 #ifdef OCCT_DEBUG
207 std::cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << pname->ToCString() << " is not defined" << std::endl;
208 #endif
209 return Standard_False;
210 }
211 str = myRC->Value ( pname->ToCString() );
212 return Standard_True;
213 }
214
215 //=======================================================================
216 //function : GetReal
217 //purpose :
218 //=======================================================================
219
GetReal(const Standard_CString param,Standard_Real & val) const220 Standard_Boolean ShapeProcess_Context::GetReal (const Standard_CString param,
221 Standard_Real &val) const
222 {
223 if ( myRC.IsNull() ) return Standard_False;
224
225 TCollection_AsciiString str;
226 if ( ! GetString ( param, str ) ) return Standard_False;
227
228 if ( str.IsRealValue() ) {
229 val = str.RealValue();
230 return Standard_True;
231 }
232
233 // if not real, try to treat as alias "¶m"
234 str.LeftAdjust();
235 if ( str.Value(1) == '&' ) {
236 TCollection_AsciiString ref = str.Split ( 1 );
237 ref.LeftAdjust();
238 ref.RightAdjust();
239 if ( ! myRC->Find ( ref.ToCString() ) ) {
240 #ifdef OCCT_DEBUG
241 std::cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << ref.ToCString() << " is not defined" << std::endl;
242 #endif
243 return Standard_False;
244 }
245 str = myRC->Value ( ref.ToCString() );
246 if ( str.IsRealValue() ) {
247 val = str.RealValue();
248 return Standard_True;
249 }
250 }
251 #ifdef OCCT_DEBUG
252 std::cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << param << " is neither Real nor reference to Real";
253 #endif
254 return Standard_False;
255 }
256
257 //=======================================================================
258 //function : GetInteger
259 //purpose :
260 //=======================================================================
261
GetInteger(const Standard_CString param,Standard_Integer & val) const262 Standard_Boolean ShapeProcess_Context::GetInteger (const Standard_CString param,
263 Standard_Integer &val) const
264 {
265 if ( myRC.IsNull() ) return Standard_False;
266
267 TCollection_AsciiString str;
268 if ( ! GetString ( param, str ) ) return Standard_False;
269
270 if ( str.IsIntegerValue() ) {
271 val = str.IntegerValue();
272 return Standard_True;
273 }
274
275 // if not integer, try to treat as alias "¶m"
276 str.LeftAdjust();
277 if ( str.Value(1) == '&' ) {
278 TCollection_AsciiString ref = str.Split ( 1 );
279 ref.LeftAdjust();
280 ref.RightAdjust();
281 if ( ! myRC->Find ( ref.ToCString() ) ) {
282 #ifdef OCCT_DEBUG
283 std::cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << ref.ToCString() << " is not defined" << std::endl;
284 #endif
285 return Standard_False;
286 }
287 str = myRC->Value ( ref.ToCString() );
288 if ( str.IsIntegerValue() ) {
289 val = str.IntegerValue();
290 return Standard_True;
291 }
292 }
293 #ifdef OCCT_DEBUG
294 std::cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << param << " is neither Integer nor reference to Integer";
295 #endif
296 return Standard_False;
297 }
298
299 //=======================================================================
300 //function : GetBoolean
301 //purpose :
302 //=======================================================================
303
GetBoolean(const Standard_CString param,Standard_Boolean & val) const304 Standard_Boolean ShapeProcess_Context::GetBoolean (const Standard_CString param,
305 Standard_Boolean &val) const
306 {
307 if ( myRC.IsNull() ) return Standard_False;
308 try {
309 OCC_CATCH_SIGNALS
310 val = myRC->Integer (MakeName (myScope, param)->ToCString()) != 0;
311 return Standard_True;
312 }
313 catch (Standard_Failure const& anException) {
314 #ifdef OCCT_DEBUG
315 std::cout << "Warning: ShapeProcess_Context::GetInteger(): " << param << ": ";
316 anException.Print(std::cout); std::cout << std::endl;
317 #endif
318 (void)anException;
319 }
320 return Standard_False;
321 }
322
323 //=======================================================================
324 //function : RealVal
325 //purpose :
326 //=======================================================================
327
RealVal(const Standard_CString param,const Standard_Real def) const328 Standard_Real ShapeProcess_Context::RealVal (const Standard_CString param,
329 const Standard_Real def) const
330 {
331 Standard_Real val;
332 return GetReal ( param, val ) ? val : def;
333 }
334
335 //=======================================================================
336 //function : BooleanVal
337 //purpose :
338 //=======================================================================
339
BooleanVal(const Standard_CString param,const Standard_Boolean def) const340 Standard_Boolean ShapeProcess_Context::BooleanVal (const Standard_CString param,
341 const Standard_Boolean def) const
342 {
343 Standard_Boolean val;
344 return GetBoolean ( param, val ) ? val : def;
345 }
346
347 //=======================================================================
348 //function : IntegerVal
349 //purpose :
350 //=======================================================================
351
IntegerVal(const Standard_CString param,const Standard_Integer def) const352 Standard_Integer ShapeProcess_Context::IntegerVal (const Standard_CString param,
353 const Standard_Integer def) const
354 {
355 Standard_Integer val;
356 return GetInteger ( param, val ) ? val : def;
357 }
358
359 //=======================================================================
360 //function : StringVal
361 //purpose :
362 //=======================================================================
363
StringVal(const Standard_CString param,const Standard_CString def) const364 Standard_CString ShapeProcess_Context::StringVal (const Standard_CString param,
365 const Standard_CString def) const
366 {
367 if ( myRC.IsNull() ) return def;
368 try {
369 OCC_CATCH_SIGNALS
370 return myRC->Value ( MakeName ( myScope, param )->ToCString() );
371 }
372 catch (Standard_Failure const& anException) {
373 #ifdef OCCT_DEBUG
374 std::cout << "Warning: ShapeProcess_Context::GetInteger(): " << param << ": ";
375 anException.Print(std::cout); std::cout << std::endl;
376 #endif
377 (void)anException;
378 }
379 return def;
380 }
381
382 //=======================================================================
383 //function : SetMessenger
384 //purpose :
385 //=======================================================================
386
SetMessenger(const Handle (Message_Messenger)& messenger)387 void ShapeProcess_Context::SetMessenger (const Handle(Message_Messenger)& messenger)
388 {
389 if ( messenger.IsNull() )
390 myMessenger = Message::DefaultMessenger();
391 else
392 myMessenger = messenger;
393 }
394
395 //=======================================================================
396 //function : Messenger
397 //purpose :
398 //=======================================================================
399
Handle(Message_Messenger)400 Handle(Message_Messenger) ShapeProcess_Context::Messenger () const
401 {
402 return myMessenger;
403 }
404
405 //=======================================================================
406 //function : SetTraceLevel
407 //purpose :
408 //=======================================================================
409
SetTraceLevel(const Standard_Integer tracelev)410 void ShapeProcess_Context::SetTraceLevel (const Standard_Integer tracelev)
411 {
412 myTraceLev = tracelev;
413 }
414
415 //=======================================================================
416 //function : TraceLevel
417 //purpose :
418 //=======================================================================
419
TraceLevel() const420 Standard_Integer ShapeProcess_Context::TraceLevel () const
421 {
422 return myTraceLev;
423 }
424