16
16
# #
17
17
# case_sensitive_sections - should section names be case sensitive #
18
18
# case_sensitive_keys - should key names be case sensitive #
19
+ # default_to_uppercase - If we are using case insensitive, default to uppercase #
19
20
# show_config_warnings - should we show config warnings #
20
21
# show_config_errors - should we show config errors #
21
22
# -------------------------------------------------------------------------------- #
22
23
23
24
declare case_sensitive_sections
24
25
declare case_sensitive_keys
26
+ declare default_to_uppercase
25
27
declare show_config_warnings
26
28
declare show_config_errors
27
29
@@ -35,7 +37,7 @@ declare show_config_errors
35
37
36
38
DEFAULT_SECTION=' default'
37
39
38
- sections=( " ${DEFAULT_SECTION} " )
40
+ sections=()
39
41
40
42
# -------------------------------------------------------------------------------- #
41
43
# Local Variables #
@@ -44,12 +46,14 @@ sections=( "${DEFAULT_SECTION}" )
44
46
# #
45
47
# local_case_sensitive_sections - should section names be case sensitive #
46
48
# local_case_sensitive_keys - should key names be case sensitive #
49
+ # default_to_uppercase - should we default to uppercase #
47
50
# local_show_config_warnings - should we show config warnings #
48
51
# local_show_config_errors - should we show config errors #
49
52
# -------------------------------------------------------------------------------- #
50
53
51
54
local_case_sensitive_sections=true
52
55
local_case_sensitive_keys=true
56
+ local_default_to_uppercase=false
53
57
local_show_config_warnings=true
54
58
local_show_config_errors=true
55
59
@@ -72,13 +76,22 @@ function setup_global_variables
72
76
local_case_sensitive_keys=${case_sensitive_keys}
73
77
fi
74
78
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
+
75
83
if [[ -n " ${show_config_warnings} " ]] && [[ " ${show_config_warnings} " = false || " ${show_config_warnings} " = true ]]; then
76
84
local_show_config_warnings=${show_config_warnings}
77
85
fi
78
86
79
87
if [[ -n " ${show_config_errors} " ]] && [[ " ${show_config_errors} " = false || " ${show_config_errors} " = true ]]; then
80
88
local_show_config_errors=${show_config_errors}
81
89
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} " )
82
95
}
83
96
84
97
# -------------------------------------------------------------------------------- #
@@ -136,6 +149,25 @@ function show_error()
136
149
fi
137
150
}
138
151
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
+
139
171
# -------------------------------------------------------------------------------- #
140
172
# Process Section Name #
141
173
# -------------------------------------------------------------------------------- #
@@ -153,7 +185,7 @@ function process_section_name()
153
185
section=$( echo -e " ${section} " | sed ' s/[^a-zA-Z0-9_]//g' ) # Remove non-alphanumberics (except underscore)
154
186
155
187
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} " )
157
189
fi
158
190
echo " ${section} "
159
191
}
@@ -174,7 +206,7 @@ function process_key_name()
174
206
key=$( echo -e " ${key} " | sed ' s/[^a-zA-Z0-9_]//g' ) # Remove non-alphanumberics (except underscore)
175
207
176
208
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} " )
178
210
fi
179
211
echo " ${key} "
180
212
}
@@ -243,29 +275,28 @@ function process_ini_file()
243
275
244
276
shopt -s extglob
245
277
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
247
280
line_number=$(( line_number+ 1 ))
248
281
249
- if [[ ${line} =~ ^# || ${line} =~ ^\; || -z ${line} ]]; then # Ignore comments / empty lines
282
+ if [[ ${line} =~ ^# || ${line} =~ ^\; || -z ${line} ]]; then # Ignore comments / empty lines
250
283
continue ;
251
284
fi
252
285
253
- if [[ ${line} =~ ^" [" (.+)" ]" $ ]]; then # Match pattern for a 'section'
286
+ if [[ ${line} =~ ^" [" (.+)" ]" $ ]]; then # Match pattern for a 'section'
254
287
section=$( process_section_name " ${BASH_REMATCH[1]} " )
255
288
256
289
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
260
293
fi
261
- elif [[ ${line} =~ ^(.* )" =" (.* ) ]]; then # Match patter for a key=value pair
294
+ elif [[ ${line} =~ ^(.* )" =" (.* ) ]]; then # Match pattern for a key=value pair
262
295
key=$( process_key_name " ${BASH_REMATCH[1]} " )
263
296
value=$( process_value " ${BASH_REMATCH[2]} " )
264
297
265
298
if [[ -z ${key} ]]; then
266
299
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} "
269
300
else
270
301
if [[ " ${section} " == " ${DEFAULT_SECTION} " ]]; then
271
302
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()
276
307
if in_array " ${key_array_name} " " ${key} " ; then
277
308
show_warning ' key %s - Defined multiple times within section %s\n' " ${key} " " ${section} "
278
309
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
282
313
fi
283
314
fi
284
315
done < " $1 "
@@ -301,6 +332,9 @@ function get_value()
301
332
section=$( process_section_name " ${1} " )
302
333
key=$( process_key_name " ${2} " )
303
334
335
+ section=$( handle_default_case " ${section} " )
336
+ key=$( handle_default_case " ${key} " )
337
+
304
338
eval " keys=( \"\$ {${section} _keys[@]}\" )"
305
339
eval " values=( \"\$ {${section} _values[@]}\" )"
306
340
@@ -358,6 +392,7 @@ function display_config_by_section()
358
392
local keys=' '
359
393
local values=' '
360
394
395
+ section=$( handle_default_case " ${section} " )
361
396
printf ' [%s]\n' " ${section} "
362
397
363
398
eval " keys=( \"\$ {${section} _keys[@]}\" )"
0 commit comments