humanize_number.c revision 1.12 1 /* $NetBSD: humanize_number.c,v 1.12 2007/03/13 02:52:10 enami Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 #if defined(LIBC_SCCS) && !defined(lint)
42 __RCSID("$NetBSD: humanize_number.c,v 1.12 2007/03/13 02:52:10 enami Exp $");
43 #endif /* LIBC_SCCS and not lint */
44
45 #include "namespace.h"
46 #include <assert.h>
47 #include <inttypes.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <locale.h>
52 #include <util.h>
53
54 int
55 humanize_number(char *buf, size_t len, int64_t bytes,
56 const char *suffix, int scale, int flags)
57 {
58 const char *prefixes, *sep;
59 int b, i, r, maxscale, s1, s2, sign;
60 int64_t divisor, max;
61 size_t baselen;
62
63 _DIAGASSERT(buf != NULL);
64 _DIAGASSERT(suffix != NULL);
65 _DIAGASSERT(scale >= 0);
66
67 if (flags & HN_DIVISOR_1000) {
68 /* SI for decimal multiplies */
69 divisor = 1000;
70 if (flags & HN_B)
71 prefixes = "B\0k\0M\0G\0T\0P\0E";
72 else
73 prefixes = "\0\0k\0M\0G\0T\0P\0E";
74 } else {
75 /*
76 * binary multiplies
77 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
78 */
79 divisor = 1024;
80 if (flags & HN_B)
81 prefixes = "B\0K\0M\0G\0T\0P\0E";
82 else
83 prefixes = "\0\0K\0M\0G\0T\0P\0E";
84 }
85
86 #define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
87 maxscale = 7;
88
89 if (scale >= maxscale &&
90 (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
91 return (-1);
92
93 if (buf == NULL || suffix == NULL)
94 return (-1);
95
96 if (len > 0)
97 buf[0] = '\0';
98 if (bytes < 0) {
99 sign = -1;
100 bytes *= -100;
101 baselen = 3; /* sign, digit, prefix */
102 } else {
103 sign = 1;
104 bytes *= 100;
105 baselen = 2; /* digit, prefix */
106 }
107 if (flags & HN_NOSPACE)
108 sep = "";
109 else {
110 sep = " ";
111 baselen++;
112 }
113 baselen += strlen(suffix);
114
115 /* Check if enough room for `x y' + suffix + `\0' */
116 if (len < baselen + 1)
117 return (-1);
118
119 if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
120 /* See if there is additional columns can be used. */
121 for (max = 100, i = len - baselen; i-- > 0;)
122 max *= 10;
123
124 /*
125 * Divide the number until it fits the given column.
126 * If there will be an overflow by the rounding below,
127 * divide once more.
128 */
129 for (i = 0; bytes >= max - 50 && i < maxscale; i++)
130 bytes /= divisor;
131
132 if (scale & HN_GETSCALE)
133 return (i);
134 } else
135 for (i = 0; i < scale && i < maxscale; i++)
136 bytes /= divisor;
137
138 /* If a value <= 9.9 after rounding and ... */
139 if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
140 /* baselen + \0 + .N */
141 if (len < baselen + 1 + 2)
142 return (-1);
143 b = ((int)bytes + 5) / 10;
144 s1 = b / 10;
145 s2 = b % 10;
146 r = snprintf(buf, len, "%d%s%d%s%s%s",
147 sign * s1, localeconv()->decimal_point, s2,
148 sep, SCALE2PREFIX(i), suffix);
149 } else
150 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
151 sign * ((bytes + 50) / 100),
152 sep, SCALE2PREFIX(i), suffix);
153
154 return (r);
155 }
156