Hi All,
I guess the lack of comments after my last post shows the interface was not
really user-friendly ... My sponsor conviced me about this, and suggested a new
one, much more like printf().
Many of you mentionned to have a look at printf() family functions before, I
though the idea was to modify the printf() function itself, which I did not
want, that's why I had removed the printf-like interface from my mind.
The goal of the function is still the same, convert a number to a string, with
automatic scaling to one of the units (kilo, mega, giga ...)
Then, here is the new proposal for the humanize_number function :
int humanize_number(int64_t value, const char *fmt, char *buffer, size_t
buf_len)
<value> is obviously the value to be converted to a string.
<fmt> is the format string
<buffer> is where the result is written
<buf_len> is the buffer's size
I guess the only parameter that deserves comments is <fmt>.
This strings describes how the <value> will be written into <buffer>, just like
for printf()
fmt is like this : "some_custom_prefixFORMATsome_custom_suffix"
FORMAT is like this : () stands for optionnal, [] is for mandatory
[%](modifiers)(len)(.decimal_precision)('b' char)[unit_char]
(modifiers) can be one or many of these chars (or none).
I won't explain them in depth since they are the same than with printf("%f").
'0' : zero padding
'-' : left aligned
'+' : 1st char is a '+' if result > 0
' ' (space char) : 1st char is a space is the result > 0. Used only if '+'
modifier not defined.
'#' : don't print trailing zero (this one comes from printf("%g"))
(len) is the total len. If the result is smaller, it'll be space padded, or
zero-padded if 0 modifier is set.
(.decprec) is the decimal precision wanted. If it not specified, printf
considers it is 6. I have choosen it to be 3 by default.
(b) if this 'b' char is present, the result is converted in 'binary' format
(value will be divided by 1024^N, instead of 1000^N which is the default)
[unit char] is one of these :
A for auto, B for bytes, K for kilo, M for mega, G for giga, P for peta, T for
tera and E for exa.
like snprintf(), the return value is 0 if all is fine, or the number of chars
that would have been written if the buffer were big enough (thus if ret >=
buflen, the buffer was certainly too short)
Examples :
Value= 123456789 - FMT='%5M' --> '123.457M'
Value= 123456789 - FMT='%5.M' --> ' 123M'
Value= 123456789 - FMT='%5.0M' --> ' 123M'
Value= 123456789 - FMT='%5.1M' --> '123.5M'
Value= 123456789 - FMT='%5.2M' --> '123.46M'
Value= 123456789 - FMT='%5.3M' --> '123.457M'
Value= 123456789 - FMT='%5.4M' --> '123.4568M'
Value= 123456789 - FMT='%+5.2K' --> '+123456.79K'
Value= 0 - FMT='%A' --> '0.000'
Value= 0 - FMT='%bB' --> '0.000'
Value= 0 - FMT='%bK' --> '0.000K'
Value= 1047552 - FMT='FOO %0#5.4bA BAR' --> 'FOO 01023K BAR'
I hope you'll like this interface much more than the previous one.
Yann
This message posted from opensolaris.org
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code