From e0976a8c3f307d412d425c501c219d622a85555e Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Fri, 15 Nov 2024 03:09:58 +0100 Subject: Support an ini config file --- config.ha | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'config.ha') diff --git a/config.ha b/config.ha index 4936492..a452665 100644 --- a/config.ha +++ b/config.ha @@ -1,4 +1,56 @@ +use dirs; +use path; +use format::ini; +use os; +use fs; +use strings; +use io; + type config = struct { tasksdir: str, context: str, }; + +type cerror = !(fs::error | path::error | ini::error); + +fn readconfig() (config | cerror) = { + const c = dirs::config("hatask"); + const buf = path::init(c, "config.ini")?; + const cpath = path::string(&buf); + const f = os::open(cpath)?; + defer io::close(f)!; + const sc = ini::scan(f); + const c = config { + tasksdir = strings::dup("tasks"), + context = strings::dup("*"), + }; + for (const en => ini::next(&sc)?) { + switch (en.1) { + case "tasksdir" => + c.tasksdir = strings::dup(strings::trim(en.2)); + case "context" => + c.tasksdir = strings::dup(strings::trim(en.2)); + case => + void; + }; + }; + return c; +}; + +fn cfinish(cfg: *config) void = { + free(cfg.tasksdir); + free(cfg.context); +}; + +fn strcerror(e: cerror) str = { + return match (e) { + case let e: fs::error => + yield fs::strerror(e); + case let e: path::error => + yield path::strerror(e); + case let e: ini::error => + yield ini::strerror(e); + case => + abort(); + }; +}; -- cgit v1.2.3