Merge branch 'vendor/TEXINFO'
[dragonfly.git] / lib / libutil / humanize_number.c
1 /*      $NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin Exp $      */
2 /* $DragonFly: src/lib/libutil/humanize_number.c,v 1.3 2008/08/04 19:09:26 swildner Exp $ */
3
4 /*
5  * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/lib/libutil/humanize_number.c,v 1.3 2008/03/08 21:55:59 antoine Exp $
34  */
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
44 #include "libutil.h"
45
46 int
47 humanize_number(char *buf, size_t len, int64_t bytes,
48     const char *suffix, int scale, int flags)
49 {
50         const char *prefixes, *sep;
51         int     b, i, r, maxscale, s1, s2, sign;
52         int64_t divisor, max;
53         size_t  baselen;
54
55         assert(buf != NULL);
56         assert(suffix != NULL);
57         assert(scale >= 0);
58
59         if (flags & HN_DIVISOR_1000) {
60                 /* SI for decimal multiplies */
61                 divisor = 1000;
62                 if (flags & HN_B)
63                         prefixes = "B\0k\0M\0G\0T\0P\0E";
64                 else
65                         prefixes = "\0\0k\0M\0G\0T\0P\0E";
66         } else {
67                 /*
68                  * binary multiplies
69                  * XXX IEC 60027-2 recommends Ki, Mi, Gi...
70                  */
71                 divisor = 1024;
72                 if (flags & HN_B)
73                         prefixes = "B\0K\0M\0G\0T\0P\0E";
74                 else
75                         prefixes = "\0\0K\0M\0G\0T\0P\0E";
76         }
77
78 #define SCALE2PREFIX(scale)     (&prefixes[(scale) << 1])
79         maxscale = 7;
80
81         if (scale >= maxscale &&
82             (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
83                 return (-1);
84
85         if (buf == NULL || suffix == NULL)
86                 return (-1);
87
88         if (len > 0)
89                 buf[0] = '\0';
90         if (bytes < 0) {
91                 sign = -1;
92                 bytes *= -100;
93                 baselen = 3;            /* sign, digit, prefix */
94         } else {
95                 sign = 1;
96                 bytes *= 100;
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 = 100, 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; bytes >= max - 50 && i < maxscale; i++)
122                         bytes /= divisor;
123
124                 if (scale & HN_GETSCALE)
125                         return (i);
126         } else
127                 for (i = 0; i < scale && i < maxscale; i++)
128                         bytes /= divisor;
129
130         /* If a value <= 9.9 after rounding and ... */
131         if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
132                 /* baselen + \0 + .N */
133                 if (len < baselen + 1 + 2)
134                         return (-1);
135                 b = ((int)bytes + 5) / 10;
136                 s1 = b / 10;
137                 s2 = b % 10;
138                 r = snprintf(buf, len, "%d%s%d%s%s%s",
139                     sign * s1, localeconv()->decimal_point, s2,
140                     sep, SCALE2PREFIX(i), suffix);
141         } else
142                 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
143                     sign * ((bytes + 50) / 100),
144                     sep, SCALE2PREFIX(i), suffix);
145
146         return (r);
147 }