Skip to content

Commit 9a1e0d8

Browse files
authored
New Updates
* Fixing raised issues * Add case handling for default to uppercase * Fix shellcheck errors
1 parent ebba2ff commit 9a1e0d8

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

demos/parse-example.sh

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515
# cleaner to do it this way and also makes sure that shellcheck is 100% clean. #
1616
# -------------------------------------------------------------------------------- #
1717

18-
declare section1_value1
1918
declare sections
19+
20+
declare section1_value1
2021
declare section1_keys
2122
declare section1_values
2223

24+
declare SECTION1_VALUE1
25+
declare SECTION1_keys
26+
declare SECTION1_values
27+
2328
# -------------------------------------------------------------------------------- #
2429
# Global Overrides #
2530
# -------------------------------------------------------------------------------- #
2631
# These variables allow us to override the parse script defaults. #
2732
# #
2833
# case_sensitive_sections - should section names be case sensitive #
2934
# case_sensitive_keys - should key names be case sensitive #
35+
# default_to_uppercase - should we default to uppercase? #
3036
# show_config_warnings - should we show config warnings #
3137
# show_config_errors - should we show config errors #
3238
# #
@@ -35,7 +41,8 @@ declare section1_values
3541
# -------------------------------------------------------------------------------- #
3642

3743
export case_sensitive_sections=false
38-
#export case_sensitive_keys=false
44+
export case_sensitive_keys=false
45+
export default_to_uppercase=true
3946
#export show_config_warnings=false
4047
#export show_config_errors=false
4148

@@ -94,7 +101,11 @@ echo "${value}"
94101
# -------------------------------------------------------------------------------- #
95102

96103
echo "Display Section 1 - Value 1 (Named variable)"
97-
echo "${section1_value1}"
104+
if [[ "${default_to_uppercase}" = false ]]; then
105+
echo "${section1_value1}"
106+
else
107+
echo "${SECTION1_VALUE1}"
108+
fi
98109

99110
# -------------------------------------------------------------------------------- #
100111
# Section, Key and Value Traversals #
@@ -106,8 +117,14 @@ echo
106117
echo "Display Section, Key and Value Traversals"
107118

108119
echo "${sections[@]}"
109-
echo "${section1_keys[@]}"
110-
echo "${section1_values[@]}"
120+
121+
if [[ "${default_to_uppercase}" = false ]]; then
122+
echo "${section1_keys[@]}"
123+
echo "${section1_values[@]}"
124+
else
125+
echo "${SECTION1_keys[@]}"
126+
echo "${SECTION1_values[@]}"
127+
fi
111128

112129
# -------------------------------------------------------------------------------- #
113130
# End of Script #

src/ini-file-parser.sh

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
# #
1717
# case_sensitive_sections - should section names be case sensitive #
1818
# case_sensitive_keys - should key names be case sensitive #
19+
# default_to_uppercase - If we are using case insensitive, default to uppercase #
1920
# show_config_warnings - should we show config warnings #
2021
# show_config_errors - should we show config errors #
2122
# -------------------------------------------------------------------------------- #
2223

2324
declare case_sensitive_sections
2425
declare case_sensitive_keys
26+
declare default_to_uppercase
2527
declare show_config_warnings
2628
declare show_config_errors
2729

@@ -35,7 +37,7 @@ declare show_config_errors
3537

3638
DEFAULT_SECTION='default'
3739

38-
sections=( "${DEFAULT_SECTION}" )
40+
sections=()
3941

4042
# -------------------------------------------------------------------------------- #
4143
# Local Variables #
@@ -44,12 +46,14 @@ sections=( "${DEFAULT_SECTION}" )
4446
# #
4547
# local_case_sensitive_sections - should section names be case sensitive #
4648
# local_case_sensitive_keys - should key names be case sensitive #
49+
# default_to_uppercase - should we default to uppercase #
4750
# local_show_config_warnings - should we show config warnings #
4851
# local_show_config_errors - should we show config errors #
4952
# -------------------------------------------------------------------------------- #
5053

5154
local_case_sensitive_sections=true
5255
local_case_sensitive_keys=true
56+
local_default_to_uppercase=false
5357
local_show_config_warnings=true
5458
local_show_config_errors=true
5559

@@ -72,13 +76,22 @@ function setup_global_variables
7276
local_case_sensitive_keys=${case_sensitive_keys}
7377
fi
7478

79+
if [[ -n "${default_to_uppercase}" ]] && [[ "${default_to_uppercase}" = false || "${default_to_uppercase}" = true ]]; then
80+
local_default_to_uppercase=${default_to_uppercase}
81+
fi
82+
7583
if [[ -n "${show_config_warnings}" ]] && [[ "${show_config_warnings}" = false || "${show_config_warnings}" = true ]]; then
7684
local_show_config_warnings=${show_config_warnings}
7785
fi
7886

7987
if [[ -n "${show_config_errors}" ]] && [[ "${show_config_errors}" = false || "${show_config_errors}" = true ]]; then
8088
local_show_config_errors=${show_config_errors}
8189
fi
90+
91+
DEFAULT_SECTION=$(handle_default_case "${DEFAULT_SECTION}")
92+
93+
# Move to from global settting to handle default uppercase option
94+
sections+=("${DEFAULT_SECTION}")
8295
}
8396

8497
# -------------------------------------------------------------------------------- #
@@ -136,6 +149,25 @@ function show_error()
136149
fi
137150
}
138151

152+
# -------------------------------------------------------------------------------- #
153+
# Handle Default Case #
154+
# -------------------------------------------------------------------------------- #
155+
# Handle the default case of a section or key. #
156+
# -------------------------------------------------------------------------------- #
157+
158+
function handle_default_case()
159+
{
160+
local str=$1
161+
162+
if [[ "${local_default_to_uppercase}" = false ]]; then
163+
str=$(echo -e "${str}" | tr '[:upper:]' '[:lower:]') # Lowercase the string
164+
else
165+
str=$(echo -e "${str}" | tr '[:lower:]' '[:upper:]') # Uppercase the str
166+
fi
167+
echo "${str}"
168+
}
169+
170+
139171
# -------------------------------------------------------------------------------- #
140172
# Process Section Name #
141173
# -------------------------------------------------------------------------------- #
@@ -153,7 +185,7 @@ function process_section_name()
153185
section=$(echo -e "${section}" | sed 's/[^a-zA-Z0-9_]//g') # Remove non-alphanumberics (except underscore)
154186

155187
if [[ "${local_case_sensitive_sections}" = false ]]; then
156-
section=$(echo -e "${section}" | tr '[:upper:]' '[:lower:]') # Lowercase the section name
188+
section=$(handle_default_case "${section}")
157189
fi
158190
echo "${section}"
159191
}
@@ -174,7 +206,7 @@ function process_key_name()
174206
key=$(echo -e "${key}" | sed 's/[^a-zA-Z0-9_]//g') # Remove non-alphanumberics (except underscore)
175207

176208
if [[ "${local_case_sensitive_keys}" = false ]]; then
177-
key=$(echo -e "${key}" | tr '[:upper:]' '[:lower:]') # Lowercase the section name
209+
key=$(handle_default_case "${key}")
178210
fi
179211
echo "${key}"
180212
}
@@ -243,29 +275,28 @@ function process_ini_file()
243275

244276
shopt -s extglob
245277

246-
while read -r line; do
278+
while IFS= read -r line || [ -n "$line" ]; do
279+
line=$(echo "$line" | tr -d '\r') # Remove carriage return if present
247280
line_number=$((line_number+1))
248281

249-
if [[ ${line} =~ ^# || ${line} =~ ^\; || -z ${line} ]]; then # Ignore comments / empty lines
282+
if [[ ${line} =~ ^# || ${line} =~ ^\; || -z ${line} ]]; then # Ignore comments / empty lines
250283
continue;
251284
fi
252285

253-
if [[ ${line} =~ ^"["(.+)"]"$ ]]; then # Match pattern for a 'section'
286+
if [[ ${line} =~ ^"["(.+)"]"$ ]]; then # Match pattern for a 'section'
254287
section=$(process_section_name "${BASH_REMATCH[1]}")
255288

256289
if ! in_array sections "${section}"; then
257-
eval "${section}_keys=()" # Use eval to declare the keys array
258-
eval "${section}_values=()" # Use eval to declare the values array
259-
sections+=("${section}") # Add the section name to the list
290+
eval "${section}_keys=()" # Use eval to declare the keys array
291+
eval "${section}_values=()" # Use eval to declare the values array
292+
sections+=("${section}") # Add the section name to the list
260293
fi
261-
elif [[ ${line} =~ ^(.*)"="(.*) ]]; then # Match patter for a key=value pair
294+
elif [[ ${line} =~ ^(.*)"="(.*) ]]; then # Match pattern for a key=value pair
262295
key=$(process_key_name "${BASH_REMATCH[1]}")
263296
value=$(process_value "${BASH_REMATCH[2]}")
264297

265298
if [[ -z ${key} ]]; then
266299
show_error 'line %d: No key name\n' "${line_number}"
267-
elif [[ -z ${value} ]]; then
268-
show_error 'line %d: No value\n' "${line_number}"
269300
else
270301
if [[ "${section}" == "${DEFAULT_SECTION}" ]]; then
271302
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()
276307
if in_array "${key_array_name}" "${key}"; then
277308
show_warning 'key %s - Defined multiple times within section %s\n' "${key}" "${section}"
278309
fi
279-
eval "${section}_keys+=(${key})" # Use eval to add to the keys array
280-
eval "${section}_values+=('${value}')" # Use eval to add to the values array
281-
eval "${section}_${key}='${value}'" # Use eval to declare a variable
310+
eval "${section}_keys+=(${key})" # Use eval to add to the keys array
311+
eval "${section}_values+=('${value}')" # Use eval to add to the values array
312+
eval "${section}_${key}='${value}'" # Use eval to declare a variable
282313
fi
283314
fi
284315
done < "$1"
@@ -301,6 +332,9 @@ function get_value()
301332
section=$(process_section_name "${1}")
302333
key=$(process_key_name "${2}")
303334

335+
section=$(handle_default_case "${section}")
336+
key=$(handle_default_case "${key}")
337+
304338
eval "keys=( \"\${${section}_keys[@]}\" )"
305339
eval "values=( \"\${${section}_values[@]}\" )"
306340

@@ -358,6 +392,7 @@ function display_config_by_section()
358392
local keys=''
359393
local values=''
360394

395+
section=$(handle_default_case "${section}")
361396
printf '[%s]\n' "${section}"
362397

363398
eval "keys=( \"\${${section}_keys[@]}\" )"

0 commit comments

Comments
 (0)