diff options
author | Philipp Gesang <phg@phi-gamma.net> | 2018-06-27 19:26:06 +0200 |
---|---|---|
committer | Philipp Gesang <phg@phi-gamma.net> | 2018-07-04 08:08:24 +0200 |
commit | a5266a028f7a9414af3ebc38ec569d3f7503cf51 (patch) | |
tree | 5ea660026795c2380ba03d7b35d1dcb73ccf8741 | |
parent | 918ea8eabd6a8ace037753d0cbe425a9143b52c6 (diff) | |
download | caldr-a5266a028f7a9414af3ebc38ec569d3f7503cf51.tar.gz |
cal: define preliminary api
-rwxr-xr-x | cal.lua | 60 | ||||
-rw-r--r-- | inout.lua | 10 |
2 files changed, 60 insertions, 10 deletions
@@ -7,6 +7,8 @@ local io = require "io" local ioopen = io.open +local iostdin = io.stdin +local iostdout = io.stdout local math = require "math" local mathmin = math.min @@ -301,21 +303,59 @@ local loaddata = function (fname) error ("could not open file " .. fname .. " for reading") end - return fh:read ("*a") + local ret = fh:read ("*a") + fh:close () + + return ret end -local main = function (argv) - if #argv ~= 1 then - error "pass me a file name, I insist!" - end +------------------------------------------------------------------------------- +--- API +------------------------------------------------------------------------------- + +local set_verbosity = function (n) verboselvl = n end + +local parse = function (raw) + local ok, obj = pcall (parse_calendar, raw) + return ok and obj +end + +local parse_handle = function (fh) + local raw = fh:read ("*a") + return raw ~= nil and parse (raw) +end - local raw = loaddata (argv [1]) - local cal = parse_calendar (raw) +local parse_file = function (fname) + local fh = ioopen (fname, "r") + local cal = parse_handle (fh) + fh:close () + return cal +end + +local parse_stdin = function () + return parse_handle (iostdin) +end - if cal ~= nil then println (fmt_calendar (cal, nil, "\n")) end +local format = function (cal, newline) + return fmt_calendar (cal, nil, newline or "\n") +end + +local output_handle = function (cal, fh) + local str = fmt_calendar (cal, nil, newline or "\n") + fh:write (str) +end - return 0 +local output = function (cal) + return output_handle (cal, iostdout) end -return main (arg) +return { set_verbosity = set_verbosity + , parse = parse + , parse_handle = parse_handle + , parse_file = parse_file + , parse_stdin = parse_stdin + , format = format + , output = output + , output_handle = output_handle + } diff --git a/inout.lua b/inout.lua new file mode 100644 index 0000000..ad1c23d --- /dev/null +++ b/inout.lua @@ -0,0 +1,10 @@ +local cal = require "cal" + +local main = function (argv) + local ok = cal.output (cal.parse_stdin ()) + + return ok and 0 or -1 +end + +os.exit (main ()) + |