#include <FileRepository.h>
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... | |
|
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
|
protected |
Vector of paths.
|
inline |
Initialize the set of paths using the environment variable SOFA_DATA_PATH as default.
|
inline |
Initialize the set of paths using the environment variable specified by the parameter envVar.
|
inline |
Initialize the set of paths using the environment variable specified by the parameter envVar and the relative path specified by the parameter relativePath.
|
inline |
Initialize the set of paths using the environment variable specified by the parameter envVar and the relative paths specified by the parameter paths.
|
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.
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.
sofa::helper::system::FileRepository::~FileRepository | ( | ) |
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.
|
static |
Replaces every occurrences of "//" by "/"
void sofa::helper::system::FileRepository::clear | ( | ) |
Remove all known paths.
|
inline |
|
inlinestatic |
OS-dependant character separating 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.
basedir | override current directory (optional) |
filename | requested file as input, resolved file path as output |
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.
basefile | override current directory by using the parent directory of the given file |
filename | requested file as input, resolved file path as output |
|
staticprotected |
Search file in a given path.
|
inline |
|
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.
|
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.
|
static |
Returns a string such as refPath + string = path if path contains refPath. Otherwise returns path.
void sofa::helper::system::FileRepository::removePath | ( | const std::string & | path | ) |
Remove a path of the set of paths.
|
inline |
|
friend |
Display all current sofa search paths.