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