shithub: mc

ref: c039e23ae4fd65431fb40f6d556d72a977725b0f
dir: /lib/inifile/access.myr/

View raw version
use std
use bio

use "types"

pkg inifile =
	type inisectiter = struct
		ini : inifile#
		idx : int
	;;
	type inikviter = struct
		iter	: std.htkviter((byte[:], byte[:]), byte[:])
	;;
	impl iterable inisectiter -> byte[:]
	impl iterable inikviter -> (byte[:], byte[:], byte[:])

	/* key getting/setting */
	const get	: (ini : inifile#, sect : byte[:], key : byte[:]	-> std.option(byte[:]))
	const getv	: (ini : inifile#, sect : byte[:], key : byte[:], val : byte[:]	-> byte[:])
	const has	: (ini : inifile#, sect : byte[:], key : byte[:] -> bool)
	const put	: (ini : inifile#, sect : byte[:], key : byte[:], val : byte[:]	-> void)

	const bysection	: (ini : inifile# -> inisectiter)
	const bykeyval	: (ini : inifile# -> inikviter)
;;

const get = {ini, sect, key
	-> std.htget(ini.elts, (sect, key))
}

const getv = {ini, sect, key, val
	-> std.htgetv(ini.elts, (sect, key), val)
}

const has = {ini, sect, key
	-> std.hthas(ini.elts, (sect, key))
}

const put = {ini, sect, key, val
	if std.hthas(ini.elts, (sect, key))
		std.slfree(std.htgetv(ini.elts, (sect, key), ""))
	else
		sect = std.sldup(sect)
		key = std.sldup(key)
	;;
	std.htput(ini.elts, (sect, key), std.sldup(val))
}

const bysection = {ini
	-> [.ini=ini, .idx=0]
}

const bykeyval = {ini
	-> [.iter = std.byhtkeyvals(ini.elts)]
}


impl iterable inikviter -> (byte[:], byte[:], byte[:]) =
	__iternext__ = {itp, valp
		var s, k, v, p, r
		r = __iternext__(&itp.iter, &p)
		((s, k), v) = p
		valp# = (s, k, v)
		-> r
	}

	__iterfin__ = {itp, valp
	}
;;

impl iterable inisectiter -> byte[:] =
	__iternext__ = {itp, valp : byte[:]#
		if itp.idx < itp.ini.sects.len
			valp# = itp.ini.sects[itp.idx]
			itp.idx++
			-> true
		else
			-> false
		;;
	}

	__iterfin__ = {itp, valp -> void
	}
;;