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
2324declare case_sensitive_sections
2425declare case_sensitive_keys
26+ declare default_to_uppercase
2527declare show_config_warnings
2628declare show_config_errors
2729
@@ -35,7 +37,7 @@ declare show_config_errors
3537
3638DEFAULT_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
5154local_case_sensitive_sections=true
5255local_case_sensitive_keys=true
56+ local_default_to_uppercase=false
5357local_show_config_warnings=true
5458local_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