Inviwo 0.9.12-pre
Inviwo documentation
Loading...
Searching...
No Matches
inviwo::filesystem Namespace Reference

Enumerations

enum class  ListMode { Files , Directories , FilesAndDirectories }
 

Functions

IVW_CORE_API FILE * fopen (const std::filesystem::path &filename, const char *mode)
 
IVW_CORE_API std::fstream fstream (const std::filesystem::path &filename, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
 
IVW_CORE_API std::ifstream ifstream (const std::filesystem::path &filename, std::ios_base::openmode mode=std::ios_base::in)
 
IVW_CORE_API std::ofstream ofstream (const std::filesystem::path &filename, std::ios_base::openmode mode=std::ios_base::out)
 
IVW_CORE_API bool skipByteOrderMark (std::istream &stream)
 
IVW_CORE_API std::filesystem::path getWorkingDirectory ()
 
IVW_CORE_API void setWorkingDirectory (const std::filesystem::path &path)
 
IVW_CORE_API std::filesystem::path getExecutablePath ()
 
IVW_CORE_API std::filesystem::path getInviwoBinDir ()
 
IVW_CORE_API std::vector< std::filesystem::path > getLoadedLibraries ()
 
IVW_CORE_API int getCurrentProcessId ()
 
IVW_CORE_API std::filesystem::path getInviwoUserSettingsPath ()
 
IVW_CORE_API bool fileExists (const std::filesystem::path &filePath)
 Check if a file exists.
 
IVW_CORE_API bool directoryExists (const std::filesystem::path &path)
 
IVW_CORE_API std::time_t fileModificationTime (const std::filesystem::path &filePath)
 Get last time file was modified. Error can occur if the file does not exist for example.
 
IVW_CORE_API bool copyFile (const std::filesystem::path &src, const std::filesystem::path &dst)
 Copy an existing file to a new file. Overwrites existing file.
 
IVW_CORE_API std::vector< std::filesystem::path > getDirectoryContents (const std::filesystem::path &path, ListMode mode=ListMode::Files)
 
IVW_CORE_API std::vector< std::filesystem::path > getDirectoryContentsRecursively (const std::filesystem::path &path, ListMode mode=ListMode::Files)
 
IVW_CORE_API bool wildcardStringMatch (const std::string &pattern, const std::string &str)
 
IVW_CORE_API bool wildcardStringMatchDigits (const std::string &pattern, const std::string &str, int &index, bool matchLess=false, bool matchMore=true)
 
IVW_CORE_API std::optional< std::filesystem::path > getParentFolderWithChildren (const std::filesystem::path &path, std::span< const std::filesystem::path > childFolders)
 
IVW_CORE_API const std::filesystem::path & findBasePath ()
 
IVW_CORE_API std::filesystem::path getPath (PathType pathType, const std::string &suffix="", const bool createFolder=false)
 
IVW_CORE_API void createDirectoryRecursively (const std::filesystem::path &path)
 
IVW_CORE_API std::filesystem::path addBasePath (const std::filesystem::path &url)
 Adds the InviwoApplication base path before the url.
 
IVW_CORE_API std::filesystem::path getFileDirectory (const std::filesystem::path &url)
 
IVW_CORE_API std::filesystem::path getFileNameWithExtension (const std::filesystem::path &url)
 
IVW_CORE_API std::filesystem::path getFileNameWithoutExtension (const std::filesystem::path &url)
 
IVW_CORE_API std::string getFileExtension (const std::filesystem::path &url)
 Returns the characters after last dot (.).
 
IVW_CORE_API std::filesystem::path replaceFileExtension (const std::filesystem::path &url, std::string_view newFileExtension)
 
IVW_CORE_API std::filesystem::path getRelativePath (const std::filesystem::path &basePath, const std::filesystem::path &absolutePath)
 Make a path relative to basePath. Requirement: basePath and absulutePath has to be absolute paths. basePath should point at directory.
 
IVW_CORE_API std::filesystem::path getCanonicalPath (const std::filesystem::path &url)
 
IVW_CORE_API bool isAbsolutePath (const std::filesystem::path &path)
 
IVW_CORE_API bool sameDrive (const std::filesystem::path &refPath, const std::filesystem::path &queryPath)
 Checks whether the second path is on the same drive as the first path.
 
IVW_CORE_API std::filesystem::path cleanupPath (std::string_view path)
 clean up path by replacing backslashes with forward slash and removing surrounding quotes
 

Detailed Description

filesystem

Function Documentation

◆ addBasePath()

IVW_CORE_API std::filesystem::path inviwo::filesystem::addBasePath ( const std::filesystem::path & url)

Adds the InviwoApplication base path before the url.

See also
InviwoApplication::getBasePath
Parameters
urlRelative path
Returns
InviwoApplication base path + url

◆ cleanupPath()

IVW_CORE_API std::filesystem::path inviwo::filesystem::cleanupPath ( std::string_view path)

clean up path by replacing backslashes with forward slash and removing surrounding quotes

Parameters
pathgiven path to be cleaned up
Returns
non-quoted path containing no backslashes as directory separators

◆ copyFile()

IVW_CORE_API bool inviwo::filesystem::copyFile ( const std::filesystem::path & src,
const std::filesystem::path & dst )

Copy an existing file to a new file. Overwrites existing file.

Parameters
srcPath to the file to the existing file
dstPath to the new file
Returns
True if source file exists and the copy is successful, false otherwise

◆ directoryExists()

IVW_CORE_API bool inviwo::filesystem::directoryExists ( const std::filesystem::path & path)

Check if the directory exists

See also
fileExists for files
Parameters
pathDirectory path
Returns
True if directory exists, false otherwise

◆ fileExists()

IVW_CORE_API bool inviwo::filesystem::fileExists ( const std::filesystem::path & filePath)

Check if a file exists.

See also
directoryExists for directories
Parameters
filePathThe path to the file
Returns
true if file exists, false otherwise

◆ fileModificationTime()

IVW_CORE_API std::time_t inviwo::filesystem::fileModificationTime ( const std::filesystem::path & filePath)

Get last time file was modified. Error can occur if the file does not exist for example.

Parameters
filePathThe path to the file
Returns
Time of last modification, or 0 if an error occured (00:00, Jan 1 1970 UTC).

◆ findBasePath()

IVW_CORE_API const std::filesystem::path & inviwo::filesystem::findBasePath ( )

Try to find the Inviwo base path containing subfolders "data/workspaces" and "modules". If not found, try searching for path containing "modules". If neither are found, return the executable path.

Returns
Inviwo base path

◆ fopen()

IVW_CORE_API FILE * inviwo::filesystem::fopen ( const std::filesystem::path & filename,
const char * mode )

Creates and returns a FILE pointer for the given file name (utf-8 encoded). The call auto f = filesystem::fopen(filename, mode); is functionally equivalent to the statement fopen(filename, mode); or _wfopen();, respectively. No checks whether the file exists or was successfully opened are performed. That is the caller has to check it. For more details check the documentation of fopen.

Since all strings within Inviwo are utf-8 encoded, this function should be used to create a file handle when reading from/writing to files.

On Windows, the file name is first converted from a utf-8 string to std::wstring and then the file handle is created using the std::wstring as fopen(const char*) does not support utf-8.

Parameters
filenameutf-8 encoded string
modemode to open the file (input or output)
Returns
file handle for the given file, i.e. fopen(filename, mode);
See also
fopen, _wfopen

◆ fstream()

IVW_CORE_API std::fstream inviwo::filesystem::fstream ( const std::filesystem::path & filename,
std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out )

Creates and returns a std::fstream for the given file name (utf-8 encoded). The call auto f = filesystem::fstream(filename, mode); is functionally equivalent to the statement std::fstream f(filename, mode);. No checks whether the file exists or was successfully opened are performed. That is the caller has to check it. For more details check the documentation of std::fstream.

Since all strings within Inviwo are utf-8 encoded, this function should be used to create a stream when reading from/writing to files using streams.

On Windows, the file name is first converted from a utf-8 string to std::wstring and then the stream is created using the std::wstring as std::fstream(std::string) does not support utf-8.

Parameters
filenameutf-8 encoded string
modemode to open the file (input or output)
Returns
stream for the given file, i.e. std::fstream(filename, mode);
See also
std::fstream

◆ getDirectoryContents()

IVW_CORE_API std::vector< std::filesystem::path > inviwo::filesystem::getDirectoryContents ( const std::filesystem::path & path,
ListMode mode = ListMode::Files )

Returns the file listing of a directory

Parameters
pathFiles are listed for this directory
modeWhat types of contents to return see ListMode
Returns
List of files residing in the given path

◆ getDirectoryContentsRecursively()

IVW_CORE_API std::vector< std::filesystem::path > inviwo::filesystem::getDirectoryContentsRecursively ( const std::filesystem::path & path,
ListMode mode = ListMode::Files )

Recursively searches and returns full path to files/directories in specified directory and its subdirectories.

Parameters
pathFiles are listed for this directory and its subdirectories
modeWhat types of contents to return see ListMode
Returns
List of files residing in the given path and its subdirectories

◆ getExecutablePath()

IVW_CORE_API std::filesystem::path inviwo::filesystem::getExecutablePath ( )

Get full/path/to/executable running the application.

Returns
Full path to the executable if successful, empty string otherwise.

◆ getFileExtension()

IVW_CORE_API std::string inviwo::filesystem::getFileExtension ( const std::filesystem::path & url)

Returns the characters after last dot (.).

Note
Avoid using this function for extracting file extensions if you are not sure that the expected extension only contains one dot. In general, there is no way to safely consider cases with multiple dots. File extensions with multiple dots can exist for compressed files (for example nii.gz). Instead, compare the url with registered extensions, see inviwo::FileExtension and inviwo::DataReaderFactory
See also
DataReaderFactory
getFileNameWithExtension
Parameters
urlPath to extract extension from.
Returns
Extension excluding ., empty if no dot or filename are found.

◆ getInviwoUserSettingsPath()

IVW_CORE_API std::filesystem::path inviwo::filesystem::getInviwoUserSettingsPath ( )

Get path to the user settings / data folder for Inviwo, i.e. a folder where we have write-access. Will be:

  • Windows: /AppData/Inviwo/
  • Linux: /home/.inviwo
  • Mac: /Library/Application Support/org.inviwo.network-editor
Returns
Path to user settings folder

◆ getParentFolderWithChildren()

IVW_CORE_API std::optional< std::filesystem::path > inviwo::filesystem::getParentFolderWithChildren ( const std::filesystem::path & path,
std::span< const std::filesystem::path > childFolders )

Traverses all parent folders of path and returns the first directory matching the list of child folders.

Parameters
pathdirectory where the search is started
childFolderslist of subfolders
Returns
path of parent directory holding all childFolders, otherwise std::nullopt

◆ getPath()

IVW_CORE_API std::filesystem::path inviwo::filesystem::getPath ( PathType pathType,
const std::string & suffix = "",
const bool createFolder = false )

Get basePath + pathType + suffix.

See also
PathType
Parameters
pathTypeEnum for type of path
suffixPath extension
createFolderif true, will create the folder on disk if it does not exists.
Returns
basePath + pathType + suffix

◆ getRelativePath()

IVW_CORE_API std::filesystem::path inviwo::filesystem::getRelativePath ( const std::filesystem::path & basePath,
const std::filesystem::path & absolutePath )

Make a path relative to basePath. Requirement: basePath and absulutePath has to be absolute paths. basePath should point at directory.

Example: basePath = "C:/foo/bar" absolutePath = "C:/foo/test/file.txt" returns "../test/file.txt"

◆ getWorkingDirectory()

IVW_CORE_API std::filesystem::path inviwo::filesystem::getWorkingDirectory ( )

Get the working directory of the application.

Note
getBasePath should be used in the framework in general.
See also
getBasePath
Returns
Full path to working directory.

◆ ifstream()

IVW_CORE_API std::ifstream inviwo::filesystem::ifstream ( const std::filesystem::path & filename,
std::ios_base::openmode mode = std::ios_base::in )

Creates and returns a std::ifstream for the given file name (utf-8 encoded). The call auto in = filesystem::ifstream(filename, mode); is functionally equivalent to the statement std::ifstream in(filename, mode);. No checks whether the file exists or was successfully opened are performed. That is the caller has to check it. For more details check the documentation of std::ifstream.

Since all strings within Inviwo are utf-8 encoded, this function should be used to create a stream when reading from files using streams.

On Windows, the file name is first converted from a utf-8 string to std::wstring and then the stream is created using the std::wstring as std::ifstream(std::string) does not support utf-8.

Parameters
filenameutf-8 encoded string
modemode to open the file (input or output)
Returns
stream for the given file, i.e. std::ifstream(filename, mode);
See also
std::ifstream

◆ ofstream()

IVW_CORE_API std::ofstream inviwo::filesystem::ofstream ( const std::filesystem::path & filename,
std::ios_base::openmode mode = std::ios_base::out )

Creates and returns a std::ofstream for the given file name (utf-8 encoded). The call auto out = filesystem::ofstream(filename, mode); is functionally equivalent to the statement std::ofstream out(filename, mode);. No checks whether the file exists or was successfully opened are performed. That is the caller has to check it. For more details check the documentation of std::ofstream.

Since all strings within Inviwo are utf-8 encoded, this function should be used to create a stream when writing to files using streams.

On Windows, the file name is first converted from a utf-8 string to std::wstring and then the stream is created using the std::wstring as std::ofstream(std::string) does not support utf-8.

Parameters
filenameutf-8 encoded string
modemode to open the file (input or output)
Returns
stream for the given file, i.e. std::ofstream(filename, mode);
See also
std::ofstream

◆ replaceFileExtension()

IVW_CORE_API std::filesystem::path inviwo::filesystem::replaceFileExtension ( const std::filesystem::path & url,
std::string_view newFileExtension )

Replace the last file extension to newFileExtension, if no extension exists append newFileExtension. newFileExtension should not contain any leading "."

◆ sameDrive()

IVW_CORE_API bool inviwo::filesystem::sameDrive ( const std::filesystem::path & refPath,
const std::filesystem::path & queryPath )

Checks whether the second path is on the same drive as the first path.

If both paths are relative, this function returns true. If only refPath is relative InviwoApplication::getBasePath is used instead as reference.

Parameters
refPathreference path, if relative then InviwoApplication::getBasePath is used instead
queryPathpath to be checked
Returns
true if queryPath and refPath are located on the same drive (on Windows), always true on all other systems

◆ setWorkingDirectory()

IVW_CORE_API void inviwo::filesystem::setWorkingDirectory ( const std::filesystem::path & path)

Set the working directory of the application.

◆ skipByteOrderMark()

IVW_CORE_API bool inviwo::filesystem::skipByteOrderMark ( std::istream & stream)

Detects the UTF-8 byte order mark (BOM) and skips it if it exists. Reads the first three characters to determine if the BOM exists. Rewinds stream if no BOM exists and otherwise leaves the stream position after the three BOM characters.

Parameters
streamstream to check and potentially modify.
Returns
true if byte order mark was found, false otherwise

◆ wildcardStringMatch()

IVW_CORE_API bool inviwo::filesystem::wildcardStringMatch ( const std::string & pattern,
const std::string & str )

Checks whether a given string matches a pattern. The pattern might contain '*' matching any string including the empty string and '?' matching a single character.

Parameters
patternThe pattern used for matching, might contain '*' and '?'
strString which needs to be checked
Returns
true if the given string matches the pattern, false otherwise.

◆ wildcardStringMatchDigits()

IVW_CORE_API bool inviwo::filesystem::wildcardStringMatchDigits ( const std::string & pattern,
const std::string & str,
int & index,
bool matchLess = false,
bool matchMore = true )

Checks whether a given string matches a pattern including digits. The pattern might contain a single sequence of '#' for indicating a number besides '*' matching any string including the empty string and '?' matching a single character.

The digit sequence indicated by '#' is extracted and returned. Depending on the flags, the number have to exactly match sequence or might be shorter (matchLess) or longer (matchMore). For example, the sequence '###' matches only a three-digit number. Enabling 'matchLess' also matches one-digit and two-digit numbers whereas 'matchMore' allows for numbers with more digits.

Examples:

  • '###*.jpg' will match all jpeg files starting with a 3-digit sequence. Setting 'matchMore = true' matches the same files, but might extract longer numbers.
  • 'myfile#.png' matches all files containing exactly one digit with 'matchMore = false'.
Parameters
patternThe pattern used for matching, might contain a single sequence of '#' besides '*', and '?'
strString which needs to be checked
indexif the match is successful, this index contains the extracted digit sequence indicated by '#'
matchLessallows to match digit sequences shorter than defined by the number of '#' (default false)
matchMoreallows to match longer digit sequences (default true)
Returns
True if the given string matches the pattern, false otherwise.