xref: /freebsd/lib/libutil/humanize_number.c (revision 0f502d1c4e60dc883af7dfc7833956d62db9ff69)
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 int
46 humanize_number(char *buf, size_t len, int64_t quotient,
47     const char *suffix, int scale, int flags)
48 {
49 	const char *prefixes, *sep;
50 	int	i, r, remainder, maxscale, s1, s2, sign;
51 	int64_t	divisor, max;
52 	size_t	baselen;
53 
54 	assert(buf != NULL);
55 	assert(suffix != NULL);
56 	assert(scale >= 0);
57 
58 	remainder = 0;
59 
60 	if (flags & HN_DIVISOR_1000) {
61 		/* SI for decimal multiplies */
62 		divisor = 1000;
63 		if (flags & HN_B)
64 			prefixes = "B\0k\0M\0G\0T\0P\0E";
65 		else
66 			prefixes = "\0\0k\0M\0G\0T\0P\0E";
67 	} else {
68 		/*
69 		 * binary multiplies
70 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
71 		 */
72 		divisor = 1024;
73 		if (flags & HN_B)
74 			prefixes = "B\0K\0M\0G\0T\0P\0E";
75 		else
76 			prefixes = "\0\0K\0M\0G\0T\0P\0E";
77 	}
78 
79 #define	SCALE2PREFIX(scale)	(&prefixes[(scale) << 1])
80 	maxscale = 7;
81 
82 	if (scale >= maxscale &&
83 	    (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
84 		return (-1);
85 
86 	if (buf == NULL || suffix == NULL)
87 		return (-1);
88 
89 	if (len > 0)
90 		buf[0] = '\0';
91 	if (quotient < 0) {
92 		sign = -1;
93 		quotient = -quotient;
94 		baselen = 3;		/* sign, digit, prefix */
95 	} else {
96 		sign = 1;
97 		baselen = 2;		/* digit, prefix */
98 	}
99 	if (flags & HN_NOSPACE)
100 		sep = "";
101 	else {
102 		sep = " ";
103 		baselen++;
104 	}
105 	baselen += strlen(suffix);
106 
107 	/* Check if enough room for `x y' + suffix + `\0' */
108 	if (len < baselen + 1)
109 		return (-1);
110 
111 	if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
112 		/* See if there is additional columns can be used. */
113 		for (max = 1, i = len - baselen; i-- > 0;)
114 			max *= 10;
115 
116 		/*
117 		 * Divide the number until it fits the given column.
118 		 * If there will be an overflow by the rounding below,
119 		 * divide once more.
120 		 */
121 		for (i = 0;
122 		    (quotient >= max || (quotient == max - 1 && remainder >= 950)) &&
123 		    i < maxscale; i++) {
124 			remainder = quotient % divisor;
125 			quotient /= divisor;
126 		}
127 
128 		if (scale & HN_GETSCALE)
129 			return (i);
130 	} else {
131 		for (i = 0; i < scale && i < maxscale; i++) {
132 			remainder = quotient % divisor;
133 			quotient /= divisor;
134 		}
135 	}
136 
137 	/* If a value <= 9.9 after rounding and ... */
138 	if (quotient <= 9 && remainder < 950 && i > 0 && flags & HN_DECIMAL) {
139 		/* baselen + \0 + .N */
140 		if (len < baselen + 1 + 2)
141 			return (-1);
142 		s1 = (int)quotient + ((remainder + 50) / 1000);
143 		s2 = ((remainder + 50) / 100) % 10;
144 		r = snprintf(buf, len, "%d%s%d%s%s%s",
145 		    sign * s1, localeconv()->decimal_point, s2,
146 		    sep, SCALE2PREFIX(i), suffix);
147 	} else
148 		r = snprintf(buf, len, "%" PRId64 "%s%s%s",
149 		    sign * (quotient + (remainder + 50) / 1000),
150 		    sep, SCALE2PREFIX(i), suffix);
151 
152 	return (r);
153 }
154 
155