Changeset 19288 for ext

Show
Ignore:
Timestamp:
12/29/07 21:30:36 (13 months ago)
Author:
lwall
Message:

[File-Util] patch from Mark A. Hershberger++

Location:
ext/File-Util
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • ext/File-Util/lib/File/Util.pm

    r17729 r19288  
    160160} 
    161161 
    162 method list_dir (Str $dirname, %options?){ 
    163      
     162method list_dir (Str $dirname, *@options){ 
    164163    # my $maxd = $.maxdives; 
    165164    my $maxd = 12; 
     
    197196    my @shadow = @dirs; 
    198197    @dirs = (); 
     198    my @files = (); 
     199    my @others = (); 
    199200    while @shadow { 
    200201        my $f = @shadow.shift; 
     202        if(@options.grep:{$_ eqv '--no-fsdots'}) { 
    201203        next if $f eq '.'; 
    202204        next if $f eq '..'; 
    203         next if $f ~~ /^\./; 
    204  
    205         my $pathname = $path ~ "/" ~ $f; 
    206         if $pathname ~~ :d { 
    207             @dirs.push($path ~ "/" ~ $f); 
    208205        } 
    209     } 
    210      
    211     for @dirs -> $dir { 
    212         self.list_dir($dir); 
    213     } 
     206 
     207        my $fullpath = $path ~ "/" ~ $f; 
     208        my $pathname; 
     209 
     210        if(@options.grep:{$_ eqv '--with-paths'}) { 
     211          $pathname = $fullpath; 
     212        } else { 
     213          $pathname = $f; 
     214        } 
     215 
     216        given $fullpath { 
     217                         when :d { 
     218                           @dirs.push($pathname); 
     219        } 
     220                         when :f { 
     221                           @files.push($pathname); 
     222    } 
     223                         default { 
     224                           @others.push($pathname); 
     225                         } 
     226                        }; 
     227      }; 
     228 
     229    my @ret; 
     230     
     231    if(@options.grep:{$_ eqv '--files-only'}) { 
     232      @ret.push(@files); 
     233    } 
     234    elsif(@options.grep:{$_ eqv '--dirs-only'}) { 
     235      @ret.push(@dirs); 
     236    } 
     237    else { 
     238      @ret.push(@dirs, @files, @others); 
     239    } 
     240 
    214241    # for @options -> $option { 
    215242    # } 
    216     return @dirs.sort; 
     243    return @ret.sort; 
    217244} 
    218245 
  • ext/File-Util/t/03.dirs.t

    r17728 r19288  
    88 
    99my $f = File::Util.new; 
     10my $d = 'ext/File-Util/t'; 
    1011 
    11 my @files = $f.list_dir('ext/File-Util/t'); 
     12my @files = $f.list_dir($d); 
     13ok( @files ); 
    1214 
    13 ok( @files ); 
     15@files = $f.list_dir($d, '--no-fsdots'); 
     16ok( @files.grep:{/^\.\.$/} == 0 ); # only .svn 
     17 
     18@files = $f.list_dir($d, '--dirs-only'); 
     19ok( @files.grep:{$_ ~~ :d} == @files && @files > 1 ); 
     20 
     21@files = $f.list_dir($d, '--dirs-only', '--no-fsdots'); 
     22ok( @files == 1 && @files.grep:{$_ ~~ :d} == 1 ); # only .svn 
     23 
     24@files = $f.list_dir($d, '--files-only'); 
     25ok( @files.grep:{"$d/$_" ~~ :f} == @files ); 
     26 
     27@files = $f.list_dir($d, '--with-paths'); 
     28ok( @files.grep:{$_.substr(0, $d.chars) eqv $d} > 0 && 
     29    @files.grep:{$_.substr(0, $d.chars) !eqv $d} == 0 );