3 # Copyright 2023, 2024 Paul Hänsch
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 [ -n "$include_db23" ] && return 0
20 . "${_EXEC:-.}/cgilite/storage.sh"
23 local call data file key val seq
24 data="${BR}${1}${BR}" call="$2"
32 cat "$file" || return 1
34 check|contains) key="$(STRING "$1")" val=''
35 val="${data##*"${BR}${key}" }" val="${val%%"${BR}"*}"
36 [ "$val" = '' ] && return 1
38 count) key="$(STRING "$1")" val='' seq=0
39 val="${data##*"${BR}${key}" }" val="${val%%"${BR}"*}"
40 [ "$val" = '' ] || val="${val} "
41 while [ "$val" != '' ]; do
42 seq=$((seq + 1)) val="${val#* }"
45 [ $seq = 0 ] && return 1
47 get) key="$(STRING "$1")" seq="${2:-1}"
48 val="${data##*"${BR}${key}" }" val="${val%%"${BR}"*}"
49 [ "$val" = '' ] && return 1 || val="${val} "
50 while [ $seq -gt 1 ]; do
51 seq=$((seq - 1)) val="${val#* }"
53 [ "$val" = '' ] && return 1
56 iterate|raw) key="$(STRING "$1")"
57 val="${data##*"${BR}${key}" }" val="${val%%"${BR}"*}"
58 [ "$val" = '' ] && return 1
61 delete|remove) key="$(STRING "$1")"
62 val="${data#*"${BR}${key}" *"${BR}"}"
63 key="${data%"${BR}${key}" *"${BR}"*}"
64 [ "${key}${BR}${val}" = "${data}" ] && return 1
65 printf '%s' "${key#"${BR}"}${BR}${val%"${BR}"}"
67 set|store) key="$(STRING "$1")" val=""
69 val="$(for v in "$@"; do STRING "$v"; printf \\t; done)"
70 if [ "${data#*"${BR}${key}" *}" != "$data" ]; then
71 data="${data%"${BR}${key}" *"${BR}"*}${BR}${key} ${val% }${BR}${data#*"${BR}${key}" *"${BR}"}"
72 data="${data#"${BR}"}" data="${data%"${BR}"}"
74 data="${data#"${BR}"}${key} ${val% }${BR}"
75 data="${data#"${BR}"}"
77 printf %s\\n "${data}"
79 append) key="$(STRING "$1")" val=""
80 val="${data##*"${BR}${key}" }" val="${val%%"${BR}"*}"
81 if [ "$val" = '' ]; then
82 printf %s\\n "${data}"
86 val="${val}$(for v in "$@"; do printf \\t; STRING "$v"; done)"
87 data="${data%"${BR}${key}" *"${BR}"*}${BR}${key} ${val% }${BR}${data#*"${BR}${key}" *"${BR}"}"
88 data="${data#"${BR}"}" data="${data%"${BR}"}"
89 printf %s\\n "${data}"
92 flush|save|write) file="$1"
93 data="${data#"${BR}"}" data="${data%"${BR}"}"
94 printf '%s\n' "$data" >"$file" || return 1
101 # wrapper function that allows easyer use of DB2
102 # by always keeping file data in $db3_data
105 new|discard|open|load|delete|remove|set|store|append)
106 db3_data="$(DB2 "$db3_data" "$@")"
109 get|count|check|contains|iterate|raw|flush|save|write)