Skip to content

Commit

Permalink
saving work on OS/2
Browse files Browse the repository at this point in the history
  • Loading branch information
labster committed Apr 2, 2013
1 parent 1c3502f commit f7b50c5
Showing 1 changed file with 54 additions and 21 deletions.
75 changes: 54 additions & 21 deletions lib/File/Spec/OS2.pm
@@ -1,27 +1,60 @@
use v6;
use File::Spec::Unix;
class File::Spec::OS2 is File::Spec::Unix;
use File::Spec::Win32;
class File::Spec::OS2 is File::Spec::Win32;

my $module = "File::Spec::Unix";
#require $module;
# Some regexes we use for path splitting
my $driveletter = regex { <[a..z A..Z]> ':' }
my $slash = regex { '/' | '\\' }

method canonpath { ::($module).canonpath() }
method catdir { ::($module).catdir() }
method catfile { ::($module).catfile() }
method tmpdir { ::($module).tmpdir() }
method no_upwards { ::($module).no_upwards() }

#method curdir { ::($module).curdir() }
#method updir { ::($module).updir() }
method devnull { '/dev/nul' }
#method rootdir { ::($module).rootdir() }
method case_tolerant { True }
method default_case_tolerant { True }
method file_name_is_absolute { ::($module).file_name_is_absolute() }
method path { ::($module).path() }
method join { ::($module).join() }
method splitpath { ::($module).splitpath() }
method splitdir ($dirs) { $dirs.split: /<[\\\/]>/ }
method catpath { ::($module).catpath() }
method abs2rel { ::($module).abs2rel() }
method rel2abs { ::($module).rel2abs() }

method file_name_is_absolute ($file) { so $file ~~ /^ <$driveletter>? <$slash>/ }

method path {
%*ENV<PATH>\
.subst(:global, '\\', '/')\
.split(';')\
.map: { $_ eq '' ?? '.' !! $_ };
}


method tmpdir {
state $tmpdir;
return $tmpdir if $tmpdir.defined;
return $tmpdir = self._tmpdir(
|%*ENV<TMPDIR TEMP TMP>,
|< /tmp / >,
self.curdir
);
}

method catdir (*@dirs) {
my $path = @dirs\
.subst(:global, '\\', '/')\
.map( { $_ ~~ m'/' $?? $_ ~ '/' !! $_ } )\
.join;
self.canonpath($path);
}

sub canonpath ($path) {
return unless defined $path;

$path ~~ s/^<[a..z]>:/{ uc $() }/;
$path ~~ s:g\\/❚; #::
#$path =~ s| ([^/])/+ |$1/|g; # xx////xx -> xx/xx
#$path ~~ s:g❚'/'** 2..*❚/❚; #:: # xx////xx -> xx/xx
#$path ~~ s:g❚'/.'+ '/'❚/❚; #:: # xx/././xx -> xx/xx
$path ~~ s:g'/' ['.'? '/']+ ❚/❚; #:: # xx///xx/././xx -> xx/xx/xx
$path ~~ s^ './'+ <?before <-[/]>>❚❚; # ./xx -> xx
$path ~~ s'/' $❚❚
unless $path ~~ m^ <$driveletter>? '/' $❚; # xx/ -> xx
$path ~~ s^ '/..' $/❚; # /.. -> /
$path ~~ s^ '/..'+ ❚❚; # /../xx -> /xx
return $path;
}




0 comments on commit f7b50c5

Please sign in to comment.