MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
marley::FileManager Class Reference

Singleton class that handles file searches. More...

#include <FileManager.hh>

Public Member Functions

 FileManager (const FileManager &)=delete
 Deleted copy constructor.
 
 FileManager (FileManager &&)=delete
 Deleted move constructor.
 
std::string find_file (const std::string &base_name, const std::string &search_path=marley::FileManager::default_search_path_) const
 Searches for a file in the given search path.
 
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.
 
std::vector< std::string > list_all_files (const std::string &search_path=FileManager::default_search_path_) const
 Get a vector of full paths for all regular files in the requested search path.
 
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.
 
std::string marley_dir () const
 Returns the path to the root MARLEY folder.
 
FileManageroperator= (const FileManager &)=delete
 Deleted copy assignment operator.
 
FileManageroperator= (FileManager &&)=delete
 Deleted move assignment operator.
 

Static Public Member Functions

static const FileManagerInstance ()
 Get a const reference to the singleton instance of the FileManager.
 
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.
 

Protected Member Functions

 FileManager ()
 Create the singleton FileManager object.
 

Static Protected Member Functions

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 through a single directory (non-recursively)
 

Protected Attributes

std::string marley_dir_
 Stores the value of the MARLEY environment variable (which points to the root folder of the source code distribution)
 

Static Protected Attributes

static std::string default_search_path_
 By default, use this search path when looking for files.
 

Detailed Description

Singleton class that handles file searches.

Definition at line 25 of file FileManager.hh.

Constructor & Destructor Documentation

◆ FileManager()

marley::FileManager::FileManager ( )
protected

Create the singleton FileManager object.

Definition at line 44 of file FileManager.cc.

44 {
45
46 // Check whether the MARLEY environment variable is set
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"
50 " try again.");
51
52 marley_dir_ = std::string( mar );
53
54 // If the MARLEY_SEARCH_PATH environment variable is set, use that
55 // instead of the default search path
56 char* msp = std::getenv("MARLEY_SEARCH_PATH");
57 if ( msp ) default_search_path_ = std::string( msp );
58 else {
60 default_search_path_ += ':' + marley_dir_ + "/data/react";
61 default_search_path_ += ':' + marley_dir_ + "/data/optical_model";
62 default_search_path_ += ':' + marley_dir_ + "/data/structure";
63 }
64
65 MARLEY_LOG( DEBUG, "io" ) << "MARLEY search path set to \""
66 << default_search_path_ << '\"';
67}
std::string marley_dir_
Stores the value of the MARLEY environment variable (which points to the root folder of the source co...
static std::string default_search_path_
By default, use this search path when looking for files.

References default_search_path_, and marley_dir_.

Member Function Documentation

◆ dir_iterate()

bool marley::FileManager::dir_iterate ( const std::string & dir_name,
const std::function< bool(const std::string &, const std::string &)> & func )
staticprotected

Helper function that executes a std::function on the name of each regular file found while scanning through a single directory (non-recursively)

Parameters
dir_nameThe name of the directory to be scanned
funcThe function to execute. The first argument is the full file name (including the path). The second is the base name of the file (no path). The return value should be true if no further iterations are needed (e.g., because a file matching a desired name was found) and false otherwise.
Returns
True if the iterations were stopped early (due to function returning true) or false if they were completed

Definition at line 175 of file FileManager.cc.

177{
178 std::error_code ec;
179 std::filesystem::directory_iterator it(dir_name,
180 std::filesystem::directory_options::skip_permission_denied, ec);
181
182 if (ec) {
183 MARLEY_LOG(WARN, "io") << "Could not read from the directory \""
184 << dir_name << '\"';
185 return false;
186 }
187
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();
194
195 MARLEY_LOG(TRACE, "io") << "marley::FileManager found file \""
196 << full_file_name << '\"';
197
198 if (entry.is_regular_file()) {
199 stop_iterations = func(full_file_name, base_name);
200 }
201
202 it.increment(ec);
203 if (ec) {
204 MARLEY_LOG(DEBUG, "io") << "Couldn't access a file in directory \""
205 << dir_name << '\"';
206 ec.clear();
207 }
208 }
209
210 return stop_iterations;
211}

Referenced by find_file(), and list_all_files().

◆ find_file() [1/2]

std::string marley::FileManager::find_file ( const std::string & base_name,
const std::string & search_path = marley::FileManager::default_search_path_ ) const

Searches for a file in the given search path.

The search is non-recursive, i.e., no subdirectories are included in the search

Parameters
base_nameThe base name (file name without any path) of the desired file
search_pathA string containing a ':'-delimited list of directories that should be searched for the file
Returns
The full path to the file, or an empty string if no match could be found

Definition at line 88 of file FileManager.cc.

90{
91 auto search_dirs = search_path_to_dir_vector( search_path );
92 return find_file(base_name, search_dirs);
93}
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.
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.

References find_file(), and search_path_to_dir_vector().

◆ find_file() [2/2]

std::string marley::FileManager::find_file ( const std::string & base_name,
const std::vector< std::string > & search_dirs ) const

Searches for a file in the given directories.

The search is non-recursive, i.e., no subdirectories are included in the search. If base_name contains directory separators, it is treated as a relative path and resolved against each search directory.

Parameters
base_nameThe base name (file name without any path) of the desired file, or a relative path containing directory separators
search_dirsA vector of strings specifying the directories that should be searched for the file
Returns
The full path to the file, or an empty string if no match could be found

Definition at line 95 of file FileManager.cc.

97{
98 std::string full_path_to_file;
99
100 // First check if we can find the file just using base_name. We can
101 // avoid the search if it's already accessible (e.g., because the
102 // full path was supplied instead of the base name)
103 if ( file_exists(base_name) ) return base_name;
104
105 // If the file name contains directory separators, try resolving
106 // the relative path against each search directory
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;
111 }
112 }
113
114 // Search the directories in the order listed. In each directory,
115 // compare all regular file names to the given base name. If a match
116 // is found, stop the search and store the result in full_path_to_file.
117 // If no match is found, full_path_to_file will remain empty.
118 for (const auto& dir : search_dirs) {
119 bool file_found = dir_iterate(dir,
120 [&full_path_to_file, &base_name](const std::string& other_full_path,
121 const std::string& other_base_name) -> bool
122 {
123 if ( base_name == other_base_name ) {
124 // We've found the desired file, so store its full path
125 full_path_to_file = other_full_path;
126 // Signal that we don't need to continue the search any longer
127 return true;
128 }
129 return false;
130 }
131 );
132
133 if ( file_found ) break;
134 }
135
136 return full_path_to_file;
137}
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...

References dir_iterate().

Referenced by marley::StrengthVariationWeightCalculator::create_instances(), and find_file().

◆ Instance()

const marley::FileManager & marley::FileManager::Instance ( )
static

Get a const reference to the singleton instance of the FileManager.

Definition at line 69 of file FileManager.cc.

69 {
70
71 // Create the mass table using a static variable. This ensures
72 // that the singleton instance is only created once.
73 static std::unique_ptr<marley::FileManager>
74 the_instance(new marley::FileManager());
75
76 // Return a reference to the singleton instance
77 return *the_instance;
78}

Referenced by marley::MassTable::MassTable(), marley::StrengthVariationWeightCalculator::create_instances(), marley::EventFileReader::deduce_file_format(), marley::StructureDatabase::get_decay_scheme(), and marley::StructureDatabase::load_optical_model_params().

◆ list_all_files() [1/2]

std::vector< std::string > marley::FileManager::list_all_files ( const std::string & search_path = FileManager::default_search_path_) const

Get a vector of full paths for all regular files in the requested search path.

Files in subdirectories are not included (the search is non-recursive)

Parameters
search_pathA string containing a ':'-delimited list of directories that should be scanned for files
Returns
A vector of strings giving the full path to each regular file that was found

Definition at line 139 of file FileManager.cc.

141{
142 auto search_dirs = search_path_to_dir_vector( search_path );
143 return list_all_files( search_dirs );
144}
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.

References list_all_files(), and search_path_to_dir_vector().

◆ list_all_files() [2/2]

std::vector< std::string > marley::FileManager::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.

Files in subdirectories are not included (the search is non-recursive)

Parameters
search_dirsA vector of strings specifying the directories that should be scanned
Returns
A vector of strings giving the full path to each regular file that was found

Definition at line 148 of file FileManager.cc.

150{
151 std::vector<std::string> result;
152
153 for (const auto& dir : search_dirs) {
154 dir_iterate(dir,
155 [&result](const std::string& full_path,
156 const std::string& /*base_name*/) -> bool
157 {
158 // Add the current file to the vector of full paths
159 result.push_back( full_path );
160 // Signal that the directory scan should continue
161 return false;
162 }
163 );
164 }
165
166 return result;
167}

References dir_iterate().

Referenced by list_all_files().

◆ marley_dir()

std::string marley::FileManager::marley_dir ( ) const
inline

Returns the path to the root MARLEY folder.

Definition at line 106 of file FileManager.hh.

106{ return marley_dir_; }

References marley_dir_.

◆ search_path_to_dir_vector()

std::vector< std::string > marley::FileManager::search_path_to_dir_vector ( const std::string & search_path = FileManager::default_search_path_)
static

Converts a ':'-delimited search path (given as a single string) into a vector of directory names.

Parameters
search_pathThe search path to convert that should be searched for the file
Returns
A vector of directory names corresponding to those given in the search path

Definition at line 80 of file FileManager.cc.

82{
83 std::vector<std::string> dir_names
84 = marley_utils::split_string(search_path, SEARCH_PATH_DELIMITER);
85 return dir_names;
86}

Referenced by find_file(), and list_all_files().

Member Data Documentation

◆ default_search_path_

std::string marley::FileManager::default_search_path_
staticprotected

By default, use this search path when looking for files.

Definition at line 128 of file FileManager.hh.

Referenced by FileManager(), and operator=().

◆ marley_dir_

std::string marley::FileManager::marley_dir_
protected

Stores the value of the MARLEY environment variable (which points to the root folder of the source code distribution)

Definition at line 132 of file FileManager.hh.

Referenced by FileManager(), and marley_dir().


The documentation for this class was generated from the following files: