Class: ALib::Find2

Inherits:
Object
  • Object
show all
Defined in:
lib/alib.rb,
lib/alib-0.3.1.rb

Overview

-*- Ruby -*- Copyright © 1998, 2001 Motoyuki Kasahara

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Traverse a file tree, replacement of ‘find.rb’.

Constant Summary collapse

FIND1 =

Constatnts

1
FIND2 =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = FIND1) ⇒ Find2

Initializer.



1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'lib/alib.rb', line 1542

def initialize(mode = FIND1)
    #--{{{
  @mode   = mode
  @depth  = false
  @follow = false
  @xdev   = false
    
  @dirname_stats = Array.new
  @found_files = Array.new
  @target_device = 0

  @handle = lambda { |file, stat_result, block|
    case @mode
      when FIND1
        block ? block.call(file) : @found_files.push(file)
      when FIND2
        block ? block.call(file, stat_result) : @found_files.push([file, stat_result])
    end
  }
    #--}}}
end

Instance Attribute Details

#depthObject (readonly)

Methods for accessing instances.



1566
1567
1568
# File 'lib/alib.rb', line 1566

def depth
  @depth
end

#followObject (readonly)

Returns the value of attribute follow.



1567
1568
1569
# File 'lib/alib.rb', line 1567

def follow
  @follow
end

#xdevObject (readonly)

Returns the value of attribute xdev.



1568
1569
1570
# File 'lib/alib.rb', line 1568

def xdev
  @xdev
end

Class Method Details

.find(*arguments, &block) ⇒ Object

Find the directory ‘dirname’. (class method)



1754
1755
1756
# File 'lib/alib.rb', line 1754

def Find2.find(*arguments, &block)
  Find2.new.find(*arguments, &block)
end

.find2(*arguments, &block) ⇒ Object

Find the directory ‘dirname’. (class method)



1760
1761
1762
# File 'lib/alib.rb', line 1760

def Find2.find2(*arguments, &block)
  Find2.new(FIND2).find(*arguments, &block)
end

.pruneObject

Prune the current visited directory.



1788
1789
1790
1791
1792
# File 'lib/alib.rb', line 1788

def Find2.prune
    #--{{{
  throw :prune
    #--}}}
end

Instance Method Details

#find(*arguments, &block) ⇒ Object

Find the directory ‘dirname’. (private)



1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
# File 'lib/alib.rb', line 1572

def find(*arguments, &block)
    #--{{{
  #
  # Parse options if specified.
  #
  #if arguments[0].type == Hash
    #parse_options(arguments.shift)
  #end

# hack
  args = []
  opts = []
  arguments.each do |arg|
    case arg
      when Hash
        opts << arg
      else
        args << arg
    end
  end
  opts.each{|opt| parse_options opt}
  files = args.flatten.compact

  #
  # If a block is not given to the `find' method, found files are
  # recorded in this array.
  #
  @dirname_stats.clear
  @found_files.clear
    
  #
  # Loop for each file in `files'.
  #
  files.each do |file|
    catch(:prune) do
    @dirname_stats.clear
    
    #
    # Get `stat' or `lstat' of `file'.
    #
    begin
      if @follow
        begin
          stat_result = File.stat(file)
        rescue Errno::ENOENT, Errno::EACCES
          stat_result = File.lstat(file)
        end
      else
        stat_result = File.lstat(file)
      end
    rescue Errno::ENOENT, Errno::EACCES
      next
    end
    
    #
    # Push `file' to the found stack, or yield with it,
    # if the depth flag is enabled.
    #
    @handle[ file, stat_result, block ] if !@depth
    
    #
    # If `file' is a directory, find files recursively.
    #
    if stat_result.directory?
      @xdev_device = stat_result.dev if @xdev
      @dirname_stats.push(stat_result)
      find_directory(file, block)
    end
    
    #
    # Push `file' to the found stack, or yield with it.
    # if the depth flag is disabled.
    #
    @handle[ file, stat_result, block ] if @depth

    end
  end

  if block == nil
    return @found_files
  else
    return nil
  end
    #--}}}
end