Advertisement
Guest User

LIB

a guest
Dec 9th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.78 KB | None | 0 0
  1. local function getTableSize(t)
  2.     local s = 0
  3.     for k, v in pairs(t) do
  4.         s = s + 1
  5.     end
  6.     return s
  7. end
  8.  
  9. function getFileKeywords(file, keywords, requireAll)
  10.     local ret = {}
  11.     if not io.open(file) then
  12.         print('[ERROR - getFileKeywords] File does not exist.')
  13.         return false
  14.     else
  15.         io.close()
  16.     end
  17.     for line in io.lines(file) do
  18.         local matched = {}
  19.         for i = 1, #keywords do
  20.             local keyword = keywords[i].keyword
  21.             if line:find(keyword ..'="(.-)"') then
  22.                 matched[keywords[i].index] = line:match(keyword .. '="(.-)"')
  23.             end
  24.         end
  25.         local sizeMatched = getTableSize(matched)
  26.         if requireAll and (sizeMatched == #keywords) then
  27.             table.insert(ret, matched)
  28.         elseif not requireAll and sizeMatched > 0 then
  29.             table.insert(ret, matched)
  30.         end
  31.     end
  32.     return ret
  33. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement