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