The Gaudi Framework  master (37c0b60a)
DirSearchPath.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifdef WIN32
12 // Disable warning
13 // C4996: '...': Function call with parameters that may be unsafe
14 // Noise probably coming from the use of Boost tokenizer
15 # pragma warning( disable : 4996 )
16 #endif
17 
18 #include <algorithm> /* find */
19 #include <iostream>
20 #include <list>
21 #ifdef __ICC
22 // disable icc warning #279: controlling expression is constant
23 // ... a lot of noise produced by the boost/filesystem/operations.hpp
24 # pragma warning( disable : 279 )
25 #endif
27 #include <boost/filesystem/operations.hpp>
28 #include <boost/tokenizer.hpp>
29 
30 using namespace std;
31 
32 using boost::filesystem::exists;
33 using boost::filesystem::filesystem_error;
34 using boost::filesystem::is_directory;
35 
36 using boost::char_separator;
37 using boost::tokenizer;
38 
39 // constructors
40 DirSearchPath::DirSearchPath( const std::string& stringifiedPath, const char* separator ) {
41  addCWD(); // FIXME is this a good idea?
42 
43  typedef tokenizer<char_separator<char>> Tokenizer;
44 
45  Tokenizer tok( stringifiedPath, char_separator<char>( separator ) );
46 
47  // add names to dir container, filtering dir names to remove invalid ones
48  // notice how we iterate over all tokens even if there is an illegal one
49  auto it = tok.begin();
50  while ( it != tok.end() ) {
51  try {
52  path p( *( it++ ) );
53  add( p );
54  } catch ( boost::filesystem::filesystem_error& /*err*/ ) {}
55  }
56 }
57 
58 // modifiers
59 bool DirSearchPath::addCWD() { return add( boost::filesystem::current_path() ); }
60 
61 bool DirSearchPath::add( const path& dir ) {
62  bool dirExist( existsDir( dir ) );
63  // add dir to path even if dir does not (yet) exist,
64  // but don't add twice same dir
65  if ( m_dirs.end() == std::find_if( m_dirs.begin(), m_dirs.end(), eqPath( dir ) ) ) m_dirs.push_back( dir );
66  return dirExist;
67 }
68 
69 // accessors
70 bool DirSearchPath::find( const string& fileName, string& fullFileName ) const {
71  bool rc( false );
72  try {
73  path fileFound;
74  if ( ( rc = find( path( fileName ), fileFound ) ) ) fullFileName = fileFound.string();
75  } catch ( ... ) {}
76  return rc;
77 }
78 
79 // accessors
80 bool DirSearchPath::find( const path& file, path& fileFound ) const {
81  bool rc( false );
82  for ( const auto& iDir : m_dirs ) {
83  path full{ iDir / file };
84  if ( exists( full ) ) {
85  fileFound = full;
86  rc = true;
87  break;
88  }
89  }
90  return rc;
91 }
92 
93 // accessors
95  std::list<path> found;
96  for ( const auto& iDir : m_dirs ) {
97  path full{ iDir / file };
98  if ( exists( full ) ) found.push_back( full );
99  }
100  return found;
101 }
102 
103 // helpers
104 bool DirSearchPath::existsDir( const std::string& dirName ) {
105  bool rc( false );
106  try {
107  rc = is_directory( path( dirName ) );
108  } catch ( ... ) {}
109  return rc;
110 }
111 bool DirSearchPath::existsDir( const path& dir ) { return ( exists( dir ) && is_directory( dir ) ); }
DirSearchPath::find
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...
Definition: DirSearchPath.cpp:70
DirSearchPath::addCWD
bool addCWD()
add current work dir (*nix pwd) to path
Definition: DirSearchPath.cpp:59
std::string
STL class.
std::list
STL class.
DirSearchPath::find_all
std::list< path > find_all(const path &file) const
returns lists of files found in search path.
Definition: DirSearchPath.cpp:94
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
std::find_if
T find_if(T... args)
DirSearchPath.h
search for files in a list of directories
std::list::push_back
T push_back(T... args)
DirSearchPath::existsDir
static bool existsDir(const std::string &dirName)
check dirName is valid
Definition: DirSearchPath.cpp:104
DirSearchPath::eqPath
Definition: DirSearchPath.h:71
DirSearchPath::path
boost::filesystem::path path
Definition: DirSearchPath.h:31
DirSearchPath::DirSearchPath
DirSearchPath()
Definition: DirSearchPath.h:35
std
STL namespace.
DirSearchPath::add
bool add(const path &dir)
Definition: DirSearchPath.cpp:61
IOTest.rc
rc
Definition: IOTest.py:114
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:50