Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/fileutil.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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fileutil.rb', line 86

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)


42
43
44
45
46
47
48
# File 'lib/fileutil.rb', line 42

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/fileutil.rb', line 19

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). head leaves cursor at the beginning of the first line it doesn’t return if reset false



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fileutil.rb', line 112

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 unless lines.length == 0
  return nil
end

#line_endObject

move to the last char of current line



167
168
169
# File 'lib/fileutil.rb', line 167

def line_end
  return ff(/$/)
end

#line_startObject

move to the first char of current line



157
158
159
160
161
162
163
164
165
# File 'lib/fileutil.rb', line 157

def line_start
  if pos > 0
    seek(-1,1) 
    return self if readchar == "\n" 
  elsif pos == 0
    return self
  end
  return rew(/^/) 
end

#repeatObject

repeat last ff/rew command



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fileutil.rb', line 3

def repeat
  if @find_registry[:method] == :ff
    seek(1,1)
    if ff(@find_registry[:re]).nil?
      seek(-1,1) 
      return nil
    else
      return self
    end
  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



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/fileutil.rb', line 52

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)


32
33
34
35
36
37
38
# File 'lib/fileutil.rb', line 32

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). tail leaves ‘cursor’ at the end of last line it doesn’t return if reset false



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fileutil.rb', line 133

def tail(n, cur=false, reset=true)
  p = pos
  chunks = '$'
  lines = 0
  cur ? line_end.seek(1,1) : 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 > buffer
  ary = chunks.split(/\n/)
  ary.pop
  if reset 
    seek(p) 
  else
    seek(p1-2-(ary.last(n).join("\n").length)) rescue seek 0
  end
  return ary.last(n).reverse
end