summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_complex.subr
blob: 64fcea5791a044b86e86d348f1cf32458f989455 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lucía Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
#

#
# rtl_export_vars() - export or unset list of variables
# @[-u]:	unset instead of exporting variables
# @...:		list of variable name-value pairs
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_export_vars() {
	local _rev_unsetfl=0;

	if [ "x${1}" = "x-u" ]; then
		_rev_unsetfl=1; shift;
	fi;

	while [ "${#}" -ge 2 ]; do
		if [ "${2:+1}" = 1 ]\
		|| [ "${_rev_unsetfl}" -eq 1 ]; then
			case "${_rev_unsetfl}" in
			0)	rtl_set_var_unsafe "${1}" "${2}";
				export "${1}"; ;;
			1)	unset "${1}"; ;;
			esac;
		fi; shift 2;
	done;

	return 0;
};

#
# rtl_percentage() - calculate percentage of two numbers
# @_in:		input number
# @_max:	input maximum
# @_rs_out:	out reference to percentage number
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_percentage() {
	local	_rp_in="${1}" _rp_max="${2}" _rp_rs_out="${3#\$}"	\
		_rp_perc;

	_rp_perc=$((100 * ${_rp_in} + ${_rp_max} / 2));
	_rp_perc=$((${_rp_perc} ? ${_rp_perc} / ${_rp_max} : ${_rp_perc}));

	eval ${_rp_rs_out}='${_rp_perc}';
	return 0;
};

#
# rtl_percentage2() - calculate percentage of two numbers
# @_rin:	in reference to input number
# @_rmax:	in reference to input maximum
# @_rs_out:	out reference to percentage number
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_percentage2() {
	local	_rp_rin="${1#\$}" _rp_rmax="${2#\$}" _rp_rs_out="${3#\$}"	\
		_rp_in=0 _rp_max=0 _rp_perc;

	eval _rp_in=\"\${${_rp_rin}}\";
	eval _rp_max=\"\${${_rp_rmax}}\";

	_rp_perc=$((100 * ${_rp_in} + ${_rp_max} / 2));
	_rp_perc=$((${_rp_perc} ? ${_rp_perc} / ${_rp_max} : ${_rp_perc}));

	eval ${_rp_rs_out}='${_rp_perc}';
	return 0;
};

#
# rtl_sunset() - unset variables
# @_rset:	in reference to list of variables to unset
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_sunset() {
	local	_rs_rset="${1#\$}"	\
		IFS=" 	";

	eval set -- "\${${_rs_rset}}";
	while [ "${#}" -gt 0 ]; do
		unset "${_rs_rset}"; shift;
	done; unset "${_rs_rset}";

	return 0;
};

# vim:filetype=sh textwidth=0