Geant4  10.03
G4UIcommandTree.cc
Go to the documentation of this file.
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 77651 2013-11-27 08:47:55Z gcosmo $
28 //
29 
30 #include "G4UIcommandTree.hh"
31 #include "G4StateManager.hh"
32 #include <fstream>
33 #include "G4ios.hh"
34 
36 :guidance(NULL),broadcastCommands(true)
37 { }
38 
39 G4UIcommandTree::G4UIcommandTree(const char * thePathName)
40 :guidance(NULL),broadcastCommands(true)
41 {
42  pathName = thePathName;
43 }
44 
46 {
47  G4int i;
48  G4int n_treeEntry = tree.size();
49  for( i=0; i < n_treeEntry; i++ )
50  { delete tree[i]; }
51 }
52 
54 {
55  return ( pathName == right.GetPathName() );
56 }
57 
59 {
60  return ( pathName != right.GetPathName() );
61 }
62 
63 void G4UIcommandTree::AddNewCommand(G4UIcommand *newCommand, G4bool workerThreadOnly)
64 {
65  G4String commandPath = newCommand->GetCommandPath();
66  G4String remainingPath = commandPath;
67  remainingPath.remove(0,pathName.length());
68  if( remainingPath.isNull() )
69  {
70  if(!guidance)
71  {
72  guidance = newCommand;
73  if(!(newCommand->ToBeBroadcasted())) broadcastCommands = false;
74  if(workerThreadOnly) newCommand->SetWorkerThreadOnly();
75  }
76  return;
77  }
78  G4int i = remainingPath.first('/');
79  if( i == G4int(std::string::npos) )
80  {
81  // Find command
82  G4int n_commandEntry = command.size();
83  for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
84  {
85  if( remainingPath == command[i_thCommand]->GetCommandName() )
86  { return; }
87  }
88  if(!broadcastCommands) newCommand->SetToBeBroadcasted(false);
89  if(workerThreadOnly) newCommand->SetWorkerThreadOnly();
90  command.push_back( newCommand );
91  return;
92  }
93  else
94  {
95  // Find path
96  G4String nextPath = pathName;
97  nextPath.append(remainingPath(0,i+1));
98  G4int n_treeEntry = tree.size();
99  for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
100  {
101  if( nextPath == tree[i_thTree]->GetPathName() )
102  {
103  if(!broadcastCommands) newCommand->SetToBeBroadcasted(false);
104  tree[i_thTree]->AddNewCommand( newCommand, workerThreadOnly );
105  return;
106  }
107  }
108  G4UIcommandTree * newTree = new G4UIcommandTree( nextPath );
109  tree.push_back( newTree );
110  if(!broadcastCommands) newCommand->SetToBeBroadcasted(false);
111  newTree->AddNewCommand( newCommand, workerThreadOnly );
112  return;
113  }
114 }
115 
116 void G4UIcommandTree::RemoveCommand(G4UIcommand *aCommand, G4bool workerThreadOnly)
117 {
118  if(workerThreadOnly && !(aCommand->IsWorkerThreadOnly())) return;
119  G4String commandPath = aCommand->GetCommandPath();
120  G4String remainingPath = commandPath;
121  remainingPath.remove(0,pathName.length());
122  if( remainingPath.isNull() )
123  {
124  guidance = NULL;
125  }
126  else
127  {
128  G4int i = remainingPath.first('/');
129  if( i == G4int(std::string::npos) )
130  {
131  // Find command
132  G4int n_commandEntry = command.size();
133  for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
134  {
135  if( remainingPath == command[i_thCommand]->GetCommandName() )
136  {
137  command.erase(command.begin()+i_thCommand);
138  break;
139  }
140  }
141  }
142  else
143  {
144  // Find path
145  G4String nextPath = pathName;
146  nextPath.append(remainingPath(0,i+1));
147  G4int n_treeEntry = tree.size();
148  for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
149  {
150  if( nextPath == tree[i_thTree]->GetPathName() )
151  {
152  tree[i_thTree]->RemoveCommand( aCommand );
153  G4int n_commandRemain = tree[i_thTree]->GetCommandEntry();
154  G4int n_treeRemain = tree[i_thTree]-> GetTreeEntry();
155  if(n_commandRemain == 0 && n_treeRemain == 0)
156  {
157  G4UIcommandTree * emptyTree = tree[i_thTree];
158  tree.erase(tree.begin()+i_thTree);
159  delete emptyTree;
160  }
161  break;
162  }
163  }
164  }
165  }
166 }
167 
168 // L. Garnier 01.28.08 This function has not a good name. In fact, it try
169 // to match a command name, not a path. It should be rename as FindCommandName
170 
171 G4UIcommand * G4UIcommandTree::FindPath(const char* commandPath) const
172 {
173  G4String remainingPath = commandPath;
174  if( remainingPath.index( pathName ) == std::string::npos )
175  { return NULL; }
176  remainingPath.remove(0,pathName.length());
177  G4int i = remainingPath.first('/');
178  if( i == G4int(std::string::npos) )
179  {
180  // Find command
181  G4int n_commandEntry = command.size();
182  for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
183  {
184  if( remainingPath == command[i_thCommand]->GetCommandName() )
185  { return command[i_thCommand]; }
186  }
187  }
188  else
189  {
190  // Find path
191  G4String nextPath = pathName;
192  nextPath.append(remainingPath(0,i+1));
193  G4int n_treeEntry = tree.size();
194  for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
195  {
196  if( nextPath == tree[i_thTree]->GetPathName() )
197  { return tree[i_thTree]->FindPath( commandPath ); }
198  }
199  }
200  return NULL;
201 }
202 
203 
210 {
211  G4String remainingPath = commandPath;
212  if( remainingPath.index( pathName ) == std::string::npos )
213  { return NULL; }
214  remainingPath.remove(0,pathName.length());
215  G4int i = remainingPath.first('/');
216  if( i != G4int(std::string::npos) )
217  {
218  // Find path
219  G4String nextPath = pathName;
220  nextPath.append(remainingPath(0,i+1));
221  G4int n_treeEntry = tree.size();
222  for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
223  {
224  if (tree[i_thTree]->GetPathName() == commandPath) {
225  return tree[i_thTree];
226  }
227  else if( nextPath == tree[i_thTree]->GetPathName() ) {
228  return tree[i_thTree]->FindCommandTree( commandPath );
229  }
230  }
231  } else {
232  return this;
233  }
234  return NULL;
235 }
236 
238 {
239  G4String pName = aCommandPath;
240  G4String remainingPath = aCommandPath;
241  G4String empty = "";
242  G4String matchingPath = empty;
243 
244  // find the tree
245  G4int jpre= pName.last('/');
246  if(jpre != G4int(G4String::npos)) pName.remove(jpre+1);
247  G4UIcommandTree* aTree = FindCommandTree(pName);
248 
249  if (!aTree) {
250  return empty;
251  }
252 
253  if( pName.index( pName ) == std::string::npos ) return empty;
254 
255  std::vector<G4String> paths;
256 
257  // list matched directories/commands
258  G4String strtmp;
259  G4int nMatch= 0;
260 
261  int Ndir= aTree-> GetTreeEntry();
262  int Ncmd= aTree-> GetCommandEntry();
263 
264  // directory ...
265  for(G4int idir=1; idir<=Ndir; idir++) {
266  G4String fpdir= aTree-> GetTree(idir)-> GetPathName();
267  // matching test
268  if( fpdir.index(remainingPath, 0) == 0) {
269  if(nMatch==0) {
270  matchingPath = fpdir;
271  } else {
272  matchingPath = GetFirstMatchedString(fpdir,matchingPath);
273  }
274  nMatch++;
275  paths.push_back(fpdir);
276  }
277  }
278 
279  if (paths.size()>=2) {
280  G4cout << "Matching directories :" << G4endl;
281  for( unsigned int i_thCommand = 0; i_thCommand < paths.size(); i_thCommand++ ) {
282  G4cout << paths[i_thCommand] << G4endl;
283  }
284  }
285 
286  // command ...
287  std::vector<G4String> commands;
288 
289  for(G4int icmd=1; icmd<=Ncmd; icmd++){
290  G4String fpcmd= aTree-> GetPathName() +
291  aTree-> GetCommand(icmd) -> GetCommandName();
292  // matching test
293  if( fpcmd.index(remainingPath, 0) ==0) {
294  if(nMatch==0) {
295  matchingPath= fpcmd + " ";
296  } else {
297  strtmp= fpcmd + " ";
298  matchingPath= GetFirstMatchedString(matchingPath, strtmp);
299  }
300  nMatch++;
301  commands.push_back(fpcmd+" ");
302  }
303  }
304 
305  if (commands.size()>=2) {
306  G4cout << "Matching commands :" << G4endl;
307  for( unsigned int i_thCommand = 0; i_thCommand < commands.size(); i_thCommand++ ) {
308  G4cout << commands[i_thCommand] << G4endl;
309  }
310  }
311 
312  return matchingPath;
313 }
314 
315 
318  const G4String& str2) const
320 {
321  int nlen1= str1.length();
322  int nlen2= str2.length();
323 
324  int nmin = nlen1<nlen2 ? nlen1 : nlen2;
325 
326  G4String strMatched;
327  for(size_t i=0; G4int(i)<nmin; i++){
328  if(str1[i]==str2[i]) {
329  strMatched+= str1[i];
330  } else {
331  break;
332  }
333  }
334 
335  return strMatched;
336 }
337 
339 {
340  G4cout << "Command directory path : " << pathName << G4endl;
341  if( guidance != NULL ) guidance->List();
342  G4cout << " Sub-directories : " << G4endl;
343  G4int n_treeEntry = tree.size();
344  for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
345  {
346  G4cout << " " << tree[i_thTree]->GetPathName();
347  if(tree[i_thTree]->GetGuidance() &&
348  tree[i_thTree]->GetGuidance()->IsWorkerThreadOnly())
349  { G4cout << " @ "; }
350  else
351  { G4cout << " "; }
352  G4cout << tree[i_thTree]->GetTitle() << G4endl;
353  }
354  G4cout << " Commands : " << G4endl;
355  G4int n_commandEntry = command.size();
356  for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
357  {
358  G4cout << " " << command[i_thCommand]->GetCommandName();
359  if(command[i_thCommand]->IsWorkerThreadOnly())
360  { G4cout << " @ "; }
361  else
362  { G4cout << " * "; }
363  G4cout << command[i_thCommand]->GetTitle() << G4endl;
364  }
365 }
366 
368 {
369  G4cout << "Command directory path : " << pathName << G4endl;
370  if( guidance != NULL ) guidance->List();
371  G4int i = 0;
372  G4cout << " Sub-directories : " << G4endl;
373  G4int n_treeEntry = tree.size();
374  for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
375  {
376  i++;
377  G4cout << " " << i << ") " << tree[i_thTree]->GetPathName()
378  << " " << tree[i_thTree]->GetTitle() << G4endl;
379  }
380  G4cout << " Commands : " << G4endl;
381  G4int n_commandEntry = command.size();
382  for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
383  {
384  i++;
385  G4cout << " " << i << ") " << command[i_thCommand]->GetCommandName()
386  << " * " << command[i_thCommand]->GetTitle() << G4endl;
387  }
388 }
389 
391 {
392  ListCurrent();
393  G4int n_commandEntry = command.size();
394  for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
395  {
396  command[i_thCommand]->List();
397  }
398  G4int n_treeEntry = tree.size();
399  for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
400  {
401  tree[i_thTree]->List();
402  }
403 }
404 
406 {
407  G4String fn = pName;
408  G4int idxs;
409  while((idxs=fn.index("/"))!=G4int(std::string::npos))
410  { fn(idxs) = '_'; }
411  fn += ".html";
412  return fn;
413 }
414 
416 {
417  G4String sx;
418  G4String str = strS;
419  for(G4int i=0;i<G4int(str.length());i++)
420  {
421  char c = str(i);
422  switch(c)
423  {
424  case '<':
425  sx += "&lt;"; break;
426  case '>':
427  sx += "&gt;"; break;
428  case '&':
429  sx += "&amp;"; break;
430  default:
431  sx += c;
432  }
433  }
434  return sx;
435 }
436 
438 {
439  G4String ofileName = CreateFileName(pathName);
440  std::ofstream oF(ofileName, std::ios::out);
441 
442  oF << "<html><head><title>Commands in " << ModStr(pathName) << "</title></head>" << G4endl;
443  oF << "<body bgcolor=\"#ffffff\"><h2>" << ModStr(pathName) << "</h2><p>" << G4endl;
444 
445  if( guidance != NULL )
446  {
447  for(G4int i=0;i<guidance->GetGuidanceEntries();i++)
448  { oF << ModStr(guidance->GetGuidanceLine(i)) << "<br>" << G4endl; }
449  }
450 
451  oF << "<p><hr><p>" << G4endl;
452 
453  oF << "<h2>Sub-directories : </h2><dl>" << G4endl;
454  for( G4int i_thTree = 0; i_thTree < G4int(tree.size()); i_thTree++ )
455  {
456  oF << "<p><br><p><dt><a href=\"" << CreateFileName(tree[i_thTree]->GetPathName())
457  << "\">" << ModStr(tree[i_thTree]->GetPathName()) << "</a>" << G4endl;
458  oF << "<p><dd>" << ModStr(tree[i_thTree]->GetTitle()) << G4endl;
459  tree[i_thTree]->CreateHTML();
460  }
461 
462  oF << "</dl><p><hr><p>" << G4endl;
463 
464  oF << "<h2>Commands : </h2><dl>" << G4endl;
465  for( G4int i_thCommand = 0; i_thCommand < G4int(command.size()); i_thCommand++ )
466  {
467  G4UIcommand* cmd = command[i_thCommand];
468  oF << "<p><br><p><dt><b>" << ModStr(cmd->GetCommandName());
469  if(cmd->GetParameterEntries()>0)
470  {
471  for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
472  { oF << " [<i>" << ModStr(cmd->GetParameter(i_thParam)->GetParameterName()) << "</i>]"; }
473  }
474  oF << "</b>" << G4endl;
475  oF << "<p><dd>" << G4endl;
476  for(G4int i=0;i<cmd->GetGuidanceEntries();i++)
477  { oF << ModStr(cmd->GetGuidanceLine(i)) << "<br>" << G4endl; }
478  if(!(cmd->GetRange()).isNull())
479  { oF << "<p><dd>Range : " << ModStr(cmd->GetRange()) << G4endl; }
480  std::vector<G4ApplicationState>* availabelStateList = cmd->GetStateList();
481  if(availabelStateList->size()==6)
482  { oF << "<p><dd>Available at all Geant4 states." << G4endl; }
483  else
484  {
485  oF << "<p><dd>Available Geant4 state(s) : ";
486  for(G4int ias=0;ias<G4int(availabelStateList->size());ias++)
487  { oF << G4StateManager::GetStateManager()->GetStateString((*availabelStateList)[ias]) << " " << G4endl; }
488  }
489  if(cmd->GetParameterEntries()>0)
490  {
491  oF << "<p><dd>Parameters<table border=1>" << G4endl;
492  for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
493  {
494  G4UIparameter* prm = cmd->GetParameter(i_thParam);
495  oF << "<tr><td>" << ModStr(prm->GetParameterName()) << G4endl;
496  oF << "<td>type " << prm->GetParameterType() << G4endl;
497  oF << "<td>";
498  if(prm->IsOmittable())
499  {
500  oF << "Omittable : ";
501  if(prm->GetCurrentAsDefault())
502  { oF << "current value is used as the default value." << G4endl; }
503  else
504  { oF << "default value = " << prm->GetDefaultValue() << G4endl; }
505  }
506  oF << "<td>";
507  if(!(prm->GetParameterRange()).isNull())
508  { oF << "Parameter range : " << ModStr(prm->GetParameterRange()) << G4endl; }
509  else if(!(prm->GetParameterCandidates()).isNull())
510  { oF << "Parameter candidates : " << ModStr(prm->GetParameterCandidates()) << G4endl; }
511  }
512  oF << "</table>" << G4endl;
513  }
514 
515  }
516 
517  oF << "</dl></body></html>" << G4endl;
518  oF.close();
519 }
520 
G4UIcommandTree * FindCommandTree(const char *commandPath)
Try to match a command or a path with the one given.
G4String GetParameterCandidates() const
const G4String & GetRange() const
Definition: G4UIcommand.hh:133
G4bool IsWorkerThreadOnly() const
Definition: G4UIcommand.hh:194
G4String & remove(str_size)
G4int first(char) const
G4UIcommand * FindPath(const char *commandPath) const
G4int GetCommandEntry() const
G4int operator==(const G4UIcommandTree &right) const
G4String GetParameterName() const
G4UIcommand * guidance
G4UIcommand * GetCommand(G4int i)
void SetToBeBroadcasted(G4bool val)
Definition: G4UIcommand.hh:184
G4int GetTreeEntry() const
G4int operator!=(const G4UIcommandTree &right) const
int G4int
Definition: G4Types.hh:78
std::vector< G4UIcommand * > command
const G4String & GetGuidanceLine(G4int i) const
Definition: G4UIcommand.hh:137
std::vector< G4UIcommandTree * > tree
void ListCurrent() const
G4bool IsOmittable() const
static G4StateManager * GetStateManager()
G4bool ToBeBroadcasted() const
Definition: G4UIcommand.hh:186
G4GLOB_DLL std::ostream G4cout
str_size index(const char *, G4int pos=0) const
G4UIcommandTree * GetTree(G4int i)
bool G4bool
Definition: G4Types.hh:79
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:145
void ListCurrentWithNum() const
void RemoveCommand(G4UIcommand *aCommand, G4bool workerThreadOnly=false)
const G4String & GetCommandPath() const
Definition: G4UIcommand.hh:139
G4String GetFirstMatchedString(const G4String &, const G4String &) const
G4String ModStr(const char *strS)
const G4String GetPathName() const
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
void SetWorkerThreadOnly(G4bool val=true)
Definition: G4UIcommand.hh:192
const G4String GetTitle() const
void AddNewCommand(G4UIcommand *newCommand, G4bool workerThreadOnly=false)
G4int last(char) const
G4String & append(const G4String &)
G4String GetDefaultValue() const
std::vector< G4ApplicationState > * GetStateList()
Definition: G4UIcommand.hh:147
const G4UIcommand * GetGuidance() const
char GetParameterType() const
#define G4endl
Definition: G4ios.hh:61
G4String CreateFileName(const char *pName)
virtual void List()
Definition: G4UIcommand.cc:348
G4String GetParameterRange() const
G4int GetGuidanceEntries() const
Definition: G4UIcommand.hh:135
G4int GetParameterEntries() const
Definition: G4UIcommand.hh:143
G4bool GetCurrentAsDefault() const
G4bool isNull() const
G4String CompleteCommandPath(const G4String &commandPath)
void List() const
G4String GetStateString(G4ApplicationState aState) const