Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement :parent for path cleanup (except Win). Add IO::Path::QNX
Logical cleanup of parent directory option added to both of
IO::Path::Unix and IO::Spec::Unix -- which magically makes Cygwin
and QNX work too.  Physical cleanup (resolve) still NYI.
  • Loading branch information
labster committed Jul 3, 2013
1 parent c929f04 commit 827bcc4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/core/IO.pm
Expand Up @@ -379,8 +379,8 @@ my class IO::Path is Cool does IO::FileTestable {
return self.new($.SPEC.abs2rel($!path, $relative_to_directory));
}

method cleanup {
return self.new($.SPEC.canonpath($!path));
method cleanup (:$parent) {
return self.new($.SPEC.canonpath($!path, :$parent));
}
method resolve {
# NYI: requires readlink()
Expand Down Expand Up @@ -437,7 +437,7 @@ my class IO::Path is Cool does IO::FileTestable {
}

method contents(IO::Path:D: Mu :$test = none('.', '..')) {
#?if parrot

CATCH {
default {
X::IO::Dir.new(
Expand All @@ -446,7 +446,7 @@ my class IO::Path is Cool does IO::FileTestable {
).throw;
}
}

#?if parrot
my Mu $RSA := pir::new__PS('OS').readdir(nqp::unbox_s($!path));
my int $elems = nqp::elems($RSA);
gather loop (my int $i = 0; $i < $elems; $i = $i + 1) {
Expand Down Expand Up @@ -484,6 +484,7 @@ my class IO::Path is Cool does IO::FileTestable {
my class IO::Path::Unix is IO::Path { method SPEC { IO::Spec::Unix }; }
my class IO::Path::Win32 is IO::Path { method SPEC { IO::Spec::Win32 }; }
my class IO::Path::Cygwin is IO::Path { method SPEC { IO::Spec::Cygwin }; }
my class IO::Path::QNX is IO::Path { method SPEC { IO::Spec::QNX }; }


sub dir(Cool $path = '.', Mu :$test = none('.', '..')) {
Expand Down
8 changes: 6 additions & 2 deletions src/core/IO/Spec/Unix.pm
Expand Up @@ -2,17 +2,21 @@ my class IO::Spec { ... }

my class IO::Spec::Unix {

method canonpath( $path is copy --> Str ) {
method canonpath( $path is copy, :$parent --> Str) {
return '' if $path eq '';

$path ~~ s:g { '//' '/'* } = '/'; # xx////xx -> xx/xx
$path ~~ s:g { '/.'+ ['/' | $] } = '/'; # xx/././xx -> xx/xx
$path ~~ s { ^ './' <!before $> } = ''; # ./xx -> xx
if $parent {
while $path ~~ s:g { [^ | <?after '/'>] <!before '../'> <-[/]>+ '/..' ['/' | $ ] } = '' { };
$path = '.' if $path eq '';
}
$path ~~ s { ^ '/..'+ ['/' | $] } = '/'; # /../..(/xx) -> /(xx)
unless $path eq "/" {
$path ~~ s { '/' $ } = ''; # xx/ -> xx :)
}
$path;
$path
}

method curdir { '.' }
Expand Down

0 comments on commit 827bcc4

Please sign in to comment.