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