| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | local strfind = string.find |
| | local strsub = string.sub |
| | local strmatch = string.match |
| | local utils = require 'pl.utils' |
| | local unpack = utils.unpack |
| | local pairs,type,tonumber = pairs,type,tonumber |
| | local patterns = utils.patterns |
| | local io = io |
| | local assert_arg = utils.assert_arg |
| |
|
| | local input = {} |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | function input.alltokens (getter,pattern,fn) |
| | local line = getter() |
| | local pos = 1 |
| | return function () |
| | while line do |
| | local s, e = strfind(line, pattern, pos) |
| | if s then |
| | pos = e + 1 |
| | local res = strsub(line, s, e) |
| | if fn then res = fn(res) end |
| | return res |
| | else |
| | line = getter() |
| | pos = 1 |
| | end |
| | end |
| | return nil |
| | end |
| | end |
| | local alltokens = input.alltokens |
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | function input.create_getter(f) |
| | if f then |
| | if type(f) == 'string' then |
| | local ls = utils.split(f,'\n') |
| | local i,n = 0,#ls |
| | return function() |
| | i = i + 1 |
| | if i > n then return nil end |
| | return ls[i] |
| | end |
| | else |
| | |
| | if not f.read then error('not a file-like object') end |
| | return function() return f:read() end |
| | end |
| | else |
| | return io.read |
| | end |
| | end |
| |
|
| | |
| | |
| | |
| | function input.numbers(f) |
| | return alltokens(input.create_getter(f), |
| | '('..patterns.FLOAT..')',tonumber) |
| | end |
| |
|
| | |
| | |
| | |
| | function input.words(f) |
| | return alltokens(input.create_getter(f),"%w+") |
| | end |
| |
|
| | local function apply_tonumber (no_fail,...) |
| | local args = {...} |
| | for i = 1,#args do |
| | local n = tonumber(args[i]) |
| | if n == nil then |
| | if not no_fail then return nil,args[i] end |
| | else |
| | args[i] = n |
| | end |
| | end |
| | return args |
| | end |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function input.fields (ids,delim,f,opts) |
| | local sep |
| | local s |
| | local getter = input.create_getter(f) |
| | local no_fail = opts and opts.no_fail |
| | local no_convert = opts and opts.no_convert |
| | if not delim or delim == ' ' then |
| | delim = '%s' |
| | sep = '%s+' |
| | s = '%s*' |
| | else |
| | sep = delim |
| | s = '' |
| | end |
| | local max_id = 0 |
| | if type(ids) == 'table' then |
| | for i,id in pairs(ids) do |
| | if id > max_id then max_id = id end |
| | end |
| | else |
| | max_id = ids |
| | ids = {} |
| | for i = 1,max_id do ids[#ids+1] = i end |
| | end |
| | local pat = '[^'..delim..']*' |
| | local k = 1 |
| | for i = 1,max_id do |
| | if ids[k] == i then |
| | k = k + 1 |
| | s = s..'('..pat..')' |
| | else |
| | s = s..pat |
| | end |
| | if i < max_id then |
| | s = s..sep |
| | end |
| | end |
| | local linecount = 1 |
| | return function() |
| | local line,results,err |
| | repeat |
| | line = getter() |
| | linecount = linecount + 1 |
| | if not line then return nil end |
| | if no_convert then |
| | results = {strmatch(line,s)} |
| | else |
| | results,err = apply_tonumber(no_fail,strmatch(line,s)) |
| | if not results then |
| | utils.quit("line "..(linecount-1)..": cannot convert '"..err.."' to number") |
| | end |
| | end |
| | until #results > 0 |
| | return unpack(results) |
| | end |
| | end |
| |
|
| | return input |
| |
|
| |
|