diff options
author | Dave Reisner <dreisner@archlinux.org> | 2012-04-25 22:27:19 -0400 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2012-06-25 23:39:12 -0500 |
commit | b58489d29a1898a4bff1b5da6ca4bfb14003dbfe (patch) | |
tree | 5c42f704cc671136ef43972cfdee1274e620e65f /scripts | |
parent | e183522e3168c4a31103b3c7910fa8d29333fb5a (diff) | |
download | pacman-b58489d29a1898a4bff1b5da6ca4bfb14003dbfe.tar.xz |
scripts/library: add human_to_size
This is a bash wrapper around an awk function that parses human readable
sizes and returns their representative values in bytes, as a string. A
small test harness is added to validate the functionality.
Signed-off-by: Dave Reisner <dreisner@archlinux.org>
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/Makefile.am | 3 | ||||
-rw-r--r-- | scripts/library/README | 8 | ||||
-rw-r--r-- | scripts/library/human_to_size.sh | 51 |
3 files changed, 61 insertions, 1 deletions
diff --git a/scripts/Makefile.am b/scripts/Makefile.am index 6bb72198..75699b2e 100644 --- a/scripts/Makefile.am +++ b/scripts/Makefile.am @@ -27,7 +27,8 @@ EXTRA_DIST = \ LIBRARY = \ library/output_format.sh \ - library/parseopts.sh + library/parseopts.sh \ + library/human_to_size.sh # Files that should be removed, but which Automake does not know. MOSTLYCLEANFILES = $(bin_SCRIPTS) diff --git a/scripts/library/README b/scripts/library/README index c71c0714..44748ee2 100644 --- a/scripts/library/README +++ b/scripts/library/README @@ -27,3 +27,11 @@ Reccommended Usage: Returns: 0: parse success 1: parse failure (error message supplied) + +human_to_size.sh: +A function to convert human readable sizes (such as "5.3 GiB") to raw byte +equivalents. base10 and base2 suffixes are supported, case sensitively. If +successful, the converted byte value is written to stdout and the function +returns 0. If an error occurs, nothing in written and the function returns 1. +Results may be inaccurate when using a broken implementation of awk, such +as mawk or busybox awk. diff --git a/scripts/library/human_to_size.sh b/scripts/library/human_to_size.sh new file mode 100644 index 00000000..11613207 --- /dev/null +++ b/scripts/library/human_to_size.sh @@ -0,0 +1,51 @@ +human_to_size() { + awk -v human="$1" ' + function trim(s) { + gsub(/^[[:space:]]+|[[:space:]]+$/, "", s) + return s + } + + function parse_units(units) { + if (!units || units == "B") + return 1 + if (match(units, /^.iB$/)) + return 1024 + if (match(units, /^.B$/)) + return 1000 + if (length(units) == 1) + return 1024 + + # parse failure: invalid base + return -1 + } + + function parse_scale(s) { + return index("BKMGTPE", s) - 1 + } + + function isnumeric(string) { + return match(string, /^[-+]?[[:digit:]]*(\.[[:digit:]]*)?/) + } + + BEGIN { + # peel off the leading number as the size, fail on invalid number + human = trim(human) + if (isnumeric(human)) + size = substr(human, RSTART, RLENGTH) + else + exit 1 + + # the trimmed remainder is assumed to be the units + units = trim(substr(human, RLENGTH + 1)) + + base = parse_units(units) + if (base < 0) + exit 1 + + scale = parse_scale(substr(units, 1, 1)) + if (scale < 0) + exit 1 + + printf "%d\n", size * base^scale + (size + 0 > 0 ? 0.5 : -0.5) + }' +} |