xo_humanize.h revision 302408
1/*	$NetBSD: humanize_number.c,v 1.8 2004/07/27 01:56:24 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
42#include <sys/types.h>
43#include <assert.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <locale.h>
48#include <stdint.h>
49#include <limits.h>
50#include <unistd.h>
51#include <stdbool.h>
52
53/* humanize_number(3) */
54#define HN_DECIMAL		0x01
55#define HN_NOSPACE		0x02
56#define HN_B			0x04
57#define HN_DIVISOR_1000		0x08
58
59#define HN_GETSCALE		0x10
60#define HN_AUTOSCALE		0x20
61
62static int
63xo_humanize_number (char *buf, size_t len, int64_t bytes,
64		    const char *suffix, int scale, int flags)
65{
66	const char *prefixes, *sep;
67	int	b, i, r, maxscale, s1, s2, sign;
68	int64_t divisor, max;
69	// We multiply bytes by 100 to deal with rounding, so we need something
70	// big enough to hold LLONG_MAX * 100. On 64-bit we can use 128-bit wide
71	// integers with __int128_t, but on 32-bit we have to use long double.
72#ifdef __LP64__
73	__int128_t scalable = (__int128_t)bytes;
74#else
75	long double scalable = (long double)bytes;
76#endif
77	size_t	baselen;
78
79	assert(buf != NULL);
80	assert(suffix != NULL);
81	assert(scale >= 0);
82
83	if (flags & HN_DIVISOR_1000) {
84		/* SI for decimal multiplies */
85		divisor = 1000;
86		if (flags & HN_B)
87			prefixes = "B\0k\0M\0G\0T\0P\0E";
88		else
89			prefixes = "\0\0k\0M\0G\0T\0P\0E";
90	} else {
91		/*
92		 * binary multiplies
93		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
94		 */
95		divisor = 1024;
96		if (flags & HN_B)
97			prefixes = "B\0K\0M\0G\0T\0P\0E";
98		else
99			prefixes = "\0\0K\0M\0G\0T\0P\0E";
100	}
101
102#define	SCALE2PREFIX(scale)	(&prefixes[(scale) << 1])
103	maxscale = 7;
104
105	if (scale >= maxscale &&
106	    (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
107		return (-1);
108
109	if (buf == NULL || suffix == NULL)
110		return (-1);
111
112	if (len > 0)
113		buf[0] = '\0';
114	if (bytes < 0) {
115		sign = -1;
116		scalable *= -100;
117		baselen = 3;		/* sign, digit, prefix */
118	} else {
119		sign = 1;
120		scalable *= 100;
121		baselen = 2;		/* digit, prefix */
122	}
123	if (flags & HN_NOSPACE)
124		sep = "";
125	else {
126		sep = " ";
127		baselen++;
128	}
129	baselen += strlen(suffix);
130
131	/* Check if enough room for `x y' + suffix + `\0' */
132	if (len < baselen + 1)
133		return (-1);
134
135	if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
136		/* See if there is additional columns can be used. */
137		for (max = 100, i = len - baselen; i-- > 0;)
138			max *= 10;
139
140		for (i = 0; scalable >= max && i < maxscale; i++)
141			scalable /= divisor;
142
143		if (scale & HN_GETSCALE)
144			return (i);
145	} else
146		for (i = 0; i < scale && i < maxscale; i++)
147			scalable /= divisor;
148
149	/* If a value <= 9.9 after rounding and ... */
150	if (scalable < 995 && i > 0 && flags & HN_DECIMAL) {
151		/* baselen + \0 + .N */
152		if (len < baselen + 1 + 2)
153			return (-1);
154		b = ((int)scalable + 5) / 10;
155		s1 = b / 10;
156		s2 = b % 10;
157		r = snprintf(buf, len, "%s%d%s%d%s%s%s",
158			((sign == -1) ? "-" : ""),
159		    s1, localeconv()->decimal_point, s2,
160		    sep, SCALE2PREFIX(i), suffix);
161	} else
162		r = snprintf(buf, len, "%s%lld%s%s%s",
163		    /* LONGLONG */
164			((sign == -1) ? "-" : ""),
165		    (long long)((scalable + 50) / 100),
166		    sep, SCALE2PREFIX(i), suffix);
167
168	return (r);
169}
170