From 1c9a72337b16b4e0fbaeaf43d8ecf9a0b6fcc788 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 29 Nov 2023 22:45:39 +0100 Subject: [PATCH] array append method --- db23.sh | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/db23.sh b/db23.sh index 53cd543..9b8204a 100755 --- a/db23.sh +++ b/db23.sh @@ -9,7 +9,7 @@ DB2() { case $call in new|discard) - printf '\n' + printf '' ;; open|load) file="$1" cat "$file" || return 1 @@ -46,23 +46,32 @@ DB2() { set|store) key="$(STRING "$1")" val="" shift 1 val="$(for v in "$@"; do STRING "$v"; printf \\t; done)" - if [ "${data#${BR}${key} *}" != "$data" ]; then + if [ "${data#*${BR}${key} *}" != "$data" ]; then data="${data%${BR}${key} *${BR}*}${BR}${key} ${val% }${BR}${data#*${BR}${key} *${BR}}" data="${data#${BR}}" data="${data%${BR}}" else data="${data#${BR}}${key} ${val% }${BR}" + data="${data#${BR}}" fi printf %s\\n "${data}" ;; - flush|save|write) file="$1" - data="${data#${BR}}" data="${data%${BR}}" - if LOCK "$file"; then - printf '%s\n' "$data" >"$file" - RELEASE "$file" - else + append) key="$(STRING "$1")" val="" + val="${data##*${BR}${key} }" val="${val%%${BR}*}" + if [ "$val" = '' ]; then + printf %s\\n "${data}" return 1 + else + shift 1 + val="${val}$(for v in "$@"; do printf \\t; STRING "$v"; done)" + data="${data%${BR}${key} *${BR}*}${BR}${key} ${val% }${BR}${data#*${BR}${key} *${BR}}" + data="${data#${BR}}" data="${data%${BR}}" + printf %s\\n "${data}" fi ;; + flush|save|write) file="$1" + data="${data#${BR}}" data="${data%${BR}}" + printf '%s\n' "$data" >"$file" || return 1 + ;; esac return 0 } @@ -72,7 +81,7 @@ DB3() { # by always keeping file data in $db3_data case "$1" in - new|discard|open|load|delete|remove|set|store) + new|discard|open|load|delete|remove|set|store|append) db3_data="$(DB2 "$db3_data" "$@")" return "$?" ;; -- 2.39.5