#!/bin/sh
-# Copyright 2018 Paul Hänsch
+# Copyright 2018, 2019 Paul Hänsch
#
# This is a file format helper, part of CGIlite.
#
[ -n "$include_storage" ] && return 0
include_storage="$0"
+CR="\r"
+BR='
+'
+
LOCK(){
+ local lock timeout block
lock="${1}.lock"
timeout="${2-20}"
if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then
}
RELEASE(){
+ local lock
lock="${1}.lock"
if [ "$(cat "$lock/pid")" = "$$" ]; then
rm "$lock/pid"
fi
}
+STRING='
+ s;\\;\\\\;g;
+ s;\n;\\n;g;
+ s;\t;\\t;g;
+ s;\r;\\r;g;
+ s;\+;\\+;g;
+ s; ;+;g;
+'
+
+STRING_OLD(){
+ { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+ | sed -E ':X; $!{N;bX;}'"$STRING"
+}
+
STRING(){
- printf %s "$*" |sed -r '
- :X; $!{N;bX;}
- s;\\;\\\\;g;
- s;\n;\\n;g;
- s;\t;\\t;g;
- s;\r;\\r;g;
- s;\+;\\+;g;
- s; ;+;g;
- '
+ local in out=''
+ [ $# -gt 0 ] && in="$*" || in="$(cat)"
+ while [ "$in" ]; do case $in in
+ \\*) out="${out}\\\\"; in="${in#\\}" ;;
+ "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
+ "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
+ " "*) out="${out}\\t"; in="${in# }" ;;
+ +*) out="${out}\\+"; in="${in#+}" ;;
+ " "*) out="${out}+"; in="${in# }" ;;
+ *) out="${out}${in%%[\\${CR}${BR} + ]*}"; in="${in#"${in%%[\\${BR}${CR} + ]*}"}" ;;
+ esac; done
+ printf '%s' "$out"
}
+
+UNSTRING='
+ :UNSTRING_X
+ s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
+ s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
+ s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
+ s;((^|[^\\])(\\\\)*)\+;\1 ;g;
+ tUNSTRING_X;
+ s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
+ s;\\\\;\\;g;
+'
+UNSTRING_OLD(){
+ { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+ | sed -E "$UNSTRING"
+}
UNSTRING(){
- printf %s "$*" |sed -r '
- :X
- s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
- s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
- s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
- s;((^|[^\\])(\\\\)*)\+;\1 ;g;
- tX;
- s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
- s;\\\\;\\;g;
- '
+ local in out=''
+ [ $# -gt 0 ] && in="$*" || in="$(cat)"
+ while [ "$in" ]; do case $in in
+ \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
+ \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
+ \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
+ \\t*) out="${out} "; in="${in#\\t}" ;;
+ \\+) out="${out}+"; in="${in#\\+}" ;;
+ +*) out="${out} "; in="${in#+}" ;;
+ \\*) in="${in#\\}" ;;
+ *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
+ esac; done
+ printf '%s' "$out"
}
+