Filesystem abstraction prototype - Paths "finished"

Untested and I might have forgotten some useful shit, but ah well.
This commit is contained in:
Emmanuel BENOîT 2017-12-26 13:18:01 +01:00
parent 81a8390606
commit f5b4113806
2 changed files with 158 additions and 0 deletions

View file

@ -180,3 +180,147 @@ int32_t T_FSPath::compareTo(
}
return cmp;
}
/*----------------------------------------------------------------------------*/
T_FSPath T_FSPath::parent( ) const noexcept
{
T_FSPath p{ *this };
if ( p.components_.size( ) ) {
p.components_.removeLast( );
}
return p;
}
T_FSPath T_FSPath::child(
T_String const& name ) const noexcept
{
T_FSPath c{ *this };
c.components_.add( name );
c.valid_ = c.valid_ && IsValidComponent( name );
return c;
}
T_FSPath T_FSPath::operator +(
T_FSPath const& other ) const noexcept
{
T_FSPath p{ *this };
if ( other.root_ ) {
p.valid_ = false;
} else {
p.valid_ = p.valid_ && other.valid_;
}
p.components_.addAll( other.components_ );
return p;
}
/*----------------------------------------------------------------------------*/
bool T_FSPath::inDirectoryOf(
T_FSPath const& other ) const noexcept
{
if ( &other == this ) {
return false;
}
if ( M_CMPSTR_( root_ , other.root_ ) != 0 ) {
return false;
}
const auto nc{ components_.size( ) };
if ( nc != other.components_.size( ) || nc == 0 ) {
return false;
}
for ( auto i = 0u ; i < nc - 1 ; i ++ ) {
if ( M_CMPSTR_( components_[ i ] , other.components_[ i ] ) != 0 ) {
return false;
}
}
return M_CMPSTR_( components_[ nc - 1 ] , other.components_[ nc - 1 ] ) != 0;
}
bool T_FSPath::isParentOf(
T_FSPath const& other ) const noexcept
{
if ( &other == this ) {
return false;
}
if ( M_CMPSTR_( root_ , other.root_ ) != 0 ) {
return false;
}
const auto nc{ components_.size( ) };
if ( nc + 1 != other.components_.size( ) ) {
return false;
}
for ( auto i = 0u ; i < nc ; i ++ ) {
if ( M_CMPSTR_( components_[ i ] , other.components_[ i ] ) != 0 ) {
return false;
}
}
return true;
}
bool T_FSPath::isAbove(
T_FSPath const& other ) const noexcept
{
if ( &other == this ) {
return false;
}
if ( M_CMPSTR_( root_ , other.root_ ) != 0 ) {
return false;
}
const auto nc{ components_.size( ) };
if ( nc >= other.components_.size( ) ) {
return false;
}
for ( auto i = 0u ; i < nc ; i ++ ) {
if ( M_CMPSTR_( components_[ i ] , other.components_[ i ] ) != 0 ) {
return false;
}
}
return true;
}
/*----------------------------------------------------------------------------*/
bool T_FSPath::isCanonical( ) const noexcept
{
if ( !valid_ ) {
return false;
}
auto const nc{ components_.size( ) };
for ( auto i = 0u ; i < nc ; i ++ ) {
if ( components_[ i ] == "." || components_[ i ] == ".." ) {
return false;
}
}
return nc > 0;
}
T_FSPath T_FSPath::canonical( ) const noexcept
{
auto const nc{ components_.size( ) };
if ( !( valid_ && nc ) ) {
return *this;
}
T_FSPath np;
np.root_ = root_;
for ( auto i = 0u ; i < nc ; i ++ ) {
auto const& cmp{ components_[ i ] };
if ( cmp == ".." ) {
if ( np.components_.size( ) ) {
np.components_.removeLast( );
}
} else if ( cmp != "." ) {
np.components_.add( cmp );
}
}
return np;
}

View file

@ -268,6 +268,20 @@ inline bool T_FSPath::operator >=(
return compareTo( other ) >= 0;
}
/*----------------------------------------------------------------------------*/
inline bool T_FSPath::isChildOf(
T_FSPath const& other ) const noexcept
{
return other.isParentOf( *this );
}
inline bool T_FSPath::isUnder(
T_FSPath const& other ) const noexcept
{
return other.isAbove( *this );
}
} // namespace ebcl
#endif // _H_EBCL_FILESYSTEM