| 1 | package Sidef::Types::Glob::DirHandle; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use parent qw( | ||
| 7 | Sidef::Object::Object | ||
| 8 | ); | ||
| 9 | |||
| 10 | require Encode; | ||
| 11 | require File::Spec; | ||
| 12 | |||
| 13 | use Sidef::Types::Bool::Bool; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my (undef, $dh, $dir) = @_; | ||
| 17 | |||
| 18 | bless { | ||
| 19 | dh => $dh, | ||
| 20 | dir => $dir, | ||
| 21 | }, | ||
| 22 | __PACKAGE__; | ||
| 23 | } | ||
| 24 | |||
| 25 | *call = \&new; | ||
| 26 | |||
| 27 | sub get_value { | ||
| 28 | $_[0]{dh}; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub dir { | ||
| 32 | $_[0]{dir}; | ||
| 33 | } | ||
| 34 | |||
| 35 | *parent = \&dir; | ||
| 36 | |||
| 37 | sub entries { | ||
| 38 | my ($self) = @_; | ||
| 39 | |||
| 40 | $self->rewind; | ||
| 41 | |||
| 42 | my @files; | ||
| 43 | while (defined(my $file = $self->read)) { | ||
| 44 | push @files, $file; | ||
| 45 | } | ||
| 46 | Sidef::Types::Array::Array->new(\@files); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub read { # read the next entry from the self directory handle | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | my $basedir = ($self->{basedir} //= "$self->{dir}"); | ||
| 53 | |||
| 54 | { | ||
| 55 | my $file = CORE::readdir($self->{dh}) // return undef; | ||
| 56 | |||
| 57 | if ($file eq '.' or $file eq '..') { | ||
| 58 | redo; | ||
| 59 | } | ||
| 60 | |||
| 61 | my $dfile = Encode::decode_utf8($file); | ||
| 62 | my $dir = File::Spec->catdir($basedir, $dfile); | ||
| 63 | |||
| 64 | lstat($dir); | ||
| 65 | if (-l _) { redo } | ||
| 66 | ; # ignore links | ||
| 67 | |||
| 68 | return ( | ||
| 69 | (-d _) | ||
| 70 | ? Sidef::Types::Glob::Dir->new($dir) | ||
| 71 | : Sidef::Types::Glob::File->new(File::Spec->catfile($basedir, $dfile)) | ||
| 72 | ); | ||
| 73 | } | ||
| 74 | |||
| 75 | return undef; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub files { | ||
| 79 | my ($self) = @_; | ||
| 80 | Sidef::Types::Array::Array->new([grep { $_->is_file } @{$self->entries}]); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub dirs { | ||
| 84 | my ($self) = @_; | ||
| 85 | Sidef::Types::Array::Array->new([grep { $_->is_dir } @{$self->entries}]); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub tell { | ||
| 89 | my ($self) = @_; | ||
| 90 | Sidef::Types::Number::Number::_set_int(CORE::telldir($self->{dh})); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub seek { | ||
| 94 | my ($self, $pos) = @_; | ||
| 95 | CORE::seekdir($self->{dh}, CORE::int($pos)) | ||
| 96 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 97 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub rewind { | ||
| 101 | my ($self) = @_; | ||
| 102 | CORE::rewinddir($self->{dh}) | ||
| 103 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 104 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub close { | ||
| 108 | my ($self) = @_; | ||
| 109 | CORE::closedir($self->{dh}) | ||
| 110 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 111 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub chdir { | ||
| 115 | my ($self) = @_; | ||
| 116 | CORE::chdir($self->{dh}) | ||
| 117 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 118 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub stat { | ||
| 122 | my ($self) = @_; | ||
| 123 | Sidef::Types::Glob::Stat->stat($self->{dh}, $self); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub lstat { | ||
| 127 | my ($self) = @_; | ||
| 128 | Sidef::Types::Glob::Stat->lstat($self->{dh}, $self); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub iter { | ||
| 132 | my ($self) = @_; | ||
| 133 | |||
| 134 | Sidef::Types::Block::Block->new( | ||
| 135 | code => sub { | ||
| 136 | Sidef::Types::String::String->new(Encode::decode_utf8(CORE::readdir($self->{dh}) // return undef)); | ||
| 137 | } | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub each { | ||
| 142 | my ($self, $code) = @_; | ||
| 143 | |||
| 144 | while (defined(my $file = CORE::readdir($self->{dh}))) { | ||
| 145 | $code->run(Sidef::Types::String::String->new(Encode::decode_utf8($file))); | ||
| 146 | } | ||
| 147 | |||
| 148 | $self; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub map { | ||
| 152 | my ($self, $block) = @_; | ||
| 153 | Sidef::Types::Array::Array->new([CORE::map { $block->run($_) } @{$self->entries}]); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub grep { | ||
| 157 | my ($self, $block) = @_; | ||
| 158 | Sidef::Types::Array::Array->new([CORE::grep { $block->run($_) } @{$self->entries}]); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub walk { | ||
| 162 | my ($self, $code) = @_; | ||
| 163 | foreach my $entry (@{$self->entries}) { | ||
| 164 | $code->run($entry); | ||
| 165 | if ($entry->is_dir) { | ||
| 166 | my $dh = $entry->open() // next; | ||
| 167 | $dh->walk($code); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | $self; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub count { | ||
| 174 | my ($self) = @_; | ||
| 175 | Sidef::Types::Number::Number::_set_int(scalar(@{$self->entries})); | ||
| 176 | } | ||
| 177 | |||
| 178 | 1 |