From b167d7758dbcc813730800def07dedbc1198902e Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Thu, 18 Sep 2008 12:16:30 +0000 Subject: Added parser library. Display filecontent descr in filedetails. git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@1474 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/Makefile | 1 + lib/parser.lua | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 lib/parser.lua (limited to 'lib') diff --git a/lib/Makefile b/lib/Makefile index 23a1a31..eb5c0fd 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -24,6 +24,7 @@ LIB_DIST=fs.lua\ modelfunctions.lua\ cfe.lua\ apk.lua\ + parser.lua\ EXTRA_DIST=README Makefile DISTFILES=$(LIB_DIST) $(EXTRA_DIST) diff --git a/lib/parser.lua b/lib/parser.lua new file mode 100644 index 0000000..7c6af67 --- /dev/null +++ b/lib/parser.lua @@ -0,0 +1,79 @@ +module(..., package.seeall) + +function parseconfigfile(file) + file = file or "" + local retval = {commented={}} + local linenum=0 + for line in string.gmatch(file, "([^\n]*)\n?") do + linenum=linenum+1 + if not string.match(line, "^[%s#]*$") then + local linetable = {linenum=linenum, line=line} + if string.match(line, "^%s*#") then + table.insert(retval.commented, linetable) + line = string.match(line, "^%s*#(.*)") + else + table.insert(retval, linetable) + end + -- Iterate through each word, being careful about quoted strings and comments + local offset = 1 + while string.find(line, "%S+", offset) do + local word = string.match(line, "%S+", offset) + local endword = select(2, string.find(line, "%S+", offset)) + 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) + end + table.insert(linetable, word) + offset = endword + 1 + end + end + end + return retval +end + +-- Removes the linenum line from file and replaces it with line. +-- Do nothing if doesn't exist +function replaceline(file, linenum, line) + -- Split the file to remove the line + local startchar, endchar = string.match(file, "^" .. string.rep("[^\n]*\n", linenum-1) .. "()[^\n]*\n?()") + if startchar and endchar then + local lines = {} + lines[1] = string.sub(file, 1, startchar-1) + lines[2] = string.sub(file, endchar, -1) + if line then + table.insert(lines, 2, line .. "\n") + end + file = table.concat(lines) + end + return file +end + +-- Inserts the line into the file after the linenum (or at the end) +function insertline(file, linenum, line) + -- Split the file to remove the line + local startchar = string.match(file, "^" .. string.rep("[^\n]*\n", linenum) .. "()") + local lines = {} + if startchar then + lines[1] = string.sub(file, 1, startchar-1) + lines[2] = string.sub(file, startchar, -1) + else + lines[1] = file + end + if line then + table.insert(lines, 2, line .. "\n") + end + file = table.concat(lines) + return file +end + +function getline(file, linenum) + -- Split the file to remove the line + local startchar, endchar = string.match(file, "^" .. string.rep("[^\n]*\n", linenum-1) .. "()[^\n]*()") + local line + if startchar and endchar then + line = string.sub(file, startchar, endchar-1) + end + return line +end -- cgit v1.2.3