1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/**
* slibc — Yet another C library
* Copyright © 2015 Mattias Andrée (maandree@member.fsf.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <slibc-human.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloca.h>
#include <errno.h>
#define SPRINTF(...) do if (m = sprintf(__VA_ARGS__), m < 0) goto fail; while (0)
#define SNPRINTF(...) do if (m = snprintf(__VA_ARGS__), m < 0) goto fail; while (0)
#define ADD_PREFIX(prefix, buffer, n) \
do \
if (prefix == 0) \
buffer[n++] = 'B'; \
else \
{ \
buffer[n++] = prefixes[prefix]; \
if (mode & HUMANSIZE_IEC_EXPLICIT) buffer[n++] = 'i'; \
if (!(mode & HUMANSIZE_PREFIX_ONLY)) buffer[n++] = 'B'; \
} \
while (0)
/**
* Convert a file size of file offset from machine representation to human representation.
* The representation is exact.
*
* @param buffer A buffer than shall be used if it is sufficiently large.
* @param bufsize The allocation size of `buffer`.
* Must be 0 if and only if `buffer` is `NULL`.
* @param mode Representation style, 0 for default.
* @param detail See documentation for the select value on `mode`.
* @param intraspacing Spacing between values and units. `NULL` or empty for none.
* This value should depend on language and context. For English
* this value should be "" or "-", but in for example Swedish it
* should always be " ". Hence this value is a string rather than
* a booleanic integer.
* @param interspacing Spacing between value–unit-pairs. `NULL` for default (" ").
* This value should depend on language and context.
* @param prefixes Prefix characters (including prefixless.)
* @param values Values for prefixes.
* @param buf Create intermediate write buffer.
* @return Human representation of the file size/offset, `NULL` on error.
* On success, the caller is responsible for deallocating the
* returned pointer, if and only if it is not `buffer`.
*
* @throws ENOMEM The process cannot allocate more memory.
*/
static char* humansize_exact(char* buffer, size_t bufsize, enum humansize_mode mode, int detail,
const char* restrict intraspacing, const char* restrict interspacing, size_t words,
const char* restrict prefixes, const size_t* restrict values, char* restrict buf)
{
size_t i, n = 0;
void* new = NULL;
int m, saved_errno;
if (detail == 0)
detail = 999;
for (i = words; (i-- > 0) && detail--;)
{
/* Check non-zero only word. */
if (!(values[i] || (!i && !n)))
continue;
/* Add interspacing. */
if (i != words)
{
memcpy(buffer + n, interspacing, strlen(interspacing) * sizeof(char));
n += strlen(interspacing);
}
/* Construct word (and intraspacing). */
SPRINTF(buf, "%zu%s", values[i], intraspacing);
ADD_PREFIX(i, buf, m);
/* Ensure the buffer is large enougth. */
if (n + (size_t)m > bufsize / sizeof(char))
{
bufsize = 7 * (size_t)detail + strlen(interspacing) * ((size_t)detail - 1) + 1;
new = malloc(bufsize *= sizeof(char));
if (new == NULL)
goto fail;
memcpy(new, buffer, n * sizeof(char));
buffer = new;
}
/* Append word. */
memcpy(buffer + n, buf, (size_t)m * sizeof(char));
n += (size_t)m;
}
return buffer[n] = 0, buffer;
fail:
saved_errno = errno;
free(new);
return errno = saved_errno, NULL;
}
/**
* Convert a file size of file offset from machine representation to human representation.
* The representation is rounded.
*
* @param buffer A buffer than shall be used if it is sufficiently large.
* @param bufsize The allocation size of `buffer`.
* Must be 0 if and only if `buffer` is `NULL`.
* @param mode Representation style, 0 for default.
* @param detail See documentation for the select value on `mode`.
* @param point The symbol to use for decimal points. `NULL` or empty for default.
* @param intraspacing Spacing between values and units. `NULL` or empty for none.
* This value should depend on language and context. For English
* this value should be "" or "-", but in for example Swedish it
* should always be " ". Hence this value is a string rather than
* a booleanic integer.
* @param interspacing Spacing between value–unit-pairs. `NULL` for default (" ").
* This value should depend on language and context.
* @param prefixes Prefix characters (including prefixless.)
* @param values Values for prefixes.
* @param buf Create intermediate write buffer.
* @param div K-multiple (1000 for SI, 1024 for IEC.)
* @return Human representation of the file size/offset, `NULL` on error.
* On success, the caller is responsible for deallocating the
* returned pointer, if and only if it is not `buffer`.
*
* @throws ENOMEM The process cannot allocate more memory.
*/
static char* humansize_round(char* buffer, size_t bufsize, enum humansize_mode mode, int detail,
const char* restrict point, const char* restrict intraspacing,
const char* restrict interspacing, size_t words, const char* restrict prefixes,
const size_t* restrict values, char* restrict buf, size_t div)
{
double total = 0, dividend = 1;
size_t prefix = words - 1, i, n, det;
char* p;
void* new = NULL;
char c;
int m, saved_errno;
/* Get single-unit value. */
while (words--)
total += ((double)values[words]) / dividend, dividend *= (double)div;
/* Ensure the buffer is large enougth. */
SNPRINTF(NULL, 0, "%.*lf%zn", (detail < 0 ? 0 : detail), (double)total, (ssize_t*)&n);
if (n + strlen(point) + 3 - (detail < 0 ? 0 : 1) > bufsize / sizeof(char))
{
bufsize = n + strlen(point) + 3 - (detail < 0 ? 0 : 1);
new = malloc(bufsize *= sizeof(char));
if (new == NULL)
goto fail;
buffer = new;
}
/* Construct string. */
SPRINTF(buffer, "%.*lf", (detail < 0 ? 0 : detail), (double)total);
/* Replace decimal point sign. */
if ((p = strchr(buffer, '.')))
{
if (detail <= 0)
n = (size_t)(p - buffer);
else
{
memmove(p + strlen(point), p + 1, (n - 1 - (size_t)(p - buffer)) * sizeof(char));
memcpy(p, point, strlen(point) * sizeof(char));
}
}
/* Round value if detail < 0. */
if ((detail < 0) && (n > 1))
{
det = (size_t)-detail;
if (det >= n)
det = n - 1;
c = buffer[n + (size_t)-detail];
for (i = n + (size_t)-detail; i < n; i++)
buffer[i] = '0';
if (c >= '5')
{
buffer[n] = 0;
i = (size_t)atoll(buffer);
div = 10;
while (det--)
div *= 10;
i += div;
SPRINTF(buffer, "%zu%zn", i, (ssize_t*)&n);
}
}
/* Add prefix (and intraspacing). */
if (*intraspacing)
{
memcpy(buffer + n, intraspacing, strlen(intraspacing) * sizeof(char));
n += strlen(intraspacing);
}
ADD_PREFIX(prefix, buffer, n);
return buffer[n] = 0, buffer;
fail:
saved_errno = errno;
free(new);
return errno = saved_errno, NULL;
}
/**
* Convert a file size of file offset from machine representation to human representation.
*
* @param buffer A buffer than shall be used if it is sufficiently large.
* @param bufsize The allocation size of `buffer`.
* Must be 0 if and only if `buffer` is `NULL`.
* @param size The value to convert.
* @param mode Representation style, 0 for default.
* @param detail See documentation for the select value on `mode`.
* @param point The symbol to use for decimal points. `NULL` or empty for default.
* @param intraspacing Spacing between values and units. `NULL` or empty for none.
* This value should depend on language and context. For English
* this value should be "" or "-", but in for example Swedish it
* should always be " ". Hence this value is a string rather than
* a booleanic integer.
* @param interspacing Spacing between value–unit-pairs. `NULL` for default (" ").
* This value should depend on language and context.
* @return Human representation of the file size/offset, `NULL` on error.
* On success, the caller is responsible for deallocating the
* returned pointer, if and only if it is not `buffer`.
*
* @throws EINVAL If `mode` is invalid.
* @throws ENOMEM The process cannot allocate more memory.
*
* @since Always.
*/
char* humansize(char* buffer, size_t bufsize, size_t size, enum humansize_mode mode, int detail,
const char* restrict point, const char* restrict intraspacing, const char* restrict interspacing)
{
#if (__LONG_LONG_BIT > 90) && (((__LONG_LONG_BIT - 90) * 3 + 7) / 8 + 3 > 7)
# define BUFFER_SIZE (((__LONG_LONG_BIT - 90) * 3 + 7) / 8 + 3)
#else
# define BUFFER_SIZE 7
#endif
char prefixes[] = { '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
size_t values[sizeof(prefixes) / sizeof(*prefixes)] = { 0 };
size_t div, i, words = 0;
char* buf;
/* Default parameters. */
if (intraspacing == NULL) intraspacing = "";
if (interspacing == NULL) interspacing = " ";
if (!point || !*point) point = ".";
/* Create intermediate write buffer. */
buf = alloca((BUFFER_SIZE + strlen(intraspacing)) * sizeof(char));
/* Get K-multiple and case of the kilo-prefix. */
switch (mode & 7)
{
case 0:
case HUMANSIZE_SI:
div = 1000;
prefixes[1] = 'k';
break;
case HUMANSIZE_IEC:
case HUMANSIZE_IEC_EXPLICIT:
div = 1024;
break;
default:
goto invalid;
}
/* Get value on prefixes. */
for (i = 0; size && (i < sizeof(values) / sizeof(*values)); i++)
values[i] = size % div, size /= div, words++;
words = words ? words : 1;
/* Construct string. */
switch (mode & 48)
{
case HUMANSIZE_EXACT:
if (detail < 0)
goto invalid;
return humansize_exact(buffer, bufsize, mode, detail, intraspacing,
interspacing, words, prefixes, values, buf);
case 0:
case HUMANSIZE_ROUND:
return humansize_round(buffer, bufsize, mode, detail, point, intraspacing,
interspacing, words, prefixes, values, buf, div);
default:
goto invalid;
}
invalid:
return errno = EINVAL, NULL;
}
|