20#include "marley/Error.hh"
21#include "marley/FileManager.hh"
22#include "marley/Logger.hh"
23#include "marley/marley_utils.hh"
28 constexpr char SEARCH_PATH_DELIMITER =
':';
30 bool file_exists(
const std::string& file_name) {
31 return std::filesystem::is_regular_file(file_name);
47 char* mar = std::getenv(
"MARLEY");
48 if ( !mar )
throw marley::Error(
"The MARLEY environment variable is not set."
49 " Please set it (e.g., by sourcing the setup_marley.sh script) and"
56 char* msp = std::getenv(
"MARLEY_SEARCH_PATH");
65 MARLEY_LOG( DEBUG,
"io" ) <<
"MARLEY search path set to \""
73 static std::unique_ptr<marley::FileManager>
81 const std::string& search_path)
83 std::vector<std::string> dir_names
84 = marley_utils::split_string(search_path, SEARCH_PATH_DELIMITER);
89 const std::string& search_path)
const
96 const std::vector<std::string>& search_dirs)
const
98 std::string full_path_to_file;
103 if ( file_exists(base_name) )
return base_name;
107 if ( base_name.find(
'/') != std::string::npos ) {
108 for (
const auto& dir : search_dirs) {
109 std::string candidate = dir +
'/' + base_name;
110 if ( file_exists(candidate) )
return candidate;
118 for (
const auto& dir : search_dirs) {
120 [&full_path_to_file, &base_name](
const std::string& other_full_path,
121 const std::string& other_base_name) ->
bool
123 if ( base_name == other_base_name ) {
125 full_path_to_file = other_full_path;
133 if ( file_found )
break;
136 return full_path_to_file;
140 const std::string& search_path)
const
149 const std::vector<std::string>& search_dirs)
const
151 std::vector<std::string> result;
153 for (
const auto& dir : search_dirs) {
155 [&result](
const std::string& full_path,
156 const std::string& ) ->
bool
159 result.push_back( full_path );
176 const std::function<
bool(
const std::string&,
const std::string&)>& func)
179 std::filesystem::directory_iterator it(dir_name,
180 std::filesystem::directory_options::skip_permission_denied, ec);
183 MARLEY_LOG(WARN,
"io") <<
"Could not read from the directory \""
188 std::filesystem::directory_iterator end;
189 bool stop_iterations =
false;
190 while (!stop_iterations && it != end) {
191 const auto& entry = *it;
192 std::string full_file_name = entry.path().string();
193 std::string base_name = entry.path().filename().string();
195 MARLEY_LOG(TRACE,
"io") <<
"marley::FileManager found file \""
196 << full_file_name <<
'\"';
198 if (entry.is_regular_file()) {
199 stop_iterations = func(full_file_name, base_name);
204 MARLEY_LOG(DEBUG,
"io") <<
"Couldn't access a file in directory \""
210 return stop_iterations;
Base class for all exceptions thrown by MARLEY functions.
Singleton class that handles file searches.
std::string marley_dir_
Stores the value of the MARLEY environment variable (which points to the root folder of the source co...
FileManager()
Create the singleton FileManager object.
std::vector< std::string > list_all_files(const std::vector< std::string > &search_dirs) const
Get a vector of full paths for all regular files in the requested directories.
static std::string default_search_path_
By default, use this search path when looking for files.
static std::vector< std::string > search_path_to_dir_vector(const std::string &search_path=FileManager::default_search_path_)
Converts a ':'-delimited search path (given as a single string) into a vector of directory names.
static bool dir_iterate(const std::string &dir_name, const std::function< bool(const std::string &, const std::string &)> &func)
Helper function that executes a std::function on the name of each regular file found while scanning t...
static const FileManager & Instance()
Get a const reference to the singleton instance of the FileManager.
std::string find_file(const std::string &base_name, const std::vector< std::string > &search_dirs) const
Searches for a file in the given directories.