summaryrefslogtreecommitdiff
path: root/db.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2018-09-09 00:38:24 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2018-09-09 00:38:24 +0200
commitfe2fbd04eca6779dddfce6dbbc565e050a05ff1f (patch)
tree68ebeee02d0d08eda1854d162c999f96776610a2 /db.lua
parent1d7af97d4cb261d5a0b82f6f0ccf7e788ff5a54a (diff)
downloadcaldr-fe2fbd04eca6779dddfce6dbbc565e050a05ff1f.tar.gz
db: add trivial json db reader
Diffstat (limited to 'db.lua')
-rw-r--r--db.lua17
1 files changed, 17 insertions, 0 deletions
diff --git a/db.lua b/db.lua
index 4e7434a..0a9d0f4 100644
--- a/db.lua
+++ b/db.lua
@@ -12,6 +12,7 @@ local pcall = pcall
local io = require "io"
local iostdout = io.stdout
+local iostdin = io.stdin
local common = require "common"
local common_status = common.status
@@ -22,10 +23,25 @@ local errorln = common.errorln
local noiseln = common.noiseln
local debugln = common.debugln
+local json_internalize = function (jsdata)
+ local jsdec = cjson.new ()
+ local parsed = jsdec.decode (jsdata)
+ if parsed == nil then return status_error end
+ return status_ok, parsed
+end
+
-------------------------------------------------------------------------------
--- API
-------------------------------------------------------------------------------
+local read_json_db = function (cal, fh)
+ fh = fh or iostdin
+ local jsdata = fh:read "*all"
+ fh:close ()
+ if jsdata == nil then return status_error end
+ return json_internalize (jsdata)
+end
+
local output_calendar = function (cal, fh)
fh = fh or iostdout
local jsenc = cjson.new ()
@@ -39,5 +55,6 @@ local output_calendar = function (cal, fh)
end
return { output_calendar = output_calendar
+ , read_json_db = read_json_db
}