All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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  auto 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 (const auto& iDir : m_dirs ) {
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
90 DirSearchPath::find_all(const path& file) const {
91  std::list<path> found;
92  for (const auto& iDir : m_dirs ) {
93  path full{iDir / file};
94  if (exists(full)) found.push_back(full);
95  }
96  return found;
97 }
98 
99 //helpers
100 bool DirSearchPath::existsDir(const std::string& dirName) {
101  bool rc(false);
102  try {
103  rc=is_directory(path(dirName));
104  } catch(...) {}
105  return rc;
106 }
107 bool DirSearchPath::existsDir(const path& dir) {
108  return (exists(dir) && is_directory(dir));
109 }
boost::filesystem::path path
Definition: DirSearchPath.h:19
STL namespace.
STL class.
T push_back(T...args)
bool add(const path &dir)
rc
Definition: IOTest.py:92
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...
STL class.
T find_if(T...args)
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