summaryrefslogtreecommitdiff
path: root/luaext/linit.lua
blob: 7b134ba7541a2173c2e95184b0acc8acceb1bf84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

rex.new = rex.newPOSIX

util = {}

function util.grep(expr, filename)
	if not posix.stat(filename, "mode") then
		return nil
	end
	local lines = {}
	local pat = rex.new(expr)
	local pos = 1
	for line in io.lines(filename) do
		if pat:match(line) then
			table.insert(lines, pos, line)
		end
		pos = pos + 1
	end
	if table.getn(lines) == 0 then
		return nil
	end
	return lines
end

function util.igrep(expr, filename)
	return ipairs(rex.grep(expr, filename))
end

function util.bgrep(expr, filename)
	if not posix.stat(filename, "mode") then
		return nil
	end
	local pat = rex.new(expr)
	for line in io.lines(filename) do
		if pat:match(line) then
			return true
		end
	end
	return false
end