SOFA API  ca665544
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 ()
 
 FileRepository (const char *envVar)
 
 FileRepository (const char *envVar, const char *relativePath)
 
 FileRepository (const char *envVar, const std::vector< std::string > &paths)
 
 FileRepository (const char *envVar, const char *relativePath, const fileKeysMap &iniFilesAndKeys)
 
 FileRepository (const char *envVar, const std::vector< std::string > &paths, const fileKeysMap &iniFilesAndKeys)
 
 ~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
 
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)
 
static char entrySeparator ()
 OS-dependant character separating 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

◆ directAccessProtocolPrefix

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

◆ vpath

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

Vector of paths.

Constructor details

◆ FileRepository() [1/6]

sofa::helper::system::FileRepository::FileRepository ( )
inline

Initialize the set of paths using the environment variable SOFA_DATA_PATH as default.

◆ FileRepository() [2/6]

sofa::helper::system::FileRepository::FileRepository ( const char envVar)
inline

Initialize the set of paths using the environment variable specified by the parameter envVar.

◆ FileRepository() [3/6]

sofa::helper::system::FileRepository::FileRepository ( const char envVar,
const char relativePath 
)
inline

Initialize the set of paths using the environment variable specified by the parameter envVar and the relative path specified by the parameter relativePath.

◆ FileRepository() [4/6]

sofa::helper::system::FileRepository::FileRepository ( const char envVar,
const std::vector< std::string > &  paths 
)
inline

Initialize the set of paths using the environment variable specified by the parameter envVar and the relative paths specified by the parameter paths.

◆ FileRepository() [5/6]

sofa::helper::system::FileRepository::FileRepository ( const char envVar,
const char relativePath,
const fileKeysMap iniFilesAndKeys 
)
inline

Initialize the set of paths using the environment variable specified by the parameter envVar, the relative path specified by the parameter relativePath and the ini files and respective keys specified by the parameter iniFilesAndKeys.

◆ FileRepository() [6/6]

sofa::helper::system::FileRepository::FileRepository ( const char envVar,
const std::vector< std::string > &  paths,
const fileKeysMap iniFilesAndKeys 
)

Initialize the set of paths using the environment variable specified by the parameter envVar, the relative paths specified by the parameter paths and the ini files and respective keys specified by the parameter iniFilesAndKeys.

◆ ~FileRepository()

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

Function details

◆ addFirstPath()

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

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

◆ addLastPath()

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

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

◆ cleanPath()

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

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

◆ clear()

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

Remove all known paths.

◆ displayPaths()

void sofa::helper::system::FileRepository::displayPaths ( ) const
inline

◆ entrySeparator()

static char sofa::helper::system::FileRepository::entrySeparator ( )
inlinestatic

OS-dependant character separating entries in list of paths.

◆ findFile()

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

◆ findFileFromFile()

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

◆ findFileIn()

bool sofa::helper::system::FileRepository::findFileIn ( std::string &  filename,
const std::string &  path 
)
staticprotected

Search file in a given path.

◆ getDirectAccessProtocolPrefix()

const std::string& sofa::helper::system::FileRepository::getDirectAccessProtocolPrefix ( ) const
inline

◆ getFile()

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

◆ getFirstPath()

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

Get the first path into the set of paths.

◆ getPaths()

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

◆ getPathsJoined()

const std::string sofa::helper::system::FileRepository::getPathsJoined ( )

◆ getTempPath()

const std::string sofa::helper::system::FileRepository::getTempPath ( ) const

◆ print()

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

Print the list of path to std::cout.

◆ relativeToPath()

std::string sofa::helper::system::FileRepository::relativeToPath ( std::string  path,
std::string  refPath 
)
static

Returns a string such as refPath + string = path if path contains refPath. Otherwise returns path.

◆ removePath()

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

Remove a path of the set of paths.

◆ setDirectAccessProtocolPrefix()

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

Related details

◆ operator<<

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

Display all current sofa search paths.