summaryrefslogtreecommitdiffstats
path: root/lib/format.lua
diff options
context:
space:
mode:
authorMike Mason <ms13sp@gmail.com>2007-12-06 21:41:20 +0000
committerMike Mason <ms13sp@gmail.com>2007-12-06 21:41:20 +0000
commit4daf25480c6bd4688d700ebc6f37db4001be68f8 (patch)
treef3c370a7c3d9eebbd1717124f4c0d80e9bda8568 /lib/format.lua
parent15f95a3134ed82b8e65b23fad5b71ef088061970 (diff)
downloadacf-core-4daf25480c6bd4688d700ebc6f37db4001be68f8.tar.bz2
acf-core-4daf25480c6bd4688d700ebc6f37db4001be68f8.tar.xz
More format libs like search_replace and such. All now documented on wiki
git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@417 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'lib/format.lua')
-rw-r--r--lib/format.lua88
1 files changed, 69 insertions, 19 deletions
diff --git a/lib/format.lua b/lib/format.lua
index 9c21395..4e483de 100644
--- a/lib/format.lua
+++ b/lib/format.lua
@@ -15,40 +15,85 @@ require ("session")
function dostounix ( a )
local data = string.gsub(a, "\r", "")
return data
-
end
--- search and remove all blank lines and commented lines in a file
+-- search and remove all blank lines and commented lines in a file or table
function remove_blanks_comments ( path )
-f = fs.read_file_as_array(path)
-local lines = {}
-for _,line in pairs(f) do
-local c = string.match(line, "^$") or string.match(line, "^%#")
-if c == nil then lines[#lines + 1] = line end
-end
+ if type(path) == "string" then
+ if fs.is_file == "false" then
+ error("Invalid file!")
+ else
+ f = fs.read_file_as_array(path)
+ end
+ elseif type(path) == "table" then
+ f = path
+ end
+ local lines = {}
+ for a,b in ipairs(f) do
+ local c = string.match(b, "^$") or string.match(b, "^%#")
+ if c == nil then lines[#lines + 1] = b end
+ end
-- returns a table to iterate over without the blank or commented lines
return lines
end
+--great for search and replace through a file or table.
+--string is easy string.gsub(string, find, replace)
+--path can be either a file or a table
+
+function search_replace (path, find, replace)
+ --would be a string if is a path to a file
+ if type(path) == "string" then
+ if fs.is_file == "false" then
+ error("Invalid file!")
+ else
+ f = fs.read_file_as_array(path)
+ end
+ elseif type(path) == "table" then
+ f = path
+ end
+ local lines = {}
+ for a,b in ipairs(f) do
+ local c = string.gsub(b, find, replace)
+ lines[#lines + 1] = c 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 ("/etc/conf.d/cron", "OPT")
+
+function search_for_lines (path, find )
+ find = "^.*" .. find .. ".*$"
+ if type(path) == "string" then
+ if fs.is_file == "false" then
+ error("Invalid file!")
+ else
+ f = format.remove_blanks_comments(path)
+ end
+ elseif type(path) == "table" then
+ f = path
+ end
+ --don't want to match commented out lines
+ local lines = {}
+ for a,b in ipairs(f) do
+ local c = string.match(b, find)
+ lines[#lines +1 ] = c end
+ return lines
+end
+
--string format function to cap the beginging of each word.
-function cap_begin_word ( a )
+function cap_begin_word ( str )
--first need to do the first word
- local data = string.gsub(a, "^%l", string.upper)
+ local data = string.gsub(str, "^%l", string.upper)
--word is any space cause no <> regex
data = string.gsub(data, " %l", string.upper)
return data
end
-function search_replace (path , find, replace)
- local f = fs.read_file_as_array(path)
- local lines = {}
- for a,b in ipairs(f) do
- local c = string.gsub(b, find, replace)
- lines[#lines + 1] = c end
- return lines
-end
-
-- This code comes from http://lua-users.org/wiki/SplitJoin
-- -- example: format.table_to_string(", ", {"Anna", "Bob", "Charlie", "Dolores"})
@@ -64,6 +109,10 @@ function table_to_string (delimiter, list)
return string
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(",%s*", "Anna, Bob, Charlie,Dolores")
function string_to_table (delimiter, text)
@@ -86,3 +135,4 @@ function string_to_table (delimiter, text)
return list
end
+