summaryrefslogtreecommitdiffstats
path: root/lib/format.lua
blob: e19899f1180244d4d39e5d48f4d34ca46a79b375 (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
42
43
44
45
46
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
--[[
	module for format changes in tables and strings
	try to keep non input specific
]]--

module (..., package.seeall)

-- find all return characters and removes them, may get this from a browser
-- that is why didn't do file specific

function dostounix ( str )
	local data = string.gsub(str, "\r", "")
	return data
end

-- search and remove all blank and commented lines from a string or table of lines
-- returns a table to iterate over without the blank or commented lines

function parse_lines ( input )
	local lines = {}

	function parse(line)
		if not string.match(line, "^%s*$") and not string.match(line, "^%s*#") then
			lines[#lines + 1] = line
		end
	end

	if type(input) == "string" then
		for line in string.gmatch(input, "([^\n]*)\n?") do
			parse(line)
		end
	elseif type(input) == "table" then
		for i,line in ipairs(input) do
			parse(line)
		end
	end	
	
	return lines
end

-- search and remove all blank and commented lines from a string or table of lines
-- parse the lines for words, looking for quotes and removing comments
-- returns a table with an array of words for each line

function parse_linesandwords ( input )
	local lines = {}
	local linenum = 0

	function parse(line)
		linenum = linenum + 1
		if not string.match(line, "^%s*$") and not string.match(line, "^%s*#") then
			local linetable = {linenum=linenum, line=line}
			local offset = 1
			while string.find(line, "%S", offset) do
				local word = string.match(line, "%S+", offset)
				local endword
				if string.find(word, "^#") then
					break
				elseif string.find(word, "^\"") then
					endword = select(2, string.find(line, "\"[^\"]*\"", offset))
					word = string.sub(line, string.find(line, "\"", offset), endword)
				else
					endword = select(2, string.find(line, "%S+", offset))
				end
				table.insert(linetable, word)
				offset = endword + 1
			end
			lines[#lines + 1] = linetable
		end
	end

	if type(input) == "string" then
		for line in string.gmatch(input, "([^\n]*)\n?") do
			parse(line)
		end
	elseif type(input) == "table" then
		for i,line in ipairs(input) do
			parse(line)
		end
	end	
	
	return lines
end

-- returns a table with label value pairs

function parse_configfile( input )
	local config = {}
	local lines = parse_linesandwords(input)

	for i,linetable in ipairs(lines) do
		config[linetable[1]] = table.concat(linetable, " ", 2) or ""
	end
	return config
end

-- search and replace through a table
-- string is easy string.gsub(string, find, replace)

function search_replace (input, find, replace)
	local lines = {}
	for i,line in ipairs(input) do
		lines[#lines + 1] = string.gsub(line, find, replace)
	end
	return lines
end

-- great for line searches through a file. /etc/conf.d/ ???
-- might be looking for more than one thing so will return a table
-- will likely want to match whole line entries
-- so we change find to include the rest of the line
-- say want all the _OPTS from a file format.search_for_lines (fs.read_file("/etc/conf.d/cron"), "OPT")
-- if want to avoid commented lines, call parse_lines first

function search_for_lines (input, find)
	local lines = {}

	function findfn(line)
		if string.find(line, find) then
			lines[#lines + 1] = line
		end
	end

	if type(input) == "string" then
		for line in string.gmatch(input, "([^\n]*)\n?") do
			findfn(line)
		end
	elseif type(input) == "table" then
		for i,line in ipairs(input) do
			findfn(line)
		end
	end	
	
	return lines
end

--string format function to capitalize the beginging of each word. 
function cap_begin_word ( str )
	--first need to do the first word
	local data = string.gsub(str, "^%l", string.upper)
	--word is any space cause no <> regex
	data = string.gsub(data, "%s%l", string.upper)
	return data
end

--for cut functionality do something like
--print(format.string_to_table("This is a test", " ")[2])
--gives you the second field which is .... is

-- This code comes from http://lua-users.org/wiki/SplitJoin
-- example: format.string_to_table( "Anna, Bob, Charlie,Dolores", ",%s*")
function string_to_table ( text, delimiter)
	local list = {}
	-- this would result in endless loops
	if string.find("", delimiter) then 
		-- delimiter matches empty string!
		for i=1,#text do
			list[#list + 1] = string.sub(text, i, i)
		end
	else
		local pos = 1
		while 1 do
			local first, last = string.find(text, delimiter, pos)
			if first then -- found?
				table.insert(list, string.sub(text, pos, first-1))
				pos = last+1
			else
				table.insert(list, string.sub(text, pos))
				break
			end
		end
	end
	return list
end

function md5sum_string ( str)
	cmd = "/bin/echo -n " .. str .. "|/usr/bin/md5sum|cut -f 1 -d \" \" "
	f = io.popen(cmd)
	local checksum =  {}
	for line in f:lines() do
		checksum[#checksum + 1] = line
		end
	f:close()
	return checksum[1]
end


-- Takes a str and expands any ${...} constructs with the Lua variable
-- ex: a="foo"; print(expand_bash_syntax_vars("a=${a}) - > "a=foo"

expand_bash_syntax_vars = function ( str )

  local deref = function ( f)
    local v = _G
    for w in string.gfind(f, "[%w_]+") do
      v = v[w]
    end
  return v
  end

  for w in string.gmatch (str, "${[^}]*}" ) do
        local rvar = string.sub(w,3,-2)
        local rval = ( deref(rvar) or "nil" )
        str = string.gsub (str, w, rval)
  end
 return (str)
end

-- Removes the linenum line from str and replaces it with line.
-- Do nothing if doesn't exist
-- Set line to nil to remove the line
function replace_line(str, linenum, line)
	-- Split the str to remove the line
	local startchar, endchar = string.match(str, "^" .. string.rep("[^\n]*\n", linenum-1) .. "()[^\n]*\n?()")
	if startchar and endchar then
		local lines = {}
		lines[1] = string.sub(str, 1, startchar-1)
		lines[2] = string.sub(str, endchar, -1)
		if line then
			table.insert(lines, 2, line .. "\n")
		end
		str = table.concat(lines)
	end
	return str
end

-- Inserts the line into the str after the linenum (or at the end)
function insert_line(str, linenum, line)
	-- Split the str to remove the line
	local startchar = string.match(str, "^" .. string.rep("[^\n]*\n", linenum) .. "()")
	local lines = {}
	if startchar then
		lines[1] = string.sub(str, 1, startchar-1)
		lines[2] = string.sub(str, startchar, -1)
	else
		lines[1] = str
	end
	if line then
		table.insert(lines, 2, line .. "\n")
	end
	str = table.concat(lines)
	return str
end

function get_line(str, linenum)
	-- Split the str to remove the line
	local startchar, endchar = string.match(str, "^" .. string.rep("[^\n]*\n", linenum-1) .. "()[^\n]*()")
	local line
	if startchar and endchar then
		line = string.sub(str, startchar, endchar-1)
	end
	return line
end