The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
DirSearchPath.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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
12#include <algorithm> /* find */
13#include <iostream>
14#include <list>
15#ifdef __ICC
16// disable icc warning #279: controlling expression is constant
17// ... a lot of noise produced by the boost/filesystem/operations.hpp
18# pragma warning( disable : 279 )
19#endif
21#include <boost/filesystem/operations.hpp>
22#include <boost/tokenizer.hpp>
23
24using namespace std;
25
26using boost::filesystem::exists;
27using boost::filesystem::filesystem_error;
28using boost::filesystem::is_directory;
29
30using boost::char_separator;
31using boost::tokenizer;
32
33// constructors
34DirSearchPath::DirSearchPath( const std::string& stringifiedPath, const char* separator ) {
35 addCWD(); // FIXME is this a good idea?
36
37 typedef tokenizer<char_separator<char>> Tokenizer;
38
39 Tokenizer tok( stringifiedPath, char_separator<char>( separator ) );
40
41 // add names to dir container, filtering dir names to remove invalid ones
42 // notice how we iterate over all tokens even if there is an illegal one
43 auto it = tok.begin();
44 while ( it != tok.end() ) {
45 try {
46 path p( *( it++ ) );
47 add( p );
48 } catch ( boost::filesystem::filesystem_error& /*err*/ ) {}
49 }
50}
51
52// modifiers
53bool DirSearchPath::addCWD() { return add( boost::filesystem::current_path() ); }
54
55bool DirSearchPath::add( const path& dir ) {
56 bool dirExist( existsDir( dir ) );
57 // add dir to path even if dir does not (yet) exist,
58 // but don't add twice same dir
59 if ( m_dirs.end() == std::find_if( m_dirs.begin(), m_dirs.end(), eqPath( dir ) ) ) m_dirs.push_back( dir );
60 return dirExist;
61}
62
63// accessors
64bool DirSearchPath::find( const string& fileName, string& fullFileName ) const {
65 bool rc( false );
66 try {
67 path fileFound;
68 if ( ( rc = find( path( fileName ), fileFound ) ) ) fullFileName = fileFound.string();
69 } catch ( ... ) {}
70 return rc;
71}
72
73// accessors
74bool DirSearchPath::find( const path& file, path& fileFound ) const {
75 bool rc( false );
76 for ( const auto& iDir : m_dirs ) {
77 path full{ iDir / file };
78 if ( exists( full ) ) {
79 fileFound = full;
80 rc = true;
81 break;
82 }
83 }
84 return rc;
85}
86
87// accessors
88std::list<DirSearchPath::path> DirSearchPath::find_all( const path& file ) const {
89 std::list<path> found;
90 for ( const auto& iDir : m_dirs ) {
91 path full{ iDir / file };
92 if ( exists( full ) ) found.push_back( full );
93 }
94 return found;
95}
96
97// helpers
98bool DirSearchPath::existsDir( const std::string& dirName ) {
99 bool rc( false );
100 try {
101 rc = is_directory( path( dirName ) );
102 } catch ( ... ) {}
103 return rc;
104}
105bool DirSearchPath::existsDir( const path& dir ) { return ( exists( dir ) && is_directory( dir ) ); }
search for files in a list of directories
std::list< path > find_all(const path &file) const
returns lists of files found in search path.
static bool existsDir(const std::string &dirName)
check dirName is valid
boost::filesystem::path path
bool addCWD()
add current work dir (*nix pwd) to path
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...
std::vector< path > m_dirs
the dir container
STL namespace.