fileutil 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/file.rb +137 -0
- metadata +45 -0
data/lib/file.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
class File
|
2
|
+
# find next (in the same direction with
|
3
|
+
# last search)
|
4
|
+
def find!
|
5
|
+
if @find_registry[:method] == :ffind
|
6
|
+
seek(1,1)
|
7
|
+
ffind(@find_registry[:re])
|
8
|
+
else
|
9
|
+
rfind(@find_registry[:re])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
# find all matches in the file
|
13
|
+
# regardless of the current position
|
14
|
+
def findall *args
|
15
|
+
p = pos
|
16
|
+
seek(0)
|
17
|
+
results = [ ffind(*args) ]
|
18
|
+
while !find!.nil?
|
19
|
+
results.push pos
|
20
|
+
end
|
21
|
+
seek(p)
|
22
|
+
return results
|
23
|
+
end
|
24
|
+
# find the last matching text backwards
|
25
|
+
# starting from the current position.
|
26
|
+
# accepts multiple search strings and/or
|
27
|
+
# regexes, returns the position of the first
|
28
|
+
# one it encounters, also move to its position
|
29
|
+
def rfind *args
|
30
|
+
p = pos
|
31
|
+
psearch = p
|
32
|
+
re = Regexp.union(args)
|
33
|
+
@find_registry = { :method => :rfind, :re => re }
|
34
|
+
begin
|
35
|
+
break if psearch == 0
|
36
|
+
512 > pos ? buffer = pos : buffer = 512 # buffer = [512, p].min
|
37
|
+
seek(-buffer, 1)
|
38
|
+
psearch = pos
|
39
|
+
chunk = read(buffer+80)
|
40
|
+
seek(-chunk.length, 1)
|
41
|
+
check = chunk.rindex(re)
|
42
|
+
if !check.nil? and buffer <= check
|
43
|
+
chunk.slice!(-(chunk.length-buffer)..-1)
|
44
|
+
check = chunk.rindex(re)
|
45
|
+
end
|
46
|
+
end while check.nil? or buffer <= check
|
47
|
+
begin
|
48
|
+
if buffer <= check
|
49
|
+
seek p
|
50
|
+
return nil
|
51
|
+
else
|
52
|
+
seek(psearch + check)
|
53
|
+
return pos
|
54
|
+
end
|
55
|
+
rescue
|
56
|
+
seek p
|
57
|
+
return nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
# find the last matching text forwards
|
61
|
+
# starting from the current position.
|
62
|
+
# accepts multiple search strings and/or
|
63
|
+
# regexes, returns the position of the first
|
64
|
+
# one it encounters, also move to its position
|
65
|
+
def ffind *args
|
66
|
+
p = pos
|
67
|
+
re = Regexp.union(args)
|
68
|
+
@find_registry = { :method => :ffind, :re => re }
|
69
|
+
buffer = 512
|
70
|
+
begin
|
71
|
+
psearch = pos
|
72
|
+
chunk = read(buffer)
|
73
|
+
break if eof?
|
74
|
+
seek(-80,1)
|
75
|
+
check = chunk.index(re)
|
76
|
+
end while check.nil?
|
77
|
+
begin
|
78
|
+
seek(psearch + check)
|
79
|
+
return pos
|
80
|
+
rescue
|
81
|
+
seek p
|
82
|
+
return nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
# unix head like utility, returns lines
|
86
|
+
# as an array. optional arguments cur for
|
87
|
+
# start from current line (default is start)
|
88
|
+
# from beggining; reset for do not change
|
89
|
+
# current position (default).
|
90
|
+
def head(n, cur=false, reset=true)
|
91
|
+
#eof guard
|
92
|
+
p = pos
|
93
|
+
line_start
|
94
|
+
lines = []
|
95
|
+
seek(0) unless cur
|
96
|
+
for i in 1..n
|
97
|
+
break if eof?
|
98
|
+
lines.push(readline.chomp)
|
99
|
+
end
|
100
|
+
seek(p) if reset
|
101
|
+
return lines
|
102
|
+
end
|
103
|
+
# unix tail like utility, returns lines
|
104
|
+
# as an array. optional arguments cur for
|
105
|
+
# start from current line (default is start)
|
106
|
+
# from end; reset for do not change
|
107
|
+
# current position (default).
|
108
|
+
def tail(n, cur=false, reset=true)
|
109
|
+
p = pos
|
110
|
+
chunks = ''
|
111
|
+
lines = 0
|
112
|
+
cur ? line_end : seek(size)
|
113
|
+
begin
|
114
|
+
p1 = pos
|
115
|
+
p1 < 512 ? buffer = p1 : buffer = 512
|
116
|
+
seek(-buffer, 1)
|
117
|
+
chunk = read(buffer)
|
118
|
+
lines += chunk.count("\n")
|
119
|
+
chunks = chunk + chunks
|
120
|
+
seek(-chunk.size,1)
|
121
|
+
end while lines < ( n + 1 ) && p1 != 0
|
122
|
+
ary = chunks.split(/\n/)
|
123
|
+
reset ? seek(p) : seek(p1-1-(ary.last(n).join("\n").length))
|
124
|
+
return ary.last(n)
|
125
|
+
end
|
126
|
+
# move to the first char of current line
|
127
|
+
def line_start
|
128
|
+
unless readchar == "\n"
|
129
|
+
return rfind(/^/)
|
130
|
+
end
|
131
|
+
return pos
|
132
|
+
end
|
133
|
+
# move to the last char of current line
|
134
|
+
def line_end
|
135
|
+
return ffind(/$/)
|
136
|
+
end
|
137
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fileutil
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sencer Selcuk
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-11 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: use ffind, rfind, findall, find! to find text in a file etc.
|
15
|
+
email: sencer@selc.uk
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/file.rb
|
21
|
+
homepage:
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.23
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Search text in a file, head, tail
|
45
|
+
test_files: []
|