Gaudi Framework, version v22r2

Home   Generated: Tue May 10 2011

PathResolver.cpp

Go to the documentation of this file.
00001 #include "GaudiKernel/PathResolver.h"
00002 #include "GaudiKernel/System.h"
00003 
00004 #ifdef WIN32
00005 // Disable warning
00006 //   C4996: 'std::copy': Function call with parameters that may be unsafe
00007 // Probably coming from Boost classification.
00008 #pragma warning(disable:4996)
00009 #endif
00010 
00011 #include <iostream>
00012 #include <string>
00013 #include <vector>
00014 #include <stdlib.h>
00015 
00016 #include <boost/algorithm/string/split.hpp>
00017 #include <boost/algorithm/string/classification.hpp>
00018 #include <boost/filesystem.hpp>
00019 
00020 namespace bf = boost::filesystem;
00021 using namespace std;
00022 
00023 #ifdef _WIN32
00024   static const char* path_separator = ",;";
00025 #else
00026   static const char* path_separator = ",:";
00027 #endif
00028 
00029 //
00031 //
00032 
00033 namespace System {
00034 
00035 typedef enum {
00036   PR_regular_file,
00037   PR_directory
00038 } PR_file_type;
00039 
00040 typedef enum {
00041   PR_local,
00042   PR_recursive
00043 } PR_search_type;
00044 
00045 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00046 
00047 static bool
00048 PR_find( const bf::path& file, const string& search_list,
00049          PR_file_type file_type, PathResolver::SearchType search_type,
00050          string& result ) {
00051 
00052   bool found(false);
00053 
00054   // look for file as specified first
00055 
00056   try {
00057     if ( ( file_type == PR_regular_file && is_regular_file( file ) ) ||
00058          ( file_type == PR_directory && is_directory( file ) ) ) {
00059       result = bf::complete(file).string();
00060       return true;
00061     }
00062   } catch (bf::filesystem_error /*err*/) {
00063   }
00064 
00065   // assume that "." is always part of the search path, so check locally first
00066 
00067   try {
00068     bf::path local = bf::initial_path() / file;
00069     if ( ( file_type == PR_regular_file && is_regular_file( local ) ) ||
00070          ( file_type == PR_directory && is_directory( local ) ) ) {
00071       result = bf::complete(file).string();
00072       return true;
00073     }
00074   } catch (bf::filesystem_error /*err*/) {
00075   }
00076 
00077 
00078   // iterate through search list
00079   vector<string> spv;
00080   split(spv, search_list, boost::is_any_of( path_separator), boost::token_compress_on);
00081   for (vector<string>::const_iterator itr = spv.begin();
00082        itr != spv.end(); ++itr ) {
00083 
00084     bf::path fp = *itr / file;
00085 
00086     try {
00087       if ( ( file_type == PR_regular_file && is_regular_file( fp ) ) ||
00088            ( file_type == PR_directory && is_directory( fp ) ) ) {
00089         result = bf::complete(fp).string();
00090         return true;
00091       }
00092     } catch (bf::filesystem_error /*err*/) {
00093     }
00094 
00095 
00096     // if recursive searching requested, drill down
00097     if (search_type == PathResolver::RecursiveSearch &&
00098         is_directory( bf::path(*itr) ) ) {
00099 
00100       bf::recursive_directory_iterator end_itr;
00101       try {
00102         for ( bf::recursive_directory_iterator ritr( *itr );
00103               ritr != end_itr; ++ritr) {
00104 
00105           // skip if not a directory
00106           if (! is_directory( bf::path(*ritr) ) ) { continue; }
00107 
00108           bf::path fp2 = bf::path(*ritr) / file;
00109           if ( ( file_type == PR_regular_file && is_regular_file( fp2 ) ) ||
00110                ( file_type == PR_directory && is_directory( fp2 ) ) ) {
00111             result = bf::complete( fp2 ).string();
00112             return true;
00113           }
00114         }
00115       } catch (bf::filesystem_error /*err*/) {
00116       }
00117     }
00118 
00119   }
00120 
00121   return found;
00122 }
00123 
00124 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00125 
00126 string
00127 PathResolver::find_file(const std::string& logical_file_name,
00128               const std::string& search_path,
00129               SearchType search_type) {
00130 
00131   std::string path_list;
00132   System::getEnv(search_path, path_list);
00133 
00134   return (find_file_from_list (logical_file_name, path_list, search_type));
00135 }
00136 
00137 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00138 
00139 std::string
00140 PathResolver::find_file_from_list (const std::string& logical_file_name,
00141                                    const std::string& search_list,
00142                                    SearchType search_type)
00143 {
00144   std::string result("");
00145 
00146   bf::path lfn( logical_file_name );
00147 
00148   /* bool found = */
00149   PR_find (lfn, search_list, PR_regular_file, search_type, result);
00150 
00151   // The following functionality was in the original PathResolver, but I believe
00152   // that it's WRONG. It extracts the filename of the requested item, and searches
00153   // for that if the preceding search fails. i.e., if you're looking for "B/a.txt",
00154   // and that fails, it will look for just "a.txt" in the search list.
00155 
00156   // if (! found && lfn.filename() != lfn ) {
00157   //   result = "";
00158   //   PR_find (lfn.filename(), search_list, PR_regular_file, search_type, result);
00159   // }
00160 
00161   return (result);
00162 }
00163 
00164 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00165 
00166 string PathResolver::find_directory (const std::string& logical_file_name,
00167                                      const std::string& search_path,
00168                                      SearchType search_type)
00169 {
00170   std::string path_list;
00171   System::getEnv(search_path, path_list);
00172 
00173   return (find_directory_from_list (logical_file_name, path_list, search_type));
00174 }
00175 
00176 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00177 
00178 string
00179 PathResolver::find_directory_from_list (const std::string& logical_file_name,
00180                                         const std::string& search_list,
00181                                         SearchType search_type)
00182 {
00183   std::string result;
00184 
00185   if (!PR_find (logical_file_name, search_list, PR_directory, search_type, result))
00186   {
00187     result = "";
00188   }
00189 
00190   return (result);
00191 }
00192 
00193 
00194 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00195 
00196 PathResolver::SearchPathStatus
00197 PathResolver::check_search_path (const std::string& search_path)
00198 {
00199   std::string path_list;
00200   if ( ! System::getEnv(search_path, path_list) )
00201     return (EnvironmentVariableUndefined);
00202 
00203   vector<string> spv;
00204   boost::split( spv, path_list, boost::is_any_of( path_separator ), boost::token_compress_on);
00205   vector<string>::iterator itr=spv.begin();
00206 
00207   try {
00208     for (; itr!= spv.end(); ++itr) {
00209       bf::path pp(*itr);
00210       if (!is_directory(pp)) {
00211         return (UnknownDirectory);
00212       }
00213     }
00214   } catch(bf::filesystem_error /*err*/) {
00215     return (UnknownDirectory);
00216   }
00217 
00218   return ( Ok );
00219 }
00220 
00221 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00222 
00223 std::string PathResolverFindXMLFile (const std::string& logical_file_name)
00224 {
00225   return PathResolver::find_file (logical_file_name, "XMLPATH");
00226 }
00227 
00228 std::string PathResolverFindDataFile (const std::string& logical_file_name)
00229 {
00230   return PathResolver::find_file (logical_file_name, "DATAPATH");
00231 }
00232 
00233 }  // System namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Tue May 10 2011 18:53:45 for Gaudi Framework, version v22r2 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004