Class: File

Inherits:
Object show all
Defined in:
lib/lib/helper/lib/file.rb

Instance Method Summary collapse

Instance Method Details

#ff(*args) ⇒ Object

fast forward until it finds one of args supplied as string or regex returns nil if nothing found



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lib/helper/lib/file.rb', line 81

def ff *args
  p = pos
  re = Regexp.union(args)
  @find_registry = { :method => :ff, :re => re }
  buffer = 512
  begin
    psearch = pos
    chunk = read(buffer)
    seek(-80,1) unless eof?
    check = chunk.index(re)
  end while check.nil? and !eof?
  begin
    seek(psearch + check)
    return self
  rescue
    seek p
    return nil
  end
end

#ff?(*args) ⇒ Boolean

do not fast forward, just give the position that ff would fast forward to.

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/lib/helper/lib/file.rb', line 37

def ff? *args
  p = pos 
  p1 = ff(*args)
  p1.nil? ? p1 = nil : p1 = pos
  seek p
  return p1
end

#findall(*args) ⇒ Object

find all occurences of args (with or) in the file egardless of the current position



14
15
16
17
18
19
20
21
22
23
# File 'lib/lib/helper/lib/file.rb', line 14

def findall *args
  p = pos
  seek(0)
  results = [ ff(*args).pos ]
  while !repeat.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).



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lib/helper/lib/file.rb', line 105

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_endObject

move to the last char of current line



149
150
151
# File 'lib/lib/helper/lib/file.rb', line 149

def line_end
  return ff(/$/)
end

#line_startObject

move to the first char of current line



142
143
144
145
146
147
# File 'lib/lib/helper/lib/file.rb', line 142

def line_start
  unless readchar == "\n"
    return rew(/^/)
  end
  return self
end

#repeatObject

repeat last ff/rew command



3
4
5
6
7
8
9
10
# File 'lib/lib/helper/lib/file.rb', line 3

def repeat
  if @find_registry[:method] == :ff
    seek(1,1)
    ff(@find_registry[:re])
  else
    rew(@find_registry[:re])
  end
end

#rew(*args) ⇒ Object

rewind until it finds one of args supplied as string or regex returns nil if nothing found



47
48
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
# File 'lib/lib/helper/lib/file.rb', line 47

def rew *args
  p = pos
  psearch = p
  re = Regexp.union(args)
  @find_registry = { :method => :rew, :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 self
    end
  rescue
    seek p
    return nil
  end
end

#rew?(*args) ⇒ Boolean

do not rewind, just give the position that rew would rewind to.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/lib/helper/lib/file.rb', line 27

def rew? *args
  p = pos 
  p1 = rew(*args)
  p1.nil? ? p1 = nil : p1 = pos
  seek p
  return p1
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).



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/lib/helper/lib/file.rb', line 123

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