1
0
mirror of https://github.com/albfan/bash-ini-parser.git synced 2025-08-06 07:02:37 +03:00

Replace invalid chars

bash-ini-parser do not support all the valid chars ini key files support.

Allow to replace them with valid ones using an external function defined
with REPLACE_FUNCTION
This commit is contained in:
albfan
2018-02-20 23:46:58 +01:00
parent d73cdd267d
commit aeb08acf9a
6 changed files with 65 additions and 2 deletions

View File

@@ -88,7 +88,10 @@ Outputs:
### Debugging ### Debugging
declare `BASH_INI_PARSER_DEBUG` and parse will output the ini file processing declare `BASH_INI_PARSER_DEBUG` and parse will output the ini file processing
### Hacking zone
bash-ini-parser is based on bash env vars, which do not support all valid characters for ini files. You can define a function to replace dangling characters, See [t0005-invalid-chars.sh](https://github.com/albfan/bash-ini-parser/blob/master/t/t0005-invalid-chars.sh)
### Drawbacks ### Drawbacks
This is more a hack than a reliable parser, so keep in mind things like This is more a hack than a reliable parser, so keep in mind things like

View File

@@ -62,6 +62,14 @@ function cfg_parser {
debug debug
ini[${#ini[*]} + 1]='}' # add the last brace ini[${#ini[*]} + 1]='}' # add the last brace
debug debug
if [ -v "REPLACE_FUNCTION" ]
then
ini=($(for i in "${ini[@]}"
do
$REPLACE_FUNCTION $i
done))
fi
debug
eval "$(echo "${ini[*]}")" # eval the result eval "$(echo "${ini[*]}")" # eval the result
EVAL_STATUS=$? EVAL_STATUS=$?
if [ $CHANGE_EXTGLOB = 1 ] if [ $CHANGE_EXTGLOB = 1 ]

View File

@@ -1,4 +1,4 @@
TEST_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \ TEST_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \
$(top_srcdir)/build-aux/tap-driver.sh $(top_srcdir)/build-aux/tap-driver.sh
TESTS = t0001-whitespace.sh t0002-invalid.sh t0003-sections.sh t0004-comments.sh TESTS = t0001-whitespace.sh t0002-invalid.sh t0003-sections.sh t0004-comments.sh t0005-invalid-chars.sh
EXTRA_DIST = $(TESTS) EXTRA_DIST = $(TESTS)

34
t/t0005-invalid-chars.sh Executable file
View File

@@ -0,0 +1,34 @@
#!/bin/bash
test_description="replace invalid chars"
. setup.sh
DIR_TEST=$SHARNESS_TEST_DIRECTORY/t0005
#replace function
function replace_chars() {
if [[ $i =~ .*=.* ]]
then
local key=$(echo $1 | sed 's/\(.*\)=.*/\1/;s/-/_/g')
local value=$(echo $1 | sed 's/.*=\(.*\)/\1/')
echo "$key=$value"
else
echo $1
fi
}
#export function to be avaliable on bash-ini-parser script
export -f replace_chars
#define var to use function inside bash-ini-parser
export REPLACE_FUNCTION=replace_chars
test_expect_success "Replace invalid chars" "
export COVERAGE_NAME=comments_parser
cp ../.simplecov .
cfg_parser $DIR_TEST/invalid.ini
cfg_writer > invalid.out
diff $DIR_TEST/invalid.out.correct invalid.out
"
test_done

9
t/t0005/invalid.ini Normal file
View File

@@ -0,0 +1,9 @@
[sec1]
var-1=foo
var2=hoge
var3=hoge#bar
var4="#hello #bye #chao"
[sec2]
var2=foo
var-4="123#456"
var5=234#456

View File

@@ -0,0 +1,9 @@
[sec1]
var_1="foo"
var2="hoge"
var3="hoge#bar"
var4="#hello #bye #chao"
[sec2]
var2="foo"
var_4="123#456"
var5="234#456"