diff options
author | davidovski <david@davidovski.xyz> | 2022-02-14 00:24:11 +0000 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2022-02-14 00:24:11 +0000 |
commit | 526e2bfcdb5f55f2bd94b29cf314f8cb4c37bf4f (patch) | |
tree | 086ea7cf31945c448b45d32ab940809d15ae9eb0 /src/parseconf.sh | |
parent | 04e80e5ceadee536ccfe0315c835ac4193a32f84 (diff) |
made parseconf 4 times faster
Diffstat (limited to 'src/parseconf.sh')
-rwxr-xr-x | src/parseconf.sh | 85 |
1 files changed, 48 insertions, 37 deletions
diff --git a/src/parseconf.sh b/src/parseconf.sh index 44089b7..aa5885d 100755 --- a/src/parseconf.sh +++ b/src/parseconf.sh @@ -13,45 +13,52 @@ Arguments: EOF } +getlevel() { + for i in $*; do + printf $i + printf '.' + done +} + +parse_line() { + [ $# == "0" ] && return + + local line="$@" + local key=$1 + shift + local value="$@" + + [ "$key" = "include" ] && parse $value && return + [ "$key" = "]" ] && unset list[-1] && printf "\n" && return + [ "$key" = "}" ] && unset level[-1] && return + + case ${value: -1} in + "{") + level+=("$key") + ;; + "[") + list+=("$key") + printf "$(getlevel ${level[@]})$key:" + ;; + *) + + [ "${#list[@]}" == "0" ] && + printf "$(getlevel ${level[@]})$key:$value\n" || + printf "$line " + ;; + esac +} + # print the parsed values from the config file in key:value format # parse () { local file="$1" - local level="" - local list="" + export level=() + export list=() while IFS= read -r line; do - line=$(sed "s/\s\+/ /g" <<< "$line" | sed "s/^\s\|\s$\|;*$//g") - - grep -q "^#" <<< "$line" && continue - grep -q "." <<< "$line" || continue - - local key=$(echo $line | cut -d" " -f1) - local value=$(echo $line | cut -d" " -f2-) - - [ "$key" = "include" ] && parse $value && continue - - case ${value: -1} in - "{") - level="$level$key." - ;; - "[") - list="$list$key." - printf "$level$key:" - ;; - "}") - level=$(sed "s/[^\.]\w*\.$//g" <<< "$level") - ;; - "]") - printf "\n" - list=$(sed "s/[^\.]\w*\.$//g" <<< "$list") - ;; - *) - - grep -q "." <<< "$list" && - printf "$line " || - printf "$level$key:$value\n" - ;; - esac + # strip whitespace + line=$(sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" <<< "$line") + parse_line $line done < "$file" } @@ -99,8 +106,12 @@ done shift $((OPTIND-1)) -[ -z ${count} ] && - filter "$@" || - filter "$@" | tail -n $count +if echo $@ | grep -q "."; then + [ -z ${count} ] && + filter "$@" || + filter "$@" | tail -n $count +else + parse $CONF_FILE +fi |