coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
path< Delim > Struct Template Reference

Encapsulates a filesystem path. More...

#include <path.h>

Public Member Functions

 path (std::string const &path)
 Explicitly construct a path from a string.
 
pathoperator= (std::string const &str)
 Assign from a string.
 
bool operator== (path const &other) const
 Equality.
 
bool operator!= (path const &other) const
 Inequality.
 
bool operator< (path const &other) const
 Less-than operator.
 
size_t elements () const
 Get the number of elements in the path More...
 
std::string const & str () const
 Get the path as a string.
 
size_t length () const
 Get the length of the path.
 
std::string element (size_t which) const
 Return a path element by index. More...
 
std::string cur_element () const
 Get the path element at the cursor.
 
std::string operator[] (size_t which) const
 The subscript operator is equivalent to element(which)
 
bool is_prefix_of (path const &other) const
 Say whether the path consists of an initial subsequence of the elements of another. More...
 
std::string segment (size_t start=0, size_t len=std::string::npos) const
 Get a sub-sequence of the path's elements as a string. More...
 
void push_back (std::string const &str)
 Append a string to the path. More...
 
void pop_back ()
 Remove the last element of the path, if any.
 
void push_front (std::string const &str)
 Prepend a string to the path. More...
 
void pop_front ()
 Remove the first element of the path, if any.
 
path operator+ (std::string const &str) const
 Get the concatenation of the path with a string. More...
 
path operator+ (path const &rhs) const
 Get the concatenation of the path with another. More...
 
pathoperator+= (std::string const &str)
 operator+=(str) is equivalent to push_back(str)
 
pathoperator+= (path const &rhs)
 operator+=(path) is equivalent to push_back(path.str())
 
void insert (size_t after, std::string const &str)
 Insert a string into the path. More...
 
void cut (size_t start=0, size_t len=std::string::npos)
 Remove a segment from the path. More...
 
void rectify ()
 Correct anomalies in the path. More...
 
int to_end ()
 Set the cursor to index the final element, or to 0 if the path is empty. More...
 
int & posn ()
 Get a [const] reference to the path's cursor. More...
 
int const & posn () const
 

Static Public Member Functions

static path common_prefix (path const &lhs, path const &rhs)
 Get the common initial prefix of two paths. More...
 

Private Types

using element_pos = std::pair< size_t, size_t >
 Type of an element or section locator in a path. More...
 

Private Member Functions

void analyse ()
 Analyse the path to determine its composition.
 
element_pos get_section (size_t start, size_t len=std::string::npos) const
 Get the location of a section of the path. More...
 

Private Attributes

std::string _path
 The string representation of the path.
 
std::vector< element_pos_elements
 The structure of the path as a sequence of element_pos.
 
int _cursor = 0
 The cursor of the path.
 
char _first = 0
 The first character of the path.
 
char _last = 0
 The last character of the path.
 

Detailed Description

template<char Delim>
struct path< Delim >

Encapsulates a filesystem path.

struct path controls a string representing an absolute or relative pathname and provides queries and operations on it.

A path contains a cursor that can be used to maintain and modify an index into the elements of the path.

Template Parameters
DelimThe character that delimits elements of a path.

Definition at line 59 of file path.h.

Member Typedef Documentation

template<char Delim>
using path< Delim >::element_pos = std::pair<size_t,size_t>
private

Type of an element or section locator in a path.

The type of an element or section locator in a path, consisting of the index and length of the element.

Definition at line 374 of file path.h.

Member Function Documentation

template<char Delim>
static path path< Delim >::common_prefix ( path< Delim > const &  lhs,
path< Delim > const &  rhs 
)
inlinestatic

Get the common initial prefix of two paths.

Parameters
lhsThe first path to compare
rhsThe second path to compare
Returns
A path comprising the common initial elements of lhs and rhs.

Definition at line 356 of file path.h.

template<char Delim>
void path< Delim >::cut ( size_t  start = 0,
size_t  len = std::string::npos 
)
inline

Remove a segment from the path.

Parameters
startThe index of the first element to remove.
lenThe number of elements to remove.

If start is out of range then nothing is done. If there are fewer than len elements at start then all remaining are removed.

Definition at line 291 of file path.h.

template<char Delim>
std::string path< Delim >::element ( size_t  which) const
inline

Return a path element by index.

Parameters
whichIndex to the requested element
Returns
The indexed path element as a string.

If index is out of range an std::out_of_range exception is thrown.

Definition at line 122 of file path.h.

template<char Delim>
size_t path< Delim >::elements ( ) const
inline

Get the number of elements in the path

Returns
The number of elements in the path.

The returned value is the number of Delim characters in the path, even if there is a trailing Delim.

Definition at line 101 of file path.h.

template<char Delim>
element_pos path< Delim >::get_section ( size_t  start,
size_t  len = std::string::npos 
) const
inlineprivate

Get the location of a section of the path.

Parameters
startThe index of the first element of the required section.
lenThe number of elements in the required section. Defaults to std::string::npos to request all elements from start.
Returns
An element_pos that delimits the required section.

Definition at line 406 of file path.h.

template<char Delim>
void path< Delim >::insert ( size_t  after,
std::string const &  str 
)
inline

Insert a string into the path.

Parameters
afterThe index of the element after which to insert.
strThe string to insert.

If after < elements() - 1, then str is inserted following element after and delimited from the next element by a Delim character. Otherwise the member function is equivalent to append(str).

Definition at line 272 of file path.h.

template<char Delim>
bool path< Delim >::is_prefix_of ( path< Delim > const &  other) const
inline

Say whether the path consists of an initial subsequence of the elements of another.

Parameters
otherThe path to be compared with *this.
Returns
True if this path consists of an initial subsequence of the elements of other.

Definition at line 149 of file path.h.

template<char Delim>
path path< Delim >::operator+ ( std::string const &  str) const
inline

Get the concatenation of the path with a string.

Parameters
strThe string to be appended.
Returns
A new path that results from appending str to a copy of *this with an intervening Delim.
The new path inherits the cursor of `*this`

Definition at line 233 of file path.h.

template<char Delim>
path path< Delim >::operator+ ( path< Delim > const &  rhs) const
inline

Get the concatenation of the path with another.

Parameters
rhsThe path to be appended.
Returns
A new path that results from appending rhs to a copy of *this with an intervening Delim.

The new path inherits the cursor of *this

Definition at line 247 of file path.h.

template<char Delim>
int& path< Delim >::posn ( )
inline

Get a [const] reference to the path's cursor.

Returns
A [const] reference to the cursor.

The cursor can be used to maintain and modify an index into the path's elements.

No operation on a path except assignment modifies its cursor, even if the operation leaves the cursor out of range.

Definition at line 333 of file path.h.

template<char Delim>
void path< Delim >::push_back ( std::string const &  str)
inline

Append a string to the path.

Parameters
strThe string to append.

A trailing Delim character is appended if not already present, then str. str is not tested non-empty or for a leading Delim.

Definition at line 178 of file path.h.

template<char Delim>
void path< Delim >::push_front ( std::string const &  str)
inline

Prepend a string to the path.

Parameters
strThe string to prepend.

A leading Delim char is prepended if not already present, then str. str is not tested non-empty or for a trailing Delim.

Definition at line 204 of file path.h.

template<char Delim>
void path< Delim >::rectify ( )
inline

Correct anomalies in the path.

All sequences of > 1 Delim are reduced to 1 and any trailing Delim is removed.

Definition at line 309 of file path.h.

template<char Delim>
std::string path< Delim >::segment ( size_t  start = 0,
size_t  len = std::string::npos 
) const
inline

Get a sub-sequence of the path's elements as a string.

Parameters
startIndex of the first element of the requested sub-sequence.
lenNumber of elements in the requested subsequence.
Returns
The requested sub-sequence as a string. If start is out of range an empty string is returned. If there are fewer than len elements at start then all remaining are returned.

Definition at line 165 of file path.h.

template<char Delim>
int path< Delim >::to_end ( )
inline

Set the cursor to index the final element, or to 0 if the path is empty.

Returns
The new cursor position.

Definition at line 344 of file path.h.


The documentation for this struct was generated from the following file: