Filesystem - Path style abstraction

A class that carries information about a filesystem path's style: case
sensitivity, path separator and whether it supports drive letters.
This commit is contained in:
Emmanuel BENOîT 2018-12-19 08:28:50 +01:00
parent 811916a4a3
commit ee02f9c5c7
2 changed files with 140 additions and 1 deletions

View file

@ -5,13 +5,68 @@
#ifndef _H_EBCL_FILESYSTEM
#define _H_EBCL_FILESYSTEM
#include <ebcl/Strings.hh>
#include <ebcl/Utilities.hh>
#include <ebcl/Strings.hh>
namespace ebcl {
/*= FILESYSTEM PATH ==========================================================*/
// Supported filesystem path separators
enum class E_FSPathSeparator
{
SLASH , BACKSLASH
};
// The style of a filesystem path.
class T_FSPathStyle
{
private:
bool caseSensitive_;
bool hasDriveLetter_;
T_Flags< E_FSPathSeparator > pathSeparators_;
public:
//----------------------------------------------------------------------
// Constructors and assignment operators
T_FSPathStyle( ) = delete;
// Initialise the filesystem path style, specifying:
// - whether it is case-sensitive,
// - whether is can use a drive letter as its root,
// - which separators it supports
T_FSPathStyle( bool cs , bool hdl , T_Flags< E_FSPathSeparator > ps ) noexcept;
// Default copy/move constructors/assignement operators
T_FSPathStyle( T_FSPathStyle const& ) noexcept = default;
T_FSPathStyle& operator =( T_FSPathStyle const& ) noexcept = default;
T_FSPathStyle( T_FSPathStyle&& ) noexcept = default;
T_FSPathStyle& operator =( T_FSPathStyle&& ) noexcept = default;
T_FSPathStyle& swap( T_FSPathStyle& other ) noexcept;
//----------------------------------------------------------------------
// Accessors
bool caseSensitive( ) const noexcept;
T_FSPathStyle& caseSensitive( bool cs ) noexcept;
bool hasDriveLetter( ) const noexcept;
T_FSPathStyle& hasDriveLetter( bool hdl ) noexcept;
T_Flags< E_FSPathSeparator > pathSeparators( ) const noexcept;
T_FSPathStyle& pathSeparator( T_Flags< E_FSPathSeparator > ps ) noexcept;
//----------------------------------------------------------------------
// Pre-initialised styles
static T_FSPathStyle Unix( );
static T_FSPathStyle Windows( );
static T_FSPathStyle System( );
};
class T_FSPath
{
public:

View file

@ -9,6 +9,90 @@
namespace ebcl {
/*= T_FSPathStyle ============================================================*/
inline T_FSPathStyle::T_FSPathStyle(
bool cs ,
bool hdl ,
T_Flags< E_FSPathSeparator > ps ) noexcept
: caseSensitive_{ cs } , hasDriveLetter_{ hdl } , pathSeparators_{ ps }
{
assert( ps );
}
/*----------------------------------------------------------------------------*/
inline T_FSPathStyle& T_FSPathStyle::swap(
T_FSPathStyle& other ) noexcept
{
using std::swap;
swap( caseSensitive_ , other.caseSensitive_ );
swap( hasDriveLetter_ , other.hasDriveLetter_ );
swap( pathSeparators_ , other.pathSeparators_ );
return *this;
}
/*----------------------------------------------------------------------------*/
inline bool T_FSPathStyle::caseSensitive( ) const noexcept
{
return caseSensitive_;
}
inline T_FSPathStyle& T_FSPathStyle::caseSensitive( bool cs ) noexcept
{
caseSensitive_ = cs;
return *this;
}
inline bool T_FSPathStyle::hasDriveLetter( ) const noexcept
{
return hasDriveLetter_;
}
inline T_FSPathStyle& T_FSPathStyle::hasDriveLetter( bool hdl ) noexcept
{
hasDriveLetter_ = hdl;
return *this;
}
inline T_Flags< E_FSPathSeparator > T_FSPathStyle::pathSeparators( ) const noexcept
{
return pathSeparators_;
}
inline T_FSPathStyle& T_FSPathStyle::pathSeparator(
T_Flags< E_FSPathSeparator > ps ) noexcept
{
assert( ps );
pathSeparators_ = ps;
return *this;
}
/*----------------------------------------------------------------------------*/
inline T_FSPathStyle T_FSPathStyle::Unix( )
{
return T_FSPathStyle{ true , false ,
T_Flags< E_FSPathSeparator >{ E_FSPathSeparator::SLASH } };
}
inline T_FSPathStyle T_FSPathStyle::Windows( )
{
return T_FSPathStyle{ false , true ,
T_Flags< E_FSPathSeparator >{ E_FSPathSeparator::BACKSLASH } };
}
inline T_FSPathStyle T_FSPathStyle::System( )
{
#ifdef _WIN32
return T_FSPathStyle::Windows( );
#else
return T_FSPathStyle::Unix( );
#endif
}
/*= T_FSPath =================================================================*/
inline T_FSPath::T_FSPath( ) noexcept