diff --git a/classes/beastfile.lua b/classes/beastfile.lua index b64663b..c6c442a 100644 --- a/classes/beastfile.lua +++ b/classes/beastfile.lua @@ -1,10 +1,18 @@ local BeastFile = Object:extend() local DataList = require "classes.datalist" +local parseFile = require "libs.filereader" + function BeastFile:new(folder, name) self.filepath = folder .. "/" .. name print("Loading " .. self.filepath) self.datas = DataList() + + self:readLines() +end + +function BeastFile:readLines() + local lines = parseFile(self.filepath, function (line) print(line) end) end function BeastFile:prepareJson() diff --git a/libs/filereader.lua b/libs/filereader.lua new file mode 100644 index 0000000..39ad24b --- /dev/null +++ b/libs/filereader.lua @@ -0,0 +1,34 @@ +-- http://lua-users.org/wiki/FileInputOutput + +local utils = require "libs.utils" + +-- see if the file exists +local function file_exists(file) + local f = io.open(file, "rb") + if f then f:close() end + return f ~= nil +end + +-- get all lines from a file, returns an empty +-- list/table if the file does not exist +local function lines_from(file) + if not file_exists(file) then error(file) end + local lines = {} + for line in io.lines(file) do + lines[#lines + 1] = line + end + return lines +end + +local function parseFile(file, func) + local lines = lines_from(file) + for k,v in pairs(lines) do + local line = utils.trim(v) + line = utils.trim(utils.split(line, "//", true)[1]) + if (line ~= "") then + func(line) + end + end +end + +return parseFile; \ No newline at end of file