summaryrefslogtreecommitdiff
path: root/src/ShapeProcess/ShapeProcess_Context.cxx
blob: 7247357ed7bdb1cf41ce7b2ec3535dd2663bdc4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// File:	ShapeProcess_Context.cxx
// Created:	Mon Aug 21 19:27:30 2000
// Author:	Andrey BETENEV
//		<abv@doomox.nnov.matra-dtv.fr>

#include <ShapeProcess_Context.ixx>

#include <Standard_ErrorHandler.hxx>
#include <Standard_Failure.hxx>
#include <TCollection_HAsciiString.hxx>
#include <Message_Messenger.hxx>
#include <Message.hxx>
#include <sys/stat.h>

//=======================================================================
//function : ShapeProcess_Context
//purpose  : 
//=======================================================================

ShapeProcess_Context::ShapeProcess_Context() 
{
  myMessenger = Message::DefaultMessenger();
  myTraceLev = 1;
}
	
//=======================================================================
//function : ShapeProcess_Context
//purpose  : 
//=======================================================================

ShapeProcess_Context::ShapeProcess_Context (const Standard_CString file,
                                            const Standard_CString scope)
{
  Init ( file, scope );
  myMessenger = Message::DefaultMessenger();
  myTraceLev = 1;
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

Standard_Boolean ShapeProcess_Context::Init (const Standard_CString file,
                                             const Standard_CString scope)
{
  myScope.Nullify();
  myRC = LoadResourceManager ( file ); 
  if ( scope && scope[0] ) {
    SetScope ( scope );
  }
  return Standard_True; // myRC->Length() >0; NOT IMPLEMENTED
}

//=======================================================================
//function : LoadResourceManager
//purpose  : 
//=======================================================================

Handle(Resource_Manager) ShapeProcess_Context::LoadResourceManager (const Standard_CString file)
{
  // Optimisation of loading resource file: file is load only once
  // and reloaded only if file date has changed
  static Handle(Resource_Manager) sRC;
  static time_t mtime;
  static TCollection_AsciiString name;
  if ( ! sRC.IsNull() && ! name.IsEqual ( file ) ) sRC.Nullify();
  if ( ! sRC.IsNull() ) {
    struct stat buf;
    if ( ! stat ( file, &buf ) && buf.st_mtime != mtime ) {
      sRC.Nullify();
      mtime = buf.st_mtime;
    }
  }
  if ( sRC.IsNull() ) {
#ifdef DEB
    cout << "Info: ShapeProcess_Context: Reload Resource_Manager: " 
         << name.ToCString() << " -> " << file << endl;
#endif
    sRC = new Resource_Manager ( file );
    name = file;
  }
  return sRC;
}

//=======================================================================
//function : ResourceManager
//purpose  : 
//=======================================================================

const Handle(Resource_Manager) &ShapeProcess_Context::ResourceManager () const
{
  return myRC;
}

//=======================================================================
//function : SetScope
//purpose  : 
//=======================================================================

void ShapeProcess_Context::SetScope (const Standard_CString scope)
{
  if ( myScope.IsNull() ) myScope = new TColStd_HSequenceOfHAsciiString;
  Handle(TCollection_HAsciiString) str;
  if ( myScope->Length() >0 ) {
    str = new TCollection_HAsciiString ( myScope->Value ( myScope->Length() ) );
    str->AssignCat ( "." );
    str->AssignCat ( scope );
  }
  else str = new TCollection_HAsciiString ( scope );
  myScope->Append ( str );
}
	
//=======================================================================
//function : UnSetScope
//purpose  : 
//=======================================================================

void ShapeProcess_Context::UnSetScope ()
{
  if ( ! myScope.IsNull() && myScope->Length() >0 ) 
    myScope->Remove ( myScope->Length() );
}
	
//=======================================================================
//function : IsParamSet
//purpose  : 
//=======================================================================

static Handle(TCollection_HAsciiString) MakeName (const Handle(TColStd_HSequenceOfHAsciiString) &scope,
						  const Standard_CString param)
{
  Handle(TCollection_HAsciiString) str;
  if ( ! scope.IsNull() && scope->Length() >0 ) {
    str = new TCollection_HAsciiString ( scope->Value ( scope->Length() )->String() );
    str->AssignCat (".");
    str->AssignCat ( param );
  }
  else str = new TCollection_HAsciiString ( param );
  return str;
}

Standard_Boolean ShapeProcess_Context::IsParamSet (const Standard_CString param) const
{
  return ! myRC.IsNull() && myRC->Find ( MakeName ( myScope, param )->ToCString() );
}

//=======================================================================
//function : GetString
//purpose  : 
//=======================================================================

Standard_Boolean ShapeProcess_Context::GetString (const Standard_CString param,
                                                  TCollection_AsciiString &str) const
{
  if ( myRC.IsNull() ) return Standard_False;
  Handle(TCollection_HAsciiString) pname = MakeName ( myScope, param );
  if ( ! myRC->Find ( pname->ToCString() ) ) {
#ifdef DEB 
    cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << pname->ToCString() << " is not defined" << endl;
#endif
    return Standard_False;
  }
  str = myRC->Value ( pname->ToCString() );
  return Standard_True;
}

//=======================================================================
//function : GetReal
//purpose  : 
//=======================================================================

Standard_Boolean ShapeProcess_Context::GetReal (const Standard_CString param,
                                                Standard_Real &val) const
{
  if ( myRC.IsNull() ) return Standard_False;
  
  TCollection_AsciiString str;
  if ( ! GetString ( param, str ) ) return Standard_False;
  
  if ( str.IsRealValue() ) {
    val = str.RealValue();
    return Standard_True;
  }

  // if not real, try to treat as alias "&param"
  str.LeftAdjust();
  if ( str.Value(1) == '&' ) {
    TCollection_AsciiString ref = str.Split ( 1 );
    ref.LeftAdjust();
    ref.RightAdjust();
    if ( ! myRC->Find ( ref.ToCString() ) ) {
#ifdef DEB 
      cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << ref.ToCString() << " is not defined" << endl;
#endif
      return Standard_False;
    }
    str = myRC->Value ( ref.ToCString() );
    if ( str.IsRealValue() ) {
      val = str.RealValue();
      return Standard_True;
    }
  }
#ifdef DEB 
  cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << param << " is neither Real nor reference to Real";
#endif
  return Standard_False;
}

//=======================================================================
//function : GetInteger
//purpose  : 
//=======================================================================

Standard_Boolean ShapeProcess_Context::GetInteger (const Standard_CString param,
                                                   Standard_Integer &val) const
{
  if ( myRC.IsNull() ) return Standard_False;
  
  TCollection_AsciiString str;
  if ( ! GetString ( param, str ) ) return Standard_False;
  
  if ( str.IsIntegerValue() ) {
    val = str.IntegerValue();
    return Standard_True;
  }

  // if not integer, try to treat as alias "&param"
  str.LeftAdjust();
  if ( str.Value(1) == '&' ) {
    TCollection_AsciiString ref = str.Split ( 1 );
    ref.LeftAdjust();
    ref.RightAdjust();
    if ( ! myRC->Find ( ref.ToCString() ) ) {
#ifdef DEB 
      cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << ref.ToCString() << " is not defined" << endl;
#endif
      return Standard_False;
    }
    str = myRC->Value ( ref.ToCString() );
    if ( str.IsIntegerValue() ) {
      val = str.IntegerValue();
      return Standard_True;
    }
  }
#ifdef DEB 
  cout << "Warning: ShapeProcess_Context::GetInteger(): Parameter " << param << " is neither Integer nor reference to Integer";
#endif
  return Standard_False;
}

//=======================================================================
//function : GetBoolean
//purpose  : 
//=======================================================================

Standard_Boolean ShapeProcess_Context::GetBoolean (const Standard_CString param,
                                                   Standard_Boolean &val) const
{
  if ( myRC.IsNull() ) return Standard_False;
  try {
    OCC_CATCH_SIGNALS
    val = (Standard_Boolean)myRC->Integer ( MakeName ( myScope, param )->ToCString() );
    return Standard_True;
  }
  catch (Standard_Failure) {
#ifdef DEB 
    cout << "Warning: ShapeProcess_Context::GetInteger(): " << param << ": ";
    Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
  }
  return Standard_False;
}

//=======================================================================
//function : RealVal
//purpose  : 
//=======================================================================

Standard_Real ShapeProcess_Context::RealVal (const Standard_CString param,
                                             const Standard_Real def) const
{
  Standard_Real val;
  return GetReal ( param, val ) ? val : def;
}

//=======================================================================
//function : BooleanVal
//purpose  : 
//=======================================================================

Standard_Boolean ShapeProcess_Context::BooleanVal (const Standard_CString param,
                                                   const Standard_Boolean def) const
{
  Standard_Boolean val;
  return GetBoolean ( param, val ) ? val : def;
}

//=======================================================================
//function : IntegerVal
//purpose  : 
//=======================================================================

Standard_Integer ShapeProcess_Context::IntegerVal (const Standard_CString param,
                                                   const Standard_Integer def) const
{
  Standard_Integer val;
  return GetInteger ( param, val ) ? val : def;
}

//=======================================================================
//function : StringVal
//purpose  : 
//=======================================================================

Standard_CString ShapeProcess_Context::StringVal (const Standard_CString param,
                                                  const Standard_CString def) const
{
  if ( myRC.IsNull() ) return def;
  try {
    OCC_CATCH_SIGNALS
    return myRC->Value ( MakeName ( myScope, param )->ToCString() );
  }
  catch (Standard_Failure) {
#ifdef DEB 
    cout << "Warning: ShapeProcess_Context::GetInteger(): " << param << ": ";
    Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
  }
  return def;
}

//=======================================================================
//function : SetMessenger
//purpose  : 
//=======================================================================

void ShapeProcess_Context::SetMessenger (const Handle(Message_Messenger)& messenger)
{
  if ( messenger.IsNull() )
    myMessenger = Message::DefaultMessenger();
  else
    myMessenger = messenger;
}

//=======================================================================
//function : Messenger
//purpose  : 
//=======================================================================

Handle(Message_Messenger) ShapeProcess_Context::Messenger () const
{
  return myMessenger;
}

//=======================================================================
//function : SetTraceLevel
//purpose  : 
//=======================================================================

void ShapeProcess_Context::SetTraceLevel (const Standard_Integer tracelev)
{
  myTraceLev = tracelev;
}

//=======================================================================
//function : TraceLevel
//purpose  : 
//=======================================================================

Standard_Integer ShapeProcess_Context::TraceLevel () const
{
  return myTraceLev;
}