From 1255adfdbf43852552f2a043eccdb92a3a47472a Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Sat, 20 Jul 2019 20:08:19 +0100 Subject: [PATCH] Only output a section array declaration if there's some elements being defined too. --- parse_ini | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/parse_ini b/parse_ini index 8329d0b..8a63c31 100755 --- a/parse_ini +++ b/parse_ini @@ -259,7 +259,7 @@ parse_ini() { local VARIABLE_DELIM="_" # Delimiter between prefix and section name, unless VARIABLE_PREFIX is empty. # Variables. - local BOOL_VALUE DELIM IGNORE_SECTION=0 INIFD KEY LINE LINENUMBER=0 PREFIX SECTIONS_SEEN=() TEMP VALUE + local BOOL_VALUE DELIM IGNORE_SECTION=0 INIFD KEY LINE LINENUMBER=0 PREFIX SECTIONS_SEEN=() SHOWN_SEC_HEAD=0 TEMP VALUE declare INIFILE # Parse options. @@ -316,10 +316,6 @@ parse_ini() { DELIM="$VARIABLE_DELIM" fi - # Output the 'global' section definition. - # FIXME: If doing validation only, don't output declaration here. - printf "declare %s -A %s%s%s\\n" "$DECLARE_SCOPE" "$PREFIX" "$DELIM" "$CURRENT_SECTION" - # Parse the INI file. while :; do LINE="" @@ -406,9 +402,8 @@ parse_ini() { # Reset the ignore flag. IGNORE_SECTION=0 - # Output the associative array declaration. - # FIXME: If doing validation only, don't output declaration here. - printf "declare %s -A %s%s%s\\n" "$DECLARE_SCOPE" "$PREFIX" "$DELIM" "$CURRENT_SECTION" + # Flag that the section header needs to be shown. + SHOWN_SEC_HEAD=0 fi elif ((IGNORE_SECTION == 0)) && [[ "$LINE" != *$KEYVALUE_DELIM* ]]; then # Process the property definition as if it's a boolean. # If the value starts with a " or ' it must end with same. @@ -439,8 +434,13 @@ parse_ini() { fi # Output the associative array element definition. - # FIXME: If doing validation only, don't output declaration here. + # FIXME: If doing validation only, don't output declarations here. if ((USE_BOOLEANS == 1)); then + # If required, output the associative array declaration. + if ((SHOWN_SEC_HEAD == 0)); then + printf "declare %s -A %s%s%s\\n" "$DECLARE_SCOPE" "$PREFIX" "$DELIM" "$CURRENT_SECTION" + SHOWN_SEC_HEAD=1 + fi printf "%s%s%s[\"%s\"]=\"%s\"\\n" "$PREFIX" "${PREFIX:+$DELIM}" "$CURRENT_SECTION" "$LINE" "$BOOL_VALUE" else echo "${0##*/}: line $LINENUMBER: key without a value - skipping property" >&2 @@ -471,6 +471,13 @@ parse_ini() { fi fi + # If required, output the associative array declaration. + # FIXME: If doing validation only, don't output declaration here. + if ((SHOWN_SEC_HEAD == 0)); then + printf "declare %s -A %s%s%s\\n" "$DECLARE_SCOPE" "$PREFIX" "$DELIM" "$CURRENT_SECTION" + SHOWN_SEC_HEAD=1 + fi + # Output the associative array element definition. # FIXME: If doing validation only, don't output declaration here. if ((DUPLICATES_MERGE == 0)); then