Inviwo  0.9.10.1
Inviwo documentation
inviwo::filesystem Namespace Reference

Enumerations

enum  ListMode { Files, Directories, FilesAndDirectories }
 

Functions

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

Detailed Description

filesystem

Function Documentation

◆ addBasePath()

std::string inviwo::filesystem::addBasePath ( const std::string &  url)

Adds the InviwoApplication base path before the url.

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

◆ cleanupPath()

std::string inviwo::filesystem::cleanupPath ( const std::string &  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::string &  src,
const std::string &  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()

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

Check if the directory exists

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

◆ fileExists()

bool inviwo::filesystem::fileExists ( const std::string &  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()

std::time_t inviwo::filesystem::fileModificationTime ( const std::string &  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()

std::string inviwo::filesystem::findBasePath ( )

Find Inviwo base path which contains subfolders "data/workspaces" and "modules"

Returns
Inviwo base path
Exceptions
exceptionin case base path could not be located

◆ fopen()

FILE * inviwo::filesystem::fopen ( const std::string &  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()

std::fstream inviwo::filesystem::fstream ( const std::string &  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()

std::vector< std::string > inviwo::filesystem::getDirectoryContents ( const std::string &  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()

std::vector< std::string > inviwo::filesystem::getDirectoryContentsRecursively ( const std::string &  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()

std::string inviwo::filesystem::getExecutablePath ( )

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

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

◆ getInviwoUserSettingsPath()

IVW_CORE_API std::string 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()

std::string inviwo::filesystem::getParentFolderWithChildren ( const std::string &  path,
const std::vector< std::string > &  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 empty string

◆ getPath()

IVW_CORE_API std::string 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()

std::string inviwo::filesystem::getRelativePath ( const std::string &  basePath,
const std::string &  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()

std::string 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()

std::ifstream inviwo::filesystem::ifstream ( const std::string &  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()

std::ofstream inviwo::filesystem::ofstream ( const std::string &  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()

std::string inviwo::filesystem::replaceFileExtension ( const std::string &  url,
const std::string &  newFileExtension 
)

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

◆ sameDrive()

bool inviwo::filesystem::sameDrive ( const std::string &  refPath,
const std::string &  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

◆ skipByteOrderMark()

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()

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()

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.