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