feat: read lines
This commit is contained in:
parent
5daa5034a2
commit
3687f5297f
2 changed files with 42 additions and 0 deletions
|
@ -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()
|
||||
|
|
34
libs/filereader.lua
Normal file
34
libs/filereader.lua
Normal file
|
@ -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;
|
Loading…
Reference in a new issue