From 9a1e0d8d322bbf2037e2df3185161b1fe2f923b5 Mon Sep 17 00:00:00 2001 From: Wolf Date: Thu, 12 Sep 2024 13:33:47 +0100 Subject: [PATCH] New Updates * Fixing raised issues * Add case handling for default to uppercase * Fix shellcheck errors --- demos/parse-example.sh | 27 ++++++++++++++---- src/ini-file-parser.sh | 65 ++++++++++++++++++++++++++++++++---------- 2 files changed, 72 insertions(+), 20 deletions(-) diff --git a/demos/parse-example.sh b/demos/parse-example.sh index b791fb1..f4dc60f 100755 --- a/demos/parse-example.sh +++ b/demos/parse-example.sh @@ -15,11 +15,16 @@ # cleaner to do it this way and also makes sure that shellcheck is 100% clean. # # -------------------------------------------------------------------------------- # -declare section1_value1 declare sections + +declare section1_value1 declare section1_keys declare section1_values +declare SECTION1_VALUE1 +declare SECTION1_keys +declare SECTION1_values + # -------------------------------------------------------------------------------- # # Global Overrides # # -------------------------------------------------------------------------------- # @@ -27,6 +32,7 @@ declare section1_values # # # case_sensitive_sections - should section names be case sensitive # # case_sensitive_keys - should key names be case sensitive # +# default_to_uppercase - should we default to uppercase? # # show_config_warnings - should we show config warnings # # show_config_errors - should we show config errors # # # @@ -35,7 +41,8 @@ declare section1_values # -------------------------------------------------------------------------------- # export case_sensitive_sections=false -#export case_sensitive_keys=false +export case_sensitive_keys=false +export default_to_uppercase=true #export show_config_warnings=false #export show_config_errors=false @@ -94,7 +101,11 @@ echo "${value}" # -------------------------------------------------------------------------------- # echo "Display Section 1 - Value 1 (Named variable)" -echo "${section1_value1}" +if [[ "${default_to_uppercase}" = false ]]; then + echo "${section1_value1}" +else + echo "${SECTION1_VALUE1}" +fi # -------------------------------------------------------------------------------- # # Section, Key and Value Traversals # @@ -106,8 +117,14 @@ echo echo "Display Section, Key and Value Traversals" echo "${sections[@]}" -echo "${section1_keys[@]}" -echo "${section1_values[@]}" + +if [[ "${default_to_uppercase}" = false ]]; then + echo "${section1_keys[@]}" + echo "${section1_values[@]}" +else + echo "${SECTION1_keys[@]}" + echo "${SECTION1_values[@]}" +fi # -------------------------------------------------------------------------------- # # End of Script # diff --git a/src/ini-file-parser.sh b/src/ini-file-parser.sh index e4fc8bb..b1f8a57 100755 --- a/src/ini-file-parser.sh +++ b/src/ini-file-parser.sh @@ -16,12 +16,14 @@ # # # case_sensitive_sections - should section names be case sensitive # # case_sensitive_keys - should key names be case sensitive # +# default_to_uppercase - If we are using case insensitive, default to uppercase # # show_config_warnings - should we show config warnings # # show_config_errors - should we show config errors # # -------------------------------------------------------------------------------- # declare case_sensitive_sections declare case_sensitive_keys +declare default_to_uppercase declare show_config_warnings declare show_config_errors @@ -35,7 +37,7 @@ declare show_config_errors DEFAULT_SECTION='default' -sections=( "${DEFAULT_SECTION}" ) +sections=() # -------------------------------------------------------------------------------- # # Local Variables # @@ -44,12 +46,14 @@ sections=( "${DEFAULT_SECTION}" ) # # # local_case_sensitive_sections - should section names be case sensitive # # local_case_sensitive_keys - should key names be case sensitive # +# default_to_uppercase - should we default to uppercase # # local_show_config_warnings - should we show config warnings # # local_show_config_errors - should we show config errors # # -------------------------------------------------------------------------------- # local_case_sensitive_sections=true local_case_sensitive_keys=true +local_default_to_uppercase=false local_show_config_warnings=true local_show_config_errors=true @@ -72,6 +76,10 @@ function setup_global_variables local_case_sensitive_keys=${case_sensitive_keys} fi + if [[ -n "${default_to_uppercase}" ]] && [[ "${default_to_uppercase}" = false || "${default_to_uppercase}" = true ]]; then + local_default_to_uppercase=${default_to_uppercase} + fi + if [[ -n "${show_config_warnings}" ]] && [[ "${show_config_warnings}" = false || "${show_config_warnings}" = true ]]; then local_show_config_warnings=${show_config_warnings} fi @@ -79,6 +87,11 @@ function setup_global_variables if [[ -n "${show_config_errors}" ]] && [[ "${show_config_errors}" = false || "${show_config_errors}" = true ]]; then local_show_config_errors=${show_config_errors} fi + + DEFAULT_SECTION=$(handle_default_case "${DEFAULT_SECTION}") + + # Move to from global settting to handle default uppercase option + sections+=("${DEFAULT_SECTION}") } # -------------------------------------------------------------------------------- # @@ -136,6 +149,25 @@ function show_error() fi } +# -------------------------------------------------------------------------------- # +# Handle Default Case # +# -------------------------------------------------------------------------------- # +# Handle the default case of a section or key. # +# -------------------------------------------------------------------------------- # + +function handle_default_case() +{ + local str=$1 + + if [[ "${local_default_to_uppercase}" = false ]]; then + str=$(echo -e "${str}" | tr '[:upper:]' '[:lower:]') # Lowercase the string + else + str=$(echo -e "${str}" | tr '[:lower:]' '[:upper:]') # Uppercase the str + fi + echo "${str}" +} + + # -------------------------------------------------------------------------------- # # Process Section Name # # -------------------------------------------------------------------------------- # @@ -153,7 +185,7 @@ function process_section_name() section=$(echo -e "${section}" | sed 's/[^a-zA-Z0-9_]//g') # Remove non-alphanumberics (except underscore) if [[ "${local_case_sensitive_sections}" = false ]]; then - section=$(echo -e "${section}" | tr '[:upper:]' '[:lower:]') # Lowercase the section name + section=$(handle_default_case "${section}") fi echo "${section}" } @@ -174,7 +206,7 @@ function process_key_name() key=$(echo -e "${key}" | sed 's/[^a-zA-Z0-9_]//g') # Remove non-alphanumberics (except underscore) if [[ "${local_case_sensitive_keys}" = false ]]; then - key=$(echo -e "${key}" | tr '[:upper:]' '[:lower:]') # Lowercase the section name + key=$(handle_default_case "${key}") fi echo "${key}" } @@ -243,29 +275,28 @@ function process_ini_file() shopt -s extglob - while read -r line; do + while IFS= read -r line || [ -n "$line" ]; do + line=$(echo "$line" | tr -d '\r') # Remove carriage return if present line_number=$((line_number+1)) - if [[ ${line} =~ ^# || ${line} =~ ^\; || -z ${line} ]]; then # Ignore comments / empty lines + if [[ ${line} =~ ^# || ${line} =~ ^\; || -z ${line} ]]; then # Ignore comments / empty lines continue; fi - if [[ ${line} =~ ^"["(.+)"]"$ ]]; then # Match pattern for a 'section' + if [[ ${line} =~ ^"["(.+)"]"$ ]]; then # Match pattern for a 'section' section=$(process_section_name "${BASH_REMATCH[1]}") if ! in_array sections "${section}"; then - eval "${section}_keys=()" # Use eval to declare the keys array - eval "${section}_values=()" # Use eval to declare the values array - sections+=("${section}") # Add the section name to the list + eval "${section}_keys=()" # Use eval to declare the keys array + eval "${section}_values=()" # Use eval to declare the values array + sections+=("${section}") # Add the section name to the list fi - elif [[ ${line} =~ ^(.*)"="(.*) ]]; then # Match patter for a key=value pair + elif [[ ${line} =~ ^(.*)"="(.*) ]]; then # Match pattern for a key=value pair key=$(process_key_name "${BASH_REMATCH[1]}") value=$(process_value "${BASH_REMATCH[2]}") if [[ -z ${key} ]]; then show_error 'line %d: No key name\n' "${line_number}" - elif [[ -z ${value} ]]; then - show_error 'line %d: No value\n' "${line_number}" else if [[ "${section}" == "${DEFAULT_SECTION}" ]]; then show_warning '%s=%s - Defined on line %s before first section - added to "%s" group\n' "${key}" "${value}" "${line_number}" "${DEFAULT_SECTION}" @@ -276,9 +307,9 @@ function process_ini_file() if in_array "${key_array_name}" "${key}"; then show_warning 'key %s - Defined multiple times within section %s\n' "${key}" "${section}" fi - eval "${section}_keys+=(${key})" # Use eval to add to the keys array - eval "${section}_values+=('${value}')" # Use eval to add to the values array - eval "${section}_${key}='${value}'" # Use eval to declare a variable + eval "${section}_keys+=(${key})" # Use eval to add to the keys array + eval "${section}_values+=('${value}')" # Use eval to add to the values array + eval "${section}_${key}='${value}'" # Use eval to declare a variable fi fi done < "$1" @@ -301,6 +332,9 @@ function get_value() section=$(process_section_name "${1}") key=$(process_key_name "${2}") + section=$(handle_default_case "${section}") + key=$(handle_default_case "${key}") + eval "keys=( \"\${${section}_keys[@]}\" )" eval "values=( \"\${${section}_values[@]}\" )" @@ -358,6 +392,7 @@ function display_config_by_section() local keys='' local values='' + section=$(handle_default_case "${section}") printf '[%s]\n' "${section}" eval "keys=( \"\${${section}_keys[@]}\" )"