Functions
basis::os::path Namespace Reference

Functions

std::string abspath (const std::string &path)
 Make path absolute. More...
 
std::string basename (const std::string &path)
 Get file name. More...
 
std::string dirname (const std::string &path)
 Get file directory. More...
 
bool exists (const std::string path)
 Test the existance of a file or directory. More...
 
bool hasext (const std::string &path, const std::set< std::string > *exts=NULL)
 Test whether a given path has an extension. More...
 
bool isabs (const std::string &path)
 Test whether a given path is absolute. More...
 
bool isdir (const std::string path)
 Test whether a given path is the path of an existent directory. More...
 
bool isfile (const std::string path)
 Test whether a given path is the path of an existent file. More...
 
bool islink (const std::string &path)
 Whether a given path is a symbolic link. More...
 
bool issep (char c)
 Determine if a given character is a path separator. More...
 
std::string join (const std::string &base, const std::string &path)
 Join two paths, e.g., base path and relative path. More...
 
std::string normpath (const std::string &path)
 Normalize path, i.e., remove occurences of "./", duplicate slashes,... More...
 
std::string ntpath (const std::string &path)
 Convert path to Windows representation. More...
 
std::string posixpath (const std::string &path)
 Convert path to Posix (e.g., Unix, Mac OS) representation. More...
 
std::string realpath (const std::string &path)
 Get canonical file path. More...
 
std::string relpath (const std::string &path, const std::string &base=std::string())
 Make path relative. More...
 
void split (const std::string &path, std::string &head, std::string &tail)
 Split path into two parts. More...
 
std::vector< std::string > split (const std::string &path)
 Split path into two parts. More...
 
void splitdrive (const std::string &path, std::string &drive, std::string &tail)
 Get drive specification of Windows path. More...
 
std::vector< std::string > splitdrive (const std::string &path)
 Get drive specification of Windows path. More...
 
void splitext (const std::string &path, std::string &head, std::string &ext, const std::set< std::string > *exts=NULL, bool icase=false)
 Get file name extension. More...
 
std::vector< std::string > splitext (const std::string &path, const std::set< std::string > *exts=NULL)
 Get file name extension. More...
 

Function Documentation

§ abspath()

string basis::os::path::abspath ( const std::string &  path)

Make path absolute.

Parameters
[in]pathAbsolute or relative path.
Returns
Absolute path. If path is already absolute, it is returned unchanged. Otherwise, it is made absolute using the current working directory.

Definition at line 302 of file path.cxx.

§ basename()

string basis::os::path::basename ( const std::string &  path)

Get file name.

Parameters
[in]pathPath.
Returns
The tail part returned by split(), i.e., the file/directory name.
See also
split()

Definition at line 273 of file path.cxx.

§ dirname()

string basis::os::path::dirname ( const std::string &  path)

Get file directory.

Parameters
[in]pathPath.
Returns
The head part returned by split().
See also
split()

Definition at line 265 of file path.cxx.

§ exists()

bool basis::os::path::exists ( const std::string  path)

Test the existance of a file or directory.

Parameters
[in]pathFile or directory path.
Returns
Whether the given file or directory exists.

Definition at line 476 of file path.cxx.

§ hasext()

bool basis::os::path::hasext ( const std::string &  path,
const std::set< std::string > *  exts = NULL 
)

Test whether a given path has an extension.

Parameters
[in]pathPath.
[in]extsSet of recognized extensions or NULL.
Returns
Whether the given path has a file name extension. If exts is not NULL, this function returns true only if the file name ends in one of the specified extensions (including dot if required). Otherwise, it only checks if the path has a dot (.) in the file name.

Definition at line 281 of file path.cxx.

§ isabs()

bool basis::os::path::isabs ( const std::string &  path)

Test whether a given path is absolute.

Parameters
path[in] Absolute or relative path.
Returns
Whether the given path is absolute.

Definition at line 292 of file path.cxx.

§ isdir()

bool basis::os::path::isdir ( const std::string  path)

Test whether a given path is the path of an existent directory.

Note
This function follows symbolic links.
Parameters
[in]pathDirectory path.
Returns
Whether the given path is an existent directory.

Definition at line 462 of file path.cxx.

§ isfile()

bool basis::os::path::isfile ( const std::string  path)

Test whether a given path is the path of an existent file.

Note
This function follows symbolic links.
Parameters
[in]pathFile path.
Returns
Whether the given path is an existent file.

Definition at line 448 of file path.cxx.

§ islink()

bool basis::os::path::islink ( const std::string &  path)

Whether a given path is a symbolic link.

Parameters
[in]pathPath.
Returns
Whether the given path denotes a symbolic link.
Exceptions
std::invalid_argumentif the given path is not valid.
See also
isvalid()

Definition at line 489 of file path.cxx.

§ issep()

bool basis::os::path::issep ( char  c)
inline

Determine if a given character is a path separator.

Parameters
[in]cCharacter.
Returns
True if c is a path separator on this platform and false otherwise.

Definition at line 57 of file path.cxx.

§ join()

string basis::os::path::join ( const std::string &  base,
const std::string &  path 
)

Join two paths, e.g., base path and relative path.

This function joins two paths. If the second path is an absolute path, this normalized absolute path is returned. Otherwise, the base path is prepended to the relative path and the resulting relative or absolute path returned after normalizing it.

Parameters
[in]baseBase path.
[in]pathRelative or absolute path.
Returns
Joined path.

Definition at line 432 of file path.cxx.

§ normpath()

string basis::os::path::normpath ( const std::string &  path)

Normalize path, i.e., remove occurences of "./", duplicate slashes,...

This function removes single periods enclosed by slashes (or backslashes), duplicate slashes (or backslashes), and further tries to reduce the number of parent directory references. Moreover, on Windows, it replaces slashes with backslashes.

For example, on Windows, "../bla//.//.\bla\\\\\bla/../.." is convert to "..\bla". On Unix, it is converted to "..". Note that on Unix, ".\bla\\\\\bla" is a directory name.

Parameters
[in]pathPath.
Returns
Normalized path.

Definition at line 81 of file path.cxx.

§ ntpath()

string basis::os::path::ntpath ( const std::string &  path)

Convert path to Windows representation.

This function first converts forward slashes to backward slashes and then performs the same normalization as normpath() does on Windows.

Parameters
[in]pathPath.
Returns
Normalized Windows path.
See also
normpath)
posixpath()

Definition at line 145 of file path.cxx.

§ posixpath()

string basis::os::path::posixpath ( const std::string &  path)

Convert path to Posix (e.g., Unix, Mac OS) representation.

This function first converts backward slashes to forward slashes and then performs the same normalization as normpath() does on Posix systems.

Parameters
[in]pathPath.
Returns
Normalized Posix path.
See also
normpath()
ntpath()

Definition at line 130 of file path.cxx.

§ realpath()

string basis::os::path::realpath ( const std::string &  path)

Get canonical file path.

This function resolves symbolic links and returns a normalized path.

Parameters
[in]pathPath.
Returns
Canonical file path.

Definition at line 386 of file path.cxx.

§ relpath()

string basis::os::path::relpath ( const std::string &  path,
const std::string &  base = std::string() 
)

Make path relative.

Parameters
[in]pathAbsolute or relative path.
[in]baseBase path used to make absolute path relative. Defaults to current working directory if no path is given. If a relative path is given as base path, it is made absolute using the current working directory.
Returns
Path relative to base.
Exceptions
std::invalid_argumenton Windows, if path and base are paths on different drives.

Definition at line 308 of file path.cxx.

§ split() [1/2]

void basis::os::path::split ( const std::string &  path,
std::string &  head,
std::string &  tail 
)

Split path into two parts.

This function splits a path into its head and tail. Trailing slashes are stripped from head unless it is the root. See the following table for an illustration:

  path                     | head          | tail
  -------------------------+-------+---------------+-------------
  "/"                      | "/"           | ""
  "/usr/bin"               | "/usr"        | "bin"
  "/home/user/info.txt"    | "/home/user"  | "info.txt"
  "word.doc"               | ""            | "word.doc"
  "../word.doc"            | ".."          | "word.doc"
  "C:/"                    | "C:/"         | ""
  "C:"                     | "C:"          | ""
  "C:/WINDOWS/regedit.exe" | "C:/WINDOWS"  | "regedit.exe"
  "d:\data"                | "d:/"         | "data"
  "/usr/local/"            | "/usr/local"  | ""

In all cases, join(head, tail) returns a path to the same location as path (but the strings may differ).

Parameters
[in]pathPath.
[out]headHead part of path.
[out]tailTail part of path.
See also
splitdrive()
splitext()
dirname()
basename()

Definition at line 164 of file path.cxx.

§ split() [2/2]

vector< string > basis::os::path::split ( const std::string &  path)

Split path into two parts.

Returns
Tuple of (head, tail), i.e., returns always a vector of size 2.
See also
split(const std::string&, std::string&, std::string&)

Definition at line 180 of file path.cxx.

§ splitdrive() [1/2]

void basis::os::path::splitdrive ( const std::string &  path,
std::string &  drive,
std::string &  tail 
)

Get drive specification of Windows path.

Parameters
[in]pathPath.
[out]driveDrive on Windows if path specifies a drive or empty string otherwise.
[out]tailRemaining path without drive specification.

Definition at line 188 of file path.cxx.

§ splitdrive() [2/2]

vector< string > basis::os::path::splitdrive ( const std::string &  path)

Get drive specification of Windows path.

Parameters
[in]pathPath.
Returns
Tuple of (drive, tail), i.e., returns always a vector of size 2.

Definition at line 204 of file path.cxx.

§ splitext() [1/2]

void basis::os::path::splitext ( const std::string &  path,
std::string &  head,
std::string &  ext,
const std::set< std::string > *  exts = NULL,
bool  icase = false 
)

Get file name extension.

Parameters
[in]pathPath.
[out]headRemaining path without extension.
[out]extExtension (including leading dot).
[in]extsSet of recognized extensions. Note that the given set can contain extensions with dots (.) as part of the extension, e.g., ".nii.gz". If NULL is given, the part after the last dot (including the dot) is considered to be the file name extension. Otherwise, the longest extension from the given set which is equal to the end of the file path is returned. If no specified extension matched, an empty string is returned as extension.
[in]icaseWhether to ignore the case of the extensions.

Definition at line 212 of file path.cxx.

§ splitext() [2/2]

vector< string > basis::os::path::splitext ( const std::string &  path,
const std::set< std::string > *  exts = NULL 
)

Get file name extension.

Parameters
[in]pathPath.
[in]extsSet of recognized extensions.
See also
splitext(const std::string&, std::string&, std::string&, const std::set<std::string>*)

Definition at line 257 of file path.cxx.