MIRA
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Typedefs | Functions
Path


For detailed information see Path. More...

Collaboration diagram for Path:

Typedefs

typedef boost::filesystem::path Path
 Typedef of a Path (shorter version for boost::filesystem::path) More...
 
typedef std::vector< PathPathVector
 Typedef for vector of Paths. More...
 

Functions

PathVector extractPaths (const std::string &env)
 Extracts and resolves all paths contained in an environment variable. More...
 
Path normalizePath (const Path &path)
 Normalizes a path: it resolves all ".." in a path. More...
 
Path resolvePath (const Path &path, bool makeAbsolute=true)
 Resolves a path. More...
 
bool isRootOfPath (const Path &root, const Path &path)
 The function takes two absolute paths and returns true if the given root path is part of the path given in the path argument. More...
 
Path getExecutableDirectory ()
 Get the path of the current running application binary. More...
 
Path getHomeDirectory ()
 Get the absolute path of the current user's home directory. More...
 
Path getAppDataDirectory (const std::string &appName="mira")
 Returns the absolute path for application-specific data. More...
 
Path getTempDirectory ()
 Get the absolute path of the temporary directory. More...
 
Path relativizePath (const Path &path, const Path &base)
 Returns relative path from base to path. More...
 
void checkForMIRAPath ()
 Checks if environment variable(s) for MIRA is/are defined. More...
 
PathVector getMIRAPaths ()
 Returns vector of all MIRA project paths. More...
 
Path findFile (const PathVector &paths, const Path &file)
 Find a file in a vector of paths. More...
 
PathVector findFiles (const PathVector &paths, const Path &file, bool recursive=false)
 Finds all files in a vector of paths. More...
 
PathVector findFiles (const Path &path, const Path &file, bool recursive=false)
 Same as findFiles above, but using only one search path. More...
 
PathVector findProjectFiles (const Path &file, bool recursive=false)
 Same as findFiles above, but using all of the paths contained in $MIRA_PATH variable. More...
 
Path findDirectory (const PathVector &paths, const Path &path)
 Find a sub directory in a vector of paths. More...
 
Path findProjectDirectory (const Path &path)
 Same as findDirectory above, but using all of the paths contained in $MIRA_PATH variable. More...
 
Path findContainingMIRAPath (const Path &path)
 Returns the MIRA path that contains the given path. More...
 
Path chopMIRAPath (const Path &path)
 Removes the root part of the given path that belongs to one of MIRA paths. More...
 
PathVector findDirectories (const PathVector &paths, const Path &path, bool recursive=false)
 Finds all sub directories in a vector of paths. More...
 
PathVector findDirectories (const Path &basePath, const Path &path, bool recursive=false)
 Same as findDirectories above, but searching only in one path. More...
 
PathVector findProjectDirectories (const Path &path, bool recursive=false)
 Same as findDirectories above, but using all of the paths contained in $MIRA_PATH variable. More...
 
std::string findProjectPath (const std::string &path)
 Finds a file or directory searching in all paths contained in $MIRA_PATH. More...
 
constexpr std::string_view findContainingMIRAPath (const std::string_view &path, const std::string_view &miraPaths)
 Returns the MIRA path that contains the given path. More...
 
constexpr std::string_view findContainingMIRAPath (const char *path, const char *miraPaths)
 As above, overload for const char* path. More...
 
constexpr std::string_view findContainingMIRAPath (const char *path)
 With this overload the MIRA paths are not provided as argument, they are expected to be defined as a compiler definition MIRA_PATHS. More...
 
constexpr std::string_view chopMIRAPath (const std::string_view &path, const std::string_view &miraPaths)
 Removes the root part of the given path that belongs to one of MIRA paths. More...
 
constexpr const char * chopMIRAPath (const char *path, const char *miraPaths)
 As above, overload for const char* path. More...
 
constexpr const char * chopMIRAPath (const char *path)
 With this overload the MIRA paths are not provided as argument, they are expected to be defined as a compiler definition MIRA_PATHS. More...
 

Detailed Description


For detailed information see Path.

Typedef Documentation

◆ Path

typedef boost::filesystem::path Path

Typedef of a Path (shorter version for boost::filesystem::path)

◆ PathVector

typedef std::vector<Path> PathVector

Typedef for vector of Paths.

Function Documentation

◆ extractPaths()

PathVector mira::extractPaths ( const std::string &  env)

Extracts and resolves all paths contained in an environment variable.

If the path is found and exists it will be added to the returned path list.

Parameters
[in]envThe environmental variable e.g. PATH (without variable token like $ or %)
Returns
All the existing paths contained in the env variable

◆ normalizePath()

Path mira::normalizePath ( const Path path)

Normalizes a path: it resolves all ".." in a path.

For optimal results the path should be made absolute before calling this function. Otherwise it is not guaranteed that all ".." are resolved e.g.

normalizePath("../my/path");

will return "../my/path" since it can not determine the parent path.

Note
This replaces boost::filesystem::path::normalize() because it is deprecated and also does not take symbolic links into account!
Parameters
[in]pathThe path to be normalized
Returns
The normalized path

◆ resolvePath()

Path mira::resolvePath ( const Path path,
bool  makeAbsolute = true 
)

Resolves a path.

It will resolve environment variables, (optionally) make the path absolute and finally normalized.

Parameters
[in]pathThe path to be resolved
[in]makeAbsoluteIf path is relative and this flag is set the path will be made absolute.
Returns
The resolved, absolute and normalized path

Note: for an empty input path, the relative result is empty, the absolute result is the same as for '.' (current working directory).

◆ isRootOfPath()

bool mira::isRootOfPath ( const Path root,
const Path path 
)

The function takes two absolute paths and returns true if the given root path is part of the path given in the path argument.

Parameters
[in]rootpath
[in]pathto check

◆ getExecutableDirectory()

Path mira::getExecutableDirectory ( )

Get the path of the current running application binary.

Returns
The base of the current executable

◆ getHomeDirectory()

Path mira::getHomeDirectory ( )

Get the absolute path of the current user's home directory.

Under Linux this will be the content of the HOME environment variable.

Exceptions
XRuntimeif no home directory can be found
Returns
The path of the current user's home directory

◆ getAppDataDirectory()

Path mira::getAppDataDirectory ( const std::string &  appName = "mira")

Returns the absolute path for application-specific data.

A sub-directory with the specified appName will be created automatically.

Under Linux the following directory will be returned: ${USER HOME}/.config/${appName}

Exceptions
XRuntimeIf the application data path does not exist or cannot be created.
Returns
The absolute path for application-specific data.

◆ getTempDirectory()

Path mira::getTempDirectory ( )

Get the absolute path of the temporary directory.

Under Linux the following checks are performed in the given order:

  • If TMPDIR is set its value is returned
  • If macro P_tmpdir is defined its value is returned
  • If /tmp exists it is returned
Exceptions
XRuntimeif none of the above checks yields a result (meaning: no temporary directory can be found)
Returns
The path of the temporary directory

◆ relativizePath()

Path mira::relativizePath ( const Path path,
const Path base 
)

Returns relative path from base to path.

E.g.:

relativizePath("/home/user/mira/toolboxes", "/home/user/mira")
// returns toolboxes

◆ checkForMIRAPath()

void mira::checkForMIRAPath ( )

Checks if environment variable(s) for MIRA is/are defined.

Exceptions
Exceptionif variables is/are not set

◆ getMIRAPaths()

PathVector mira::getMIRAPaths ( )

Returns vector of all MIRA project paths.

◆ findFile()

Path mira::findFile ( const PathVector paths,
const Path file 
)

Find a file in a vector of paths.

Parameters
[in]pathsThe list of paths to search in.
[in]fileThe file to find.

If the file exists in different paths, the first occurrence will be returned. A sub directory structure can be given e.g.

findFile(paths, "resource/MyResourceFolder/Image.png")
Exceptions
XIOif no file was found
Returns
The path to the found file.

◆ findFiles() [1/2]

PathVector mira::findFiles ( const PathVector paths,
const Path file,
bool  recursive = false 
)

Finds all files in a vector of paths.

Parameters
[in]pathsThe list of paths to search in.
[in]fileThe file (can also contain wildcards like * and ?)
[in]recursiveFlag for recursive search.

A sub directory structure can be given within file e.g.

findFiles(paths, "resource/MyResourceFolder/Image*.png").

e.g. If paths=/usr/local/MIRA it will look for all files matching /usr/local/MIRA/resource/MyResourceFolder/Image*.png.

If recursive=true it will search (in the above example) for all occurrences of resource/MyResourceFolder/Image*.png in all sub directories of /usr/local/MIRA i.e. it will also find /usr/local/MIRA/AnotherFolder/SubFolder/resource/MyResourceFolder/Image1.png

Returns
All the found file paths.

◆ findFiles() [2/2]

PathVector mira::findFiles ( const Path path,
const Path file,
bool  recursive = false 
)

Same as findFiles above, but using only one search path.

◆ findProjectFiles()

PathVector mira::findProjectFiles ( const Path file,
bool  recursive = false 
)

Same as findFiles above, but using all of the paths contained in $MIRA_PATH variable.

◆ findDirectory()

Path mira::findDirectory ( const PathVector paths,
const Path path 
)

Find a sub directory in a vector of paths.

Parameters
[in]pathsThe list of paths to search in.
[in]pathThe sub path to find.

A sub directory structure can be given e.g.

findDirectory(paths, "resource/MyResourceFolder/")
Exceptions
XIOif no directory was found
Returns
The path to the found sub directory.

◆ findProjectDirectory()

Path mira::findProjectDirectory ( const Path path)

Same as findDirectory above, but using all of the paths contained in $MIRA_PATH variable.

◆ findContainingMIRAPath() [1/4]

Path mira::findContainingMIRAPath ( const Path path)

Returns the MIRA path that contains the given path.

Parameters
[in]paththe path to be processed
Returns
the mira path that contains the given path
Exceptions
XLogicalif path is not located in one of the MIRA paths

◆ chopMIRAPath() [1/4]

Path mira::chopMIRAPath ( const Path path)

Removes the root part of the given path that belongs to one of MIRA paths.

E.g.:

chopMIRAPath('/home/user/mira/some/sub/folder')

will return 'some/sub/folder' if '/home/user/mira' is set as MIRA path.

Parameters
[in]paththe path to be processed
Returns
the relative path from the containing mira folder to the given path
Exceptions
XLogicalif path is not located in one of the MIRA paths

◆ findDirectories() [1/2]

PathVector mira::findDirectories ( const PathVector paths,
const Path path,
bool  recursive = false 
)

Finds all sub directories in a vector of paths.

Parameters
[in]pathsThe list of paths to search in.
[in]pathThe sub path to find.
[in]recursiveFlag for recursive search.

A sub directory structure can be given e.g.

findDirectories(paths, "resource/MyResourceFolder/").

e.g. If paths=[/usr/local/MyProject, /usr/local/AnotherProject] it will look for all occurrences of resource/MyResourceFolder/ in /usr/local/MyProject/ and /usr/local/AnotherProject.

  • if recursive = true it will also search for all occurrences of resource/MyResourceFolder/ in all sub directories of /usr/local/MyProject and /usr/local/AnotherProject i.e. it will also find /usr/local/MyProject/AnotherFolder/SubFolder/resource/MyResourceFolder/
  • if recursive = false it will only find all directories that match the combination of paths[X]/path e.g. in the above example it would find exactly 2 directories /usr/local/MyProject/resource/MyResourceFolder/ and /usr/local/AnotherProject/resource/MyResourceFolder/
Returns
All the found directories.

◆ findDirectories() [2/2]

PathVector mira::findDirectories ( const Path basePath,
const Path path,
bool  recursive = false 
)

Same as findDirectories above, but searching only in one path.

◆ findProjectDirectories()

PathVector mira::findProjectDirectories ( const Path path,
bool  recursive = false 
)

Same as findDirectories above, but using all of the paths contained in $MIRA_PATH variable.

◆ findProjectPath()

std::string mira::findProjectPath ( const std::string &  path)

Finds a file or directory searching in all paths contained in $MIRA_PATH.

Tries to find the file or directory by calling:

  • 1. findProjectFile, if no file found
  • 2. findProjectDirectory, if no directory found
  • 3. findProjectFiles recursively in all subdirs, if no file found
  • 4. findProjectDirectories recursively in all subdirs
    Exceptions
    XIOIf no file or directory is found or multiple matches are found
    Parameters
    pathThe path to be found
    Returns
    The found file or directory

◆ findContainingMIRAPath() [2/4]

constexpr std::string_view mira::findContainingMIRAPath ( const std::string_view &  path,
const std::string_view &  miraPaths 
)

Returns the MIRA path that contains the given path.

Parameters
[in]paththe path to be processed
[in]miraPathsthe MIRA paths
Returns
the mira path that contains the given path, or "" if none

The option to provide MIRA paths as argument is useful for testing. See below for an overload that does not require this.

◆ findContainingMIRAPath() [3/4]

constexpr std::string_view mira::findContainingMIRAPath ( const char *  path,
const char *  miraPaths 
)

As above, overload for const char* path.

Note that you cannot just convert the result to a const char*, as the size is needed in addition to the data pointer (matching null-termination can not be assumed)!

◆ findContainingMIRAPath() [4/4]

constexpr std::string_view mira::findContainingMIRAPath ( const char *  path)

With this overload the MIRA paths are not provided as argument, they are expected to be defined as a compiler definition MIRA_PATHS.

Otherwise this will return an empty string literal "".

Note that you cannot just convert the result to a const char*, as the size is needed in addition to the data pointer (matching null-termination can not be assumed)!

◆ chopMIRAPath() [2/4]

constexpr std::string_view mira::chopMIRAPath ( const std::string_view &  path,
const std::string_view &  miraPaths 
)

Removes the root part of the given path that belongs to one of MIRA paths.

E.g.:

chopMIRAPath('/home/user/mira/some/sub/folder')

will return 'some/sub/folder' if '/home/user/mira' is set as MIRA path.

Parameters
[in]paththe path to be processed
[in]miraPathsthe MIRA paths
Returns
the relative path from the containing MIRA folder to the given path, or just the given path if it is not within a MIRA path

The option to provide MIRA paths as argument is useful for testing. See below for an overload that does not require this.

◆ chopMIRAPath() [3/4]

constexpr const char* mira::chopMIRAPath ( const char *  path,
const char *  miraPaths 
)

As above, overload for const char* path.

As this only removes prefixes from path, null-termination persists and it is safe to return a const char*

◆ chopMIRAPath() [4/4]

constexpr const char* mira::chopMIRAPath ( const char *  path)

With this overload the MIRA paths are not provided as argument, they are expected to be defined as a compiler definition MIRA_PATHS.

Otherwise this will return the unchanged argument path.

As this only removes prefixes from path, null-termination persists and it is safe to return a const char*