Skip to content

Commit

Permalink
New Updates
Browse files Browse the repository at this point in the history
* Fixing raised issues

* Add case handling for default to uppercase

* Fix shellcheck errors
  • Loading branch information
TGWolf committed Sep 12, 2024
1 parent ebba2ff commit 9a1e0d8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
27 changes: 22 additions & 5 deletions demos/parse-example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
# 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 #
# -------------------------------------------------------------------------------- #
# These variables allow us to override the parse script defaults. #
# #
# 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 #
# #
Expand All @@ -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

Expand Down Expand Up @@ -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 #
Expand All @@ -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 #
Expand Down
65 changes: 50 additions & 15 deletions src/ini-file-parser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -35,7 +37,7 @@ declare show_config_errors

DEFAULT_SECTION='default'

sections=( "${DEFAULT_SECTION}" )
sections=()

# -------------------------------------------------------------------------------- #
# Local Variables #
Expand All @@ -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

Expand All @@ -72,13 +76,22 @@ 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

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}")
}

# -------------------------------------------------------------------------------- #
Expand Down Expand Up @@ -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 #
# -------------------------------------------------------------------------------- #
Expand All @@ -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}"
}
Expand All @@ -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}"
}
Expand Down Expand Up @@ -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}"
Expand All @@ -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"
Expand All @@ -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[@]}\" )"

Expand Down Expand Up @@ -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[@]}\" )"
Expand Down

0 comments on commit 9a1e0d8

Please sign in to comment.