aboutsummaryrefslogtreecommitdiff
path: root/lib/config.ha
blob: def33ddaa8cc35be8999aafb11e5fca06cca3f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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);
};