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

Last change on this file since 1247 was 1228, checked in by garnier, 16 years ago

update geant4.9.3 tag

File size: 14.9 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.16 2009/11/06 06:16:07 kmura Exp $
28// GEANT4 tag $Name: geant4-09-03 $
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 G4int n_treeRemain = tree[i_thTree]-> GetTreeEntry();
146 if(n_commandRemain == 0 && n_treeRemain == 0)
147 {
148 G4UIcommandTree * emptyTree = tree[i_thTree];
149 tree.erase(tree.begin()+i_thTree);
150 delete emptyTree;
151 }
152 break;
153 }
154 }
155 }
156 }
157}
158
159// L. Garnier 01.28.08 This function has not a good name. In fact, it try
160// to match a command name, not a path. It should be rename as FindCommandName
161
162G4UIcommand * G4UIcommandTree::FindPath(const char* commandPath)
163{
164 G4String remainingPath = commandPath;
165 if( remainingPath.index( pathName ) == std::string::npos )
166 { return NULL; }
167 remainingPath.remove(0,pathName.length());
168 G4int i = remainingPath.first('/');
169 if( i == G4int(std::string::npos) )
170 {
171 // Find command
172 G4int n_commandEntry = command.size();
173 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
174 {
175 if( remainingPath == command[i_thCommand]->GetCommandName() )
176 { return command[i_thCommand]; }
177 }
178 }
179 else
180 {
181 // Find path
182 G4String nextPath = pathName;
183 nextPath.append(remainingPath(0,i+1));
184 G4int n_treeEntry = tree.size();
185 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
186 {
187 if( nextPath == tree[i_thTree]->GetPathName() )
188 { return tree[i_thTree]->FindPath( commandPath ); }
189 }
190 }
191 return NULL;
192}
193
194
195/**
196 * Try to match a command or a path with the one given.
197 * @commandPath : command or path to match
198 * @return the commandTree found or NULL if not
199 */
200G4UIcommandTree * G4UIcommandTree::FindCommandTree(const char* commandPath)
201{
202 G4String remainingPath = commandPath;
203 if( remainingPath.index( pathName ) == std::string::npos )
204 { return NULL; }
205 remainingPath.remove(0,pathName.length());
206 G4int i = remainingPath.first('/');
207 if( i != G4int(std::string::npos) )
208 {
209 // Find path
210 G4String nextPath = pathName;
211 nextPath.append(remainingPath(0,i+1));
212 G4int n_treeEntry = tree.size();
213 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
214 {
215 if (tree[i_thTree]->GetPathName() == commandPath) {
216 return tree[i_thTree];
217 }
218 else if( nextPath == tree[i_thTree]->GetPathName() ) {
219 return tree[i_thTree]->FindCommandTree( commandPath );
220 }
221 }
222 } else {
223 return this;
224 }
225 return NULL;
226}
227
228G4String G4UIcommandTree::CompleteCommandPath(const G4String aCommandPath)
229{
230 G4String pathName = aCommandPath;
231 G4String remainingPath = aCommandPath;
232 G4String empty = "";
233 G4String matchingPath = empty;
234
235 // find the tree
236 G4int jpre= pathName.last('/');
237 if(jpre != G4int(G4String::npos)) pathName.remove(jpre+1);
238 G4UIcommandTree* aTree = FindCommandTree(pathName);
239
240 if (!aTree) {
241 return empty;
242 }
243
244 if( pathName.index( pathName ) == std::string::npos ) return empty;
245
246 std::vector<G4String> paths;
247
248 // list matched directories/commands
249 G4String strtmp;
250 G4int nMatch= 0;
251
252 int Ndir= aTree-> GetTreeEntry();
253 int Ncmd= aTree-> GetCommandEntry();
254
255 // directory ...
256 for(G4int idir=1; idir<=Ndir; idir++) {
257 G4String fpdir= aTree-> GetTree(idir)-> GetPathName();
258 // matching test
259 if( fpdir.index(remainingPath, 0) == 0) {
260 if(nMatch==0) {
261 matchingPath = fpdir;
262 } else {
263 matchingPath = GetFirstMatchedString(fpdir,matchingPath);
264 }
265 nMatch++;
266 paths.push_back(fpdir);
267 }
268 }
269
270 if (paths.size()>=2) {
271 G4cout << "Matching directories :" << G4endl;
272 for( unsigned int i_thCommand = 0; i_thCommand < paths.size(); i_thCommand++ ) {
273 G4cout << paths[i_thCommand] << G4endl;
274 }
275 }
276
277 // command ...
278 std::vector<G4String> commands;
279
280 for(G4int icmd=1; icmd<=Ncmd; icmd++){
281 G4String fpcmd= aTree-> GetPathName() +
282 aTree-> GetCommand(icmd) -> GetCommandName();
283 // matching test
284 if( fpcmd.index(remainingPath, 0) ==0) {
285 if(nMatch==0) {
286 matchingPath= fpcmd + " ";
287 } else {
288 strtmp= fpcmd + " ";
289 matchingPath= GetFirstMatchedString(matchingPath, strtmp);
290 }
291 nMatch++;
292 commands.push_back(fpcmd+" ");
293 }
294 }
295
296 if (commands.size()>=2) {
297 G4cout << "Matching commands :" << G4endl;
298 for( unsigned int i_thCommand = 0; i_thCommand < commands.size(); i_thCommand++ ) {
299 G4cout << commands[i_thCommand] << G4endl;
300 }
301 }
302
303 return matchingPath;
304}
305
306
307////////////////////////////////////////////////////////////////////
308G4String G4UIcommandTree::GetFirstMatchedString(const G4String& str1,
309 const G4String& str2) const
310////////////////////////////////////////////////////////////////////
311{
312 int nlen1= str1.length();
313 int nlen2= str2.length();
314
315 int nmin = nlen1<nlen2 ? nlen1 : nlen2;
316
317 G4String strMatched;
318 for(size_t i=0; G4int(i)<nmin; i++){
319 if(str1[i]==str2[i]) {
320 strMatched+= str1[i];
321 } else {
322 break;
323 }
324 }
325
326 return strMatched;
327}
328
329void G4UIcommandTree::ListCurrent()
330{
331 G4cout << "Command directory path : " << pathName << G4endl;
332 if( guidance != NULL ) guidance->List();
333 G4cout << " Sub-directories : " << G4endl;
334 G4int n_treeEntry = tree.size();
335 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
336 {
337 G4cout << " " << tree[i_thTree]->GetPathName()
338 << " " << tree[i_thTree]->GetTitle() << G4endl;
339 }
340 G4cout << " Commands : " << G4endl;
341 G4int n_commandEntry = command.size();
342 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
343 {
344 G4cout << " " << command[i_thCommand]->GetCommandName()
345 << " * " << command[i_thCommand]->GetTitle() << G4endl;
346 }
347}
348
349void G4UIcommandTree::ListCurrentWithNum()
350{
351 G4cout << "Command directory path : " << pathName << G4endl;
352 if( guidance != NULL ) guidance->List();
353 G4int i = 0;
354 G4cout << " Sub-directories : " << G4endl;
355 G4int n_treeEntry = tree.size();
356 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
357 {
358 i++;
359 G4cout << " " << i << ") " << tree[i_thTree]->GetPathName()
360 << " " << tree[i_thTree]->GetTitle() << G4endl;
361 }
362 G4cout << " Commands : " << G4endl;
363 G4int n_commandEntry = command.size();
364 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
365 {
366 i++;
367 G4cout << " " << i << ") " << command[i_thCommand]->GetCommandName()
368 << " * " << command[i_thCommand]->GetTitle() << G4endl;
369 }
370}
371
372void G4UIcommandTree::List()
373{
374 ListCurrent();
375 G4int n_commandEntry = command.size();
376 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
377 {
378 command[i_thCommand]->List();
379 }
380 G4int n_treeEntry = tree.size();
381 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
382 {
383 tree[i_thTree]->List();
384 }
385}
386
387G4String G4UIcommandTree::CreateFileName(const char* pName)
388{
389 G4String fn = pName;
390 G4int idxs;
391 while((idxs=fn.index("/"))!=G4int(std::string::npos))
392 { fn(idxs) = '_'; }
393 fn += ".html";
394 return fn;
395}
396
397G4String G4UIcommandTree::ModStr(const char* strS)
398{
399 G4String sx;
400 G4String str = strS;
401 for(G4int i=0;i<G4int(str.length());i++)
402 {
403 char c = str(i);
404 switch(c)
405 {
406 case '<':
407 sx += "&lt;"; break;
408 case '>':
409 sx += "&gt;"; break;
410 case '&':
411 sx += "&amp;"; break;
412 default:
413 sx += c;
414 }
415 }
416 return sx;
417}
418
419void G4UIcommandTree::CreateHTML()
420{
421 G4String ofileName = CreateFileName(pathName);
422 std::ofstream oF(ofileName, std::ios::out);
423
424 oF << "<html><head><title>Commands in " << ModStr(pathName) << "</title></head>" << G4endl;
425 oF << "<body bgcolor=\"#ffffff\"><h2>" << ModStr(pathName) << "</h2><p>" << G4endl;
426
427 if( guidance != NULL )
428 {
429 for(G4int i=0;i<guidance->GetGuidanceEntries();i++)
430 { oF << ModStr(guidance->GetGuidanceLine(i)) << "<br>" << G4endl; }
431 }
432
433 oF << "<p><hr><p>" << G4endl;
434
435 oF << "<h2>Sub-directories : </h2><dl>" << G4endl;
436 for( G4int i_thTree = 0; i_thTree < G4int(tree.size()); i_thTree++ )
437 {
438 oF << "<p><br><p><dt><a href=\"" << CreateFileName(tree[i_thTree]->GetPathName())
439 << "\">" << ModStr(tree[i_thTree]->GetPathName()) << "</a>" << G4endl;
440 oF << "<p><dd>" << ModStr(tree[i_thTree]->GetTitle()) << G4endl;
441 tree[i_thTree]->CreateHTML();
442 }
443
444 oF << "</dl><p><hr><p>" << G4endl;
445
446 oF << "<h2>Commands : </h2><dl>" << G4endl;
447 for( G4int i_thCommand = 0; i_thCommand < G4int(command.size()); i_thCommand++ )
448 {
449 G4UIcommand* cmd = command[i_thCommand];
450 oF << "<p><br><p><dt><b>" << ModStr(cmd->GetCommandName());
451 if(cmd->GetParameterEntries()>0)
452 {
453 for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
454 { oF << " [<i>" << ModStr(cmd->GetParameter(i_thParam)->GetParameterName()) << "</i>]"; }
455 }
456 oF << "</b>" << G4endl;
457 oF << "<p><dd>" << G4endl;
458 for(G4int i=0;i<cmd->GetGuidanceEntries();i++)
459 { oF << ModStr(cmd->GetGuidanceLine(i)) << "<br>" << G4endl; }
460 if(!(cmd->GetRange()).isNull())
461 { oF << "<p><dd>Range : " << ModStr(cmd->GetRange()) << G4endl; }
462 std::vector<G4ApplicationState>* availabelStateList = cmd->GetStateList();
463 if(availabelStateList->size()==6)
464 { oF << "<p><dd>Available at all Geant4 states." << G4endl; }
465 else
466 {
467 oF << "<p><dd>Available Geant4 state(s) : ";
468 for(G4int ias=0;ias<G4int(availabelStateList->size());ias++)
469 { oF << G4StateManager::GetStateManager()->GetStateString((*availabelStateList)[ias]) << " " << G4endl; }
470 }
471 if(cmd->GetParameterEntries()>0)
472 {
473 oF << "<p><dd>Parameters<table border=1>" << G4endl;
474 for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
475 {
476 G4UIparameter* prm = cmd->GetParameter(i_thParam);
477 oF << "<tr><td>" << ModStr(prm->GetParameterName()) << G4endl;
478 oF << "<td>type " << prm->GetParameterType() << G4endl;
479 oF << "<td>";
480 if(prm->IsOmittable())
481 {
482 oF << "Omittable : ";
483 if(prm->GetCurrentAsDefault())
484 { oF << "current value is used as the default value." << G4endl; }
485 else
486 { oF << "default value = " << prm->GetDefaultValue() << G4endl; }
487 }
488 oF << "<td>";
489 if(!(prm->GetParameterRange()).isNull())
490 { oF << "Parameter range : " << ModStr(prm->GetParameterRange()) << G4endl; }
491 else if(!(prm->GetParameterCandidates()).isNull())
492 { oF << "Parameter candidates : " << ModStr(prm->GetParameterCandidates()) << G4endl; }
493 }
494 oF << "</table>" << G4endl;
495 }
496
497 }
498
499 oF << "</dl></body></html>" << G4endl;
500 oF.close();
501}
502
Note: See TracBrowser for help on using the repository browser.