SOFA API  3ea83cbd
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 bool removeFile (const std::string &path)
 
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...
 
static std::string append (const std::string_view &existingPath, const std::string_view &toAppend)
 
template<typename... Args>
static std::string append (const std::string_view &existingPath, const std::string_view &toAppend, Args &&... args)
 

Function details

◆ append() [1/2]

std::string sofa::helper::system::FileSystem::append ( const std::string_view &  existingPath,
const std::string_view &  toAppend 
)
static

Appends a string to an existing path, ensuring exactly one directory separator (/) between each part.

◆ append() [2/2]

template<typename... Args>
static std::string sofa::helper::system::FileSystem::append ( const std::string_view &  existingPath,
const std::string_view &  toAppend,
Args &&...  args 
)
inlinestatic

Appends strings to an existing path, ensuring exactly one directory separator (/) between each part.

◆ cleanPath()

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

Clean path (backslashes, extra slashes...)

◆ convertBackSlashesToSlashes()

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

Replace backslashes with slashes.

◆ convertSlashesToBackSlashes()

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

Replace slashes with backslashes.

◆ createDirectory()

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

◆ exists()

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

Return true if and only if the given file exists.

◆ findFiles()

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

◆ findOrCreateAValidPath()

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.

◆ getExtension()

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"

◆ getParentDirectory()

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.

◆ isAbsolute()

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

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

◆ isDirectory()

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.

◆ isFile()

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.

◆ listDirectory() [1/2]

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

◆ listDirectory() [2/2]

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

◆ removeAll()

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

◆ removeDirectory()

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

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

Returns
true on error

◆ removeExtraBackSlashes()

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

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

◆ removeExtraSlashes()

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

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

◆ removeFile()

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

◆ stripDirectory()

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

◆ separator

Enumerator
SLASH 
BACKSLASH