Skip to content
Snippets Groups Projects
Commit ff22cf44 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

update ini.class

parent a8294e6a
No related branches found
No related tags found
1 merge request!1277009 better safename for windows
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
# #
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# 2024-02-04 v0.1 Initial version # 2024-02-04 v0.1 Initial version
# 2024-02-08 v0.2 add ini.varexport; improve replacements of quotes
# ====================================================================== # ======================================================================
INI_FILE= INI_FILE=
...@@ -72,11 +73,8 @@ function ini.section(){ ...@@ -72,11 +73,8 @@ function ini.section(){
# param1 - name of the ini file # param1 - name of the ini file
# param2 - name of the section in ini file # param2 - name of the section in ini file
function ini.keys(){ function ini.keys(){
local myfile=$1 local myfile=${1:-$INI_FILE}
local mysection=$2 local mysection=${2:-$INI_SECTION}
test -z "$myfile" && myfile=$INI_FILE
test -z "$mysection" && mysection=$INI_SECTION
ini.section "${myfile}" "${mysection}" \ ini.section "${myfile}" "${mysection}" \
| grep "^[\ \t]*[^=]" \ | grep "^[\ \t]*[^=]" \
...@@ -98,13 +96,43 @@ function ini.value(){ ...@@ -98,13 +96,43 @@ function ini.value(){
local myfile=$1 local myfile=$1
local mysection=$2 local mysection=$2
local myvarname=$3 local myvarname=$3
ini.section "${myfile}" "${mysection}" \ local out
out=$(ini.section "${myfile}" "${mysection}" \
| grep "^[\ \t]*${myvarname}[\ \t]*=.*" \ | grep "^[\ \t]*${myvarname}[\ \t]*=.*" \
| cut -f 2- -d "=" \ | cut -f 2- -d "=" \
| sed -e 's,^\ *,,' -e 's, *$,,' \ | sed -e 's,^\ *,,' -e 's, *$,,'
-e 's,^",,g' -e 's,"$,,g' \ )
-e "s,^',,g" -e "s,'$,,g"
# if value does not end with [] than the last found value wins
grep "\[\]$" <<< "myvarname" >/dev/null && out="echo $out | tail -1"
# delete quote chars on start and end
grep '^".*"$' <<< "$out" >/dev/null && out=$(echo "$out" | sed -e's,^"\(.*\)"$,\1,g')
grep "^'.*'$" <<< "$out" >/dev/null && out=$(echo "$out" | sed -e"s,^'\(.*\)'$,\1,g")
echo "$out"
fi fi
} }
# Create bash code to export all variables as hash.
# Example: eval "$( ini.varexport "cfg_" "$inifile" )"
#
# param string prefix for the variables
# param string ini file to read
function ini.varexport(){
local myprefix="$1"
local myfile="$2"
local var=
for mysection in $(ini.sections "$myfile"); do
var="${myprefix}${mysection}"
echo "declare -A ${var}; "
echo "export ${var}; "
for mykey in $(ini.keys "$myfile" "$mysection"); do
value="$(ini.value "$myfile" "$mysection" "$mykey")"
echo ${var}[$mykey]="\"$value\""
done
done
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment