Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Path Class Reference

#include <elm/sys/Path.h>

Classes

class  DirIter
 
class  DirReader
 
class  PathIter
 
class  PathSplit
 

Public Member Functions

 Path (void)
 
 Path (const char *path)
 
 Path (CString path)
 
 Path (const String &path)
 
 Path (const Path &path)
 
Path canonical (void) const
 
Path absolute (void) const
 
Path append (Path path) const
 
Path parent (void) const
 
Path setExtension (CString new_extension) const
 
Path setExt (cstring ext) const
 
Path relativeTo (Path base) const
 
Path withoutExt (void) const
 
const StringtoString (void) const
 
String namePart (void) const
 
sys::Path dirPart (void) const
 
Path basePart (void) const
 
String extension (void) const
 
bool isEmpty (void) const
 
bool isAbsolute (void) const
 
bool isRelative (void) const
 
bool isHomeRelative (void) const
 
bool equals (Path &path) const
 
bool subPathOf (const Path &path) const
 
bool isPrefixOf (const Path &path) const
 
bool prefixedBy (const Path &path) const
 
const char * asSysString () const
 
bool exists (void) const
 
bool isFile (void) const
 
bool isDir (void) const
 
bool isReadable (void) const
 
bool isWritable (void) const
 
bool isExecutable (void) const
 
void remove (void)
 
void makeDir (void)
 
io::InStreamread (void)
 
io::OutStreamwrite (void)
 
io::OutStreamappend (void)
 
Pathoperator= (const char *str)
 
Pathoperator= (CString str)
 
Pathoperator= (const String &str)
 
Pathoperator= (const Path &path)
 
bool operator== (Path path) const
 
bool operator!= (Path path) const
 
Path operator/ (const Path &path) const
 
 operator const String & (void) const
 
 operator bool (void) const
 
DirReader readDir (void) const
 
void makeDirs (void) const
 
bool contains (const Path &path) const
 

Static Public Member Functions

static bool isSeparator (char c)
 
static void setCurrent (Path &path)
 
static Path current (void)
 
static Path home (void)
 
static Path temp (void)
 
static PathSplit splitPaths (string paths)
 

Static Public Attributes

static const char SEPARATOR = '/'
 
static const char PATH_SEPARATOR = ':'
 
static const string BACK_PATH = ".."
 

Detailed Description

This class represents a file path in the current file system.

It provides facilities to:

  • build and manage paths and list of paths,
  • test paths for existence and type and accessebility checking,
  • read directories content,
  • shortcuts to read and write files
  • shortcuts to read and create directories.

Constructor & Destructor Documentation

◆ Path() [1/5]

◆ Path() [2/5]

Path ( const char *  path)
inline

◆ Path() [3/5]

Path ( CString  path)
inline

◆ Path() [4/5]

Path ( const String path)
inline

◆ Path() [5/5]

Path ( const Path path)
inline

Member Function Documentation

◆ absolute()

Path absolute ( void  ) const

Get the absolute path matching the current path.

Returns
Matching absolute path.

References Path::current(), Path::home(), Path::isAbsolute(), Path::isHomeRelative(), Path::Path(), and String::substring().

Referenced by Path::canonical().

◆ append() [1/2]

Path append ( Path  path) const

Build a new path by appending two paths.

Parameters
pathPath to append.
Returns
New path.

References Path::Path(), Path::SEPARATOR, and StringBuffer::toString().

Referenced by FileOutput::FileOutput().

◆ append() [2/2]

io::OutStream * append ( void  )

Open or create a file with this path and write by appending to the content.

Returns
Opened file.
Exceptions
SystemExceptionThrown if there is an IO error during operation.

References System::appendFile().

Referenced by Path::operator/().

◆ asSysString()

◆ basePart()

Path basePart ( void  ) const

Get the base part of the path, that is, the path without the extension of the file part.

Returns
Base part of the path.

References String::lastIndexOf(), and String::substring().

◆ canonical()

Path canonical ( void  ) const

Get the canonical form of the current path. Usually, a relative path becomes an absolute one. Relative operators (.., .) are removed if it is possible.

References Path::absolute(), Path::isRelative(), and Path::SEPARATOR.

Referenced by FileItem::get(), and System::getUnitPath().

◆ contains()

bool contains ( const Path path) const
inline

References Path::subPathOf().

◆ current()

Path current ( void  )
static

Get the current path.

Returns
Current path.

References Path::Path().

Referenced by Path::absolute(), and Path::temp().

◆ dirPart()

sys::Path dirPart ( void  ) const

Return the directory part of the path.

Returns
Directory part.

References Path::Path(), and String::substring().

Referenced by elm::sys::evaluate().

◆ equals()

bool equals ( Path path) const
inline

Test if two paths are lexically equals.

Returns
True if they equals, false else.

Referenced by Path::operator!=(), and Path::operator==().

◆ exists()

bool exists ( void  ) const

Test if the path matches a file, a directory or any file system object.

Returns
True if it exists (or is not accessible), false else.

References Path::asSysString().

Referenced by Plugger::plugFile().

◆ extension()

String extension ( void  ) const

Get the extension of the referenced file name.

Returns
Extension (without ".") or an empty string if there is no extension.

References String::lastIndexOf(), and String::substring().

Referenced by Plugger::plugFile().

◆ home()

Path home ( void  )
static

Get the path of the home directory.

Returns
Home directory.

References Path::Path().

Referenced by Path::absolute(), and Path::temp().

◆ isAbsolute()

bool isAbsolute ( void  ) const

Test if the path is absolute.

Returns
True if it is absolute, false else.

References String::length(), and Path::SEPARATOR.

Referenced by Path::absolute(), and Path::isRelative().

◆ isDir()

bool isDir ( void  ) const

Test if the path matches a directory.

Returns
True if it is a directory (and is accessible), false else.

References Path::asSysString().

Referenced by Path::temp().

◆ isEmpty()

bool isEmpty ( void  ) const
inline

Test if the path is empty.

Returns
True if the path is empty, false else.

References String::isEmpty().

◆ isExecutable()

bool isExecutable ( void  ) const

Test if the path matches an executable file system object.

Returns
True if it executable (and accessible), false else.

References Path::asSysString().

◆ isFile()

bool isFile ( void  ) const

Test if the path matches a file.

Returns
True if it is a file (and is accessible), false else.

References Path::asSysString().

Referenced by Plugger::plugFile().

◆ isHomeRelative()

bool isHomeRelative ( void  ) const

Test if the path is home-relative. On Unix, it is usually starting with "~".

Returns
True if it is home-relative, false else.

References String::length().

Referenced by Path::absolute().

◆ isPrefixOf()

bool isPrefixOf ( const Path path) const
inline

Test if the current path is prefix of the given one.

Parameters
pathPath to test.
Returns
True if the current path is prefix of the given one.

References String::length(), Path::SEPARATOR, and String::startsWith().

Referenced by Path::prefixedBy().

◆ isReadable()

bool isReadable ( void  ) const

Test if the path matches a readable file system object.

Returns
True if it readable (and accessible), false else.

References Path::asSysString().

Referenced by Plugger::plugFile().

◆ isRelative()

bool isRelative ( void  ) const

Test if the path is relative.

Returns
True if it is relative, false else.

References Path::isAbsolute().

Referenced by Path::canonical().

◆ isSeparator()

static bool isSeparator ( char  c)
inlinestatic

References Path::SEPARATOR.

◆ isWritable()

bool isWritable ( void  ) const

Test if the path matches a writable file system object.

Returns
True if it writable (and accessible), false else.

References Path::asSysString().

Referenced by Path::temp().

◆ makeDir()

void makeDir ( void  )

Build the directory corresponding to this path.

Exceptions
SystemExceptionThrown if there is an IO error during operation.

References System::makeDir().

◆ makeDirs()

void makeDirs ( void  ) const

Create the directory given by this path and missing parent directories. Does nothing if the directory already exists.

Exceptions
SystemExceptionIf there is an OS error or if a file with same name exist.

References System::makeDirs().

◆ namePart()

String namePart ( void  ) const

Return the name part of the path.

Returns
Name part.

References String::substring().

Referenced by Plugger::Iter::item(), FileItem::name(), and TestCase::test().

◆ operator bool()

operator bool ( void  ) const
inline

Test if the path is null.

Returns
True if it is not null, false else.

◆ operator const String &()

operator const String & ( void  ) const
inline

References Path::toString().

◆ operator!=()

bool operator!= ( Path  path) const
inline

Same as !equals().

References Path::equals().

◆ operator/()

Path operator/ ( const Path path) const
inline

Same as append.

References Path::append().

◆ operator=() [1/4]

Path& operator= ( const char *  str)
inline

References elm::str().

◆ operator=() [2/4]

Path& operator= ( const Path path)
inline

◆ operator=() [3/4]

Path& operator= ( const String str)
inline

References elm::str().

◆ operator=() [4/4]

Path& operator= ( CString  str)
inline

References elm::str().

◆ operator==()

bool operator== ( Path  path) const
inline

Same as equals().

References Path::equals().

◆ parent()

Path parent ( void  ) const

Find the parent path of the current or an empty path if there is no parent.

Returns
Parent path.

References Path::Path(), and String::substring().

Referenced by FileItem::isDeletable().

◆ prefixedBy()

bool prefixedBy ( const Path path) const
inline

Test if the current path if prefixed by the given one.

Parameters
pathPrefix path.
Returns
True if path is a prefix of the current path.

References Path::isPrefixOf().

◆ read()

io::InStream * read ( void  )

Open for reading the file corresponding to this path.

Returns
Opened file.
Exceptions
SystemExceptionThrown if there is an IO error during operation.

References System::readFile().

◆ readDir()

DirReader readDir ( void  ) const
inline

Allow to read the content of a directory.

Returns
Reader of the directory.
Exceptions
SystemExceptionIf the path is not a directory or can not be accessed.

◆ relativeTo()

Path relativeTo ( Path  base) const

Build a path which is the current path relative to the given base path.

Parameters
baseBase path to be relative to.
Returns
Same path but relative to the base.

References Path::BACK_PATH, elm::io::base(), Path::Path(), String::substring(), and Path::toString().

◆ remove()

void remove ( void  )

Remove the file or the directory corresponding to this path. If this is a non-empty directory, remove its content recursively. No error is issued if the path does not exist.

Exceptions
SystemExceptionThrown if there is an IO error during operation.

References System::remove().

◆ setCurrent()

void setCurrent ( Path path)
static

Change the current path.

Parameters
pathNew current path.

References Path::asSysString().

◆ setExt()

Path setExt ( cstring  ext) const
inline

Change the extension if there is some one or add the given extension.

Parameters
extNew extension to put in.
Returns
New path with extension set.

References Path::setExtension().

◆ setExtension()

Path setExtension ( CString  new_extension) const

Change the extension if there is some one or add the given extension.

Parameters
new_extensionNew extension to put in.
Returns
New path with extension set.

References String::lastIndexOf(), and StringBuffer::toString().

Referenced by Plugger::plugFile(), and Path::setExt().

◆ splitPaths()

static PathSplit splitPaths ( string  paths)
inlinestatic

◆ subPathOf()

bool subPathOf ( const Path path) const
inline

Test if the current path is a sub-path of given path. This means that the path is a prefix of the current path.

Returns
True if it contains, false else.
Deprecated:
Use isPrefixedBy() instead.

References String::startsWith().

Referenced by Path::contains().

◆ temp()

Path temp ( void  )
static

Get a temporary directory. If the OS does not provide any temporary directory, the current directory is selected. As a fallback, a "tmp" directory is created in the user home. If it can't be, the user home is returned.

Returns
Temporary directory path.

References Path::current(), Path::home(), Path::isDir(), Path::isWritable(), System::makeDir(), and Path::Path().

◆ toString()

String & toString ( void  ) const
inline

◆ withoutExt()

Path withoutExt ( void  ) const

Return the path with the last component extension removed.

Returns
Path without the extension of the last component.

References String::lastIndexOf(), and String::substring().

◆ write()

io::OutStream * write ( void  )

Create a file with this path or overwrite an existing one.

Returns
Opened file.
Exceptions
SystemExceptionThrown if there is an IO error during operation.

References System::createFile().

Referenced by FileOutput::FileOutput().

Member Data Documentation

◆ BACK_PATH

const string BACK_PATH = ".."
static

Symbol used to represent a backward move in the path.

Referenced by Path::relativeTo().

◆ PATH_SEPARATOR

const char PATH_SEPARATOR = ':'
static

Separator character for a list of paths.

Referenced by Plugger::Plugger().

◆ SEPARATOR

const char SEPARATOR = '/'
static

Separator character for the components of a path.

Referenced by Path::append(), Path::canonical(), Path::isAbsolute(), Path::isPrefixOf(), and Path::isSeparator().


The documentation for this class was generated from the following files: