| # Gaudi Framework, version v21r10p1 |

Home |  | Generated: 29 Jul 2010

# DirSearchPath.cpp

Go to the documentation of this file.

    00001 // $Id: DirSearchPath.cpp,v 1.2 2007/10/16 15:37:25 marcocle Exp $
    00002
    00003 //<<<<<< INCLUDES                                                       >>>>>>
    00004 #include <algorithm>  /* find */
    00005 #include <iostream>
    00006 #ifdef __ICC
    00007 // disable icc warning #279: controlling expression is constant
    00008 // ... a lot of noise produced by the boost/filesystem/operations.hpp
    00009 #pragma warning(disable:279)
    00010 #endif
    00011 #include "boost/filesystem/operations.hpp"
    00012 #include "boost/tokenizer.hpp"
    00013 #include "GaudiKernel/DirSearchPath.h"
    00014
    00015 using namespace std;
    00016
    00017 using boost::filesystem::filesystem_error;
    00018 using boost::filesystem::exists;
    00019 using boost::filesystem::is_directory;
    00020
    00021 using boost::tokenizer;
    00022 using boost::char_separator;
    00023
    00024 //structors
    00025 DirSearchPath::DirSearchPath(const std::string& stringifiedPath, const char* separator) {
    00026   addCWD(); //FIXME is this a good idea?
    00027
    00028   typedef  tokenizer<char_separator<char> > Tokenizer;
    00029
    00030   Tokenizer tok(stringifiedPath, char_separator<char>(separator));
    00031
    00032
    00033   //add names to dir container, filtering dir names to remove invalid ones
    00034   //notice how we iterate over all tokens even if there is an illegal one
    00035   Tokenizer::iterator it = tok.begin();
    00036   while(it != tok.end()) {
    00037     try {
    00038       //path p( *(it++), boost::filesystem::native);
    00039       // For some reason native() does not work with boost 1.31. Changed to no_check and cross the fingers....
    00040       path p( *(it++), boost::filesystem::no_check);
    00041       add(p);
    00042     }
    00043     catch (boost::filesystem::filesystem_error /*err*/) {
    00044     }
    00045   }
    00046 }
    00047
    00048 //modifiers
    00049 // bool DirSearchPath::add(const std::string& dirName) {
    00050 //   bool rc(false);
    00051 //   try {
    00052 //     rc=add(path(dirName));
    00053 //   } catch (const filesystem_error& err) {
    00054 // #ifndef NDEBUG
    00055 //     cerr << "DirSearchPath::DirSearchPath: ERROR adding dir "
    00056 //       << err.what() << endl;
    00057 //     throw err;
    00058 // #endif
    00059 //   }
    00060 //   return rc;
    00061 // }
    00062
    00063 bool DirSearchPath::addCWD() {
    00064   return add(boost::filesystem::current_path());
    00065 }
    00066
    00067 bool DirSearchPath::add(const path& dir) {
    00068   bool dirExist(existsDir(dir));
    00069   //add dir to path even if dir does not (yet) exist,
    00070   // but don't add twice same dir
    00071   if (m_dirs.end() == std::find_if(m_dirs.begin(), m_dirs.end(), eqPath(dir)))
    00072     m_dirs.push_back(dir);
    00073   return dirExist;
    00074 }
    00075
    00076 //accessors
    00077 bool DirSearchPath::find(const string& fileName, string& fullFileName) const {
    00078   bool rc(false);
    00079   try {
    00080     path fileFound;
    00081     if ( (rc = find(path(fileName), fileFound)) )
    00082       fullFileName = fileFound.native_directory_string();
    00083   } catch (...) {}
    00084   return rc;
    00085 }
    00086
    00087 //accessors
    00088 bool DirSearchPath::find(const path& file, path& fileFound) const {
    00089   bool rc(false);
    00090   for (std::list<path>::const_iterator iDir=m_dirs.begin(); iDir!=m_dirs.end(); ++iDir) {
    00091     path full(*iDir / file);
    00092     if (exists(full)) {
    00093       fileFound = full;
    00094       rc = true;
    00095       break;
    00096     }
    00097   }
    00098   return rc;
    00099 }
    00100
    00101 //accessors
    00102 std::list<DirSearchPath::path>
    00103 DirSearchPath::find_all(const path& file) const {
    00104   std::list<path> found;
    00105   for (std::list<path>::const_iterator iDir=m_dirs.begin(); iDir!=m_dirs.end(); ++iDir) {
    00106     path full(*iDir / file);
    00107     if (exists(full)) {
    00108       found.push_back(full);
    00109     }
    00110   }
    00111   return found;
    00112 }
    00113
    00114 //helpers
    00115 bool DirSearchPath::existsDir(const std::string& dirName) {
    00116   bool rc(false);
    00117   try {
    00118     rc=is_directory(path(dirName));
    00119   } catch(...) {}
    00120   return rc;
    00121 }
    00122 bool DirSearchPath::existsDir(const path& dir) {
    00123   return (exists(dir) && is_directory(dir));
    00124 }