BASIS  r3148
basis::os::path Namespace Reference

Functions

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

Function Documentation

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 298 of file path.cxx.

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 269 of file path.cxx.

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 261 of file path.cxx.

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 472 of file path.cxx.

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 277 of file path.cxx.

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 288 of file path.cxx.

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 458 of file path.cxx.

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 444 of file path.cxx.

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 485 of file path.cxx.

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 53 of file path.cxx.

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 428 of file path.cxx.

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 77 of file path.cxx.

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 141 of file path.cxx.

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 126 of file path.cxx.

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 382 of file path.cxx.

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 304 of file path.cxx.

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 160 of file path.cxx.

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 176 of file path.cxx.

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 184 of file path.cxx.

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 200 of file path.cxx.

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 208 of file path.cxx.

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 253 of file path.cxx.