You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
1.1 KiB

  1. # Copyright (c) 2018 Sebastian Gniazdowski
  2. #
  3. # $1 - path to the ini file to parse
  4. # $2 - name of output hash (INI by default)
  5. # $3 - prefix for keys in the hash (can be empty)
  6. #
  7. # Writes to given hash under keys built in following way: ${3}<section>_field.
  8. # Values are values from ini file.
  9. local __ini_file="$1" __out_hash="${2:-INI}" __key_prefix="$3"
  10. local IFS='' __line __cur_section="void" __access_string
  11. local -a match mbegin mend
  12. [[ ! -r "$__ini_file" ]] && { builtin print -r "fast-syntax-highlighting: an ini file is unreadable ($__ini_file)"; return 1; }
  13. while read -r -t 1 __line; do
  14. if [[ "$__line" = [[:blank:]]#\;* ]]; then
  15. continue
  16. elif [[ "$__line" = (#b)[[:blank:]]#\[([^\]]##)\][[:blank:]]# ]]; then
  17. __cur_section="${match[1]}"
  18. elif [[ "$__line" = (#b)[[:blank:]]#([^[:blank:]=]##)[[:blank:]]#[=][[:blank:]]#(*) ]]; then
  19. match[2]="${match[2]%"${match[2]##*[! $'\t']}"}" # remove trailing whitespace
  20. __access_string="${__out_hash}[${__key_prefix}<$__cur_section>_${match[1]}]"
  21. : "${(P)__access_string::=${match[2]}}"
  22. fi
  23. done < "$__ini_file"
  24. return 0
  25. # vim:ft=zsh:sw=4:sts=4:et