On Mar 23, 2006, at 2:23 PM, Yann POUPET wrote:
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.
What about milli, micro, nano and pico ???
With these added there will be possibilities of
more universal use of this function.
Kaiser Jasse -- Authorized Stealth Oracle
The axioms of wisdom:
1. You can't outstubborn a cat
2. You can't conquer the universe without the knowledge of FORTRAN
3. In the Unix realm, 10% of work fixes 90% of the problems
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code