Integration of OCCT 6.5.0 from SVN
[occt.git] / src / MoniTool / MoniTool_Profile.cxx
1 #include <MoniTool_Profile.ixx>
2 #include <TCollection_HAsciiString.hxx>
3 #include <Dico_IteratorOfDictionaryOfTransient.hxx>
4 #include <MoniTool_TypedValue.hxx>
5
6
7 static Standard_Boolean IsCurrent (const Standard_CString name)
8 {  return (name[0] == '.' && name[1] == '\0');  }
9
10     MoniTool_Profile::MoniTool_Profile ()
11 {
12   theopts  = new Dico_DictionaryOfTransient;
13   theconfs = new Dico_DictionaryOfTransient;
14 //    Current
15   Handle(Dico_DictionaryOfTransient) conf = new Dico_DictionaryOfTransient;
16   theconfs->SetItem (".",conf);
17   thecurname.AssignCat(".");
18   thecurconf = conf;
19 }
20
21     void  MoniTool_Profile::AddOption
22   (const Handle(MoniTool_Option)& option,
23    const Standard_CString name)
24 {
25   if (option.IsNull()) return;
26   if (name[0] == '\0') theopts->SetItem (option->Name().ToCString(),option);
27   else theopts->SetItem (name,option);
28 }
29
30     Handle(MoniTool_Option)  MoniTool_Profile::Option
31   (const Standard_CString name) const
32 {
33   Handle(MoniTool_Option) opt;
34   if (!theopts->GetItem (name,opt,Standard_True)) opt.Nullify();
35   return opt;
36 }
37
38     Handle(TColStd_HSequenceOfAsciiString)  MoniTool_Profile::OptionList () const
39 {
40   Handle(TColStd_HSequenceOfAsciiString) list = new TColStd_HSequenceOfAsciiString();
41   Dico_IteratorOfDictionaryOfTransient iter(theopts);
42   for (; iter.More(); iter.Next())  list->Append (iter.Name());
43   return list;
44 }
45
46     Handle(TColStd_HSequenceOfAsciiString)  MoniTool_Profile::TypedValueList () const
47 {
48   Handle(TColStd_HSequenceOfAsciiString) list = new TColStd_HSequenceOfAsciiString();
49   Dico_IteratorOfDictionaryOfTransient iter(theopts);
50   for (; iter.More(); iter.Next()) {
51     Handle(MoniTool_Option) opt = Handle(MoniTool_Option)::DownCast(iter.Value());
52     if (!opt->TypedValue().IsNull())  list->Append (iter.Name());
53   }
54   return list;
55 }
56
57     void  MoniTool_Profile::NewConf (const Standard_CString name)
58 {
59   if (IsCurrent (name)) return;
60   Handle(Dico_DictionaryOfTransient) conf = new Dico_DictionaryOfTransient;
61   theconfs->SetItem (name,conf);
62 //  thecurname.Clear();  thecurname.AssignCat (name);
63 //  thecurconf = conf;
64 }
65
66     void  MoniTool_Profile::AddConf (const Standard_CString name)
67 {
68   if (IsCurrent (name)) return;
69   Handle(Dico_DictionaryOfTransient) conf;
70   if (theconfs->GetItem (name,conf,Standard_False)) return;
71   conf = new Dico_DictionaryOfTransient;
72   theconfs->SetItem (name,conf);
73 //  thecurname.Clear();  thecurname.AssignCat (name);
74 //  thecurconf = conf;
75 }
76
77     Standard_Boolean  MoniTool_Profile::HasConf (const Standard_CString name) const
78       {  return theconfs->HasItem (name,Standard_False);  }
79
80     Handle(TColStd_HSequenceOfAsciiString)  MoniTool_Profile::ConfList () const
81
82   Handle(TColStd_HSequenceOfAsciiString) list = new TColStd_HSequenceOfAsciiString();
83   Dico_IteratorOfDictionaryOfTransient iter(theconfs);
84   for (; iter.More(); iter.Next())  list->Append (iter.Name());
85   return list;
86 }
87
88     Handle(Dico_DictionaryOfTransient)  MoniTool_Profile::Conf
89   (const Standard_CString name) const
90 {
91   Handle(Dico_DictionaryOfTransient) conf;
92   if (!theconfs->GetItem (name,conf,Standard_False)) conf.Nullify();
93   return conf;
94 }
95
96
97     Standard_Boolean  MoniTool_Profile::ClearConf
98   (const Standard_CString confname)
99 {
100   Handle(Dico_DictionaryOfTransient) conf;
101   if (!theconfs->GetItem (confname,conf,Standard_False)) return Standard_False;
102   conf->Clear();
103   return Standard_True;
104 }
105
106     Standard_Boolean  MoniTool_Profile::AddFromOtherConf
107   (const Standard_CString confname, const Standard_CString otherconf)
108 {
109   Handle(Dico_DictionaryOfTransient) conf, other;
110   if (!theconfs->GetItem (confname,conf,Standard_False)) return Standard_False;
111   if (!theconfs->GetItem (otherconf,other,Standard_False)) return Standard_False;
112   if (conf == other) return Standard_True;
113   Dico_IteratorOfDictionaryOfTransient iter(other);
114   for (; iter.More(); iter.Next())    conf->SetItem (iter.Name(),iter.Value());
115   return Standard_True;
116 }
117
118     Standard_Boolean  MoniTool_Profile::SetFromCurrent
119   (const Standard_CString confname)
120 {
121   Handle(Dico_DictionaryOfTransient) conf;
122   if (!theconfs->GetItem (confname,conf,Standard_False)) return Standard_False;
123   Dico_IteratorOfDictionaryOfTransient iter(theopts);
124   for (; iter.More(); iter.Next()) {
125     TCollection_AsciiString name = iter.Name();
126     TCollection_AsciiString cn   = CaseName (name.ToCString());
127     AddSwitch (name.ToCString(), cn.ToCString() );
128   }
129   return Standard_True;
130 }
131
132
133     Standard_Boolean  MoniTool_Profile::AddSwitch
134   (const Standard_CString confname,
135    const Standard_CString optname,
136    const Standard_CString casename)
137 {
138   Handle(Dico_DictionaryOfTransient) conf = Conf (confname);
139   Handle(MoniTool_Option) opt = Option (optname);
140   if (conf.IsNull() || opt.IsNull()) return Standard_False;
141   Handle(TCollection_HAsciiString) sw = new TCollection_HAsciiString(casename);
142   if (casename[0] == '\0') sw = new TCollection_HAsciiString (opt->CaseName());
143   Handle(Standard_Transient) val;
144   if (!opt->Item (sw->ToCString(),val)) return Standard_False;
145   conf->SetItem (optname,sw);
146   return Standard_True;
147 }
148
149     Standard_Boolean  MoniTool_Profile::RemoveSwitch
150   (const Standard_CString confname,
151    const Standard_CString optname)
152 {
153   Handle(Dico_DictionaryOfTransient) conf = Conf (confname);
154   Handle(MoniTool_Option) opt = Option (optname);
155   if (conf.IsNull() || opt.IsNull()) return Standard_False;
156   conf->RemoveItem (optname,Standard_False,Standard_False);
157   return Standard_True;
158 }
159
160
161     void  MoniTool_Profile::SwitchList
162   (const Standard_CString confname,
163    Handle(TColStd_HSequenceOfAsciiString)& optlist,
164    Handle(TColStd_HSequenceOfAsciiString)& caselist) const
165 {
166   optlist  = new TColStd_HSequenceOfAsciiString();
167   caselist = new TColStd_HSequenceOfAsciiString();
168   Handle(Dico_DictionaryOfTransient) conf = Conf (confname);
169   if (conf.IsNull()) return;
170   Dico_IteratorOfDictionaryOfTransient iter (conf);
171   for (; iter.More(); iter.Next()) {
172     TCollection_AsciiString optname = iter.Name();
173     Handle(TCollection_HAsciiString) cn =
174       Handle(TCollection_HAsciiString)::DownCast(iter.Value());
175     TCollection_AsciiString casename(cn->ToCString());
176     optlist->Append(optname);
177     caselist->Append(casename);
178   }
179 }
180
181
182     Standard_Boolean  MoniTool_Profile::SetCurrent
183   (const Standard_CString confname)
184 {
185   if (!AddFromOtherConf (".",confname)) return Standard_False;
186   thecurname.Clear();  thecurname.AssignCat (confname);
187   return Standard_True;
188 }
189
190     void  MoniTool_Profile::RecordCurrent ()
191 {
192   Dico_IteratorOfDictionaryOfTransient iter(theconfs);
193   for (; iter.More(); iter.Next()) {
194     Handle(MoniTool_Option) opt = Option (iter.Name().ToCString());
195     Handle(TCollection_HAsciiString) val =
196       Handle(TCollection_HAsciiString)::DownCast (iter.Value());
197     if (!val.IsNull())  opt->Switch (val->ToCString());
198   }
199 }
200
201     const TCollection_AsciiString&  MoniTool_Profile::Current () const
202       {  return thecurname;  }
203
204     TCollection_AsciiString  MoniTool_Profile::CaseName
205   (const Standard_CString optname, const Standard_Boolean proper) const
206 {
207   TCollection_AsciiString cn;
208   Handle(TCollection_HAsciiString) sw;
209   if (thecurconf->GetItem (optname,sw,Standard_True)) {
210     cn.AssignCat (sw->ToCString());
211     return cn;
212   }
213   if (proper) return cn;
214   Handle(MoniTool_Option) opt = Option (optname);
215   if (opt.IsNull()) return cn;
216   return opt->CaseName();
217 }
218
219     Handle(Standard_Transient)  MoniTool_Profile::CaseValue
220   (const Standard_CString optname) const
221 {
222   Handle(Standard_Transient) val;
223   Handle(MoniTool_Option) opt = Option (optname);
224   if (opt.IsNull()) return val;
225
226   Handle(TCollection_HAsciiString) sw;
227   if (!thecurconf->GetItem (optname,sw,Standard_True)) sw.Nullify();
228
229   if (sw.IsNull()) return opt->CaseValue();
230   if (!opt->Item (sw->ToCString(),val)) val.Nullify();
231   return val;
232 }
233
234
235     Standard_Boolean  MoniTool_Profile::Value
236   (const Standard_CString optname,
237    Handle(Standard_Transient)& val) const
238 {
239   Handle(MoniTool_Option) opt = Option (optname);
240   if (opt.IsNull()) return Standard_False;
241
242   Handle(TCollection_HAsciiString) sw;
243   if (!thecurconf->GetItem (optname,sw,Standard_True)) sw.Nullify();
244
245   if (sw.IsNull()) {  opt->Value(val);  return Standard_True;  }
246   return opt->Item (sw->ToCString(),val);
247 }
248
249
250     void  MoniTool_Profile::SetTypedValues
251   (const Standard_Boolean proper, const Standard_CString name) const
252 {
253   Dico_IteratorOfDictionaryOfTransient iter(theopts,name);
254   for (; iter.More(); iter.Next()) {
255     Handle(MoniTool_Option) opt = Handle(MoniTool_Option)::DownCast(iter.Value());
256     Handle(MoniTool_TypedValue) tv = opt->TypedValue();
257     if (tv.IsNull())  continue;
258
259 //    On recherche la valeur: en conf courante, sinon dans l option (cf proper)
260
261     Handle(TCollection_HAsciiString) sw, val;
262     if (!thecurconf->GetItem (name,sw,Standard_True)) sw.Nullify();
263     if (!sw.IsNull()) {
264       if (!opt->Item (sw->ToCString(),val)) val.Nullify();
265     }
266     if (val.IsNull() && !proper) opt->Value(val);
267
268 //    On applique
269     if (!val.IsNull()) tv->SetHStringValue (val);
270
271   }
272 }
273
274     void  MoniTool_Profile::SetFast (const Standard_CString confname)
275 {
276   Standard_Boolean cur = (confname[0] == '\0');
277   thefastval = new Dico_DictionaryOfTransient;
278   Dico_IteratorOfDictionaryOfTransient iter(theopts);
279   for (; iter.More(); iter.Next()) {
280     Handle(MoniTool_Option) opt = Handle(MoniTool_Option)::DownCast(iter.Value());
281     if (opt.IsNull()) continue;
282     Standard_Boolean iaopt = Standard_True;
283     TCollection_AsciiString optnam = iter.Name();
284     Standard_CString optname = optnam.ToCString();
285     Handle(Standard_Transient) val;
286
287     if (cur) {
288       Handle(TCollection_HAsciiString) sw;
289       if (!thecurconf->GetItem (optname,sw,Standard_True)) sw.Nullify();
290
291       if (sw.IsNull()) opt->Value(val);
292       else iaopt = opt->Item (sw->ToCString(),val);
293     }
294     else iaopt = opt->Item (confname,val);
295
296 //   Now, recording
297     if (iaopt) thefastval->SetItem (optname,val);
298   }
299 }
300
301
302     void  MoniTool_Profile::ClearFast ()
303       {  thefastval.Nullify(); }
304
305
306     Standard_Boolean  MoniTool_Profile::FastValue
307   (const Standard_CString optname,
308    Handle(Standard_Transient)& val) const
309 {
310   if (!thefastval.IsNull()) {
311     if (thefastval->GetItem (optname,val,Standard_True)) return Standard_True;
312   }
313   return Value (optname,val);
314 }