- drop term-site association, it was wrong idea from the beginning. If it's needed...
[cl-trane.git] / src / ini.bash
blob256855004c8a68d96511d983a926d1068daba813
1 # http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/
3 function cfg.parser ()
5 _old_IFS="$IFS" # save $IFS
6 IFS=$'\n' && ini=( $(<$1) ) # convert to line-array
7 ini=( ${ini[*]//;*/} ) # remove comments `;'
8 ini=( ${ini[*]//\#*/} ) # remove comments `#'
9 ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around `=`
10 ini=( ${ini[*]/#[/\}$'\n'cfg.section.} ) # set section prefix
11 ini=( ${ini[*]/%]/ \(} ) # convert text2function (1)
12 ini=( ${ini[*]/=/=\( } ) # convert item to array
13 ini=( ${ini[*]/%/ \)} ) # close array parenthesis
14 ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
15 ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
16 ini=( ${ini[*]/#\ */} ) # remove blank lines
17 ini=( ${ini[*]/#\ */} ) # remove blank lines with tabs
18 ini[0]='' # remove first element
19 ini[${#ini[*]} + 1]='}' # add the last brace
20 # printf "%s\n" ${ini[*]}
21 eval "$(echo "${ini[*]}")" # eval the result
22 if [ -z "$_old_IFS" ] # restore old $IFS
23 then unset IFS
24 else IFS="$_old_IFS"