humanize_number.c revision 1.14.8.2 1 1.14.8.2 martin /* $NetBSD: humanize_number.c,v 1.14.8.2 2008/04/28 20:23:00 martin Exp $ */
2 1.14.8.2 martin
3 1.14.8.2 martin /*
4 1.14.8.2 martin * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 1.14.8.2 martin * All rights reserved.
6 1.14.8.2 martin *
7 1.14.8.2 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.14.8.2 martin * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.14.8.2 martin * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
10 1.14.8.2 martin *
11 1.14.8.2 martin * Redistribution and use in source and binary forms, with or without
12 1.14.8.2 martin * modification, are permitted provided that the following conditions
13 1.14.8.2 martin * are met:
14 1.14.8.2 martin * 1. Redistributions of source code must retain the above copyright
15 1.14.8.2 martin * notice, this list of conditions and the following disclaimer.
16 1.14.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
17 1.14.8.2 martin * notice, this list of conditions and the following disclaimer in the
18 1.14.8.2 martin * documentation and/or other materials provided with the distribution.
19 1.14.8.2 martin *
20 1.14.8.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.14.8.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.14.8.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.14.8.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.14.8.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.14.8.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.14.8.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.14.8.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.14.8.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.14.8.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.14.8.2 martin * POSSIBILITY OF SUCH DAMAGE.
31 1.14.8.2 martin */
32 1.14.8.2 martin
33 1.14.8.2 martin #include <sys/cdefs.h>
34 1.14.8.2 martin #if defined(LIBC_SCCS) && !defined(lint)
35 1.14.8.2 martin __RCSID("$NetBSD: humanize_number.c,v 1.14.8.2 2008/04/28 20:23:00 martin Exp $");
36 1.14.8.2 martin #endif /* LIBC_SCCS and not lint */
37 1.14.8.2 martin
38 1.14.8.2 martin #include "namespace.h"
39 1.14.8.2 martin #include <assert.h>
40 1.14.8.2 martin #include <inttypes.h>
41 1.14.8.2 martin #include <stdio.h>
42 1.14.8.2 martin #include <stdlib.h>
43 1.14.8.2 martin #include <string.h>
44 1.14.8.2 martin #include <locale.h>
45 1.14.8.2 martin
46 1.14.8.2 martin int
47 1.14.8.2 martin humanize_number(char *buf, size_t len, int64_t bytes,
48 1.14.8.2 martin const char *suffix, int scale, int flags)
49 1.14.8.2 martin {
50 1.14.8.2 martin const char *prefixes, *sep;
51 1.14.8.2 martin int b, i, r, maxscale, s1, s2, sign;
52 1.14.8.2 martin int64_t divisor, max;
53 1.14.8.2 martin size_t baselen;
54 1.14.8.2 martin
55 1.14.8.2 martin _DIAGASSERT(buf != NULL);
56 1.14.8.2 martin _DIAGASSERT(suffix != NULL);
57 1.14.8.2 martin _DIAGASSERT(scale >= 0);
58 1.14.8.2 martin
59 1.14.8.2 martin if (flags & HN_DIVISOR_1000) {
60 1.14.8.2 martin /* SI for decimal multiplies */
61 1.14.8.2 martin divisor = 1000;
62 1.14.8.2 martin if (flags & HN_B)
63 1.14.8.2 martin prefixes = "B\0k\0M\0G\0T\0P\0E";
64 1.14.8.2 martin else
65 1.14.8.2 martin prefixes = "\0\0k\0M\0G\0T\0P\0E";
66 1.14.8.2 martin } else {
67 1.14.8.2 martin /*
68 1.14.8.2 martin * binary multiplies
69 1.14.8.2 martin * XXX IEC 60027-2 recommends Ki, Mi, Gi...
70 1.14.8.2 martin */
71 1.14.8.2 martin divisor = 1024;
72 1.14.8.2 martin if (flags & HN_B)
73 1.14.8.2 martin prefixes = "B\0K\0M\0G\0T\0P\0E";
74 1.14.8.2 martin else
75 1.14.8.2 martin prefixes = "\0\0K\0M\0G\0T\0P\0E";
76 1.14.8.2 martin }
77 1.14.8.2 martin
78 1.14.8.2 martin #define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
79 1.14.8.2 martin maxscale = 7;
80 1.14.8.2 martin
81 1.14.8.2 martin if (scale >= maxscale &&
82 1.14.8.2 martin (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
83 1.14.8.2 martin return (-1);
84 1.14.8.2 martin
85 1.14.8.2 martin if (buf == NULL || suffix == NULL)
86 1.14.8.2 martin return (-1);
87 1.14.8.2 martin
88 1.14.8.2 martin if (len > 0)
89 1.14.8.2 martin buf[0] = '\0';
90 1.14.8.2 martin if (bytes < 0) {
91 1.14.8.2 martin sign = -1;
92 1.14.8.2 martin bytes *= -100;
93 1.14.8.2 martin baselen = 3; /* sign, digit, prefix */
94 1.14.8.2 martin } else {
95 1.14.8.2 martin sign = 1;
96 1.14.8.2 martin bytes *= 100;
97 1.14.8.2 martin baselen = 2; /* digit, prefix */
98 1.14.8.2 martin }
99 1.14.8.2 martin if (flags & HN_NOSPACE)
100 1.14.8.2 martin sep = "";
101 1.14.8.2 martin else {
102 1.14.8.2 martin sep = " ";
103 1.14.8.2 martin baselen++;
104 1.14.8.2 martin }
105 1.14.8.2 martin baselen += strlen(suffix);
106 1.14.8.2 martin
107 1.14.8.2 martin /* Check if enough room for `x y' + suffix + `\0' */
108 1.14.8.2 martin if (len < baselen + 1)
109 1.14.8.2 martin return (-1);
110 1.14.8.2 martin
111 1.14.8.2 martin if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
112 1.14.8.2 martin /* See if there is additional columns can be used. */
113 1.14.8.2 martin for (max = 100, i = len - baselen; i-- > 0;)
114 1.14.8.2 martin max *= 10;
115 1.14.8.2 martin
116 1.14.8.2 martin /*
117 1.14.8.2 martin * Divide the number until it fits the given column.
118 1.14.8.2 martin * If there will be an overflow by the rounding below,
119 1.14.8.2 martin * divide once more.
120 1.14.8.2 martin */
121 1.14.8.2 martin for (i = 0; bytes >= max - 50 && i < maxscale; i++)
122 1.14.8.2 martin bytes /= divisor;
123 1.14.8.2 martin
124 1.14.8.2 martin if (scale & HN_GETSCALE)
125 1.14.8.2 martin return (i);
126 1.14.8.2 martin } else
127 1.14.8.2 martin for (i = 0; i < scale && i < maxscale; i++)
128 1.14.8.2 martin bytes /= divisor;
129 1.14.8.2 martin
130 1.14.8.2 martin /* If a value <= 9.9 after rounding and ... */
131 1.14.8.2 martin if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
132 1.14.8.2 martin /* baselen + \0 + .N */
133 1.14.8.2 martin if (len < baselen + 1 + 2)
134 1.14.8.2 martin return (-1);
135 1.14.8.2 martin b = ((int)bytes + 5) / 10;
136 1.14.8.2 martin s1 = b / 10;
137 1.14.8.2 martin s2 = b % 10;
138 1.14.8.2 martin r = snprintf(buf, len, "%d%s%d%s%s%s",
139 1.14.8.2 martin sign * s1, localeconv()->decimal_point, s2,
140 1.14.8.2 martin sep, SCALE2PREFIX(i), suffix);
141 1.14.8.2 martin } else
142 1.14.8.2 martin r = snprintf(buf, len, "%" PRId64 "%s%s%s",
143 1.14.8.2 martin sign * ((bytes + 50) / 100),
144 1.14.8.2 martin sep, SCALE2PREFIX(i), suffix);
145 1.14.8.2 martin
146 1.14.8.2 martin return (r);
147 1.14.8.2 martin }
148