Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Borrow chopped up versions of labster++'s IO::FileTestable and IO::Path.
  • Loading branch information
colomon committed Jun 3, 2013
1 parent 94cad70 commit 95dde43
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 1 deletion.
160 changes: 159 additions & 1 deletion lib/CORE.setting
Expand Up @@ -43,6 +43,7 @@ my class Enum { ... }
my class Range { ... }
my class Whatever { ... }
my class IO { ... }
our class IO::Path { ... }
my class ObjAt { ... }
my class Proxy { ... }
my class KeySet { ... }
Expand Down Expand Up @@ -976,7 +977,7 @@ my class Str is Cool {
method ACCEPTS(\t) { defined(self) ?? self eq t !! t.^does(self) }
method chars() { chars(self) }
method IO() { IO.new(path => self) }
method path() { IO.new(path => self) }
method path() { IO::Path.new(self) }
method say() { $*OUT.say(self) }
multi method WHICH(Str:D:) {
ObjAt.new(str => ("str|" ~ self), ref => self.WHAT)
Expand Down Expand Up @@ -3803,6 +3804,163 @@ my class IO {
method chmod($mode) is unsafe { Q:CgOp { (box Int (path_chmod (obj_getstr {self}) (obj_getnum {$mode}))) } }
}
role IO::FileTestable {
method f() is unsafe { Q:CgOp { (box Bool (path_file_exists (obj_getstr {$!path}))) } }
method d() is unsafe { Q:CgOp { (box Bool (path_dir_exists (obj_getstr {$!path}))) } }
method e() is unsafe { Q:CgOp { (box Bool (path_any_exists (obj_getstr {$!path}))) } }
method r() is unsafe { Q:CgOp { (box Bool (path_eaccess_readable (obj_getstr {$!path}))) } }
method R() is unsafe { Q:CgOp { (box Bool (path_access_readable (obj_getstr {$!path}))) } }
method w() is unsafe { Q:CgOp { (box Bool (path_eaccess_writable (obj_getstr {$!path}))) } }
method W() is unsafe { Q:CgOp { (box Bool (path_access_writable (obj_getstr {$!path}))) } }
method x() is unsafe { Q:CgOp { (box Bool (path_eaccess_executable (obj_getstr {$!path}))) } }
method X() is unsafe { Q:CgOp { (box Bool (path_access_executable (obj_getstr {$!path}))) } }
method o() is unsafe { Q:CgOp { (box Bool (path_eaccess_owned (obj_getstr {$!path}))) } }
method O() is unsafe { Q:CgOp { (box Bool (path_access_owned (obj_getstr {$!path}))) } }
# method l() {
# nqp::p6bool(pir::new__Ps('File').is_link(nqp::unbox_s(self.Str)))
# }
# method s() {
# self.e
# && nqp::p6box_i( nqp::stat(nqp::unbox_s(self.Str),
# nqp::const::STAT_FILESIZE) );
# }
method z() { self.e && self.s == 0; }
# method modified() {
# nqp::p6box_i(nqp::stat(nqp::unbox_s(self.Str), nqp::const::STAT_MODIFYTIME));
# }
#
# method accessed() {
# nqp::p6box_i(nqp::stat(nqp::unbox_s(self.Str), nqp::const::STAT_ACCESSTIME));
# }
#
# method changed() {
# nqp::p6box_i(nqp::stat(nqp::unbox_s(self.Str), nqp::const::STAT_CHANGETIME));
# }
}
class IO::Path is Cool does IO::FileTestable {
# method SPEC { IO::Spec.MODULE };
has Str $.basename;
has Str $.directory = '.';
has Str $.volume = '';
method dir() {
die "IO::Path.dir is deprecated in favor of .directory";
}
submethod BUILD(:$!basename, :$!directory, :$!volume, :$dir) {
die "Named paramter :dir in IO::Path.new deprecated in favor of :directory"
if defined $dir;
}
multi method new(Str:D $path) {
# This seems hacky, but mono's GetFileName and GetDirectoryName
# react weirdly to '/'
if $path eq '/' {
self.bless(*, :basename(""),
:directory("/"),
:volume(""));
} else {
self.bless(*, :basename(Q:CgOp { (box Str (path_get_file_name (obj_getstr {$path}))) }),
:directory(Q:CgOp { (box Str (path_get_directory_name (obj_getstr {$path}))) }),
:volume(""));
}
}
multi method Str(IO::Path:D:) {
sub Combine($a, $b) {
Q:CgOp { (box Str (path_combine (obj_getstr {$a}) (obj_getstr {$b}))) }.IO
}
Combine(Combine($.volume, $.directory), $.basename);
}
method gist() {
self.defined ?? "{self.^name}<{self.Str}>" !! "(IO::Path)";
}
method perl() {
"IO::Path.new(:basename({ self.basename.perl }), :directory({ self.directory.perl }), :volume({ self.volume.perl }))"
}
# multi method Numeric(IO::Path:D:) {
# self.basename.Numeric;
# }
# method Bridge(IO::Path:D:) {
# self.basename.Bridge;
# }
# method Int(IO::Path:D:) {
# self.basename.Int;
# }
method path(IO::Path:D:) {
self;
}
method IO(IO::Path:D: *%opts) {
# IO::Handle.new(:path(~self), |%opts);
IO.new(path => ~self);
}
method open(IO::Path:D: *%opts) {
open(~self, |%opts);
}
method contents(IO::Path:D: *%opts) {
dir(~self, |%opts);
}
# method is-absolute {
# $.SPEC.is-absolute(~self);
# }
# method is-relative {
# ! $.SPEC.is-absolute(~self);
# }
# method absolute ($base = $*CWD) {
# return self.new($.SPEC.rel2abs(~self, $base))
# }
# method relative ($relative_to_directory = $*CWD) {
# # return self.new($.SPEC.abs2rel(~self, $relative_to_directory));
# $base.IO.append(~self)
# }
#
# method cleanup {
# fail "Not Yet Implemented";
# # return self.new($.SPEC.canonpath(~self));
# }
# method resolve {
# fail "Not Yet Implemented: requires readlink()";
# }
# method parent {
# if self.is-absolute {
# return self.new($.SPEC.join($.volume, $.directory, ''));
# }
# elsif all($.basename, $.directory) eq $.SPEC.curdir {
# return self.new(:$.volume, directory=>$.SPEC.curdir,
# basename=>$.SPEC.updir);
# }
# elsif $.basename eq $.SPEC.updir && $.directory eq $.SPEC.curdir
# or !grep({$_ ne $.SPEC.updir}, $.SPEC.splitdir($.directory)) {
# return self.new( # If all updirs, then add one more
# :$.volume,
# directory => $.SPEC.catdir($.directory, $.SPEC.updir),
# :$.basename );
# }
# else {
# return self.new( $.SPEC.join($.volume, $.directory, '') );
# }
# }
# method child ($childname) {
# self.new($.SPEC.join: $.volume,
# $.SPEC.catdir($.directory, $.basename),
# $childname);
# }
}
my class TextReader {
method get() {
Q:CgOp {
Expand Down
2 changes: 2 additions & 0 deletions lib/CodeGen.cs
Expand Up @@ -3058,6 +3058,8 @@ class NamProcessor {
thandlers["stab_privatemethod"] = Methody(null, Tokens.STable.GetMethod("GetPrivateMethod"));
thandlers["path_file_exists"] = Methody(null, typeof(File).GetMethod("Exists"));
thandlers["path_dir_exists"] = Methody(null, typeof(Directory).GetMethod("Exists"));
thandlers["path_get_directory_name"] = Methody(null, typeof(Path).GetMethod("GetDirectoryName", new Type[] { Tokens.String }));
thandlers["path_get_file_name"] = Methody(null, typeof(Path).GetMethod("GetFileName", new Type[] { Tokens.String }));
thandlers["path_combine"] = Methody(null, typeof(Path).GetMethod("Combine", new Type[] { Tokens.String, Tokens.String }));
thandlers["path_change_ext"] = Methody(null, typeof(Path).GetMethod("ChangeExtension", new Type[] { Tokens.String, Tokens.String }));
thandlers["path_realpath"] = Methody(null, typeof(Path).GetMethod("GetFullPath"));
Expand Down
1 change: 1 addition & 0 deletions t/spectest.data
Expand Up @@ -500,6 +500,7 @@ S32-hash/push.t
S32-hash/slice.t
S32-io/chdir.t
S32-io/file-tests.t
S32-io/io-path.t
S32-io/note.t
S32-list/categorize.t
S32-list/classify.t
Expand Down

0 comments on commit 95dde43

Please sign in to comment.