xref: /dragonfly/lib/libutil/humanize_number.c (revision 70fef11b)
18e14c45fSSascha Wildner /*	$NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin Exp $	*/
2f41183adSMatthew Dillon 
3f41183adSMatthew Dillon /*
4f41183adSMatthew Dillon  * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5d316f7c9SJohn Marino  * Copyright 2013 John-Mark Gurney <jmg@FreeBSD.org>
6f41183adSMatthew Dillon  * All rights reserved.
7f41183adSMatthew Dillon  *
8f41183adSMatthew Dillon  * This code is derived from software contributed to The NetBSD Foundation
9f41183adSMatthew Dillon  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10f41183adSMatthew Dillon  * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
11f41183adSMatthew Dillon  *
12f41183adSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
13f41183adSMatthew Dillon  * modification, are permitted provided that the following conditions
14f41183adSMatthew Dillon  * are met:
15f41183adSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
16f41183adSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
17f41183adSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
18f41183adSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
19f41183adSMatthew Dillon  *    documentation and/or other materials provided with the distribution.
20f41183adSMatthew Dillon  *
21f41183adSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22f41183adSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23f41183adSMatthew Dillon  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24f41183adSMatthew Dillon  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25f41183adSMatthew Dillon  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26f41183adSMatthew Dillon  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27f41183adSMatthew Dillon  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28f41183adSMatthew Dillon  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29f41183adSMatthew Dillon  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30f41183adSMatthew Dillon  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31f41183adSMatthew Dillon  * POSSIBILITY OF SUCH DAMAGE.
32f41183adSMatthew Dillon  *
33d316f7c9SJohn Marino  * $FreeBSD: head/lib/libutil/humanize_number.c 256130 2013-10-07 22:22:57Z jmg $
34f41183adSMatthew Dillon  */
35f41183adSMatthew Dillon 
36f41183adSMatthew Dillon #include <sys/types.h>
37f41183adSMatthew Dillon #include <assert.h>
388e14c45fSSascha Wildner #include <inttypes.h>
39f41183adSMatthew Dillon #include <stdio.h>
40f41183adSMatthew Dillon #include <stdlib.h>
41f41183adSMatthew Dillon #include <string.h>
428e14c45fSSascha Wildner #include <locale.h>
43d316f7c9SJohn Marino #include <libutil.h>
44ca008f5eSChris Pressey 
45d316f7c9SJohn Marino static const int maxscale = 7;
46f41183adSMatthew Dillon 
47f41183adSMatthew Dillon int
humanize_number(char * buf,size_t len,int64_t quotient,const char * suffix,int scale,int flags)48d316f7c9SJohn Marino humanize_number(char *buf, size_t len, int64_t quotient,
49f41183adSMatthew Dillon     const char *suffix, int scale, int flags)
50f41183adSMatthew Dillon {
51f41183adSMatthew Dillon 	const char *prefixes, *sep;
52d316f7c9SJohn Marino 	int	i, r, remainder, s1, s2, sign;
53d316f7c9SJohn Marino 	int	divisordeccut;
54f41183adSMatthew Dillon 	int64_t	divisor, max;
55f41183adSMatthew Dillon 	size_t	baselen;
56f41183adSMatthew Dillon 
57d316f7c9SJohn Marino 	/* validate args */
58*70fef11bSAaron LI 	if (buf == NULL)
59d316f7c9SJohn Marino 		return (-1);
60d316f7c9SJohn Marino 	if (scale < 0)
61d316f7c9SJohn Marino 		return (-1);
62d316f7c9SJohn Marino 	else if (scale >= maxscale &&
63d316f7c9SJohn Marino 	    ((scale & ~(HN_AUTOSCALE|HN_GETSCALE)) != 0))
64d316f7c9SJohn Marino 		return (-1);
65d316f7c9SJohn Marino 	if ((flags & HN_DIVISOR_1000) && (flags & HN_IEC_PREFIXES))
66d316f7c9SJohn Marino 		return (-1);
67d316f7c9SJohn Marino 
6888b59fa5SAaron LI 	/* Since so many callers don't check -1, NUL terminate the buffer */
6988b59fa5SAaron LI 	if (len > 0)
7088b59fa5SAaron LI 		buf[0] = '\0';
7188b59fa5SAaron LI 
72*70fef11bSAaron LI 	if (suffix == NULL)
73*70fef11bSAaron LI 		suffix = "";
74*70fef11bSAaron LI 
75d316f7c9SJohn Marino 	/* setup parameters */
76d316f7c9SJohn Marino 	remainder = 0;
77d316f7c9SJohn Marino 
78d316f7c9SJohn Marino 	if (flags & HN_IEC_PREFIXES) {
79d316f7c9SJohn Marino 		baselen = 2;
80d316f7c9SJohn Marino 		/*
81d316f7c9SJohn Marino 		 * Use the prefixes for power of two recommended by
82d316f7c9SJohn Marino 		 * the International Electrotechnical Commission
83d316f7c9SJohn Marino 		 * (IEC) in IEC 80000-3 (i.e. Ki, Mi, Gi...).
84d316f7c9SJohn Marino 		 *
85d316f7c9SJohn Marino 		 * HN_IEC_PREFIXES implies a divisor of 1024 here
86d316f7c9SJohn Marino 		 * (use of HN_DIVISOR_1000 would have triggered
87d316f7c9SJohn Marino 		 * an assertion earlier).
88d316f7c9SJohn Marino 		 */
89d316f7c9SJohn Marino 		divisor = 1024;
90d316f7c9SJohn Marino 		divisordeccut = 973;	/* ceil(.95 * 1024) */
91d316f7c9SJohn Marino 		if (flags & HN_B)
92d316f7c9SJohn Marino 			prefixes = "B\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
93d316f7c9SJohn Marino 		else
94d316f7c9SJohn Marino 			prefixes = "\0\0\0Ki\0Mi\0Gi\0Ti\0Pi\0Ei";
95d316f7c9SJohn Marino 	} else {
96d316f7c9SJohn Marino 		baselen = 1;
97d316f7c9SJohn Marino 	if (flags & HN_DIVISOR_1000) {
98d316f7c9SJohn Marino 		divisor = 1000;
99d316f7c9SJohn Marino 			divisordeccut = 950;
100d316f7c9SJohn Marino 		if (flags & HN_B)
101d316f7c9SJohn Marino 				prefixes = "B\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
102d316f7c9SJohn Marino 		else
103d316f7c9SJohn Marino 				prefixes = "\0\0\0k\0\0M\0\0G\0\0T\0\0P\0\0E";
104d316f7c9SJohn Marino 	} else {
105d316f7c9SJohn Marino 		divisor = 1024;
106d316f7c9SJohn Marino 			divisordeccut = 973;	/* ceil(.95 * 1024) */
107d316f7c9SJohn Marino 		if (flags & HN_B)
108d316f7c9SJohn Marino 				prefixes = "B\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E";
109d316f7c9SJohn Marino 		else
110d316f7c9SJohn Marino 				prefixes = "\0\0\0K\0\0M\0\0G\0\0T\0\0P\0\0E";
111d316f7c9SJohn Marino 		}
112d316f7c9SJohn Marino 	}
113d316f7c9SJohn Marino 
114d316f7c9SJohn Marino #define	SCALE2PREFIX(scale)	(&prefixes[(scale) * 3])
115d316f7c9SJohn Marino 
116d316f7c9SJohn Marino 	if (quotient < 0) {
117f41183adSMatthew Dillon 		sign = -1;
118d316f7c9SJohn Marino 		quotient = -quotient;
119d316f7c9SJohn Marino 		baselen += 2;		/* sign, digit */
120f41183adSMatthew Dillon 	} else {
121f41183adSMatthew Dillon 		sign = 1;
122d316f7c9SJohn Marino 		baselen += 1;		/* digit */
123f41183adSMatthew Dillon 	}
124f41183adSMatthew Dillon 	if (flags & HN_NOSPACE)
125f41183adSMatthew Dillon 		sep = "";
126f41183adSMatthew Dillon 	else {
127f41183adSMatthew Dillon 		sep = " ";
128f41183adSMatthew Dillon 		baselen++;
129f41183adSMatthew Dillon 	}
130f41183adSMatthew Dillon 	baselen += strlen(suffix);
131f41183adSMatthew Dillon 
132f41183adSMatthew Dillon 	/* Check if enough room for `x y' + suffix + `\0' */
133f41183adSMatthew Dillon 	if (len < baselen + 1)
134f41183adSMatthew Dillon 		return (-1);
135f41183adSMatthew Dillon 
136f41183adSMatthew Dillon 	if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
137f41183adSMatthew Dillon 		/* See if there is additional columns can be used. */
138d316f7c9SJohn Marino 		for (max = 1, i = len - baselen; i-- > 0;)
139f41183adSMatthew Dillon 			max *= 10;
140f41183adSMatthew Dillon 
1418e14c45fSSascha Wildner 		/*
1428e14c45fSSascha Wildner 		 * Divide the number until it fits the given column.
1438e14c45fSSascha Wildner 		 * If there will be an overflow by the rounding below,
1448e14c45fSSascha Wildner 		 * divide once more.
1458e14c45fSSascha Wildner 		 */
146d316f7c9SJohn Marino 		for (i = 0;
147d316f7c9SJohn Marino 		    (quotient >= max || (quotient == max - 1 &&
148d316f7c9SJohn Marino 		    remainder >= divisordeccut)) && i < maxscale; i++) {
149d316f7c9SJohn Marino 			remainder = quotient % divisor;
150d316f7c9SJohn Marino 			quotient /= divisor;
151d316f7c9SJohn Marino 		}
152f41183adSMatthew Dillon 
153f41183adSMatthew Dillon 		if (scale & HN_GETSCALE)
154f41183adSMatthew Dillon 			return (i);
155d316f7c9SJohn Marino 	} else {
156d316f7c9SJohn Marino 		for (i = 0; i < scale && i < maxscale; i++) {
157d316f7c9SJohn Marino 			remainder = quotient % divisor;
158d316f7c9SJohn Marino 			quotient /= divisor;
159d316f7c9SJohn Marino 		}
160d316f7c9SJohn Marino 	}
161f41183adSMatthew Dillon 
162d316f7c9SJohn Marino 	/*
163a4b1d485SMatthew Dillon 	 * Generate base output
164d316f7c9SJohn Marino 	 */
165a4b1d485SMatthew Dillon 	r = snprintf(buf, len, "%" PRId64 "%s%s%s",
166a4b1d485SMatthew Dillon 	    sign * (quotient + (remainder + divisor / 2) / divisor),
167a4b1d485SMatthew Dillon 	    sep, SCALE2PREFIX(i), suffix);
168a4b1d485SMatthew Dillon 
169a4b1d485SMatthew Dillon 	if ((flags & HN_FRACTIONAL) && (u_int)r + 3 <= len && i) {
170a4b1d485SMatthew Dillon 		/*
171a4b1d485SMatthew Dillon 		 * If FRACTIONAL is specified output up to two fractional
172a4b1d485SMatthew Dillon 		 * digits, unless the value was not divided out.
173a4b1d485SMatthew Dillon 		 */
174a4b1d485SMatthew Dillon 		int64_t frac;
175a4b1d485SMatthew Dillon 		int n;
176a4b1d485SMatthew Dillon 
177a4b1d485SMatthew Dillon 		n = (int)len - r - 2;
178a4b1d485SMatthew Dillon 		frac = 1;
179a4b1d485SMatthew Dillon 		if (n > 2)	/* max 2 fractional digits */
180a4b1d485SMatthew Dillon 			n = 2;
181a4b1d485SMatthew Dillon 
182a4b1d485SMatthew Dillon 		while (n) {
183a4b1d485SMatthew Dillon 			frac = frac * 10;
184a4b1d485SMatthew Dillon 			--n;
185a4b1d485SMatthew Dillon 		}
186a4b1d485SMatthew Dillon 		s1 = (int)quotient + ((remainder * frac + divisor / 2) /
187a4b1d485SMatthew Dillon 		    divisor / frac);
188a4b1d485SMatthew Dillon 		s2 = ((remainder * frac + divisor / 2) / divisor) % frac;
189a4b1d485SMatthew Dillon 		r = snprintf(buf, len, "%d%s%d%s%s%s",
190a4b1d485SMatthew Dillon 		    sign * s1, localeconv()->decimal_point, s2,
191a4b1d485SMatthew Dillon 		    sep, SCALE2PREFIX(i), suffix);
192a4b1d485SMatthew Dillon 	} else if ((flags & HN_DECIMAL) && (u_int)r + 3 <= len &&
193a4b1d485SMatthew Dillon 		(((quotient == 9 && remainder < divisordeccut) ||
194a4b1d485SMatthew Dillon 		    quotient < 9) && i > 0)) {
195a4b1d485SMatthew Dillon 		/*
196a4b1d485SMatthew Dillon 		 * If DECIMAL is specified and the value is <= 9.9 after
197a4b1d485SMatthew Dillon 		 * rounding, and it fits, add one fractional digit.
198a4b1d485SMatthew Dillon 		 */
199d316f7c9SJohn Marino 		s1 = (int)quotient + ((remainder * 10 + divisor / 2) /
200d316f7c9SJohn Marino 		    divisor / 10);
201d316f7c9SJohn Marino 		s2 = ((remainder * 10 + divisor / 2) / divisor) % 10;
202f41183adSMatthew Dillon 		r = snprintf(buf, len, "%d%s%d%s%s%s",
203f41183adSMatthew Dillon 		    sign * s1, localeconv()->decimal_point, s2,
204f41183adSMatthew Dillon 		    sep, SCALE2PREFIX(i), suffix);
205a4b1d485SMatthew Dillon 	}
206f41183adSMatthew Dillon 	return (r);
207f41183adSMatthew Dillon }
208