Filesystem paths - Path style support

This commit is contained in:
Emmanuel BENOîT 2018-12-23 18:17:55 +01:00
parent 96d81b9cd3
commit fc3415047f
4 changed files with 114 additions and 82 deletions
include/ebcl

View file

@ -5,8 +5,8 @@
#ifndef _H_EBCL_FILESYSTEM
#define _H_EBCL_FILESYSTEM
#include <ebcl/Utilities.hh>
#include <ebcl/Strings.hh>
#include <ebcl/Types.hh>
namespace ebcl {
@ -17,6 +17,7 @@ enum class E_FSPathSeparator
{
SLASH , BACKSLASH
};
using T_OptPathSeparator = T_Optional< E_FSPathSeparator >;
// The style of a filesystem path.
class T_FSPathStyle
@ -72,8 +73,10 @@ class T_FSPathStyle
static constexpr T_FSPathStyle Windows( );
static constexpr T_FSPathStyle System( );
};
M_DECLARE_SWAP( T_FSPathStyle );
// Representation of a path
class T_FSPath
{
public:
@ -81,23 +84,31 @@ class T_FSPath
private:
// Path style
T_FSPathStyle style_;
// Root will be '/' on Unix, or a drive letter on Windows. Relative
// paths will have a blank root.
T_String root_;
T_Components components_;
// Is the path valid?
bool valid_;
// String comparison helpers
using F_StrCmp_ = int (*)( T_String const& , T_String const& );
F_StrCmp_ cmpFunction_( ) const noexcept;
public:
//----------------------------------------------------------------------
// Helpers
static bool IsValidComponent( T_String const& string ) noexcept;
static bool IsValidRoot( T_String const& string ) noexcept;
//----------------------------------------------------------------------
// Basic constructors and assignment operators
T_FSPath( ) noexcept;
T_FSPath( T_FSPathStyle style = T_FSPathStyle::System( ) ) noexcept;
T_FSPath( T_FSPath const& other ) noexcept;
T_FSPath& operator =( T_FSPath const& other ) noexcept;
@ -108,12 +119,15 @@ class T_FSPath
T_FSPath& swap( T_FSPath& other ) noexcept;
// Construct from a string
T_FSPath( T_String const& path ) noexcept;
T_FSPath( T_String const& path ,
T_FSPathStyle style = T_FSPathStyle::System( ) ) noexcept;
//----------------------------------------------------------------------
bool isValid( ) const noexcept;
T_FSPathStyle const& style( ) const noexcept;
T_String const& root( ) const noexcept;
T_Components const& components( ) const noexcept;
@ -123,11 +137,12 @@ class T_FSPath
//----------------------------------------------------------------------
// Convert the path into a string
T_String toString( ) const noexcept;
T_String toString( T_OptPathSeparator separator = {} ) const noexcept;
// Compute a hash value for the path
uint32_t computeHash( ) const noexcept;
// Append to a string builder
void appendTo( T_StringBuilder& sb ) const noexcept;
void appendTo( T_StringBuilder& sb ,
T_OptPathSeparator separator = {} ) const noexcept;
// Comparisons. On Windows the comparison will be case-insensitive.
int32_t compareTo( T_FSPath const& other ) const noexcept;

View file

@ -32,6 +32,11 @@ inline T_FSPathStyle& T_FSPathStyle::swap(
return *this;
}
inline M_DEFINE_SWAP( T_FSPathStyle )
{
lhs.swap( rhs );
}
/*----------------------------------------------------------------------------*/
inline constexpr bool T_FSPathStyle::caseSensitive( ) const noexcept
@ -104,21 +109,22 @@ inline constexpr T_FSPathStyle T_FSPathStyle::System( )
/*= T_FSPath =================================================================*/
inline T_FSPath::T_FSPath( ) noexcept
: root_{ } , components_{ } , valid_{ true }
inline T_FSPath::T_FSPath( T_FSPathStyle style ) noexcept
: style_{ style } , root_{ } , components_{ } , valid_{ true }
{ }
/*----------------------------------------------------------------------------*/
inline T_FSPath::T_FSPath(
T_FSPath const& other ) noexcept
: root_{ other.root_ } , components_{ other.components_ } ,
valid_{ other.valid_ }
: style_{ other.style_ } , root_{ other.root_ } ,
components_{ other.components_ } , valid_{ other.valid_ }
{ }
inline T_FSPath& T_FSPath::operator =(
T_FSPath const& other ) noexcept
{
style_ = other.style_;
root_ = other.root_;
components_ = other.components_;
valid_ = other.valid_;
@ -166,6 +172,11 @@ inline bool T_FSPath::isValid( ) const noexcept
return valid_;
}
inline T_FSPathStyle const& T_FSPath::style( ) const noexcept
{
return style_;
}
inline T_String const& T_FSPath::root( ) const noexcept
{
return root_;
@ -188,10 +199,11 @@ inline bool T_FSPath::isAbsolute( ) const noexcept
/*----------------------------------------------------------------------------*/
inline T_String T_FSPath::toString( ) const noexcept
inline T_String T_FSPath::toString(
T_OptPathSeparator separator ) const noexcept
{
T_StringBuilder sb;
appendTo( sb );
appendTo( sb , separator );
return T_String{ std::move( sb ) };
}