Chaste  Release::2017.1
FileFinder Class Reference

#include <FileFinder.hpp>

+ Inheritance diagram for FileFinder:
+ Collaboration diagram for FileFinder:

Public Member Functions

 FileFinder ()
 
 FileFinder (const std::string &rPath, RelativeTo::Value relativeTo)
 
 FileFinder (const std::string &rLeafName, const FileFinder &rParentOrSibling)
 
 FileFinder (const fs::path &rPath)
 
virtual ~FileFinder ()
 
virtual void SetPath (const std::string &rPath, RelativeTo::Value relativeTo)
 
virtual void SetPath (const std::string &rLeafName, const FileFinder &rParentOrSibling)
 
bool IsPathSet () const
 
bool Exists () const
 
bool IsFile () const
 
bool IsDir () const
 
bool IsEmpty () const
 
std::string GetAbsolutePath () const
 
bool IsNewerThan (const FileFinder &rOtherEntity) const
 
std::string GetLeafName () const
 
std::string GetLeafNameNoExtension () const
 
std::string GetExtension () const
 
FileFinder GetParent () const
 
std::string GetRelativePath (const FileFinder &rBasePath) const
 
FileFinder CopyTo (const FileFinder &rDest) const
 
void Remove () const
 
void DangerousRemove () const
 
std::vector< FileFinderFindMatches (const std::string &rPattern) const
 

Static Public Member Functions

static bool IsAbsolutePath (const std::string &rPath)
 
static void ReplaceSpacesWithUnderscores (std::string &rPath)
 
static void ReplaceUnderscoresWithSpaces (std::string &rPath)
 
static void FakePath (RelativeTo::Value fakeWhat, const std::string &rFakePath)
 
static void StopFaking ()
 

Private Member Functions

void PrivateRemove (bool dangerous=false) const
 

Private Attributes

std::string mAbsPath
 

Static Private Attributes

static bool msFaking = false
 
static RelativeTo::Value msFakeWhat = RelativeTo::Absolute
 
static std::string msFakePath = ""
 

Detailed Description

A helper class for finding files or directories, given paths which can be relative to various locations (e.g. the Chaste source tree root, the current directory, the Chaste test output directory, or an absolute path).

Definition at line 69 of file FileFinder.hpp.

Constructor & Destructor Documentation

FileFinder::FileFinder ( )

Default constructor for subclasses to use. They should call SetPath() in their constructor.

This also allows classes to store a FileFinder instance that hasn't been properly set up yet, and assign to it later using operator=.

Definition at line 75 of file FileFinder.cpp.

Referenced by CopyTo(), FindMatches(), GetParent(), and PrivateRemove().

FileFinder::FileFinder ( const std::string &  rPath,
RelativeTo::Value  relativeTo 
)

Main constructor.

Parameters
rPaththe path to the file/dir to find
relativeTohow to interpret this path

Definition at line 80 of file FileFinder.cpp.

References SetPath().

FileFinder::FileFinder ( const std::string &  rLeafName,
const FileFinder rParentOrSibling 
)

Find a file (or folder) relative to some file or directory. If the second argument is a directory, we look for the given leaf name within it. If the second argument is a file, then we look for a sibling. An exception is raised if rParentOrSibling does not exist.

Parameters
rLeafNamethe leaf name of the file/dir to find
rParentOrSiblingwhere to look for it

Definition at line 85 of file FileFinder.cpp.

References SetPath().

FileFinder::FileFinder ( const fs::path &  rPath)

Conversion constructor from a Boost Filesystem path object. Note that since fs::path has a conversion constructor from std::string, this allows us to be initialised with a string or character constant, too. The path will be interpreted as relative to the current working directory, unless it is an absolute path.

Parameters
rPaththe path to the file/dir to find

Definition at line 90 of file FileFinder.cpp.

References RelativeTo::Absolute, and SetPath().

FileFinder::~FileFinder ( )
virtual

Needed because we have virtual methods.

Definition at line 95 of file FileFinder.cpp.

Member Function Documentation

FileFinder FileFinder::CopyTo ( const FileFinder rDest) const

Copy this file or folder (recursively in the latter case) to the given destination. If the destination is a folder that exists, the source will be copied with the same name inside that folder. Otherwise the source will be copied with the given destination name.

If the source is a file and the destination is a file that exists it will be removed prior to copying. If the source is a folder and the destination is a file that exists then an error is thrown.

Parameters
rDestwhere to copy to
Returns
a finder for the copied entity

Definition at line 308 of file FileFinder.cpp.

References EXCEPTION, Exists(), FileFinder(), IsDir(), IsFile(), and mAbsPath.

Referenced by CellMLToSharedLibraryConverter::ConvertCellmlToSo(), HeartConfig::CopySchema(), CardiacSimulation::CreateAndRun(), AbstractCardiacTissue< SPACE_DIM >::save(), and AbstractTetrahedralMesh< SPACE_DIM, SPACE_DIM >::save().

void FileFinder::DangerousRemove ( ) const

This method will allow you to remove any file from under either * CHASTE_TEST_OUTPUT or * the source tree (but not elsewhere).

For this reason it is a very dangerous operation and should not be used if Remove could be instead.

BEWARE: if you have managed to set CHASTE_TEST_OUTPUT to "/" this could wipe your system!

Definition at line 426 of file FileFinder.cpp.

References PrivateRemove().

Referenced by CellMLToSharedLibraryConverter::ConvertCellmlToSo().

void FileFinder::FakePath ( RelativeTo::Value  fakeWhat,
const std::string &  rFakePath 
)
static

For testing purposes, fake the value of one of the normally fixed paths, e.g. ChasteSourceRoot.

Parameters
fakeWhatwhich path to fake
rFakePathits fake value

Definition at line 553 of file FileFinder.cpp.

References msFakePath, msFakeWhat, and msFaking.

std::vector< FileFinder > FileFinder::FindMatches ( const std::string &  rPattern) const
Returns
a list of files in this folder matching a simple glob pattern. This method must be called on a FileFinder that points at a folder, and the pattern will be matched against file (or folder) names in that folder. The pattern can use a subset of shell-style glob syntax. A '?' anywhere in the string matches any single character at that position. A '*' may be used at the start or end of the string to match any number of leading or trailing characters, respectively. Hidden files (names starting with a '.') will never be matched.
Parameters
rPatternthe pattern to match names against

Definition at line 432 of file FileFinder.cpp.

References EXCEPTION, FileFinder(), IsDir(), mAbsPath, and PATH_LEAF_NAME.

Referenced by CellMLToSharedLibraryConverter::ConvertCellmlToSo(), and AbstractTetrahedralMesh< SPACE_DIM, SPACE_DIM >::save().

std::string FileFinder::GetExtension ( ) const
Returns
the extension of the leaf name of this file or directory, if any. The '.' will be included in the extension if an extension exists.

Definition at line 247 of file FileFinder.cpp.

References mAbsPath.

Referenced by AbstractTetrahedralMesh< SPACE_DIM, SPACE_DIM >::save().

std::string FileFinder::GetLeafName ( ) const
Returns
the leaf name of this file or directory.

i.e. the individual file or directory name and none of the preceding folders on its path.

Definition at line 237 of file FileFinder.cpp.

References mAbsPath, and PATH_LEAF_NAME.

Referenced by CellMLLoader::LoadCellMLFile().

std::string FileFinder::GetLeafNameNoExtension ( ) const
Returns
the leaf name of this file or directory, with any file extension removed.

i.e. the individual file or directory name and none of the preceding folders on its path.

Definition at line 242 of file FileFinder.cpp.

References mAbsPath.

Referenced by FibreConverter::Convert(), CellMLToSharedLibraryConverter::ConvertCellmlToSo(), CellMLLoader::LoadCellMLFile(), and AbstractTetrahedralMesh< SPACE_DIM, SPACE_DIM >::save().

FileFinder FileFinder::GetParent ( ) const
Returns
a finder for the folder containing this file or directory.

Definition at line 252 of file FileFinder.cpp.

References RelativeTo::Absolute, EXCEPT_IF_NOT, FileFinder(), and mAbsPath.

Referenced by FibreConverter::Convert(), CellMLToSharedLibraryConverter::ConvertCellmlToSo(), AbstractTetrahedralMesh< SPACE_DIM, SPACE_DIM >::save(), and SetPath().

std::string FileFinder::GetRelativePath ( const FileFinder rBasePath) const
Returns
the relative path to this finder from another. Throws if this is not found under rBasePath.
Parameters
rBasePathwhere the returned path should be relative to

Definition at line 261 of file FileFinder.cpp.

References EXCEPT_IF_NOT, EXCEPTION, and GetAbsolutePath().

Referenced by OutputFileHandler::GetRelativePath(), Hdf5ToMeshalyzerConverter< ELEMENT_DIM, SPACE_DIM >::Hdf5ToMeshalyzerConverter(), Hdf5ToVtkConverter< ELEMENT_DIM, SPACE_DIM >::Hdf5ToVtkConverter(), OutputFileHandler::OutputFileHandler(), and PostProcessingWriter< ELEMENT_DIM, SPACE_DIM >::WriteOutputDataToHdf5().

bool FileFinder::IsAbsolutePath ( const std::string &  rPath)
static
bool FileFinder::IsEmpty ( ) const
Returns
true if this is a file of size zero or if this is a folder, whether it contains no non-hidden items. If this doesn't exist, throws.

Definition at line 195 of file FileFinder.cpp.

References EXCEPTION, IsDir(), IsFile(), mAbsPath, and PATH_LEAF_NAME.

bool FileFinder::IsFile ( ) const
Returns
true if we are pointing at a file

Definition at line 185 of file FileFinder.cpp.

References mAbsPath.

Referenced by OutputFileHandler::CopyFileTo(), CopyTo(), Hdf5ToMeshalyzerConverter< ELEMENT_DIM, SPACE_DIM >::Hdf5ToMeshalyzerConverter(), IsEmpty(), and PrivateRemove().

bool FileFinder::IsNewerThan ( const FileFinder rOtherEntity) const
Returns
true if this file/dir is newer than another file/dir. Compares modification times.
Parameters
rOtherEntitythe entity to test against.

Definition at line 230 of file FileFinder.cpp.

References Exists(), and mAbsPath.

Referenced by CellMLToSharedLibraryConverter::Convert().

void FileFinder::PrivateRemove ( bool  dangerous = false) const
private

This is code common to Remove() and DangerousRemove(). Should remain private and not to be called from elsewhere. Remove() is only allowed to delete things with a .chaste_deletable_folder in the testoutput directory.

DangerousRemove() is allowed to delete anything in the source or testoutput directories.

Parameters
dangerouswhether we are doing a dangerous remove.

Definition at line 357 of file FileFinder.cpp.

References RelativeTo::ChasteBuildRoot, RelativeTo::ChasteSourceRoot, EXCEPTION, Exists(), FileFinder(), GetAbsolutePath(), OutputFileHandler::GetChasteTestOutputDirectory(), IsFile(), mAbsPath, OutputFileHandler::SIG_FILE_NAME, and ChastePosixPathFixer::ToPosix().

Referenced by DangerousRemove(), and Remove().

void FileFinder::Remove ( ) const

Recursively remove this file or folder. Since this is a potentially very dangerous operation, only locations under the Chaste test output folder may be removed.

Only folders created by an OutputFileHandler, or the contents of such a folder, may be deleted (folders that have .chaste_deletable_folder present).

If you need to delete a file or folder without .chaste_deletable_folder, then you have to use DangerousRemove().

Definition at line 421 of file FileFinder.cpp.

References PrivateRemove().

void FileFinder::ReplaceSpacesWithUnderscores ( std::string &  rPath)
static

Replace any spaces in a path or filename with underscores.

Parameters
rPatha path or file name

Definition at line 531 of file FileFinder.cpp.

void FileFinder::ReplaceUnderscoresWithSpaces ( std::string &  rPath)
static

Replace any underscores in a path or filename with spaces (for making titles etc.).

Parameters
rPatha path or file name

Definition at line 542 of file FileFinder.cpp.

void FileFinder::SetPath ( const std::string &  rLeafName,
const FileFinder rParentOrSibling 
)
virtual

Change this FileFinder to point at a new location, relative to some file or directory.

Parameters
rLeafNamethe leaf name of the file/dir to find
rParentOrSiblingwhere to look for it

Definition at line 159 of file FileFinder.cpp.

References RelativeTo::Absolute, EXCEPTION, Exists(), GetAbsolutePath(), GetParent(), IsDir(), and SetPath().

void FileFinder::StopFaking ( )
static

Stop faking one of the fixed paths.

Definition at line 560 of file FileFinder.cpp.

References msFaking.

Member Data Documentation

std::string FileFinder::mAbsPath
private
std::string FileFinder::msFakePath = ""
staticprivate

The fake value of the faked path.

Definition at line 306 of file FileFinder.hpp.

Referenced by FakePath(), and SetPath().

RelativeTo::Value FileFinder::msFakeWhat = RelativeTo::Absolute
staticprivate

Which path to fake.

Definition at line 303 of file FileFinder.hpp.

Referenced by FakePath(), and SetPath().

bool FileFinder::msFaking = false
staticprivate

Whether to fake one of the fixed paths, e.g. ChasteSourceRoot.

Definition at line 300 of file FileFinder.hpp.

Referenced by FakePath(), SetPath(), and StopFaking().


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