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

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

en test. Ok

File size: 18.1 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 printf("G4UIcommandTree::FindCommandTree %s\n",commandPath);
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#ifdef G4DEBUG
231 printf("G4UIcommandTree::FindMatchingPath %s\n",aCommandPath.c_str());
232#endif
233 G4String pathName = aCommandPath;
234 G4String remainingPath = aCommandPath;
235
236 // find the tree
237 G4int jpre= pathName.last('/');
238 if(jpre != G4int(G4String::npos)) pathName.remove(jpre+1);
239 G4UIcommandTree* aTree = FindCommandTree(pathName);
240
241 G4String empty = "";
242 if (!aTree) {
243 return empty;
244 }
245
246 if( pathName.index( pathName ) == std::string::npos ) return empty;
247
248#ifdef G4DEBUG
249 printf("G4UIcommandTree::FindMatchingPath remainingPath:%s\n",remainingPath.c_str());
250#endif
251
252 std::vector<G4String> paths;
253
254 // list matched directories/commands
255 G4String stream, strtmp;
256 G4int nMatch= 0;
257
258 int Ndir= aTree-> GetTreeEntry();
259 int Ncmd= aTree-> GetCommandEntry();
260
261 // directory ...
262 for(G4int idir=1; idir<=Ndir; idir++) {
263 G4String fpdir= aTree-> GetTree(idir)-> GetPathName();
264#ifdef G4DEBUG
265 printf("G4UIcommandTree::FindMatchingPath command %d/%d %s\n",idir,Ndir,fpdir.c_str());
266#endif
267 // matching test
268 if( fpdir.index(remainingPath, 0) == 0) {
269 if(nMatch==0) {
270#ifdef G4DEBUG
271 printf("G4UIcommandTree::FindMatchingPath directory match0 :%d\n",nMatch);
272#endif
273 stream = fpdir;
274 } else {
275#ifdef G4DEBUG
276 printf("G4UIcommandTree::FindMatchingPath directory match+ :%d\n",nMatch);
277#endif
278 stream = GetFirstMatchedString(fpdir,stream);
279#ifdef G4DEBUG
280 printf("G4UIcommandTree::FindMatchingPath directory match++++ :%s\n",stream.c_str());
281#endif
282 }
283 nMatch++;
284 paths.push_back(fpdir);
285 }
286 }
287
288 if (paths.size()>=2) {
289 G4cout << "Matching directories :" << G4endl;
290 for( unsigned int i_thCommand = 0; i_thCommand < paths.size(); i_thCommand++ ) {
291 G4cout << paths[i_thCommand] << G4endl;
292 }
293 }
294
295 // command ...
296 std::vector<G4String> commands;
297
298 for(G4int icmd=1; icmd<=Ncmd; icmd++){
299 G4String fpcmd= aTree-> GetPathName() +
300 aTree-> GetCommand(icmd) -> GetCommandName();
301 // matching test
302 if( fpcmd.index(remainingPath, 0) ==0) {
303 if(nMatch==0) {
304#ifdef G4DEBUG
305 printf("G4UIcommandTree::FindMatchingPath command match0 :%d\n",nMatch);
306#endif
307 stream= fpcmd + " ";
308 } else {
309#ifdef G4DEBUG
310 printf("G4UIcommandTree::FindMatchingPath command match0 :%d\n",nMatch);
311#endif
312 strtmp= fpcmd + " ";
313 stream= GetFirstMatchedString(stream, strtmp);
314 }
315 nMatch++;
316 commands.push_back(fpcmd+" ");
317 }
318 }
319
320 if (commands.size()>=2) {
321 G4cout << "Matching commands :" << G4endl;
322 for( unsigned int i_thCommand = 0; i_thCommand < commands.size(); i_thCommand++ ) {
323 G4cout << commands[i_thCommand] << G4endl;
324 }
325 }
326
327
328
329
330// if( remainingPath == std::string::npos ) {
331// #ifdef G4DEBUG
332// printf("G4UIcommandTree::FindMatchingPath npos \n");
333// #endif
334// // Look for number of matching commands :
335// G4int n_commandEntry = aTree->GetCommandEntry();
336// for( G4int i_thCommand = 1; i_thCommand <= n_commandEntry; i_thCommand++ ) {
337// G4UIcommand* cmd = aTree->GetCommand(i_thCommand);
338// G4String ss = cmd->GetCommandName();
339// ss.resize(remainingPath.length());
340// if( remainingPath == ss ) commands.push_back(cmd);
341// #ifdef G4DEBUG
342// printf("look for command check %s %s \n",ss.c_str(),remainingPath.c_str());
343// #endif
344// }
345// n_commandEntry = commands.size();
346// #ifdef G4DEBUG
347// printf("%d found\n",n_commandEntry);
348// #endif
349// if(n_commandEntry==1) {
350// return (pathName + commands[0]->GetCommandName());
351// } else if (n_commandEntry>=2) {
352// G4cout << "Matching commands :" << G4endl;
353// for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ ) {
354// G4UIcommand* cmd = commands[i_thCommand];
355// G4cout << cmd->GetCommandName() << G4endl;
356// }
357// return empty;
358// }
359// // Look for sub tree :
360// std::vector<G4UIcommandTree*> trees;
361// G4String nextPath = pathName;
362// nextPath.append(remainingPath);
363// G4int n_treeEntry = aTree->GetTreeEntry();
364// for( G4int i_thTree = 1; i_thTree <= n_treeEntry; i_thTree++ ) {
365// G4UIcommandTree* tree = aTree->GetTree(i_thTree);
366// G4String ss = tree->GetPathName();
367// ss.resize(nextPath.length());
368// if( nextPath == ss ) trees.push_back(tree);
369// }
370// n_treeEntry = trees.size();
371// if(n_treeEntry==1) {
372// return trees[0]->GetPathName();
373// } else if (n_treeEntry>=2) {
374// G4cout << "Matching directories :" << G4endl;
375// for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ ) {
376// G4UIcommandTree* tree = trees[i_thTree];
377// G4cout << tree->GetPathName() << G4endl;
378// }
379// return empty;
380// } else {
381// return empty; // No match.
382// }
383// } else {
384// // Find path
385// G4String nextPath = pathName;
386// nextPath.append(remainingPath(0,i+1));
387// G4int n_treeEntry = aTree->GetTreeEntry();
388// for( G4int i_thTree = 1; i_thTree <= n_treeEntry; i_thTree++ ) {
389// G4UIcommandTree* tree = aTree->GetTree(i_thTree);
390// if( nextPath == tree->GetPathName() ) {
391// return tree->CompleteCommandPath(pathName );
392// }
393// }
394// }
395 // n_commandEntry = commands.size();
396 return stream;
397}
398
399
400////////////////////////////////////////////////////////////////////
401G4String G4UIcommandTree::GetFirstMatchedString(const G4String& str1,
402 const G4String& str2) const
403////////////////////////////////////////////////////////////////////
404{
405 int nlen1= str1.length();
406 int nlen2= str2.length();
407
408 int nmin = nlen1<nlen2 ? nlen1 : nlen2;
409
410 G4String strMatched;
411 for(size_t i=0; G4int(i)<nmin; i++){
412 if(str1[i]==str2[i]) {
413 strMatched+= str1[i];
414 } else {
415 break;
416 }
417 }
418
419 return strMatched;
420}
421
422
423
424
425void G4UIcommandTree::ListCurrent()
426{
427 G4cout << "Command directory path : " << pathName << G4endl;
428 if( guidance != NULL ) guidance->List();
429 G4cout << " Sub-directories : " << G4endl;
430 G4int n_treeEntry = tree.size();
431 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
432 {
433 G4cout << " " << tree[i_thTree]->GetPathName()
434 << " " << tree[i_thTree]->GetTitle() << G4endl;
435 }
436 G4cout << " Commands : " << G4endl;
437 G4int n_commandEntry = command.size();
438 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
439 {
440 G4cout << " " << command[i_thCommand]->GetCommandName()
441 << " * " << command[i_thCommand]->GetTitle() << G4endl;
442 }
443}
444
445void G4UIcommandTree::ListCurrentWithNum()
446{
447 G4cout << "Command directory path : " << pathName << G4endl;
448 if( guidance != NULL ) guidance->List();
449 G4int i = 0;
450 G4cout << " Sub-directories : " << G4endl;
451 G4int n_treeEntry = tree.size();
452 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
453 {
454 i++;
455 G4cout << " " << i << ") " << tree[i_thTree]->GetPathName()
456 << " " << tree[i_thTree]->GetTitle() << G4endl;
457 }
458 G4cout << " Commands : " << G4endl;
459 G4int n_commandEntry = command.size();
460 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
461 {
462 i++;
463 G4cout << " " << i << ") " << command[i_thCommand]->GetCommandName()
464 << " * " << command[i_thCommand]->GetTitle() << G4endl;
465 }
466}
467
468void G4UIcommandTree::List()
469{
470 ListCurrent();
471 G4int n_commandEntry = command.size();
472 for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
473 {
474 command[i_thCommand]->List();
475 }
476 G4int n_treeEntry = tree.size();
477 for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
478 {
479 tree[i_thTree]->List();
480 }
481}
482
483G4String G4UIcommandTree::CreateFileName(const char* pName)
484{
485 G4String fn = pName;
486 G4int idxs;
487 while((idxs=fn.index("/"))!=G4int(std::string::npos))
488 { fn(idxs) = '_'; }
489 fn += ".html";
490 return fn;
491}
492
493G4String G4UIcommandTree::ModStr(const char* strS)
494{
495 G4String sx;
496 G4String str = strS;
497 for(G4int i=0;i<G4int(str.length());i++)
498 {
499 char c = str(i);
500 switch(c)
501 {
502 case '<':
503 sx += "&lt;"; break;
504 case '>':
505 sx += "&gt;"; break;
506 case '&':
507 sx += "&amp;"; break;
508 default:
509 sx += c;
510 }
511 }
512 return sx;
513}
514
515void G4UIcommandTree::CreateHTML()
516{
517 G4String ofileName = CreateFileName(pathName);
518 std::ofstream oF(ofileName, std::ios::out);
519
520 oF << "<html><head><title>Commands in " << ModStr(pathName) << "</title></head>" << G4endl;
521 oF << "<body bgcolor=\"#ffffff\"><h2>" << ModStr(pathName) << "</h2><p>" << G4endl;
522
523 if( guidance != NULL )
524 {
525 for(G4int i=0;i<guidance->GetGuidanceEntries();i++)
526 { oF << ModStr(guidance->GetGuidanceLine(i)) << "<br>" << G4endl; }
527 }
528
529 oF << "<p><hr><p>" << G4endl;
530
531 oF << "<h2>Sub-directories : </h2><dl>" << G4endl;
532 for( G4int i_thTree = 0; i_thTree < G4int(tree.size()); i_thTree++ )
533 {
534 oF << "<p><br><p><dt><a href=\"" << CreateFileName(tree[i_thTree]->GetPathName())
535 << "\">" << ModStr(tree[i_thTree]->GetPathName()) << "</a>" << G4endl;
536 oF << "<p><dd>" << ModStr(tree[i_thTree]->GetTitle()) << G4endl;
537 tree[i_thTree]->CreateHTML();
538 }
539
540 oF << "</dl><p><hr><p>" << G4endl;
541
542 oF << "<h2>Commands : </h2><dl>" << G4endl;
543 for( G4int i_thCommand = 0; i_thCommand < G4int(command.size()); i_thCommand++ )
544 {
545 G4UIcommand* cmd = command[i_thCommand];
546 oF << "<p><br><p><dt><b>" << ModStr(cmd->GetCommandName());
547 if(cmd->GetParameterEntries()>0)
548 {
549 for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
550 { oF << " [<i>" << ModStr(cmd->GetParameter(i_thParam)->GetParameterName()) << "</i>]"; }
551 }
552 oF << "</b>" << G4endl;
553 oF << "<p><dd>" << G4endl;
554 for(G4int i=0;i<cmd->GetGuidanceEntries();i++)
555 { oF << ModStr(cmd->GetGuidanceLine(i)) << "<br>" << G4endl; }
556 if(!(cmd->GetRange()).isNull())
557 { oF << "<p><dd>Range : " << ModStr(cmd->GetRange()) << G4endl; }
558 std::vector<G4ApplicationState>* availabelStateList = cmd->GetStateList();
559 if(availabelStateList->size()==6)
560 { oF << "<p><dd>Available at all Geant4 states." << G4endl; }
561 else
562 {
563 oF << "<p><dd>Available Geant4 state(s) : ";
564 for(G4int ias=0;ias<G4int(availabelStateList->size());ias++)
565 { oF << G4StateManager::GetStateManager()->GetStateString((*availabelStateList)[ias]) << " " << G4endl; }
566 }
567 if(cmd->GetParameterEntries()>0)
568 {
569 oF << "<p><dd>Parameters<table border=1>" << G4endl;
570 for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
571 {
572 G4UIparameter* prm = cmd->GetParameter(i_thParam);
573 oF << "<tr><td>" << ModStr(prm->GetParameterName()) << G4endl;
574 oF << "<td>type " << prm->GetParameterType() << G4endl;
575 oF << "<td>";
576 if(prm->IsOmittable())
577 {
578 oF << "Omittable : ";
579 if(prm->GetCurrentAsDefault())
580 { oF << "current value is used as the default value." << G4endl; }
581 else
582 { oF << "default value = " << prm->GetDefaultValue() << G4endl; }
583 }
584 oF << "<td>";
585 if(!(prm->GetParameterRange()).isNull())
586 { oF << "Parameter range : " << ModStr(prm->GetParameterRange()) << G4endl; }
587 else if(!(prm->GetParameterCandidates()).isNull())
588 { oF << "Parameter candidates : " << ModStr(prm->GetParameterCandidates()) << G4endl; }
589 }
590 oF << "</table>" << G4endl;
591 }
592
593 }
594
595 oF << "</dl></body></html>" << G4endl;
596 oF.close();
597}
598
Note: See TracBrowser for help on using the repository browser.