humanize_number.c revision 1.7 1 /* $NetBSD: humanize_number.c,v 1.7 2004/07/12 09:21:20 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 #ifndef __lint
42 __COPYRIGHT("@(#) Copyright (c) 2002\n\
43 The NetBSD Foundation, inc. All rights reserved.\n");
44 __RCSID("$NetBSD: humanize_number.c,v 1.7 2004/07/12 09:21:20 enami Exp $");
45 #endif /* !__lint */
46
47 #include <assert.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;
59 int i, r;
60 int64_t divisor, max, s1, s2, sign;
61 size_t baselen, suffixlen;
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 prefixes = " kMGTPE";
71 } else {
72 /*
73 * binary multiplies
74 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
75 */
76 divisor = 1024;
77 prefixes = " KMGTPE";
78 }
79
80 if ((size_t) scale >= strlen(prefixes) && scale != HN_AUTOSCALE &&
81 scale != HN_GETSCALE)
82 return (-1);
83
84 if (buf == NULL || suffix == NULL)
85 return (-1);
86
87 if (len > 0)
88 buf[0] = '\0';
89 if (bytes < 0) {
90 sign = -1;
91 bytes *= -100;
92 baselen = 4;
93 } else {
94 sign = 1;
95 bytes *= 100;
96 baselen = 3;
97 }
98
99 suffixlen = strlen(suffix);
100
101 /* check if enough room for `x y' + suffix + `\0' */
102 if (len < baselen + suffixlen + 1)
103 return (-1);
104
105 max = 100;
106 for (i = 0;
107 (size_t) i < len - suffixlen - baselen + ((flags & HN_NOSPACE) ?
108 1 : 0); i++)
109 max *= 10;
110
111 if ((scale & HN_AUTOSCALE) || (scale & HN_GETSCALE)) {
112 for (i = 0; bytes >= max && prefixes[i + 1]; i++)
113 bytes /= divisor;
114 } else {
115 for (i = 0; i < scale && prefixes[i + 1]; i++)
116 bytes /= divisor;
117 }
118
119 if (scale & HN_GETSCALE)
120 return (i);
121
122 if (bytes < 995 && flags & HN_DECIMAL) {
123 if (len < (baselen + 2 + ((flags & HN_NOSPACE) || (i == 0 &&
124 !(flags & HN_B)) ? 0 : 1)))
125 return (-1);
126 s1 = bytes / 100;
127 if ((s2 = (((bytes % 100) + 5) / 10)) == 10) {
128 s1++;
129 s2 = 0;
130 }
131 if (s1 < 10 && i == 0)
132 /* Don't ever use .0 for a number less than 10. */
133 r = snprintf(buf, len, "%lld%s%c%s",
134 /* LONGLONG */
135 (long long)(sign * s1),
136 (i == 0 && !(flags & HN_B)) || flags & HN_NOSPACE ?
137 "" : " ", (i == 0 && (flags & HN_B)) ? 'B' :
138 prefixes[i], suffix);
139 else
140 r = snprintf(buf, len, "%lld%s%lld%s%c%s",
141 /* LONGLONG */
142 (long long)(sign * s1),
143 localeconv()->decimal_point,
144 /* LONGLONG */
145 (long long)s2,
146 (i == 0 && !(flags & HN_B)) || flags & HN_NOSPACE ?
147 "" : " ", (i == 0 && (flags & HN_B)) ? 'B' :
148 prefixes[i], suffix);
149
150 } else
151 r = snprintf(buf, len, "%lld%s%c%s",
152 /* LONGLONG */
153 (long long)(sign * ((bytes + 50) / 100)),
154 i == 0 || flags & HN_NOSPACE ? "" : " ", (i == 0 &&
155 (flags & HN_B)) ? 'B' : prefixes[i], suffix);
156
157 return (r);
158 }
159