From aeb08acf9a29e4f4fa272f17754ffc04f31881fd Mon Sep 17 00:00:00 2001 From: albfan Date: Tue, 20 Feb 2018 23:46:58 +0100 Subject: [PATCH] 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 --- README.md | 5 ++++- bash-ini-parser | 8 ++++++++ t/Makefile.am | 2 +- t/t0005-invalid-chars.sh | 34 ++++++++++++++++++++++++++++++++++ t/t0005/invalid.ini | 9 +++++++++ t/t0005/invalid.out.correct | 9 +++++++++ 6 files changed, 65 insertions(+), 2 deletions(-) create mode 100755 t/t0005-invalid-chars.sh create mode 100644 t/t0005/invalid.ini create mode 100644 t/t0005/invalid.out.correct diff --git a/README.md b/README.md index e6c5a24..69be091 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,10 @@ Outputs: ### Debugging 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 diff --git a/bash-ini-parser b/bash-ini-parser index 23384e0..3881da6 100755 --- a/bash-ini-parser +++ b/bash-ini-parser @@ -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 ] diff --git a/t/Makefile.am b/t/Makefile.am index 46ad065..0df77c7 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -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) diff --git a/t/t0005-invalid-chars.sh b/t/t0005-invalid-chars.sh new file mode 100755 index 0000000..ee01ce7 --- /dev/null +++ b/t/t0005-invalid-chars.sh @@ -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 diff --git a/t/t0005/invalid.ini b/t/t0005/invalid.ini new file mode 100644 index 0000000..0afe0ab --- /dev/null +++ b/t/t0005/invalid.ini @@ -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 diff --git a/t/t0005/invalid.out.correct b/t/t0005/invalid.out.correct new file mode 100644 index 0000000..4805bed --- /dev/null +++ b/t/t0005/invalid.out.correct @@ -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"