1
0
mirror of https://github.com/albfan/bash-ini-parser.git synced 2025-08-07 18:02:52 +03:00

support complex whitespace indentation

closes #2
This commit is contained in:
albfan
2015-05-15 18:28:19 +02:00
parent 5bae5fa6eb
commit d4da89a1be

View File

@@ -10,44 +10,49 @@ function debug {
} }
function cfg_parser { function cfg_parser {
ini="$(<$1)" # read the file shopt -p extglob &> /dev/null
CHANGE_EXTGLOB=$?
if [ $CHANGE_EXTGLOB = 1 ]
then
shopt -s extglob
fi
ini="$(<$1)" # read the file
ini="${ini//[/\\[}" # escape [ ini="${ini//[/\\[}" # escape [
debug debug
ini="${ini//]/\\]}" # escape ] ini="${ini//]/\\]}" # escape ]
debug debug
IFS=$'\n' && ini=( ${ini} ) # convert to line-array IFS=$'\n' && ini=( ${ini} ) # convert to line-array
debug debug
ini=( ${ini[*]//;*/} ) # remove comments with ; ini=( ${ini[*]//;*/} ) # remove comments with ;
debug debug
ini=( ${ini[*]/ =/=} ) # remove tabs before = ini=( ${ini[*]/#+([[:space:]])/} ) # remove init whitespace
debug debug "whitespace around"
ini=( ${ini[*]/# /} ) # remove init tabs #TODO: remove all not just one ini=( ${ini[*]/*([[:space:]])=*([[:space:]])/=} ) # remove whitespace around =
debug
ini=( ${ini[*]/# /} ) # remove init space #TODO: remove all not just one
debug
ini=( ${ini[*]/= /=} ) # remove tabs after =
debug
ini=( ${ini[*]/ = /=} ) # remove anything with a space around =
debug debug
ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix
debug debug
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1) ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
debug debug
ini=( ${ini[*]/=/=\( } ) # convert item to array ini=( ${ini[*]/=/=\( } ) # convert item to array
debug debug
ini=( ${ini[*]/%/ \)} ) # close array parenthesis ini=( ${ini[*]/%/ \)} ) # close array parenthesis
debug debug
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
debug debug
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2) ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
debug debug
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
debug debug
ini[0]="" # remove first element ini[0]="" # remove first element
debug debug
ini[${#ini[*]} + 1]='}' # add the last brace ini[${#ini[*]} + 1]='}' # add the last brace
debug debug
eval "$(echo "${ini[*]}")" # eval the result eval "$(echo "${ini[*]}")" # eval the result
if [ $CHANGE_EXTGLOB = 1 ]
then
shopt -u extglob
fi
} }
function cfg_writer { function cfg_writer {