source: trunk/source/intercoms/src/G4UIcommandTree.cc@ 1043

Last change on this file since 1043 was 1031, checked in by garnier, 17 years ago

en test before cvs

File size: 14.7 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27// $Id: G4UIcommandTree.cc,v 1.14 2008/01/30 11:20:03 lgarnier Exp $
28// GEANT4 tag $Name: geant4-09-02 $
29//
30
31#include "G4UIcommandTree.hh"
32#include "G4StateManager.hh"
33#include <fstream>
34#include "G4ios.hh"
35
36G4UIcommandTree::G4UIcommandTree()
37:guidance(NULL)
38{ }
39
40G4UIcommandTree::G4UIcommandTree(const char * thePathName)
41:guidance(NULL)
42{
43 pathName = thePathName;
44}
45
46G4UIcommandTree::~G4UIcommandTree()
47{
48 G4int i;
49 G4int n_treeEntry = tree.size();
50 for( i=0; i < n_treeEntry; i++ )
51 { delete tree[i]; }
52}
53
54G4int G4UIcommandTree::operator==(const G4UIcommandTree &right) const
55{
56 return ( pathName == right.GetPathName() );
57}
58
59G4int G4UIcommandTree::operator!=(const G4UIcommandTree &right) const
60{
61 return ( pathName != right.GetPathName() );
62}
63
64void G4UIcommandTree::AddNewCommand(G4UIcommand *newCommand)
65{
66 G4String commandPath = newCommand->GetCommandPath();
67 G4String remainingPath = commandPath;
68 remainingPath.remove(0,pathName.length());
69 if( remainingPath.isNull() )
70 {
71 guidance = newCommand;
72 return;
73 }
74 G4int i = remainingPath.first('/');
75 if( i == G4int(std::string::npos) )
76 {
77 // Find command
78 G4int n_commandEntry = command.size();
79 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
80 {
81 if( remainingPath == command[i_thCommand]->GetCommandName() )
82 { return; }
83 }
84 command.push_back( newCommand );
85 return;
86 }
87 else
88 {
89 // Find path
90 G4String nextPath = pathName;
91 nextPath.append(remainingPath(0,i+1));
92 G4int n_treeEntry = tree.size();
93 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
94 {
95 if( nextPath == tree[i_thTree]->GetPathName() )
96 {
97 tree[i_thTree]->AddNewCommand( newCommand );
98 return;
99 }
100 }
101 G4UIcommandTree * newTree = new G4UIcommandTree( nextPath );
102 tree.push_back( newTree );
103 newTree->AddNewCommand( newCommand );
104 return;
105 }
106}
107
108void G4UIcommandTree::RemoveCommand(G4UIcommand *aCommand)
109{
110 G4String commandPath = aCommand->GetCommandPath();
111 G4String remainingPath = commandPath;
112 remainingPath.remove(0,pathName.length());
113 if( remainingPath.isNull() )
114 {
115 guidance = NULL;
116 }
117 else
118 {
119 G4int i = remainingPath.first('/');
120 if( i == G4int(std::string::npos) )
121 {
122 // Find command
123 G4int n_commandEntry = command.size();
124 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
125 {
126 if( remainingPath == command[i_thCommand]->GetCommandName() )
127 {
128 command.erase(command.begin()+i_thCommand);
129 break;
130 }
131 }
132 }
133 else
134 {
135 // Find path
136 G4String nextPath = pathName;
137 nextPath.append(remainingPath(0,i+1));
138 G4int n_treeEntry = tree.size();
139 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
140 {
141 if( nextPath == tree[i_thTree]->GetPathName() )
142 {
143 tree[i_thTree]->RemoveCommand( aCommand );
144 G4int n_commandRemain = tree[i_thTree]->GetCommandEntry();
145 if(n_commandRemain==0)
146 {
147 G4UIcommandTree * emptyTree = tree[i_thTree];
148 tree.erase(tree.begin()+i_thTree);
149 delete emptyTree;
150 }
151 break;
152 }
153 }
154 }
155 }
156}
157
158// L. Garnier 01.28.08 This function has not a good name. In fact, it try
159// to match a command name, not a path. It should be rename as FindCommandName
160
161G4UIcommand * G4UIcommandTree::FindPath(const char* commandPath)
162{
163 G4String remainingPath = commandPath;
164 if( remainingPath.index( pathName ) == std::string::npos )
165 { return NULL; }
166 remainingPath.remove(0,pathName.length());
167 G4int i = remainingPath.first('/');
168 if( i == G4int(std::string::npos) )
169 {
170 // Find command
171 G4int n_commandEntry = command.size();
172 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
173 {
174 if( remainingPath == command[i_thCommand]->GetCommandName() )
175 { return command[i_thCommand]; }
176 }
177 }
178 else
179 {
180 // Find path
181 G4String nextPath = pathName;
182 nextPath.append(remainingPath(0,i+1));
183 G4int n_treeEntry = tree.size();
184 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
185 {
186 if( nextPath == tree[i_thTree]->GetPathName() )
187 { return tree[i_thTree]->FindPath( commandPath ); }
188 }
189 }
190 return NULL;
191}
192
193
194/**
195 * Try to match a command or a path with the one given.
196 * @commandPath : command or path to match
197 * @return the commandTree found or NULL if not
198 */
199G4UIcommandTree * G4UIcommandTree::FindCommandTree(const char* commandPath)
200{
201 G4String remainingPath = commandPath;
202 if( remainingPath.index( pathName ) == std::string::npos )
203 { return NULL; }
204 remainingPath.remove(0,pathName.length());
205 G4int i = remainingPath.first('/');
206 if( i != G4int(std::string::npos) )
207 {
208 // Find path
209 G4String nextPath = pathName;
210 nextPath.append(remainingPath(0,i+1));
211 G4int n_treeEntry = tree.size();
212 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
213 {
214 if (tree[i_thTree]->GetPathName() == commandPath) {
215 return tree[i_thTree];
216 }
217 else if( nextPath == tree[i_thTree]->GetPathName() ) {
218 return tree[i_thTree]->FindCommandTree( commandPath );
219 }
220 }
221 } else {
222 return this;
223 }
224 return NULL;
225}
226
227G4String G4UIcommandTree::CompleteCommandPath(const G4String aCommandPath)
228{
229 G4String pathName = aCommandPath;
230 G4String remainingPath = aCommandPath;
231
232 // find the tree
233 G4int jpre= pathName.last('/');
234 if(jpre != G4int(G4String::npos)) pathName.remove(jpre+1);
235 G4UIcommandTree* aTree = FindCommandTree(pathName);
236
237 G4String empty = "";
238 if (!aTree) {
239 return empty;
240 }
241
242 if( pathName.index( pathName ) == std::string::npos ) return empty;
243
244 std::vector<G4String> paths;
245
246 // list matched directories/commands
247 G4String stream, strtmp;
248 G4int nMatch= 0;
249
250 int Ndir= aTree-> GetTreeEntry();
251 int Ncmd= aTree-> GetCommandEntry();
252
253 // directory ...
254 for(G4int idir=1; idir<=Ndir; idir++) {
255 G4String fpdir= aTree-> GetTree(idir)-> GetPathName();
256 // matching test
257 if( fpdir.index(remainingPath, 0) == 0) {
258 if(nMatch==0) {
259 stream = fpdir;
260 } else {
261 stream = GetFirstMatchedString(fpdir,stream);
262 }
263 nMatch++;
264 paths.push_back(fpdir);
265 }
266 }
267
268 if (paths.size()>=2) {
269 G4cout << "Matching directories :" << G4endl;
270 for( unsigned int i_thCommand = 0; i_thCommand < paths.size(); i_thCommand++ ) {
271 G4cout << paths[i_thCommand] << G4endl;
272 }
273 }
274
275 // command ...
276 std::vector<G4String> commands;
277
278 for(G4int icmd=1; icmd<=Ncmd; icmd++){
279 G4String fpcmd= aTree-> GetPathName() +
280 aTree-> GetCommand(icmd) -> GetCommandName();
281 // matching test
282 if( fpcmd.index(remainingPath, 0) ==0) {
283 if(nMatch==0) {
284 stream= fpcmd + " ";
285 } else {
286 strtmp= fpcmd + " ";
287 stream= GetFirstMatchedString(stream, strtmp);
288 }
289 nMatch++;
290 commands.push_back(fpcmd+" ");
291 }
292 }
293
294 if (commands.size()>=2) {
295 G4cout << "Matching commands :" << G4endl;
296 for( unsigned int i_thCommand = 0; i_thCommand < commands.size(); i_thCommand++ ) {
297 G4cout << commands[i_thCommand] << G4endl;
298 }
299 }
300
301 return stream;
302}
303
304
305////////////////////////////////////////////////////////////////////
306G4String G4UIcommandTree::GetFirstMatchedString(const G4String& str1,
307 const G4String& str2) const
308////////////////////////////////////////////////////////////////////
309{
310 int nlen1= str1.length();
311 int nlen2= str2.length();
312
313 int nmin = nlen1<nlen2 ? nlen1 : nlen2;
314
315 G4String strMatched;
316 for(size_t i=0; G4int(i)<nmin; i++){
317 if(str1[i]==str2[i]) {
318 strMatched+= str1[i];
319 } else {
320 break;
321 }
322 }
323
324 return strMatched;
325}
326
327void G4UIcommandTree::ListCurrent()
328{
329 G4cout << "Command directory path : " << pathName << G4endl;
330 if( guidance != NULL ) guidance->List();
331 G4cout << " Sub-directories : " << G4endl;
332 G4int n_treeEntry = tree.size();
333 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
334 {
335 G4cout << " " << tree[i_thTree]->GetPathName()
336 << " " << tree[i_thTree]->GetTitle() << G4endl;
337 }
338 G4cout << " Commands : " << G4endl;
339 G4int n_commandEntry = command.size();
340 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
341 {
342 G4cout << " " << command[i_thCommand]->GetCommandName()
343 << " * " << command[i_thCommand]->GetTitle() << G4endl;
344 }
345}
346
347void G4UIcommandTree::ListCurrentWithNum()
348{
349 G4cout << "Command directory path : " << pathName << G4endl;
350 if( guidance != NULL ) guidance->List();
351 G4int i = 0;
352 G4cout << " Sub-directories : " << G4endl;
353 G4int n_treeEntry = tree.size();
354 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
355 {
356 i++;
357 G4cout << " " << i << ") " << tree[i_thTree]->GetPathName()
358 << " " << tree[i_thTree]->GetTitle() << G4endl;
359 }
360 G4cout << " Commands : " << G4endl;
361 G4int n_commandEntry = command.size();
362 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
363 {
364 i++;
365 G4cout << " " << i << ") " << command[i_thCommand]->GetCommandName()
366 << " * " << command[i_thCommand]->GetTitle() << G4endl;
367 }
368}
369
370void G4UIcommandTree::List()
371{
372 ListCurrent();
373 G4int n_commandEntry = command.size();
374 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
375 {
376 command[i_thCommand]->List();
377 }
378 G4int n_treeEntry = tree.size();
379 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
380 {
381 tree[i_thTree]->List();
382 }
383}
384
385G4String G4UIcommandTree::CreateFileName(const char* pName)
386{
387 G4String fn = pName;
388 G4int idxs;
389 while((idxs=fn.index("/"))!=G4int(std::string::npos))
390 { fn(idxs) = '_'; }
391 fn += ".html";
392 return fn;
393}
394
395G4String G4UIcommandTree::ModStr(const char* strS)
396{
397 G4String sx;
398 G4String str = strS;
399 for(G4int i=0;i<G4int(str.length());i++)
400 {
401 char c = str(i);
402 switch(c)
403 {
404 case '<':
405 sx += "&lt;"; break;
406 case '>':
407 sx += "&gt;"; break;
408 case '&':
409 sx += "&amp;"; break;
410 default:
411 sx += c;
412 }
413 }
414 return sx;
415}
416
417void G4UIcommandTree::CreateHTML()
418{
419 G4String ofileName = CreateFileName(pathName);
420 std::ofstream oF(ofileName, std::ios::out);
421
422 oF << "<html><head><title>Commands in " << ModStr(pathName) << "</title></head>" << G4endl;
423 oF << "<body bgcolor=\"#ffffff\"><h2>" << ModStr(pathName) << "</h2><p>" << G4endl;
424
425 if( guidance != NULL )
426 {
427 for(G4int i=0;i<guidance->GetGuidanceEntries();i++)
428 { oF << ModStr(guidance->GetGuidanceLine(i)) << "<br>" << G4endl; }
429 }
430
431 oF << "<p><hr><p>" << G4endl;
432
433 oF << "<h2>Sub-directories : </h2><dl>" << G4endl;
434 for( G4int i_thTree = 0; i_thTree < G4int(tree.size()); i_thTree++ )
435 {
436 oF << "<p><br><p><dt><a href=\"" << CreateFileName(tree[i_thTree]->GetPathName())
437 << "\">" << ModStr(tree[i_thTree]->GetPathName()) << "</a>" << G4endl;
438 oF << "<p><dd>" << ModStr(tree[i_thTree]->GetTitle()) << G4endl;
439 tree[i_thTree]->CreateHTML();
440 }
441
442 oF << "</dl><p><hr><p>" << G4endl;
443
444 oF << "<h2>Commands : </h2><dl>" << G4endl;
445 for( G4int i_thCommand = 0; i_thCommand < G4int(command.size()); i_thCommand++ )
446 {
447 G4UIcommand* cmd = command[i_thCommand];
448 oF << "<p><br><p><dt><b>" << ModStr(cmd->GetCommandName());
449 if(cmd->GetParameterEntries()>0)
450 {
451 for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
452 { oF << " [<i>" << ModStr(cmd->GetParameter(i_thParam)->GetParameterName()) << "</i>]"; }
453 }
454 oF << "</b>" << G4endl;
455 oF << "<p><dd>" << G4endl;
456 for(G4int i=0;i<cmd->GetGuidanceEntries();i++)
457 { oF << ModStr(cmd->GetGuidanceLine(i)) << "<br>" << G4endl; }
458 if(!(cmd->GetRange()).isNull())
459 { oF << "<p><dd>Range : " << ModStr(cmd->GetRange()) << G4endl; }
460 std::vector<G4ApplicationState>* availabelStateList = cmd->GetStateList();
461 if(availabelStateList->size()==6)
462 { oF << "<p><dd>Available at all Geant4 states." << G4endl; }
463 else
464 {
465 oF << "<p><dd>Available Geant4 state(s) : ";
466 for(G4int ias=0;ias<G4int(availabelStateList->size());ias++)
467 { oF << G4StateManager::GetStateManager()->GetStateString((*availabelStateList)[ias]) << " " << G4endl; }
468 }
469 if(cmd->GetParameterEntries()>0)
470 {
471 oF << "<p><dd>Parameters<table border=1>" << G4endl;
472 for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
473 {
474 G4UIparameter* prm = cmd->GetParameter(i_thParam);
475 oF << "<tr><td>" << ModStr(prm->GetParameterName()) << G4endl;
476 oF << "<td>type " << prm->GetParameterType() << G4endl;
477 oF << "<td>";
478 if(prm->IsOmittable())
479 {
480 oF << "Omittable : ";
481 if(prm->GetCurrentAsDefault())
482 { oF << "current value is used as the default value." << G4endl; }
483 else
484 { oF << "default value = " << prm->GetDefaultValue() << G4endl; }
485 }
486 oF << "<td>";
487 if(!(prm->GetParameterRange()).isNull())
488 { oF << "Parameter range : " << ModStr(prm->GetParameterRange()) << G4endl; }
489 else if(!(prm->GetParameterCandidates()).isNull())
490 { oF << "Parameter candidates : " << ModStr(prm->GetParameterCandidates()) << G4endl; }
491 }
492 oF << "</table>" << G4endl;
493 }
494
495 }
496
497 oF << "</dl></body></html>" << G4endl;
498 oF.close();
499}
500
Note: See TracBrowser for help on using the repository browser.