| # Gaudi Framework, version v22r2 |

Home |  | Generated: Tue May 10 2011

# DirSearchPath.cpp

Go to the documentation of this file.

    00001 #ifdef WIN32
    00002 // Disable warning
    00003 //    C4996: '...': Function call with parameters that may be unsafe
    00004 // Noise probably coming from the use of Boost tokenizer
    00005 #pragma warning(disable:4996)
    00006 #endif
    00007
    00008 #include <algorithm>  /* find */
    00009 #include <iostream>
    00010 #ifdef __ICC
    00011 // disable icc warning #279: controlling expression is constant
    00012 // ... a lot of noise produced by the boost/filesystem/operations.hpp
    00013 #pragma warning(disable:279)
    00014 #endif
    00015 #include "boost/filesystem/operations.hpp"
    00016 #include "boost/tokenizer.hpp"
    00017 #include "GaudiKernel/DirSearchPath.h"
    00018
    00019 using namespace std;
    00020
    00021 using boost::filesystem::filesystem_error;
    00022 using boost::filesystem::exists;
    00023 using boost::filesystem::is_directory;
    00024
    00025 using boost::tokenizer;
    00026 using boost::char_separator;
    00027
    00028 //constructors
    00029 DirSearchPath::DirSearchPath(const std::string& stringifiedPath, const char* separator) {
    00030   addCWD(); //FIXME is this a good idea?
    00031
    00032   typedef  tokenizer<char_separator<char> > Tokenizer;
    00033
    00034   Tokenizer tok(stringifiedPath, char_separator<char>(separator));
    00035
    00036   //add names to dir container, filtering dir names to remove invalid ones
    00037   //notice how we iterate over all tokens even if there is an illegal one
    00038   Tokenizer::iterator it = tok.begin();
    00039   while(it != tok.end()) {
    00040     try {
    00041       path p(*(it++));
    00042       add(p);
    00043     }
    00044     catch (boost::filesystem::filesystem_error &/*err*/) {
    00045     }
    00046   }
    00047 }
    00048
    00049 //modifiers
    00050 bool DirSearchPath::addCWD() {
    00051   return add(boost::filesystem::current_path());
    00052 }
    00053
    00054 bool DirSearchPath::add(const path& dir) {
    00055   bool dirExist(existsDir(dir));
    00056   //add dir to path even if dir does not (yet) exist,
    00057   // but don't add twice same dir
    00058   if (m_dirs.end() == std::find_if(m_dirs.begin(), m_dirs.end(), eqPath(dir)))
    00059     m_dirs.push_back(dir);
    00060   return dirExist;
    00061 }
    00062
    00063 //accessors
    00064 bool DirSearchPath::find(const string& fileName, string& fullFileName) const {
    00065   bool rc(false);
    00066   try {
    00067     path fileFound;
    00068     if ( (rc = find(path(fileName), fileFound)) )
    00069       fullFileName = fileFound.native_directory_string();
    00070   } catch (...) {}
    00071   return rc;
    00072 }
    00073
    00074 //accessors
    00075 bool DirSearchPath::find(const path& file, path& fileFound) const {
    00076   bool rc(false);
    00077   for (std::list<path>::const_iterator iDir=m_dirs.begin(); iDir!=m_dirs.end(); ++iDir) {
    00078     path full(*iDir / file);
    00079     if (exists(full)) {
    00080       fileFound = full;
    00081       rc = true;
    00082       break;
    00083     }
    00084   }
    00085   return rc;
    00086 }
    00087
    00088 //accessors
    00089 std::list<DirSearchPath::path>
    00090 DirSearchPath::find_all(const path& file) const {
    00091   std::list<path> found;
    00092   for (std::list<path>::const_iterator iDir=m_dirs.begin(); iDir!=m_dirs.end(); ++iDir) {
    00093     path full(*iDir / file);
    00094     if (exists(full)) {
    00095       found.push_back(full);
    00096     }
    00097   }
    00098   return found;
    00099 }
    00100
    00101 //helpers
    00102 bool DirSearchPath::existsDir(const std::string& dirName) {
    00103   bool rc(false);
    00104   try {
    00105     rc=is_directory(path(dirName));
    00106   } catch(...) {}
    00107   return rc;
    00108 }
    00109 bool DirSearchPath::existsDir(const path& dir) {
    00110   return (exists(dir) && is_directory(dir));
    00111 }

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines