summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2022-02-14 00:35:47 +0000
committerdavidovski <david@davidovski.xyz>2022-02-14 00:35:47 +0000
commitd3093d4d72283019226ab0f8f76de520a0509e2a (patch)
treeca681fa87d37c1ad526f38d25ef5926861e114cb
parent526e2bfcdb5f55f2bd94b29cf314f8cb4c37bf4f (diff)
made parseconf insanely fast
-rwxr-xr-xsrc/parseconf.sh41
-rwxr-xr-xtest/parseconf.sh2
2 files changed, 13 insertions, 30 deletions
diff --git a/src/parseconf.sh b/src/parseconf.sh
index aa5885d..f92bf0e 100755
--- a/src/parseconf.sh
+++ b/src/parseconf.sh
@@ -28,7 +28,7 @@ parse_line() {
shift
local value="$@"
- [ "$key" = "include" ] && parse $value && return
+ [ "$key" = "include" ] && cat $value | parse && return
[ "$key" = "]" ] && unset list[-1] && printf "\n" && return
[ "$key" = "}" ] && unset level[-1] && return
@@ -53,30 +53,13 @@ parse_line() {
#
parse () {
local file="$1"
+
export level=()
export list=()
while IFS= read -r line; do
# strip whitespace
- line=$(sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" <<< "$line")
- parse_line $line
- done < "$file"
-}
-
-# Filter the parsed file for specific keys
-#
-filter () {
- local pattern=
-
- [ $# = 0 ] &&
- pattern=".*" ||
- pattern=$(sed "s/\*/[^:]*/g"<<< "$@")
-
- $print_keys &&
- pattern="s/^($pattern:.+)/\1/p" ||
- pattern="s/^$pattern:(.+)/\1/p"
-
-
- parse $CONF_FILE | sed -rn $pattern
+ parse_line $line
+ done < "/dev/stdin"
}
# Use the env variable if exists
@@ -98,7 +81,7 @@ while getopts ":f:c:v" opt; do
print_keys=false
;;
c)
- count="${OPTARG}"
+ count="${OPTARG}q"
;;
*)
esac
@@ -106,12 +89,12 @@ done
shift $((OPTIND-1))
-if echo $@ | grep -q "."; then
- [ -z ${count} ] &&
- filter "$@" ||
- filter "$@" | tail -n $count
-else
- parse $CONF_FILE
-fi
+[ $# = 0 ] &&
+ pattern=".*" ||
+ pattern=$(sed "s/\*/[^:]*/g" <<< "$@")
+$print_keys &&
+ pattern="s/^($pattern:.+)/\1/p;${count}" ||
+ pattern="s/^$pattern:(.+)/\1/p;${count}"
+sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" $CONF_FILE | parse $@ | sed -rn $pattern
diff --git a/test/parseconf.sh b/test/parseconf.sh
index 3af55e1..69dd78c 100755
--- a/test/parseconf.sh
+++ b/test/parseconf.sh
@@ -116,6 +116,6 @@ dict {
}
"
retval=$(${PARSECONF} -c 1 "dict.*" <<< "$config")
- [ "$retval" = "dict.d:4" ]
+ [ "$retval" = "dict.a:1" ]
}