G4UIcommandTree.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 
00030 #include "G4UIcommandTree.hh"
00031 #include "G4StateManager.hh"
00032 #include <fstream>
00033 #include "G4ios.hh"
00034 
00035 G4UIcommandTree::G4UIcommandTree()
00036 :guidance(NULL)
00037 { }
00038 
00039 G4UIcommandTree::G4UIcommandTree(const char * thePathName)
00040 :guidance(NULL)
00041 {
00042   pathName = thePathName;
00043 }
00044 
00045 G4UIcommandTree::~G4UIcommandTree()
00046 {
00047   G4int i;
00048   G4int n_treeEntry = tree.size();
00049   for( i=0; i < n_treeEntry; i++ )
00050   { delete tree[i]; }
00051 }
00052 
00053 G4int G4UIcommandTree::operator==(const G4UIcommandTree &right) const
00054 {
00055   return ( pathName == right.GetPathName() );
00056 }
00057 
00058 G4int G4UIcommandTree::operator!=(const G4UIcommandTree &right) const
00059 {
00060   return ( pathName != right.GetPathName() );
00061 }
00062 
00063 void G4UIcommandTree::AddNewCommand(G4UIcommand *newCommand)
00064 {
00065   G4String commandPath = newCommand->GetCommandPath();
00066   G4String remainingPath = commandPath;
00067   remainingPath.remove(0,pathName.length());
00068   if( remainingPath.isNull() )
00069   {
00070     guidance = newCommand;
00071     return;
00072   }
00073   G4int i = remainingPath.first('/');
00074   if( i == G4int(std::string::npos) )
00075   {
00076     // Find command
00077     G4int n_commandEntry = command.size();
00078     for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
00079     {
00080       if( remainingPath == command[i_thCommand]->GetCommandName() )
00081       { return; }
00082     }
00083     command.push_back( newCommand );
00084     return;
00085   }
00086   else
00087   {
00088     // Find path
00089     G4String nextPath = pathName;
00090     nextPath.append(remainingPath(0,i+1));
00091     G4int n_treeEntry = tree.size();
00092     for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
00093     {
00094       if( nextPath == tree[i_thTree]->GetPathName() )
00095       { 
00096         tree[i_thTree]->AddNewCommand( newCommand );
00097         return; 
00098       }
00099     }
00100     G4UIcommandTree * newTree = new G4UIcommandTree( nextPath );
00101     tree.push_back( newTree );
00102     newTree->AddNewCommand( newCommand );
00103     return;
00104   }
00105 }
00106 
00107 void G4UIcommandTree::RemoveCommand(G4UIcommand *aCommand)
00108 {
00109   G4String commandPath = aCommand->GetCommandPath();
00110   G4String remainingPath = commandPath;
00111   remainingPath.remove(0,pathName.length());
00112   if( remainingPath.isNull() )
00113   {
00114     guidance = NULL;
00115   }
00116   else
00117   {
00118     G4int i = remainingPath.first('/');
00119     if( i == G4int(std::string::npos) )
00120     {
00121       // Find command
00122       G4int n_commandEntry = command.size();
00123       for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
00124       {
00125         if( remainingPath == command[i_thCommand]->GetCommandName() )
00126         { 
00127           command.erase(command.begin()+i_thCommand);
00128           break;
00129         }
00130       }
00131     }
00132     else
00133     {
00134       // Find path
00135       G4String nextPath = pathName;
00136       nextPath.append(remainingPath(0,i+1));
00137       G4int n_treeEntry = tree.size();
00138       for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
00139       {
00140         if( nextPath == tree[i_thTree]->GetPathName() )
00141         { 
00142           tree[i_thTree]->RemoveCommand( aCommand );
00143           G4int n_commandRemain = tree[i_thTree]->GetCommandEntry();
00144         G4int n_treeRemain = tree[i_thTree]-> GetTreeEntry();
00145           if(n_commandRemain == 0 && n_treeRemain == 0)
00146           {
00147             G4UIcommandTree * emptyTree = tree[i_thTree];
00148             tree.erase(tree.begin()+i_thTree);
00149             delete emptyTree;
00150           }
00151           break;
00152         }
00153       }
00154     }
00155   }
00156 }
00157 
00158 // L. Garnier 01.28.08 This function has not a good name. In fact, it try
00159 // to match a command name, not a path. It should be rename as FindCommandName
00160 
00161 G4UIcommand * G4UIcommandTree::FindPath(const char* commandPath) const
00162 {
00163   G4String remainingPath = commandPath;
00164   if( remainingPath.index( pathName ) == std::string::npos )
00165   { return NULL; }
00166   remainingPath.remove(0,pathName.length());
00167   G4int i = remainingPath.first('/');
00168   if( i == G4int(std::string::npos) )
00169   {
00170     // Find command
00171     G4int n_commandEntry = command.size();
00172     for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
00173     {
00174       if( remainingPath == command[i_thCommand]->GetCommandName() )
00175       { return command[i_thCommand]; }
00176     }
00177   }
00178   else
00179   {
00180     // Find path
00181     G4String nextPath = pathName;
00182     nextPath.append(remainingPath(0,i+1));
00183     G4int n_treeEntry = tree.size();
00184     for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
00185     {
00186       if( nextPath == tree[i_thTree]->GetPathName() )
00187       { return tree[i_thTree]->FindPath( commandPath ); }
00188     }
00189   }
00190   return NULL;
00191 }
00192 
00193 
00199 G4UIcommandTree* G4UIcommandTree::FindCommandTree(const char* commandPath)
00200 {
00201   G4String remainingPath = commandPath;
00202   if( remainingPath.index( pathName ) == std::string::npos )
00203   { return NULL; }
00204   remainingPath.remove(0,pathName.length());
00205   G4int i = remainingPath.first('/');
00206   if( i != G4int(std::string::npos) )
00207   {
00208     // Find path
00209     G4String nextPath = pathName;
00210     nextPath.append(remainingPath(0,i+1));
00211     G4int n_treeEntry = tree.size();
00212     for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
00213     {
00214       if (tree[i_thTree]->GetPathName() == commandPath) {
00215         return tree[i_thTree];
00216       }
00217       else if( nextPath == tree[i_thTree]->GetPathName() ) {
00218         return tree[i_thTree]->FindCommandTree( commandPath );
00219       }
00220     }
00221   } else {
00222     return this;
00223   }
00224   return NULL;
00225 }
00226 
00227 G4String G4UIcommandTree::CompleteCommandPath(const G4String& aCommandPath)
00228 {
00229   G4String pName = aCommandPath;
00230   G4String remainingPath = aCommandPath;
00231   G4String empty = "";
00232   G4String matchingPath = empty;
00233 
00234   // find the tree
00235   G4int jpre= pName.last('/');
00236   if(jpre != G4int(G4String::npos)) pName.remove(jpre+1);
00237   G4UIcommandTree* aTree = FindCommandTree(pName);
00238 
00239   if (!aTree) {
00240     return empty;
00241   }
00242   
00243   if( pName.index( pName ) == std::string::npos ) return empty;
00244 
00245   std::vector<G4String> paths;
00246 
00247   // list matched directories/commands
00248   G4String strtmp;
00249   G4int nMatch= 0;
00250 
00251   int Ndir= aTree-> GetTreeEntry();
00252   int Ncmd= aTree-> GetCommandEntry();
00253   
00254   // directory ...
00255   for(G4int idir=1; idir<=Ndir; idir++) {
00256     G4String fpdir= aTree-> GetTree(idir)-> GetPathName();
00257     // matching test
00258     if( fpdir.index(remainingPath, 0) == 0) {
00259       if(nMatch==0) {
00260         matchingPath = fpdir;
00261       } else {
00262         matchingPath = GetFirstMatchedString(fpdir,matchingPath);
00263       }
00264       nMatch++;
00265       paths.push_back(fpdir);
00266     }
00267   }
00268   
00269   if (paths.size()>=2) {
00270     G4cout << "Matching directories :" << G4endl; 
00271     for( unsigned int i_thCommand = 0; i_thCommand < paths.size(); i_thCommand++ ) {
00272       G4cout << paths[i_thCommand] << G4endl; 
00273     }
00274   }
00275   
00276   // command ...
00277   std::vector<G4String> commands;
00278 
00279   for(G4int icmd=1; icmd<=Ncmd; icmd++){
00280     G4String fpcmd= aTree-> GetPathName() +
00281                     aTree-> GetCommand(icmd) -> GetCommandName();
00282     // matching test
00283     if( fpcmd.index(remainingPath, 0) ==0) {
00284       if(nMatch==0) {
00285         matchingPath= fpcmd + " ";
00286       } else {
00287         strtmp= fpcmd + " ";
00288         matchingPath= GetFirstMatchedString(matchingPath, strtmp);
00289       }
00290       nMatch++;
00291       commands.push_back(fpcmd+" ");
00292     }
00293   }
00294 
00295   if (commands.size()>=2) {
00296     G4cout << "Matching commands :" << G4endl; 
00297     for( unsigned int i_thCommand = 0; i_thCommand < commands.size(); i_thCommand++ ) {
00298       G4cout << commands[i_thCommand] << G4endl; 
00299     }
00300   }
00301 
00302   return matchingPath;
00303 }
00304 
00305 
00307 G4String G4UIcommandTree::GetFirstMatchedString(const G4String& str1, 
00308                                          const G4String& str2) const
00310 {
00311   int nlen1= str1.length();
00312   int nlen2= str2.length();
00313 
00314   int nmin = nlen1<nlen2 ? nlen1 : nlen2;
00315 
00316   G4String strMatched;
00317   for(size_t i=0; G4int(i)<nmin; i++){
00318     if(str1[i]==str2[i]) {
00319       strMatched+= str1[i];
00320     } else {
00321       break;
00322     }
00323   }
00324 
00325   return strMatched;
00326 }
00327 
00328 void G4UIcommandTree::ListCurrent() const
00329 {
00330   G4cout << "Command directory path : " << pathName << G4endl;
00331   if( guidance != NULL ) guidance->List();
00332   G4cout << " Sub-directories : " << G4endl;
00333   G4int n_treeEntry = tree.size();
00334   for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
00335   {
00336     G4cout << "   " << tree[i_thTree]->GetPathName() 
00337          << "   " << tree[i_thTree]->GetTitle() << G4endl;
00338   }
00339   G4cout << " Commands : " << G4endl;
00340   G4int n_commandEntry = command.size();
00341   for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
00342   {
00343     G4cout << "   " << command[i_thCommand]->GetCommandName() 
00344          << " * " << command[i_thCommand]->GetTitle() << G4endl;
00345   }
00346 }
00347 
00348 void G4UIcommandTree::ListCurrentWithNum() const
00349 {
00350   G4cout << "Command directory path : " << pathName << G4endl;
00351   if( guidance != NULL ) guidance->List();
00352   G4int i = 0;
00353   G4cout << " Sub-directories : " << G4endl;
00354   G4int n_treeEntry = tree.size();
00355   for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
00356   {
00357     i++;
00358     G4cout << " " << i << ") " << tree[i_thTree]->GetPathName() 
00359          << "   " << tree[i_thTree]->GetTitle() << G4endl;
00360   }
00361   G4cout << " Commands : " << G4endl;
00362   G4int n_commandEntry = command.size();
00363   for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
00364   {
00365     i++;
00366     G4cout << " " << i << ") " << command[i_thCommand]->GetCommandName() 
00367          << " * " << command[i_thCommand]->GetTitle() << G4endl;
00368   }
00369 }
00370 
00371 void G4UIcommandTree::List() const
00372 {
00373   ListCurrent();
00374   G4int n_commandEntry = command.size();
00375   for( G4int i_thCommand = 0; i_thCommand < n_commandEntry; i_thCommand++ )
00376   {
00377     command[i_thCommand]->List();
00378   }
00379   G4int n_treeEntry = tree.size();
00380   for( G4int i_thTree = 0; i_thTree < n_treeEntry; i_thTree++ )
00381   {
00382     tree[i_thTree]->List();
00383   }
00384 }
00385 
00386 G4String G4UIcommandTree::CreateFileName(const char* pName)
00387 {
00388   G4String fn = pName;
00389   G4int idxs;
00390   while((idxs=fn.index("/"))!=G4int(std::string::npos))
00391   { fn(idxs) = '_'; }
00392   fn += ".html";
00393   return fn;
00394 }
00395 
00396 G4String G4UIcommandTree::ModStr(const char* strS)
00397 {
00398   G4String sx;
00399   G4String str = strS;
00400   for(G4int i=0;i<G4int(str.length());i++)
00401   {
00402     char c = str(i);
00403     switch(c)
00404     {
00405     case '<':
00406       sx += "&lt;"; break;
00407     case '>':
00408       sx += "&gt;"; break;
00409     case '&':
00410       sx += "&amp;"; break;
00411     default:
00412       sx += c;
00413     }
00414   }
00415   return sx;
00416 }
00417 
00418 void G4UIcommandTree::CreateHTML()
00419 {
00420   G4String ofileName = CreateFileName(pathName);
00421   std::ofstream oF(ofileName, std::ios::out);
00422 
00423   oF << "<html><head><title>Commands in " << ModStr(pathName) << "</title></head>" << G4endl;
00424   oF << "<body bgcolor=\"#ffffff\"><h2>" << ModStr(pathName) << "</h2><p>" << G4endl;
00425 
00426   if( guidance != NULL ) 
00427   {
00428     for(G4int i=0;i<guidance->GetGuidanceEntries();i++)
00429     { oF << ModStr(guidance->GetGuidanceLine(i)) << "<br>" << G4endl; }
00430   }
00431 
00432   oF << "<p><hr><p>" << G4endl;
00433   
00434   oF << "<h2>Sub-directories : </h2><dl>" << G4endl;
00435   for( G4int i_thTree = 0; i_thTree < G4int(tree.size()); i_thTree++ )
00436   {
00437     oF << "<p><br><p><dt><a href=\"" << CreateFileName(tree[i_thTree]->GetPathName())
00438        << "\">" << ModStr(tree[i_thTree]->GetPathName()) << "</a>" << G4endl;
00439     oF << "<p><dd>" << ModStr(tree[i_thTree]->GetTitle()) << G4endl;
00440     tree[i_thTree]->CreateHTML();
00441   }
00442 
00443   oF << "</dl><p><hr><p>" << G4endl;
00444   
00445   oF << "<h2>Commands : </h2><dl>" << G4endl;
00446   for( G4int i_thCommand = 0; i_thCommand < G4int(command.size()); i_thCommand++ )
00447   {
00448     G4UIcommand* cmd = command[i_thCommand];
00449     oF << "<p><br><p><dt><b>" << ModStr(cmd->GetCommandName());
00450     if(cmd->GetParameterEntries()>0)
00451     {
00452       for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
00453       { oF << " [<i>" << ModStr(cmd->GetParameter(i_thParam)->GetParameterName()) << "</i>]"; }
00454     }
00455     oF << "</b>" << G4endl;
00456     oF << "<p><dd>" << G4endl;
00457     for(G4int i=0;i<cmd->GetGuidanceEntries();i++)
00458     { oF << ModStr(cmd->GetGuidanceLine(i)) << "<br>" << G4endl; }
00459     if(!(cmd->GetRange()).isNull())
00460     { oF << "<p><dd>Range : " << ModStr(cmd->GetRange()) << G4endl; }
00461     std::vector<G4ApplicationState>* availabelStateList = cmd->GetStateList();
00462     if(availabelStateList->size()==6)
00463     { oF << "<p><dd>Available at all Geant4 states." << G4endl; }
00464     else
00465     {
00466       oF << "<p><dd>Available Geant4 state(s) : ";
00467       for(G4int ias=0;ias<G4int(availabelStateList->size());ias++)
00468       { oF << G4StateManager::GetStateManager()->GetStateString((*availabelStateList)[ias]) << " " << G4endl; }
00469     }
00470     if(cmd->GetParameterEntries()>0)
00471     {
00472       oF << "<p><dd>Parameters<table border=1>" << G4endl;
00473       for(G4int i_thParam=0;i_thParam<cmd->GetParameterEntries();i_thParam++)
00474       {
00475         G4UIparameter* prm = cmd->GetParameter(i_thParam);
00476         oF << "<tr><td>" << ModStr(prm->GetParameterName()) << G4endl;
00477         oF << "<td>type " << prm->GetParameterType() << G4endl;
00478         oF << "<td>";
00479         if(prm->IsOmittable())
00480         { 
00481           oF << "Omittable : ";
00482           if(prm->GetCurrentAsDefault())
00483           { oF << "current value is used as the default value." << G4endl; }
00484           else
00485           { oF << "default value = " << prm->GetDefaultValue() << G4endl; }
00486         }
00487         oF << "<td>";
00488         if(!(prm->GetParameterRange()).isNull())
00489         { oF << "Parameter range : " << ModStr(prm->GetParameterRange()) << G4endl; }
00490         else if(!(prm->GetParameterCandidates()).isNull())
00491         { oF << "Parameter candidates : " << ModStr(prm->GetParameterCandidates()) << G4endl; }
00492       }
00493       oF << "</table>" << G4endl;
00494     }
00495 
00496   }
00497   
00498   oF << "</dl></body></html>" << G4endl;
00499   oF.close();
00500 }
00501 

Generated on Mon May 27 17:50:05 2013 for Geant4 by  doxygen 1.4.7