SOFA API  c76874b7
Open source framework for multi-physics simuation
sofa::helper::system::FileSystem Class Reference

#include <FileSystem.h>

Contains functions to interact with the file system. More...

Detailed Description

Contains functions to interact with the file system.

Those functions are here only to avoid depending on an external library, and they provide only basic functionality. In particular, most of them were only written to work with absolute paths.

This set of functions is not meant to be complete, but it can be completed if need be.

Static Public Member Functions

static std::string getExtension (const std::string &filename)
 Get the extension of a file from an absolute path description. More...
 
static bool listDirectory (const std::string &directoryPath, std::vector< std::string > &outputFilenames)
 List the content of a directory. More...
 
static bool listDirectory (const std::string &directoryPath, std::vector< std::string > &outputFilenames, const std::string &extension)
 List the files in a directory which match the given extension. More...
 
static int findFiles (const std::string &directoryPath, std::vector< std::string > &outputFilePaths, const std::string &extension, const int depth=0)
 Find the files from a directory which match the given extension. More...
 
static bool createDirectory (const std::string &path)
 Create a directory. (This function accepts relative paths) More...
 
static bool removeDirectory (const std::string &path)
 Remove an empty directory. (This function accepts relative paths) More...
 
static bool removeAll (const std::string &path)
 Remove a non-empty directory. (This function accepts relative paths) More...
 
static std::string findOrCreateAValidPath (const std::string path)
 check that all element in the path exists or create them. (This function accepts relative paths) More...
 
static bool exists (const std::string &path)
 Return true if and only if the given file exists. More...
 
static bool isDirectory (const std::string &path)
 Return true if and only if the given file path corresponds to a directory. More...
 
static bool isAbsolute (const std::string &path)
 Return true if and only if the given file path is absolute. More...
 
static bool isFile (const std::string &path)
 Return true if and only if the given file path is an existing file. More...
 
static std::string convertBackSlashesToSlashes (const std::string &path)
 Replace backslashes with slashes. More...
 
static std::string convertSlashesToBackSlashes (const std::string &path)
 Replace slashes with backslashes. More...
 
static std::string removeExtraSlashes (const std::string &path)
 Replace consecutive occurrences of '/' with a single '/'. More...
 
static std::string removeExtraBackSlashes (const std::string &path)
 Replace consecutive occurrences of '\' with a single '\'. More...
 
static std::string cleanPath (const std::string &path, separator s=SLASH)
 Clean path (backslashes, extra slashes...) More...
 
static std::string getParentDirectory (const std::string &path)
 Strip the last component from a path. Return the path given in argument with its last non-slash component and trailing slashes removed, or "." if the path contains no slashes. E.g. /a/b/c –> /a/b. More...
 
static std::string stripDirectory (const std::string &path)
 Strip the directory components from a path. E.g. /a/b/c –> c. More...
 

Function details

std::string sofa::helper::system::FileSystem::cleanPath ( const std::string &  path,
separator  s = SLASH 
)
static

Clean path (backslashes, extra slashes...)

std::string sofa::helper::system::FileSystem::convertBackSlashesToSlashes ( const std::string &  path)
static

Replace backslashes with slashes.

std::string sofa::helper::system::FileSystem::convertSlashesToBackSlashes ( const std::string &  path)
static

Replace slashes with backslashes.

bool sofa::helper::system::FileSystem::createDirectory ( const std::string &  path)
static

Create a directory. (This function accepts relative paths)

On Unix platform, the directory is created with permissions 0755. On Windows, it is created with no special attributes.

Returns
true on error
bool sofa::helper::system::FileSystem::exists ( const std::string &  path)
static

Return true if and only if the given file exists.

int sofa::helper::system::FileSystem::findFiles ( const std::string &  directoryPath,
std::vector< std::string > &  outputFilePaths,
const std::string &  extension,
const int  depth = 0 
)
static

Find the files from a directory which match the given extension.

It pushes the files absolute paths in the vector provided in argument.

Warning
The directory must exist.
Returns
the number of files found or -1 as error
std::string sofa::helper::system::FileSystem::findOrCreateAValidPath ( const std::string  path)
static

check that all element in the path exists or create them. (This function accepts relative paths)

Returns
the valid path.
std::string sofa::helper::system::FileSystem::getExtension ( const std::string &  filename)
static

Get the extension of a file from an absolute path description.

Returns
i.e. if given"a/b/c.d", return "d"
std::string sofa::helper::system::FileSystem::getParentDirectory ( const std::string &  path)
static

Strip the last component from a path. Return the path given in argument with its last non-slash component and trailing slashes removed, or "." if the path contains no slashes. E.g. /a/b/c –> /a/b.

bool sofa::helper::system::FileSystem::isAbsolute ( const std::string &  path)
static

Return true if and only if the given file path is absolute.

bool sofa::helper::system::FileSystem::isDirectory ( const std::string &  path)
static

Return true if and only if the given file path corresponds to a directory.

Warning
The path must exist.
bool sofa::helper::system::FileSystem::isFile ( const std::string &  path)
static

Return true if and only if the given file path is an existing file.

bool sofa::helper::system::FileSystem::listDirectory ( const std::string &  directoryPath,
std::vector< std::string > &  outputFilenames 
)
static

List the content of a directory.

It pushes the filenames (not their absolute paths) in the vector provided in argument.

Warning
The directory must exist.
Returns
true on error
bool sofa::helper::system::FileSystem::listDirectory ( const std::string &  directoryPath,
std::vector< std::string > &  outputFilenames,
const std::string &  extension 
)
static

List the files in a directory which match the given extension.

It pushes the filenames (not their absolute paths) in the vector provided in argument.

Warning
The directory must exist.
Returns
true on error
bool sofa::helper::system::FileSystem::removeAll ( const std::string &  path)
static

Remove a non-empty directory. (This function accepts relative paths)

Returns
true on error
bool sofa::helper::system::FileSystem::removeDirectory ( const std::string &  path)
static

Remove an empty directory. (This function accepts relative paths)

Returns
true on error
std::string sofa::helper::system::FileSystem::removeExtraBackSlashes ( const std::string &  path)
static

Replace consecutive occurrences of '\' with a single '\'.

std::string sofa::helper::system::FileSystem::removeExtraSlashes ( const std::string &  path)
static

Replace consecutive occurrences of '/' with a single '/'.

std::string sofa::helper::system::FileSystem::stripDirectory ( const std::string &  path)
static

Strip the directory components from a path. E.g. /a/b/c –> c.

Enum details

Enumerator
SLASH 
BACKSLASH