Class: Recls::FileSearch

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/recls/recls.rb,
lib/recls/filesearch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_root, patterns, flags) ⇒ FileSearch

Returns a new instance of FileSearch.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/recls/filesearch.rb', line 49

def initialize(search_root, patterns, flags)

  if not search_root
    search_root = '.'
  else
    search_root = search_root.to_s
  end
  search_root = '.' if search_root.empty?
  search_root = File.expand_path(search_root) if '~' == search_root[0]

  case patterns
  when NilClass
    patterns = []
  when String
    patterns = patterns.split(/[|#{Recls::Ximpl::OS::PATH_SEPARATOR}]/)
  when Array
  else
    patterns = patterns.to_a
  end

  patterns = [ Recls::WILDCARDS_ALL ] if patterns.empty?

  if(0 == (Recls::TYPEMASK & flags))
    flags |= Recls::FILES
  end

  # now de-dup the patterns, to avoid duplicates in search
  patterns = patterns.flatten
  patterns = patterns.uniq

  @search_root = search_root
  @patterns  =  patterns
  @flags   =  flags
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



86
87
88
# File 'lib/recls/filesearch.rb', line 86

def flags
  @flags
end

#patternsObject (readonly) Also known as: pattern

Returns the value of attribute patterns.



85
86
87
# File 'lib/recls/filesearch.rb', line 85

def patterns
  @patterns
end

#search_rootObject (readonly) Also known as: searchRoot

Returns the value of attribute search_root.



84
85
86
# File 'lib/recls/filesearch.rb', line 84

def search_root
  @search_root
end

Instance Method Details

#each(&blk) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/recls/filesearch.rb', line 88

def each(&blk)

  search_root = @search_root
  search_root = Recls::Ximpl::absolute_path search_root

  search_root = search_root.gsub(/\\/, '/') if Recls::Ximpl::OS::OS_IS_WINDOWS

  # set the (type part of the) flags to zero if we want
  # everything, to facilitate later optimisation

  flags = @flags

  if(Recls::Ximpl::OS::OS_IS_WINDOWS)
    mask = (Recls::FILES | Recls::DIRECTORIES)
  else
    mask = (Recls::FILES | Recls::DIRECTORIES | Recls::LINKS | Recls::DEVICES)
  end

  if(mask == (mask & flags))
    flags = flags & ~Recls::TYPEMASK
  end

  patterns = @patterns

  patterns = patterns.map do |pattern|

    pattern = pattern.gsub(/\./, '\\.')
    pattern = pattern.gsub(/\?/, '.')
    pattern = pattern.gsub(/\*/, '.*')
    pattern
  end

  search_dir = search_root
  search_root  =  Recls::Ximpl::Util.append_trailing_slash search_root

  FileSearch::search_directory_(search_root, search_dir, patterns, flags, &blk)
end