]> git.ozlabs.org Git - ccan/commit
opt: add integer helpers that accept k, M, G, T, P, E suffixes
authorDouglas Bagnall <douglas@paradise.net.nz>
Sat, 13 Aug 2011 12:19:59 +0000 (21:49 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Sat, 13 Aug 2011 12:19:59 +0000 (21:49 +0930)
commitaf7afcd46e70e87397c33408d88f2bedd2c90bd8
tree536e67c79461d2f8641fb4383a3545696ee4d8bc
parent736e5860207154ce01d79629813f181611468321
opt: add integer helpers that accept k, M, G, T, P, E suffixes

These functions come in two flavours: those ending with "_si", which
have 1000-based interpretations of the suffixes; and those ending with
"_bi", which use base 1024.  There are versions for signed and
unsigned int, long, and long long destinations, with tests for all 12
new functions.  The tests get a bit repetitive, I am afraid.

As an example, if the -x option were using the opt_set_intval_bi
function, then all of these would do the same thing:

$ foo -x 5M
$ foo -x $((5 * 1024 * 1024))
$ foo -x 5242880
$ foo -x 5120k

quite what that thing is depends on the size of your int -- people
with 16 bit ints would see an "out of range" error message.

The arithmetic for unsigned variations is actually done using signed
long long integers, so the maximum possible value is LLONG_MAX, not
ULLONG_MAX.  This follows the practice of existing functions, and
avoids tedious work.
ccan/opt/helpers.c
ccan/opt/opt.h
ccan/opt/test/run-helpers.c