__ini_value: implement multi-line buffer

This buffer gives the ability to look a bit longer into the past than
just a single line. This is helpfull to get a bigger context when
fiddling around comments. This also erases the need of
`lastlinepopulated` as there is something in the pipe or nothing.
This commit is contained in:
matze 2021-03-06 17:06:32 +01:00
parent 8c7a6906de
commit 10427fde84
6 changed files with 38 additions and 31 deletions

View File

@ -1,6 +1,7 @@
BEGIN {
#bufindex = 0
#maxbuflen = 2
bufindex = -1
buflen = 0
maxbuflen = 2
# no section means the start to the first section
if(section == "") {
@ -9,15 +10,31 @@ BEGIN {
}
}
# legacy function
# controls the line buffer
function flush_buffer() {
#while(bufindex > 0) {
# _pop_buffer()
#}
if(lastlinepopulated) {
print lastline
lastlinepopulated = 0
}
while(buflen > 0)
_pop_line()
}
function push_line() {
linebuf[++bufindex] = $0
buflen++;
while(buflen > maxbuflen) _pop_line()
}
function revert_line() {
# no delete, because it will be overwritten by the next line if any ..
bufindex--
buflen--
}
function lastline() {
if(buflen > 0) return linebuf[bufindex]
}
function pop_line() {
if(buflen > 0) _pop_line()
}
function _pop_line() {
_index = bufindex - (--buflen)
print linebuf[_index]
delete linebuf[_index]
}
# excepts the first character is the sign to check (string is trimmed)
@ -47,18 +64,10 @@ found {print; next}
line = trim($0)
# short curcit on empty lines
if(line == "") {
# TODO code to function!
flush_buffer()
lastline = $0
lastlinepopulated = 1
push_line()
next
}
# write to linebuffer
#if(bufindex > maxbuflen)
# _pop_buffer()
#linebuffer[++bufindex] = $0
# check for a ini section
if(substr(line, 1, 1) == "[" && substr(line, length(line), 1) == "]") {
is_section = 1
@ -104,12 +113,8 @@ found {print; next}
}
}
# TODO
# works cause no next statement from above *structual programming*
#print lastline
flush_buffer()
lastline = $0
lastlinepopulated = 1
push_line()
}
END {

View File

@ -1,3 +1,5 @@
# flush last line if it was not the meant comment
if(!was_comment(lastline, comment)) flush_buffer()
else lastlinepopulated = 0
# revert line if it was a comment
if(was_comment(lastline, comment)) revert_line()
# value line was not pushed to the buffer yet
flush_buffer()

View File

@ -1,5 +1,5 @@
# check if last line was the comment
was_com_there = was_comment(lastline, comment)
was_com_there = was_comment(lastline(), comment)
# print + comment if not there
flush_buffer()

View File

@ -1,4 +1,4 @@
was_com_there = was_comment(lastline, comment)
was_com_there = was_comment(lastline(), comment)
# print before and comment
flush_buffer()

View File

@ -1,5 +1,5 @@
# check if last line was the comment
was_com_there = was_comment(lastline, comment)
was_com_there = was_comment(lastline(), comment)
# print + comment if not there
flush_buffer()

View File

@ -1,4 +1,4 @@
was_com_there = was_comment(lastline, comment)
was_com_there = was_comment(lastline(), comment)
# print before and comment
flush_buffer()