xref: /freebsd/lib/libutil/humanize_number.c (revision 6f9c8e5b074419423648ffb89b83fd2f257e90b7)
1 /*	$NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #include <assert.h>
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <locale.h>
43 #include <libutil.h>
44 
45 static const int maxscale = 7;
46 
47 int
48 humanize_number(char *buf, size_t len, int64_t quotient,
49     const char *suffix, int scale, int flags)
50 {
51 	const char *prefixes, *sep;
52 	int	i, r, remainder, s1, s2, sign;
53 	int64_t	divisor, max;
54 	size_t	baselen;
55 
56 	assert(buf != NULL);
57 	assert(suffix != NULL);
58 	assert(scale >= 0);
59 	assert(scale < maxscale || (((scale & (HN_AUTOSCALE | HN_GETSCALE)) != 0)));
60 	assert(!((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES)));
61 
62 	remainder = 0;
63 
64 	if (flags & HN_IEC_PREFIXES) {
65 		baselen = 2;
66 		/*
67 		 * Use the prefixes for power of two recommended by
68 		 * the International Electrotechnical Commission
69 		 * (IEC) in IEC 80000-3 (i.e. Ki, Mi, Gi...).
70 		 *
71 		 * HN_IEC_PREFIXES implies a divisor of 1024 here
72 		 * (use of HN_DIVISOR_1000 would have triggered
73 		 * an assertion earlier).
74 		 */
75 		divisor = 1024;
76 		if (flags & HN_B)
77 			prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
78 		else
79 			prefixes = "\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
80 	} else {
81 		baselen = 1;
82 		if (flags & HN_DIVISOR_1000)
83 			divisor = 1000;
84 		else
85 			divisor = 1024;
86 
87 		if (flags & HN_B)
88 			prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
89 		else
90 			prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
91 	}
92 
93 #define	SCALE2PREFIX(scale)	(&prefixes[(scale) * 3])
94 
95 	if (scale < 0 || (scale >= maxscale &&
96 	    (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0))
97 		return (-1);
98 
99 	if (buf == NULL || suffix == NULL)
100 		return (-1);
101 
102 	if (len > 0)
103 		buf[0] = '\0';
104 	if (quotient < 0) {
105 		sign = -1;
106 		quotient = -quotient;
107 		baselen += 2;		/* sign, digit */
108 	} else {
109 		sign = 1;
110 		baselen += 1;		/* digit */
111 	}
112 	if (flags & HN_NOSPACE)
113 		sep = "";
114 	else {
115 		sep = " ";
116 		baselen++;
117 	}
118 	baselen += strlen(suffix);
119 
120 	/* Check if enough room for `x y' + suffix + `\0' */
121 	if (len < baselen + 1)
122 		return (-1);
123 
124 	if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
125 		/* See if there is additional columns can be used. */
126 		for (max = 1, i = len - baselen; i-- > 0;)
127 			max *= 10;
128 
129 		/*
130 		 * Divide the number until it fits the given column.
131 		 * If there will be an overflow by the rounding below,
132 		 * divide once more.
133 		 */
134 		for (i = 0;
135 		    (quotient >= max || (quotient == max - 1 && remainder >= 950)) &&
136 		    i < maxscale; i++) {
137 			remainder = quotient % divisor;
138 			quotient /= divisor;
139 		}
140 
141 		if (scale & HN_GETSCALE)
142 			return (i);
143 	} else {
144 		for (i = 0; i < scale && i < maxscale; i++) {
145 			remainder = quotient % divisor;
146 			quotient /= divisor;
147 		}
148 	}
149 
150 	/* If a value <= 9.9 after rounding and ... */
151 	if (quotient <= 9 && remainder < 950 && i > 0 && flags & HN_DECIMAL) {
152 		/* baselen + \0 + .N */
153 		if (len < baselen + 1 + 2)
154 			return (-1);
155 		s1 = (int)quotient + ((remainder + 50) / 1000);
156 		s2 = ((remainder + 50) / 100) % 10;
157 		r = snprintf(buf, len, "%d%s%d%s%s%s",
158 		    sign * s1, localeconv()->decimal_point, s2,
159 		    sep, SCALE2PREFIX(i), suffix);
160 	} else
161 		r = snprintf(buf, len, "%" PRId64 "%s%s%s",
162 		    sign * (quotient + (remainder + 50) / 1000),
163 		    sep, SCALE2PREFIX(i), suffix);
164 
165 	return (r);
166 }
167 
168