All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DirSearchPath.cpp
Go to the documentation of this file.
1 #ifdef WIN32
2 // Disable warning
3 // C4996: '...': Function call with parameters that may be unsafe
4 // Noise probably coming from the use of Boost tokenizer
5 #pragma warning(disable:4996)
6 #endif
7 
8 #include <algorithm> /* find */
9 #include <iostream>
10 #ifdef __ICC
11 // disable icc warning #279: controlling expression is constant
12 // ... a lot of noise produced by the boost/filesystem/operations.hpp
13 #pragma warning(disable:279)
14 #endif
15 #include "boost/filesystem/operations.hpp"
16 #include "boost/tokenizer.hpp"
18 
19 using namespace std;
20 
21 using boost::filesystem::filesystem_error;
22 using boost::filesystem::exists;
23 using boost::filesystem::is_directory;
24 
25 using boost::tokenizer;
26 using boost::char_separator;
27 
28 //constructors
29 DirSearchPath::DirSearchPath(const std::string& stringifiedPath, const char* separator) {
30  addCWD(); //FIXME is this a good idea?
31 
32  typedef tokenizer<char_separator<char> > Tokenizer;
33 
34  Tokenizer tok(stringifiedPath, char_separator<char>(separator));
35 
36  //add names to dir container, filtering dir names to remove invalid ones
37  //notice how we iterate over all tokens even if there is an illegal one
38  Tokenizer::iterator it = tok.begin();
39  while(it != tok.end()) {
40  try {
41  path p(*(it++));
42  add(p);
43  }
44  catch (boost::filesystem::filesystem_error &/*err*/) {
45  }
46  }
47 }
48 
49 //modifiers
51  return add(boost::filesystem::current_path());
52 }
53 
54 bool DirSearchPath::add(const path& dir) {
55  bool dirExist(existsDir(dir));
56  //add dir to path even if dir does not (yet) exist,
57  // but don't add twice same dir
58  if (m_dirs.end() == std::find_if(m_dirs.begin(), m_dirs.end(), eqPath(dir)))
59  m_dirs.push_back(dir);
60  return dirExist;
61 }
62 
63 //accessors
64 bool DirSearchPath::find(const string& fileName, string& fullFileName) const {
65  bool rc(false);
66  try {
67  path fileFound;
68  if ( (rc = find(path(fileName), fileFound)) )
69  fullFileName = fileFound.string();
70  } catch (...) {}
71  return rc;
72 }
73 
74 //accessors
75 bool DirSearchPath::find(const path& file, path& fileFound) const {
76  bool rc(false);
77  for (std::list<path>::const_iterator iDir=m_dirs.begin(); iDir!=m_dirs.end(); ++iDir) {
78  path full(*iDir / file);
79  if (exists(full)) {
80  fileFound = full;
81  rc = true;
82  break;
83  }
84  }
85  return rc;
86 }
87 
88 //accessors
89 std::list<DirSearchPath::path>
91  std::list<path> found;
92  for (std::list<path>::const_iterator iDir=m_dirs.begin(); iDir!=m_dirs.end(); ++iDir) {
93  path full(*iDir / file);
94  if (exists(full)) {
95  found.push_back(full);
96  }
97  }
98  return found;
99 }
100 
101 //helpers
102 bool DirSearchPath::existsDir(const std::string& dirName) {
103  bool rc(false);
104  try {
105  rc=is_directory(path(dirName));
106  } catch(...) {}
107  return rc;
108 }
109 bool DirSearchPath::existsDir(const path& dir) {
110  return (exists(dir) && is_directory(dir));
111 }
boost::filesystem::path path
Definition: DirSearchPath.h:21
bool add(const path &dir)
STL Include files.
Definition: Tokenizer.h:24
bool find(const std::string &fileName, std::string &fullFileName) const
returns a flag if fileName found in search path, and sets ref to fully qualified file name (in native...
tuple rc
Definition: IOTest.py:92
list file
Definition: ana.py:160
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
std::list< path > find_all(const path &file) const
returns lists of files found in search path.
bool addCWD()
add current work dir (*nix pwd) to path
search for files in a list of directories
static bool existsDir(const std::string &dirName)
check dirName is valid