SOFA API  014b24ea
Open source framework for multi-physics simuation
sofa::helper::system::FileRepository Class Reference

#include <FileRepository.h>

Detailed Description

Helper class to find files in a list of directories.

Each file is searched as follow:

1: Using the specified filename in current directory, or in the specified directory. If the filename does not start with "/", "./", or "../" : 2: In the directory path specified using addFirstPath method. 3: In the directory path specified using an environment variable (default to SOFA_DATA_PATH). 4: In the default directories relative to the main executable (default to ../share). 5: In the directory path specified using addLastPath method.

For file name starting with '/', './' or '../' only the first step is used.

A path is considered as a concatenation of directories separated by : on linux / mac and ; on windows

Protected Attributes

std::string directAccessProtocolPrefix
 
std::vector< std::string > vpath
 Vector of paths. More...
 

Public Member Functions

 FileRepository (const char *envVar="SOFA_DATA_PATH", const char *relativePath=nullptr, const fileKeysMap &iniFilesAndKeys={})
 Initialize the set of paths from an environment variable. More...
 
 ~FileRepository ()
 
void addFirstPath (const std::string &path)
 Adds a path to the front of the set of paths. More...
 
void addLastPath (const std::string &path)
 Adds a path to the back of the set of paths. More...
 
void removePath (const std::string &path)
 Remove a path of the set of paths. More...
 
void clear ()
 Remove all known paths. More...
 
std::string getFirstPath ()
 Get the first path into the set of paths. More...
 
const std::vector< std::string > & getPaths () const
 
const std::string getPathsJoined ()
 
const std::string & getDirectAccessProtocolPrefix () const
 
void setDirectAccessProtocolPrefix (const std::string &protocolPrefix)
 
bool findFile (std::string &filename, const std::string &basedir="", std::ostream *errlog=&std::cerr)
 
std::string getFile (std::string filename, const std::string &basedir="", std::ostream *errlog=&std::cerr)
 
bool findFileFromFile (std::string &filename, const std::string &basefile, std::ostream *errlog=&std::cerr)
 
void print ()
 Print the list of path to std::cout. More...
 
void displayPaths ()
 
const std::string getTempPath () const
 

Static Public Member Functions

static std::string cleanPath (const std::string &path)
 
static std::string relativeToPath (std::string path, std::string refPath, bool doLowerCaseOnWin32=true)
 
static char entrySeparator ()
 OS-dependant character separing entries in list of paths. More...
 

Static Protected Member Functions

static bool findFileIn (std::string &filename, const std::string &path)
 Search file in a given path. More...
 

Friends

std::ostream & operator<< (std::ostream &_flux, FileRepository _fr)
 Display all current sofa search paths. More...
 

Attribute details

std::string sofa::helper::system::FileRepository::directAccessProtocolPrefix
protected

A protocol like http: or file: which will bypass the file search if found in the filename of the findFile* functions that directly returns the path as if the function succeeded Use case: add the prefix ram: as the direct protocol, this way the FileRepository will not try to look for the file on the hard disk and will directly return then the inherited FileAccess singleton enhanced with the capacity to find ram file will deliver a correct stream to this in-ram virtual file

std::vector<std::string> sofa::helper::system::FileRepository::vpath
protected

Vector of paths.

Constructor details

sofa::helper::system::FileRepository::FileRepository ( const char *  envVar = "SOFA_DATA_PATH",
const char *  relativePath = nullptr,
const fileKeysMap iniFilesAndKeys = {} 
)

Initialize the set of paths from an environment variable.

sofa::helper::system::FileRepository::~FileRepository ( )

Function details

void sofa::helper::system::FileRepository::addFirstPath ( const std::string &  path)

Adds a path to the front of the set of paths.

void sofa::helper::system::FileRepository::addLastPath ( const std::string &  path)

Adds a path to the back of the set of paths.

std::string sofa::helper::system::FileRepository::cleanPath ( const std::string &  path)
static

Replaces every occurrences of "//" by "/"

void sofa::helper::system::FileRepository::clear ( )

Remove all known paths.

void sofa::helper::system::FileRepository::displayPaths ( )
inline
static char sofa::helper::system::FileRepository::entrySeparator ( )
inlinestatic

OS-dependant character separing entries in list of paths.

bool sofa::helper::system::FileRepository::findFile ( std::string &  filename,
const std::string &  basedir = "",
std::ostream *  errlog = &std::cerr 
)

Find file using the stored set of paths.

Parameters
basediroverride current directory (optional)
filenamerequested file as input, resolved file path as output
Returns
true if the file was found in one of the directories, false otherwise
bool sofa::helper::system::FileRepository::findFileFromFile ( std::string &  filename,
const std::string &  basefile,
std::ostream *  errlog = &std::cerr 
)

Find file using the stored set of paths.

Parameters
basefileoverride current directory by using the parent directory of the given file
filenamerequested file as input, resolved file path as output
Returns
true if the file was found in one of the directories, false otherwise
bool sofa::helper::system::FileRepository::findFileIn ( std::string &  filename,
const std::string &  path 
)
staticprotected

Search file in a given path.

const std::string& sofa::helper::system::FileRepository::getDirectAccessProtocolPrefix ( ) const
inline
std::string sofa::helper::system::FileRepository::getFile ( std::string  filename,
const std::string &  basedir = "",
std::ostream *  errlog = &std::cerr 
)
inline

Alias for findFile, but returning the resolved file as the result. Less informative for errors, but sometimes easier to use

std::string sofa::helper::system::FileRepository::getFirstPath ( )

Get the first path into the set of paths.

const std::vector< std::string >& sofa::helper::system::FileRepository::getPaths ( ) const
inline
const std::string sofa::helper::system::FileRepository::getPathsJoined ( )
const std::string sofa::helper::system::FileRepository::getTempPath ( ) const
void sofa::helper::system::FileRepository::print ( )

Print the list of path to std::cout.

std::string sofa::helper::system::FileRepository::relativeToPath ( std::string  path,
std::string  refPath,
bool  doLowerCaseOnWin32 = true 
)
static

Returns a string such as refPath + string = path if path contains refPath. Otherwise returns path. On WIN32 the implementation was also returning the path in lower case. This behavior is now deprecated and should be remove the 2018-05-01. Until this date new implementation can be used by setting doLowerCaseOnWin32=false;

void sofa::helper::system::FileRepository::removePath ( const std::string &  path)

Remove a path of the set of paths.

void sofa::helper::system::FileRepository::setDirectAccessProtocolPrefix ( const std::string &  protocolPrefix)
inline

Related details

std::ostream& operator<< ( std::ostream &  _flux,
FileRepository  _fr 
)
friend

Display all current sofa search paths.