__ini_value: add delimiter space detection

This adds spaces around the delimiter to provide some flexibility than
just do it in the delimiter string directly. It can be set via the
parameter `--delimiter-space`.
This commit is contained in:
matze 2021-03-11 21:50:55 +01:00
parent 9006994023
commit b76d848540
4 changed files with 34 additions and 15 deletions

View file

@ -48,6 +48,7 @@ BEGIN {
getline delimiter < (_param "delimiter") getline delimiter < (_param "delimiter")
getline value < (_param "value") getline value < (_param "value")
getline indentation < (_param "indentation") getline indentation < (_param "indentation")
getline delimiter_space < (_param "delimiter-space")
do_normalization = (system("test -f " (_param "normalize")) == 0) do_normalization = (system("test -f " (_param "normalize")) == 0)
@ -108,10 +109,9 @@ found_section {
# must only contain spaces # must only contain spaces
else start_spaces = ikey - 1 else start_spaces = ikey - 1
} }
# start_spaces set or 0
idelspace_start = ikey + length(key) idelspace_start = ikey + length(key)
idelspace_length = idel - (idelspace_start) idelspace_length = idel - idelspace_start
# check for delimiter is only preceded with spaces # check for delimiter is only preceded with spaces
if(idelspace_length == 0 || check_spaces(substr(line, idelspace_start, idelspace_length))) { if(idelspace_length == 0 || check_spaces(substr(line, idelspace_start, idelspace_length))) {
found = 1 found = 1
@ -129,8 +129,16 @@ found_section {
else { else {
# check if the format is important # check if the format is important
if(do_normalization) { if(do_normalization) {
if(match(found_value, /^[ \t]+/) == 1) {
found_value = substr(found_value, 1 + RLENGTH)
del_val_spacelen = RLENGTH
}
else
del_val_spacelen = 0
# the format must exactly match, else it is incorrect # the format must exactly match, else it is incorrect
if(indentation != start_spaces || idelspace_length != 0 || found_value != is_value) if(start_spaces != indentation || found_value != is_value ||
idelspace_length != delimiter_space || del_val_spacelen != delimiter_space)
state("wrongformat") state("wrongformat")
} }

View file

@ -6,9 +6,14 @@ BEGIN {
value = get_param_string("value") value = get_param_string("value")
comment = get_param_string("comment") comment = get_param_string("comment")
indentation = get_param_string("indentation") indentation = get_param_string("indentation")
delimiter_space = get_param_string("delimiter-space")
get_param_array("comment-sign", comment_signs) get_param_array("comment-sign", comment_signs)
comment_sign = comment_signs[0] comment_sign = comment_signs[0]
base_spaces = spaces(indentation)
delimiter_spaces = spaces(delimiter_space
delimiter_w_spaces = (delimiter_spaces delimiter delimiter_spaces)
} }
function trim(var) { function trim(var) {
@ -16,6 +21,12 @@ function trim(var) {
sub(/[ \t]*$/, "", var) sub(/[ \t]*$/, "", var)
return var return var
} }
function spaces(a) {
rspaces = ""
for(b = 0; b < a; b++)
rspaces = (rspaces " ")
return rspaces
}
function check_spaces(part) { function check_spaces(part) {
return match(part, /^[ \t]*$/) == 1 return match(part, /^[ \t]*$/) == 1
} }
@ -40,22 +51,13 @@ function get_param_array(name, arr) {
# print value # print value
function v_print() { function v_print() {
spaces = "" printf "%s%s%s%s%s", base_spaces, key, delimiter_w_spaces, value, ORS
for(i = 0; i < indentation; i++)
spaces = (spaces " ")
printf "%s%s%s%s%s", spaces, key, delimiter, value, ORS
} }
# print commented value # print commented value
function v_print_commented() { function v_print_commented() {
spaces = "" printf "%s%s%s%s%s%s", base_spaces, comment_sign, key, delimiter_w_spaces, value, ORS
for(i = 0; i < indentation; i++)
spaces = (spaces " ")
printf "%s%s%s%s%s%s", spaces, comment_sign, key, delimiter, value, ORS
} }
# print comment # print comment
function c_print() { function c_print() {
spaces = "" printf "%s%s%s%s%s", base_spaces, comment_sign, " ", comment, ORS
for(i = 0; i < indentation; i++)
spaces = (spaces " ")
printf "%s%s%s%s%s", spaces, comment_sign, " ", comment, ORS
} }

View file

@ -63,6 +63,14 @@ comment-sign
parameters are possible. It uses the first specified sign as comment parameters are possible. It uses the first specified sign as comment
character if this type needs to insert comments. character if this type needs to insert comments.
delimiter-space
The number of spaces before and after the delimiter which should be free.
This number applies to each site of the delimiter separately, so one space
means one space to the left and right side of the delimiter.
The delimiter will be matched independendtly of this parameter and will
only be corrected if ``--normalize`` is set.
BOOLEAN PARAMETERS BOOLEAN PARAMETERS
------------------ ------------------

View file

@ -4,3 +4,4 @@ state
value value
indentation indentation
comment comment
delimiter-space