Class: File
- Inherits:
-
Object
- Object
- File
- Defined in:
- lib/fileutil.rb
Instance Method Summary collapse
-
#ffind(*args) ⇒ Object
find the last matching text forwards starting from the current position.
-
#find! ⇒ Object
find next (in the same direction with last search).
-
#findall(*args) ⇒ Object
find all matches in the file regardless of the current position.
-
#head(n, cur = false, reset = true) ⇒ Object
unix head like utility, returns lines as an array.
-
#line_end ⇒ Object
move to the last char of current line.
-
#line_start ⇒ Object
move to the first char of current line.
-
#rfind(*args) ⇒ Object
find the last matching text backwards starting from the current position.
-
#tail(n, cur = false, reset = true) ⇒ Object
unix tail like utility, returns lines as an array.
Instance Method Details
#ffind(*args) ⇒ Object
find the last matching text forwards starting from the current position. accepts multiple search strings and/or regexes, returns the position of the first one it encounters, also move to its position
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/fileutil.rb', line 65 def ffind *args p = pos re = Regexp.union(args) @find_registry = { :method => :ffind, :re => re } buffer = 512 begin psearch = pos chunk = read(buffer) break if eof? seek(-80,1) check = chunk.index(re) end while check.nil? begin seek(psearch + check) return pos rescue seek p return nil end end |
#find! ⇒ Object
find next (in the same direction with last search)
4 5 6 7 8 9 10 11 |
# File 'lib/fileutil.rb', line 4 def find! if @find_registry[:method] == :ffind seek(1,1) ffind(@find_registry[:re]) else rfind(@find_registry[:re]) end end |
#findall(*args) ⇒ Object
find all matches in the file regardless of the current position
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fileutil.rb', line 14 def findall *args p = pos seek(0) results = [ ffind(*args) ] while !find!.nil? results.push pos end seek(p) return results end |
#head(n, cur = false, reset = true) ⇒ Object
unix head like utility, returns lines as an array. optional arguments cur for start from current line (default is start) from beggining; reset for do not change current position (default).
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/fileutil.rb', line 90 def head(n, cur=false, reset=true) #eof guard p = pos line_start lines = [] seek(0) unless cur for i in 1..n break if eof? lines.push(readline.chomp) end seek(p) if reset return lines end |
#line_end ⇒ Object
move to the last char of current line
134 135 136 |
# File 'lib/fileutil.rb', line 134 def line_end return ffind(/$/) end |
#line_start ⇒ Object
move to the first char of current line
127 128 129 130 131 132 |
# File 'lib/fileutil.rb', line 127 def line_start unless readchar == "\n" return rfind(/^/) end return pos end |
#rfind(*args) ⇒ Object
find the last matching text backwards starting from the current position. accepts multiple search strings and/or regexes, returns the position of the first one it encounters, also move to its position
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fileutil.rb', line 29 def rfind *args p = pos psearch = p re = Regexp.union(args) @find_registry = { :method => :rfind, :re => re } begin break if psearch == 0 512 > pos ? buffer = pos : buffer = 512 # buffer = [512, p].min seek(-buffer, 1) psearch = pos chunk = read(buffer+80) seek(-chunk.length, 1) check = chunk.rindex(re) if !check.nil? and buffer <= check chunk.slice!(-(chunk.length-buffer)..-1) check = chunk.rindex(re) end end while check.nil? or buffer <= check begin if buffer <= check seek p return nil else seek(psearch + check) return pos end rescue seek p return nil end end |
#tail(n, cur = false, reset = true) ⇒ Object
unix tail like utility, returns lines as an array. optional arguments cur for start from current line (default is start) from end; reset for do not change current position (default).
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/fileutil.rb', line 108 def tail(n, cur=false, reset=true) p = pos chunks = '' lines = 0 cur ? line_end : seek(size) begin p1 = pos p1 < 512 ? buffer = p1 : buffer = 512 seek(-buffer, 1) chunk = read(buffer) lines += chunk.count("\n") chunks = chunk + chunks seek(-chunk.size,1) end while lines < ( n + 1 ) && p1 != 0 ary = chunks.split(/\n/) reset ? seek(p) : seek(p1-1-(ary.last(n).join("\n").length)) return ary.last(n) end |