2021-12-13 14:36:36 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2021-12-15 05:45:40 -05:00
|
|
|
#include <kernaux/itoa.h>
|
|
|
|
#include <kernaux/libc.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(
|
|
|
|
const size_t value,
|
|
|
|
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
|
|
|
|
) {
|
2021-12-13 14:59:00 -05:00
|
|
|
char tmp_buffer[TMP_BUFFER_SIZE];
|
|
|
|
char *tmp = tmp_buffer;
|
|
|
|
|
|
|
|
kernaux_itoa(value, tmp, 10);
|
|
|
|
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(
|
|
|
|
const size_t value,
|
|
|
|
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
|
|
|
|
) {
|
2021-12-13 14:59:00 -05:00
|
|
|
char tmp_buffer[TMP_BUFFER_SIZE];
|
|
|
|
char *tmp = tmp_buffer;
|
|
|
|
|
|
|
|
kernaux_itoa(value, tmp, 10);
|
|
|
|
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(
|
|
|
|
const size_t value,
|
|
|
|
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;
|
|
|
|
|
|
|
|
kernaux_itoa(value, tmp, 10);
|
|
|
|
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
|
|
|
}
|