workplace/lib/ini
2024-05-01 19:48:46 +02:00

35 lines
947 B
Bash

#!/bin/bash
ini_parse(){
local filename="$1"
sections="$(gawk '{ if ($1 ~ /^\[/) section=tolower(gensub(/\[(.+)\]/,"\\1",1,$1)); configuration[section]=1 } END {for (key in configuration) { print key} }' $filename)"
for section in $sections; do
array_name="ini_${section}"
declare -g -A ${array_name}
done
eval $(gawk -F= '{
if($1 ~ /^\[/)
section=tolower(gensub(/\[(.+)\]/,"\\1",1,$1))
else if($1 !~ /^$/ && $1 !~ /^;/){
gsub(/^[ \t]+|[ \t]+$/, "", $1);
gsub(/[\[\]]/, "", $1);
gsub(/^[ \t]+|[ \t]+$/, "", $2);
if(configuration[section][$1] == "")
configuration[section][$1]=$2
else
configuration[section][$1]=configuration[section][$1]" "$2
}
}
END {
for(section in configuration)
for(key in configuration[section]){
section_name = section
gsub("-", "_", section_name)
print "ini_" section_name "[\""key"\"]=\""configuration[section][key]"\";"
}
}' ${filename}
)
}