You've already forked bash-ini-parser
mirror of
https://github.com/albfan/bash-ini-parser.git
synced 2025-08-07 18:02:52 +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:
@@ -89,6 +89,9 @@ Outputs:
|
||||
|
||||
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
|
||||
|
||||
This is more a hack than a reliable parser, so keep in mind things like
|
||||
|
@@ -62,6 +62,14 @@ function cfg_parser {
|
||||
debug
|
||||
ini[${#ini[*]} + 1]='}' # add the last brace
|
||||
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_STATUS=$?
|
||||
if [ $CHANGE_EXTGLOB = 1 ]
|
||||
|
@@ -1,4 +1,4 @@
|
||||
TEST_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \
|
||||
$(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)
|
||||
|
34
t/t0005-invalid-chars.sh
Executable file
34
t/t0005-invalid-chars.sh
Executable 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
9
t/t0005/invalid.ini
Normal 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
|
9
t/t0005/invalid.out.correct
Normal file
9
t/t0005/invalid.out.correct
Normal 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"
|
Reference in New Issue
Block a user