Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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
16 #include "boost/filesystem/operations.hpp"
17 #include "boost/tokenizer.hpp"
18 
19 using namespace std;
20 
21 using boost::filesystem::exists;
22 using boost::filesystem::filesystem_error;
23 using boost::filesystem::is_directory;
24 
25 using boost::char_separator;
26 using boost::tokenizer;
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  } catch ( boost::filesystem::filesystem_error& /*err*/ ) {}
44  }
45 }
46 
47 // modifiers
48 bool DirSearchPath::addCWD() { return add( boost::filesystem::current_path() ); }
49 
50 bool DirSearchPath::add( const path& dir ) {
51  bool dirExist( existsDir( dir ) );
52  // add dir to path even if dir does not (yet) exist,
53  // but don't add twice same dir
54  if ( m_dirs.end() == std::find_if( m_dirs.begin(), m_dirs.end(), eqPath( dir ) ) ) m_dirs.push_back( dir );
55  return dirExist;
56 }
57 
58 // accessors
59 bool DirSearchPath::find( const string& fileName, string& fullFileName ) const {
60  bool rc( false );
61  try {
62  path fileFound;
63  if ( ( rc = find( path( fileName ), fileFound ) ) ) fullFileName = fileFound.string();
64  } catch ( ... ) {}
65  return rc;
66 }
67 
68 // accessors
69 bool DirSearchPath::find( const path& file, path& fileFound ) const {
70  bool rc( false );
71  for ( const auto& iDir : m_dirs ) {
72  path full{iDir / file};
73  if ( exists( full ) ) {
74  fileFound = full;
75  rc = true;
76  break;
77  }
78  }
79  return rc;
80 }
81 
82 // accessors
84  std::list<path> found;
85  for ( const auto& iDir : m_dirs ) {
86  path full{iDir / file};
87  if ( exists( full ) ) found.push_back( full );
88  }
89  return found;
90 }
91 
92 // helpers
93 bool DirSearchPath::existsDir( const std::string& dirName ) {
94  bool rc( false );
95  try {
96  rc = is_directory( path( dirName ) );
97  } catch ( ... ) {}
98  return rc;
99 }
100 bool DirSearchPath::existsDir( const path& dir ) { return ( exists( dir ) && is_directory( dir ) ); }
boost::filesystem::path path
Definition: DirSearchPath.h:19
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