Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r1p1 (ae26267b)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
DirSearchPath.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 #ifdef __ICC
21 // disable icc warning #279: controlling expression is constant
22 // ... a lot of noise produced by the boost/filesystem/operations.hpp
23 # pragma warning( disable : 279 )
24 #endif
26 #include "boost/filesystem/operations.hpp"
27 #include "boost/tokenizer.hpp"
28 
29 using namespace std;
30 
31 using boost::filesystem::exists;
32 using boost::filesystem::filesystem_error;
33 using boost::filesystem::is_directory;
34 
35 using boost::char_separator;
36 using boost::tokenizer;
37 
38 // constructors
39 DirSearchPath::DirSearchPath( const std::string& stringifiedPath, const char* separator ) {
40  addCWD(); // FIXME is this a good idea?
41 
42  typedef tokenizer<char_separator<char>> Tokenizer;
43 
44  Tokenizer tok( stringifiedPath, char_separator<char>( separator ) );
45 
46  // add names to dir container, filtering dir names to remove invalid ones
47  // notice how we iterate over all tokens even if there is an illegal one
48  auto it = tok.begin();
49  while ( it != tok.end() ) {
50  try {
51  path p( *( it++ ) );
52  add( p );
53  } catch ( boost::filesystem::filesystem_error& /*err*/ ) {}
54  }
55 }
56 
57 // modifiers
58 bool DirSearchPath::addCWD() { return add( boost::filesystem::current_path() ); }
59 
60 bool DirSearchPath::add( const path& dir ) {
61  bool dirExist( existsDir( dir ) );
62  // add dir to path even if dir does not (yet) exist,
63  // but don't add twice same dir
64  if ( m_dirs.end() == std::find_if( m_dirs.begin(), m_dirs.end(), eqPath( dir ) ) ) m_dirs.push_back( dir );
65  return dirExist;
66 }
67 
68 // accessors
69 bool DirSearchPath::find( const string& fileName, string& fullFileName ) const {
70  bool rc( false );
71  try {
72  path fileFound;
73  if ( ( rc = find( path( fileName ), fileFound ) ) ) fullFileName = fileFound.string();
74  } catch ( ... ) {}
75  return rc;
76 }
77 
78 // accessors
79 bool DirSearchPath::find( const path& file, path& fileFound ) const {
80  bool rc( false );
81  for ( const auto& iDir : m_dirs ) {
82  path full{ iDir / file };
83  if ( exists( full ) ) {
84  fileFound = full;
85  rc = true;
86  break;
87  }
88  }
89  return rc;
90 }
91 
92 // accessors
94  std::list<path> found;
95  for ( const auto& iDir : m_dirs ) {
96  path full{ iDir / file };
97  if ( exists( full ) ) found.push_back( full );
98  }
99  return found;
100 }
101 
102 // helpers
103 bool DirSearchPath::existsDir( const std::string& dirName ) {
104  bool rc( false );
105  try {
106  rc = is_directory( path( dirName ) );
107  } catch ( ... ) {}
108  return rc;
109 }
110 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:69
DirSearchPath::addCWD
bool addCWD()
add current work dir (*nix pwd) to path
Definition: DirSearchPath.cpp:58
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:93
GaudiAlg.HistoUtils.path
path
Definition: HistoUtils.py:960
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:103
DirSearchPath::eqPath
Definition: DirSearchPath.h:69
DirSearchPath::path
boost::filesystem::path path
Definition: DirSearchPath.h:29
DirSearchPath::DirSearchPath
DirSearchPath()
Definition: DirSearchPath.h:33
std
STL namespace.
DirSearchPath::add
bool add(const path &dir)
Definition: DirSearchPath.cpp:60
IOTest.rc
rc
Definition: IOTest.py:112
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:49