SFrame 3.6
core/src/SParLocator.cxx
Go to the documentation of this file.
00001 // $Id: SParLocator.cxx 120 2009-08-27 12:02:57Z krasznaa $
00002 /***************************************************************************
00003  * @Project: SFrame - ROOT-based analysis framework for ATLAS
00004  * @Package: Core
00005  *
00006  * @author Stefan Ask       <Stefan.Ask@cern.ch>           - Manchester
00007  * @author David Berge      <David.Berge@cern.ch>          - CERN
00008  * @author Johannes Haller  <Johannes.Haller@cern.ch>      - Hamburg
00009  * @author A. Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN/Debrecen
00010  *
00011  ***************************************************************************/
00012 
00013 // System include(s):
00014 #include <sys/types.h>
00015 #include <dirent.h>
00016 
00017 // ROOT include(s):
00018 #include <TSystem.h>
00019 #include <TObjArray.h>
00020 #include <TObjString.h>
00021 
00022 // Local include(s):
00023 #include "../include/SParLocator.h"
00024 
00026 static const char* PAR_PATH_NAME = "PAR_PATH";
00027 
00028 //
00029 // Initialise the static members:
00030 //
00031 std::list< TString > SParLocator::m_parDirs;
00032 SLogger              SParLocator::m_logger( "SParLocator" );
00033 
00038 TString SParLocator::Locate( const TString& parName ) {
00039 
00040    // Read in the list of directories to be searched:
00041    if( ! m_parDirs.size() ) {
00042       ReadParDirs();
00043    }
00044 
00045    // If the full path name is defined in the configuration, don't bother
00046    // looking for the file:
00047    if( parName.Contains( "/" ) ) {
00048       m_logger << DEBUG << "Treating received file name as full path name..." << SLogger::endmsg;
00049       return parName;
00050    }
00051 
00052    //
00053    // Loop over all the directories:
00054    //
00055    for( std::list< TString >::const_iterator dir = m_parDirs.begin();
00056         dir != m_parDirs.end(); ++dir ) {
00057 
00058       //
00059       // Loop over all the files in the directory (Google magic...)
00060       // This should be pretty POSIX compliant...
00061       //
00062       DIR* d = ::opendir( *dir );
00063       struct dirent* file;
00064       while( ( file = ::readdir( d ) ) != NULL ) {
00065          if( parName == file->d_name ) {
00066             m_logger << DEBUG << parName << " found in directory: " << *dir << SLogger::endmsg;
00067             ::closedir( d );
00068             return ( *dir + "/" + parName );
00069          }
00070       }
00071       ::closedir( d );
00072 
00073    }
00074 
00075    m_logger << ERROR << parName << " couldn't be found" << SLogger::endmsg;
00076    return "";
00077 
00078 }
00079 
00084 void SParLocator::ReadParDirs() {
00085 
00086    // Clear the current directories:
00087    m_parDirs.clear();
00088 
00089    //
00090    // Get the environment variable and split it up:
00091    //
00092    TString par_path = gSystem->Getenv( PAR_PATH_NAME );
00093    TObjArray* par_array = par_path.Tokenize( ":" );
00094 
00095    //
00096    // Add all proper path names to the list:
00097    //
00098    for( Int_t i = 0; i < par_array->GetSize(); ++i ) {
00099 
00100       TObjString* path_element = dynamic_cast< TObjString* >( par_array->At( i ) );
00101       if( ! path_element ) continue;
00102 
00103       if( path_element->GetString() != "" ) {
00104          m_parDirs.push_back( path_element->GetString() );
00105       }
00106 
00107    }
00108 
00109    delete par_array;
00110 
00111    //
00112    // Add a failsafe if the environment variable was empty:
00113    //
00114    if( ! m_parDirs.size() ) {
00115       m_logger << WARNING << "No directories set in the " << PAR_PATH_NAME
00116                << " environment variable" << SLogger::endmsg;
00117       m_logger << WARNING << "Only the local directory will be searched!" << SLogger::endmsg;
00118       m_parDirs.push_back( "./" );
00119    }
00120 
00121    return;
00122 
00123 }