The Gaudi Framework  v29r0 (ff2e7097)
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
16 #include "boost/filesystem/operations.hpp"
17 #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 {
31  addCWD(); // FIXME is this a good idea?
32 
33  typedef tokenizer<char_separator<char>> Tokenizer;
34 
35  Tokenizer tok( stringifiedPath, char_separator<char>( separator ) );
36 
37  // add names to dir container, filtering dir names to remove invalid ones
38  // notice how we iterate over all tokens even if there is an illegal one
39  auto it = tok.begin();
40  while ( it != tok.end() ) {
41  try {
42  path p( *( it++ ) );
43  add( p );
44  } catch ( boost::filesystem::filesystem_error& /*err*/ ) {
45  }
46  }
47 }
48 
49 // modifiers
50 bool DirSearchPath::addCWD() { return add( boost::filesystem::current_path() ); }
51 
52 bool DirSearchPath::add( const path& dir )
53 {
54  bool dirExist( existsDir( dir ) );
55  // add dir to path even if dir does not (yet) exist,
56  // but don't add twice same dir
57  if ( m_dirs.end() == std::find_if( m_dirs.begin(), m_dirs.end(), eqPath( dir ) ) ) m_dirs.push_back( dir );
58  return dirExist;
59 }
60 
61 // accessors
62 bool DirSearchPath::find( const string& fileName, string& fullFileName ) const
63 {
64  bool rc( false );
65  try {
66  path fileFound;
67  if ( ( rc = find( path( fileName ), fileFound ) ) ) fullFileName = fileFound.string();
68  } catch ( ... ) {
69  }
70  return rc;
71 }
72 
73 // accessors
74 bool DirSearchPath::find( const path& file, path& fileFound ) const
75 {
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 {
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 {
102  bool rc( false );
103  try {
104  rc = is_directory( path( dirName ) );
105  } catch ( ... ) {
106  }
107  return rc;
108 }
109 bool DirSearchPath::existsDir( const path& dir ) { return ( exists( dir ) && is_directory( dir ) ); }
boost::filesystem::path path
Definition: DirSearchPath.h:20
STL namespace.
STL class.
T push_back(T...args)
bool add(const path &dir)
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