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