From 4708d5e010f8e787a06d2ec3361d70756d03d75f Mon Sep 17 00:00:00 2001 From: Autumn Date: Sun, 23 Aug 2026 11:44:35 +0100 Subject: [bunnytask] added config file support --- lib/config.ha | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 lib/config.ha (limited to 'lib') diff --git a/lib/config.ha b/lib/config.ha new file mode 100644 index 0000000..def33dd --- /dev/null +++ b/lib/config.ha @@ -0,0 +1,50 @@ +use dirs; +use format::ini; +use fs; +use io; +use strings; +use os; + +export type config = struct { files: []str }; + +fn load_item(scanner: *ini::scanner) (ini::entry | done) = { + match(ini::next(scanner)) { + case let entry: ini::entry => return entry; + case => return done; + }; +}; + +// caller must free after use using [[config_free]] +export fn config_read() (*config | fs::error | nomem) = { + + const config_file = dirs::config("bunnytask"); + const config_file = strings::concat(config_file, "/config.ini")?; + defer free(config_file); + + // open + const config_file = os::open(config_file)?; + defer io::close(config_file)!; + + const scanner = ini::scan(config_file); + defer ini::finish(&scanner); + + // read + let files: []str = []; + + for (let entry => load_item(&scanner)) { + if (strings::trim(entry.1) == "todo_file") { + // TODO shell expand + const file = strings::trim(entry.2); + + append(files, strings::dup(file)?)?; + }; + }; + + // return + return alloc(config { files = files })?; +}; + +export fn config_free(config: *config) void = { + strings::freeall(config.files); + free(config); +}; -- cgit v1.3