2021-12-13 14:36:36 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2022-05-24 19:01:51 -04:00
|
|
|
#include <kernaux/assert.h>
|
2022-06-07 01:19:14 -04:00
|
|
|
#include <kernaux/libc.h>
|
2022-01-19 05:38:29 -05:00
|
|
|
#include <kernaux/ntoa.h>
|
2021-12-14 15:44:09 -05:00
|
|
|
#include <kernaux/units.h>
|
2021-12-13 14:36:36 -05:00
|
|
|
|
2021-12-13 14:59:00 -05:00
|
|
|
#define TMP_BUFFER_SIZE (64)
|
|
|
|
|
2021-12-13 14:36:36 -05:00
|
|
|
bool kernaux_units_human_raw(
|
2022-02-09 16:30:13 -05:00
|
|
|
const uint64_t value,
|
2021-12-13 14:36:36 -05:00
|
|
|
const enum KernAux_Unit unit,
|
2021-12-13 14:59:00 -05:00
|
|
|
char *const buffer,
|
2021-12-13 14:36:36 -05:00
|
|
|
const size_t buffer_size
|
|
|
|
) {
|
2022-05-24 19:01:51 -04:00
|
|
|
KERNAUX_NOTNULL_RETVAL(buffer, false);
|
|
|
|
KERNAUX_ASSERT_RETVAL(buffer_size > 0, false);
|
|
|
|
|
2021-12-13 14:59:00 -05:00
|
|
|
char tmp_buffer[TMP_BUFFER_SIZE];
|
|
|
|
char *tmp = tmp_buffer;
|
|
|
|
|
2022-01-18 02:42:21 -05:00
|
|
|
kernaux_utoa10(value, tmp);
|
2021-12-13 14:59:00 -05:00
|
|
|
while (*tmp != '\0') ++tmp;
|
|
|
|
*(tmp++) = ' ';
|
2021-12-13 14:36:36 -05:00
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
case KERNAUX_UNIT_BIT:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'b';
|
|
|
|
*(tmp++) = 'i';
|
|
|
|
*(tmp++) = 't';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
case KERNAUX_UNIT_BYTE:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'B';
|
|
|
|
*(tmp++) = 'y';
|
|
|
|
*(tmp++) = 't';
|
|
|
|
*(tmp++) = 'e';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-12-13 14:59:00 -05:00
|
|
|
|
|
|
|
*(tmp++) = '\0';
|
|
|
|
|
2021-12-15 05:45:40 -05:00
|
|
|
const size_t tmp_size = strlen(tmp_buffer) + 1;
|
2021-12-13 14:59:00 -05:00
|
|
|
if (tmp_size > buffer_size) return false;
|
|
|
|
|
2021-12-15 05:45:40 -05:00
|
|
|
strcpy(buffer, tmp_buffer);
|
2021-12-13 14:59:00 -05:00
|
|
|
return true;
|
2021-12-13 14:36:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool kernaux_units_human_dec(
|
2022-02-09 16:30:13 -05:00
|
|
|
const uint64_t value,
|
2021-12-13 14:36:36 -05:00
|
|
|
const enum KernAux_Unit unit,
|
|
|
|
const enum KernAux_UnitPrefixDec prefix,
|
2021-12-13 14:59:00 -05:00
|
|
|
char *const buffer,
|
2021-12-13 14:36:36 -05:00
|
|
|
const size_t buffer_size
|
|
|
|
) {
|
2022-05-24 19:01:51 -04:00
|
|
|
KERNAUX_NOTNULL_RETVAL(buffer, false);
|
|
|
|
KERNAUX_ASSERT_RETVAL(buffer_size > 0, false);
|
|
|
|
|
2021-12-13 14:59:00 -05:00
|
|
|
char tmp_buffer[TMP_BUFFER_SIZE];
|
|
|
|
char *tmp = tmp_buffer;
|
|
|
|
|
2022-01-18 02:42:21 -05:00
|
|
|
kernaux_utoa10(value, tmp);
|
2021-12-13 14:59:00 -05:00
|
|
|
while (*tmp != '\0') ++tmp;
|
|
|
|
*(tmp++) = ' ';
|
2021-12-13 14:36:36 -05:00
|
|
|
|
|
|
|
switch (prefix) {
|
|
|
|
case KERNAUX_UNITPFX_KILO:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'k';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
case KERNAUX_UNITPFX_MEGA:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'M';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
case KERNAUX_UNITPFX_GIGA:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'G';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
case KERNAUX_UNIT_BIT:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'b';
|
|
|
|
*(tmp++) = 'i';
|
|
|
|
*(tmp++) = 't';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
case KERNAUX_UNIT_BYTE:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'B';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-12-13 14:59:00 -05:00
|
|
|
|
|
|
|
*(tmp++) = '\0';
|
|
|
|
|
2021-12-15 05:45:40 -05:00
|
|
|
const size_t tmp_size = strlen(tmp_buffer) + 1;
|
2021-12-13 14:59:00 -05:00
|
|
|
if (tmp_size > buffer_size) return false;
|
|
|
|
|
2021-12-15 05:45:40 -05:00
|
|
|
strcpy(buffer, tmp_buffer);
|
2021-12-13 14:59:00 -05:00
|
|
|
return true;
|
2021-12-13 14:36:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool kernaux_units_human_bin(
|
2022-02-09 16:30:13 -05:00
|
|
|
const uint64_t value,
|
2021-12-13 14:36:36 -05:00
|
|
|
const enum KernAux_Unit unit,
|
|
|
|
const enum KernAux_UnitPrefixBin prefix,
|
2021-12-13 14:59:00 -05:00
|
|
|
char *const buffer,
|
2021-12-13 14:36:36 -05:00
|
|
|
const size_t buffer_size
|
|
|
|
) {
|
2021-12-13 14:59:00 -05:00
|
|
|
char tmp_buffer[TMP_BUFFER_SIZE];
|
|
|
|
char *tmp = tmp_buffer;
|
|
|
|
|
2022-01-18 02:42:21 -05:00
|
|
|
kernaux_utoa10(value, tmp);
|
2021-12-13 14:59:00 -05:00
|
|
|
while (*tmp != '\0') ++tmp;
|
|
|
|
*(tmp++) = ' ';
|
2021-12-13 14:36:36 -05:00
|
|
|
|
|
|
|
switch (prefix) {
|
|
|
|
case KERNAUX_UNITPFX_KIBI:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'K';
|
|
|
|
*(tmp++) = 'i';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
case KERNAUX_UNITPFX_MEBI:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'M';
|
|
|
|
*(tmp++) = 'i';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
case KERNAUX_UNITPFX_GIBI:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'G';
|
|
|
|
*(tmp++) = 'i';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
case KERNAUX_UNIT_BIT:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'b';
|
|
|
|
*(tmp++) = 'i';
|
|
|
|
*(tmp++) = 't';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
case KERNAUX_UNIT_BYTE:
|
2021-12-13 14:59:00 -05:00
|
|
|
*(tmp++) = 'B';
|
2021-12-13 14:36:36 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-12-13 14:59:00 -05:00
|
|
|
|
|
|
|
*(tmp++) = '\0';
|
|
|
|
|
2021-12-15 05:45:40 -05:00
|
|
|
const size_t tmp_size = strlen(tmp_buffer) + 1;
|
2021-12-13 14:59:00 -05:00
|
|
|
if (tmp_size > buffer_size) return false;
|
|
|
|
|
2021-12-15 05:45:40 -05:00
|
|
|
strcpy(buffer, tmp_buffer);
|
2021-12-13 14:59:00 -05:00
|
|
|
return true;
|
2021-12-13 14:36:36 -05:00
|
|
|
}
|