changeset 499:562e630afb25

Implement a debug output routine for arrays: array_debug()
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 30 Aug 2024 17:29:20 +0200
parents 46406503a635
children 7d498093d4c2
files share/local-bsdtools/array.sh
diffstat 1 files changed, 42 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/array.sh	Fri Aug 30 16:49:36 2024 +0200
+++ b/share/local-bsdtools/array.sh	Fri Aug 30 17:29:20 2024 +0200
@@ -344,3 +344,45 @@
     done
     return 0
 }
+
+
+#:
+#: Print the contents of an array to stderr
+#:
+#: Args:
+#:   $1 (str): The name of an array. The array may exist or not.
+#:
+#: Returns:
+#:   0
+#:
+array_debug() {
+    local _name
+
+    local _gvrname _l
+
+    [ $# -lt 1 ] && _array_fatal "missing array name"
+    _name=$1
+    _gvrname=${_farr_global_prefix}$1
+
+    # Check whether the variable already exists
+    eval _l=\${${_gvrname}__:-__UNSET__}
+    if [ "${_l}" = "__UNSET__" ]; then
+	echo "DEBUG: array \`${_name}' does not exist" 1>&2
+        return 0
+    fi
+    echo "DEBUG: array \`${_name}' has length ${_l}" 1>&2
+    if [ ${_l} -gt 0 ]; then
+        echo "DEBUG:   its contents:" 1>&2
+        array_for_each ${_name} _array_debug_print_value
+    fi
+    return 0
+}
+
+
+#:
+#: Debug output helper for `array_debug`
+#:
+_array_debug_print_value() {
+    printf "DEBUG:     idx: %s, val: \`%s'\\n" "$2" "$3" 1>&2
+    return 0
+}