1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13 
14 #include <Interface_Static.hxx>
15 
16 #include <Interface_InterfaceError.hxx>
17 #include <OSD_Path.hxx>
18 #include <Standard_Transient.hxx>
19 #include <Standard_Type.hxx>
20 #include <TCollection_HAsciiString.hxx>
21 
22 #include <stdio.h>
23 IMPLEMENT_STANDARD_RTTIEXT(Interface_Static,Interface_TypedValue)
24 
25 static char defmess[31];
26 
27 //  Fonctions Satisfies offertes en standard ...
28 
29 //svv #2
30 //static Standard_Boolean StaticPath(const Handle(TCollection_HAsciiString)& val)
31 //{
32 //  OSD_Path apath;
33 //  return apath.IsValid (TCollection_AsciiString(val->ToCString()));
34 //}
35 
36 
Interface_Static(const Standard_CString family,const Standard_CString name,const Interface_ParamType type,const Standard_CString init)37     Interface_Static::Interface_Static
38   (const Standard_CString family,  const Standard_CString name,
39    const Interface_ParamType type, const Standard_CString init)
40     : Interface_TypedValue (name,type,init) ,
41       thefamily (family) ,
42       theupdate (Standard_True)    {  }
43 
Interface_Static(const Standard_CString family,const Standard_CString name,const Handle (Interface_Static)& other)44     Interface_Static::Interface_Static
45   (const Standard_CString family,  const Standard_CString name,
46    const Handle(Interface_Static)& other)
47     : Interface_TypedValue (name, other->Type(), "") ,
48       thefamily (family) ,
49       theupdate (Standard_True)
50 {
51   switch (Type()) {
52     case Interface_ParamInteger : {
53       Standard_Integer lim;
54       if (other->IntegerLimit (Standard_True ,lim)) SetIntegerLimit (Standard_True ,lim);
55       if (other->IntegerLimit (Standard_False,lim)) SetIntegerLimit (Standard_False,lim);
56     }
57       break;
58     case Interface_ParamReal : {
59       Standard_Real lim;
60       if (other->RealLimit (Standard_True ,lim)) SetRealLimit (Standard_True ,lim);
61       if (other->RealLimit (Standard_False,lim)) SetRealLimit (Standard_False,lim);
62       SetUnitDef (other->UnitDef());
63     }
64       break;
65     case Interface_ParamEnum : {
66       Standard_Boolean match;  Standard_Integer e0,e1,i;
67       other->EnumDef (e0,e1,match);
68       StartEnum (e0,match);
69 //      if (e1 >= e0) theenums = new TColStd_HArray1OfAsciiString(e0,e1);
70       for (i = e0; i <= e1; i ++)
71 	AddEnum (other->EnumVal(i));
72     }
73       break;
74     case Interface_ParamIdent :
75       SetObjectType (other->ObjectType());
76       break;
77     default :  break;
78   }
79 
80   if (other->IsSetValue()) SetCStringValue (other->CStringValue());
81 }
82 
83 
84 //  ##   Print   ##
85 
PrintStatic(Standard_OStream & S) const86 void  Interface_Static::PrintStatic (Standard_OStream& S) const
87 {
88   S <<"--- Static Value : "<<Name()<<"  Family:"<<Family();
89   Print (S);
90   if (!thewild.IsNull())
91     S <<" -- Attached to wild-card : "<<thewild->Name()<<std::endl;
92   S <<"--- Actual status : "<<(theupdate ? "" : "original")<<"  Value : ";
93 
94   if (thesatisf) S <<" -- Specific Function for Satisfies : "<<thesatisn.ToCString()<<std::endl;
95 }
96 
97 
98 //  #########    COMPLEMENTS    ##########
99 
Family() const100 Standard_CString  Interface_Static::Family () const
101 {
102   return thefamily.ToCString();
103 }
104 
105 
Handle(Interface_Static)106 Handle(Interface_Static)  Interface_Static::Wild () const
107 {
108   return thewild;
109 }
110 
111 
SetWild(const Handle (Interface_Static)& wild)112 void  Interface_Static::SetWild (const Handle(Interface_Static)& wild)
113 {
114   thewild = wild;
115 }
116 
117 
118 //  #########   UPDATE    ##########
119 
SetUptodate()120 void  Interface_Static::SetUptodate ()
121 {
122   theupdate = Standard_True;
123 }
124 
125 
UpdatedStatus() const126 Standard_Boolean  Interface_Static::UpdatedStatus () const
127 {
128   return theupdate;
129 }
130 
131 
132 
133 //  #######################################################################
134 //  #########    DICTIONNAIRE DES STATICS (static sur Static)    ##########
135 
Init(const Standard_CString family,const Standard_CString name,const Interface_ParamType type,const Standard_CString init)136 Standard_Boolean  Interface_Static::Init
137   (const Standard_CString family,  const Standard_CString name,
138    const Interface_ParamType type, const Standard_CString init)
139 {
140   if (name[0] == '\0') return Standard_False;
141 
142   if (MoniTool_TypedValue::Stats().IsBound(name)) return Standard_False;
143   Handle(Interface_Static) item;
144   if (type == Interface_ParamMisc) {
145     Handle(Interface_Static) other = Interface_Static::Static(init);
146     if (other.IsNull()) return Standard_False;
147     item = new Interface_Static (family,name,other);
148   }
149   else item = new Interface_Static (family,name,type,init);
150 
151   MoniTool_TypedValue::Stats().Bind (name,item);
152   return Standard_True;
153 }
154 
155 
Init(const Standard_CString family,const Standard_CString name,const Standard_Character type,const Standard_CString init)156 Standard_Boolean  Interface_Static::Init
157   (const Standard_CString family,  const Standard_CString name,
158    const Standard_Character type, const Standard_CString init)
159 {
160   Interface_ParamType epyt;
161   switch (type) {
162     case 'e' : epyt = Interface_ParamEnum;     break;
163     case 'i' : epyt = Interface_ParamInteger;  break;
164     case 'o' : epyt = Interface_ParamIdent;    break;
165     case 'p' : epyt = Interface_ParamText;     break;
166     case 'r' : epyt = Interface_ParamReal;     break;
167     case 't' : epyt = Interface_ParamText;     break;
168     case '=' : epyt = Interface_ParamMisc;     break;
169     case '&' : {
170       Handle(Interface_Static) unstat = Interface_Static::Static(name);
171       if (unstat.IsNull()) return Standard_False;
172 //    Editions : init donne un petit texte d edition, en 2 termes "cmd var" :
173 //  imin <ival>  imax <ival>  rmin <rval>  rmax <rval>  unit <def>
174 //  enum <from>  ematch <from>  eval <cval>
175       Standard_Integer i,iblc = 0;
176       for (i = 0; init[i] != '\0'; i ++) if (init[i] == ' ') iblc = i+1;
177 //  Reconnaissance du sous-cas et aiguillage
178       if      (init[0] == 'i' && init[2] == 'i')
179 	unstat->SetIntegerLimit (Standard_False,atoi(&init[iblc]));
180       else if (init[0] == 'i' && init[2] == 'a')
181 	unstat->SetIntegerLimit (Standard_True ,atoi(&init[iblc]));
182       else if (init[0] == 'r' && init[2] == 'i')
183 	unstat->SetRealLimit (Standard_False,Atof(&init[iblc]));
184       else if (init[0] == 'r' && init[2] == 'a')
185 	unstat->SetRealLimit (Standard_True ,Atof(&init[iblc]));
186       else if (init[0] == 'u')
187 	unstat->SetUnitDef (&init[iblc]);
188       else if (init[0] == 'e' && init[1] == 'm')
189 	unstat->StartEnum (atoi(&init[iblc]),Standard_True);
190       else if (init[0] == 'e' && init[1] == 'n')
191 	unstat->StartEnum (atoi(&init[iblc]),Standard_False);
192       else if (init[0] == 'e' && init[1] == 'v')
193 	unstat->AddEnum (&init[iblc]);
194       else return Standard_False;
195       return Standard_True;
196     }
197     default  : return Standard_False;
198   }
199   if (!Interface_Static::Init (family,name,epyt,init)) return Standard_False;
200   if (type != 'p') return Standard_True;
201   Handle(Interface_Static) stat = Interface_Static::Static(name);
202 //NT  stat->SetSatisfies (StaticPath,"Path");
203   if (!stat->Satisfies(stat->HStringValue())) stat->SetCStringValue("");
204   return Standard_True;
205 }
206 
207 
Handle(Interface_Static)208 Handle(Interface_Static)  Interface_Static::Static
209   (const Standard_CString name)
210 {
211   Handle(Standard_Transient) result;
212   MoniTool_TypedValue::Stats().Find(name, result);
213   return Handle(Interface_Static)::DownCast(result);
214 }
215 
216 
IsPresent(const Standard_CString name)217 Standard_Boolean  Interface_Static::IsPresent (const Standard_CString name)
218 {
219   return MoniTool_TypedValue::Stats().IsBound (name);
220 }
221 
222 
CDef(const Standard_CString name,const Standard_CString part)223 Standard_CString  Interface_Static::CDef
224   (const Standard_CString name, const Standard_CString part)
225 {
226   if (!part || part[0] == '\0') return "";
227   Handle(Interface_Static) stat = Interface_Static::Static(name);
228   if (stat.IsNull()) return "";
229   if (part[0] == 'f' && part[1] == 'a') return stat->Family();
230   if (part[0] == 'l' && part[1] == 'a') return stat->Label();
231   if (part[0] == 's' && part[1] == 'a') return stat->SatisfiesName();
232   if (part[0] == 't' && part[1] == 'y') {
233     Interface_ParamType typ = stat->Type();
234     if (typ == Interface_ParamInteger)  return "integer";
235     if (typ == Interface_ParamReal)     return "real";
236     if (typ == Interface_ParamText)     return "text";
237     if (typ == Interface_ParamEnum)     return "enum";
238     return "?type?";
239   }
240   if (part[0] == 'e') {
241     Standard_Integer nume = 0;
242     sscanf (part,"%30s %d",defmess,&nume);
243     return stat->EnumVal(nume);
244   }
245   if (part[0] == 'i') {
246     Standard_Integer ilim;
247     if (!stat->IntegerLimit((part[2] == 'a'),ilim)) return "";
248     Sprintf(defmess,"%d",ilim);  return defmess;
249   }
250   if (part[0] == 'r') {
251     Standard_Real rlim;
252     if (!stat->RealLimit((part[2] == 'a'),rlim)) return "";
253     Sprintf(defmess,"%f",rlim);  return defmess;
254   }
255   if (part[0] == 'u') return stat->UnitDef();
256   return "";
257 }
258 
259 
IDef(const Standard_CString name,const Standard_CString part)260 Standard_Integer  Interface_Static::IDef
261   (const Standard_CString name, const Standard_CString part)
262 {
263   if (!part || part[0] == '\0') return 0;
264   Handle(Interface_Static) stat = Interface_Static::Static(name);
265   if (stat.IsNull()) return 0;
266   if (part[0] == 'i') {
267     Standard_Integer ilim;
268     if (!stat->IntegerLimit((part[2] == 'a'),ilim)) return 0;
269     return ilim;
270   }
271   if (part[0] == 'e') {
272     Standard_Integer startcase,endcase;  Standard_Boolean match;
273     stat->EnumDef (startcase,endcase,match);
274     if (part[1] == 's') return startcase;
275     if (part[1] == 'c') return (endcase - startcase + 1);
276     if (part[1] == 'm') return (match ? 1 : 0);
277     if (part[1] == 'v') {
278       char vale[51];
279       sscanf (part,"%30s %50s",defmess,vale);
280       return stat->EnumCase (vale);
281     }
282   }
283   return 0;
284 }
285 
286 
287 //  ##########  VALEUR COURANTE  ###########
288 
IsSet(const Standard_CString name,const Standard_Boolean proper)289 Standard_Boolean  Interface_Static::IsSet
290   (const Standard_CString name, const Standard_Boolean proper)
291 {
292   Handle(Interface_Static) item = Interface_Static::Static(name);
293   if (item.IsNull()) return Standard_False;
294   if (item->IsSetValue()) return Standard_True;
295   if (proper) return Standard_False;
296   item = item->Wild();
297   return item->IsSetValue();
298 }
299 
300 
CVal(const Standard_CString name)301 Standard_CString  Interface_Static::CVal  (const Standard_CString name)
302 {
303   Handle(Interface_Static) item = Interface_Static::Static(name);
304   if (item.IsNull()) {
305 #ifdef OCCT_DEBUG
306     std::cout << "Warning: Interface_Static::CVal: incorrect parameter " << name << std::endl;
307 #endif
308     return "";
309   }
310   return item->CStringValue();
311 }
312 
313 
IVal(const Standard_CString name)314 Standard_Integer  Interface_Static::IVal  (const Standard_CString name)
315 {
316   Handle(Interface_Static) item = Interface_Static::Static(name);
317   if (item.IsNull()) {
318 #ifdef OCCT_DEBUG
319     std::cout << "Warning: Interface_Static::IVal: incorrect parameter " << name << std::endl;
320 #endif
321     return 0;
322   }
323   return item->IntegerValue();
324 }
325 
326 
RVal(const Standard_CString name)327 Standard_Real Interface_Static::RVal (const Standard_CString name)
328 {
329   Handle(Interface_Static) item = Interface_Static::Static(name);
330   if (item.IsNull()) {
331 #ifdef OCCT_DEBUG
332     std::cout << "Warning: Interface_Static::RVal: incorrect parameter " << name << std::endl;
333 #endif
334     return 0.0;
335   }
336   return item->RealValue();
337 }
338 
339 
SetCVal(const Standard_CString name,const Standard_CString val)340 Standard_Boolean  Interface_Static::SetCVal
341   (const Standard_CString name, const Standard_CString val)
342 {
343   Handle(Interface_Static) item = Interface_Static::Static(name);
344   if (item.IsNull()) return Standard_False;
345   return item->SetCStringValue(val);
346 }
347 
348 
SetIVal(const Standard_CString name,const Standard_Integer val)349 Standard_Boolean  Interface_Static::SetIVal
350   (const Standard_CString name, const Standard_Integer val)
351 {
352   Handle(Interface_Static) item = Interface_Static::Static(name);
353   if (item.IsNull()) return Standard_False;
354   if (!item->SetIntegerValue(val)) return Standard_False;
355   return Standard_True;
356 }
357 
358 
SetRVal(const Standard_CString name,const Standard_Real val)359 Standard_Boolean  Interface_Static::SetRVal
360   (const Standard_CString name, const Standard_Real val)
361 {
362   Handle(Interface_Static) item = Interface_Static::Static(name);
363   if (item.IsNull()) return Standard_False;
364   return item->SetRealValue(val);
365 }
366 
367 //    UPDATE
368 
Update(const Standard_CString name)369 Standard_Boolean  Interface_Static::Update (const Standard_CString name)
370 {
371   Handle(Interface_Static) item = Interface_Static::Static(name);
372   if (item.IsNull()) return Standard_False;
373   item->SetUptodate();
374   return Standard_True;
375 }
376 
IsUpdated(const Standard_CString name)377 Standard_Boolean  Interface_Static::IsUpdated (const Standard_CString name)
378 {
379   Handle(Interface_Static) item = Interface_Static::Static(name);
380   if (item.IsNull()) return Standard_False;
381   return item->UpdatedStatus();
382 }
383 
Handle(TColStd_HSequenceOfHAsciiString)384 Handle(TColStd_HSequenceOfHAsciiString)  Interface_Static::Items
385   (const Standard_Integer mode, const Standard_CString criter)
386 {
387   Standard_Integer modup = (mode / 100);  // 0 any, 1 non-update, 2 update
388   Handle(TColStd_HSequenceOfHAsciiString) list =
389     new TColStd_HSequenceOfHAsciiString();
390   NCollection_DataMap<TCollection_AsciiString, Handle(Standard_Transient)>::Iterator iter(MoniTool_TypedValue::Stats());
391   for (; iter.More(); iter.Next()) {
392     Handle(Interface_Static) item =
393       Handle(Interface_Static)::DownCast(iter.Value());
394     if (item.IsNull()) continue;
395     Standard_Boolean ok = Standard_True;
396     if (criter[0] == '$' && criter[1] == '\0') {
397       if ( (item->Family())[0] != '$') ok = Standard_False;
398     } else if (criter[0] != '\0') {
399       if (strcmp(criter, item->Family())) continue;
400       ok = Standard_True;
401     } else {     // tous ... sauf famille a $
402       if (item->Family()[0] == '$') continue;
403     }
404     if (ok && (modup == 1)) ok = !item->UpdatedStatus();
405     if (ok && (modup == 2)) ok =  item->UpdatedStatus();
406 
407     if (ok) list->Append (new TCollection_HAsciiString (iter.Key()) );
408   }
409   return list;
410 }
411