Go to the documentation of this file.00001 #ifdef WIN32
00002
00003
00004
00005 #pragma warning(disable:4996)
00006 #endif
00007
00008 #include <algorithm>
00009 #include <iostream>
00010 #ifdef __ICC
00011
00012
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
00029 DirSearchPath::DirSearchPath(const std::string& stringifiedPath, const char* separator) {
00030 addCWD();
00031
00032 typedef tokenizer<char_separator<char> > Tokenizer;
00033
00034 Tokenizer tok(stringifiedPath, char_separator<char>(separator));
00035
00036
00037
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 &) {
00045 }
00046 }
00047 }
00048
00049
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
00057
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
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
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
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
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 }