Implemented the add-entry command
This commit is contained in:
37
src/utils.c
37
src/utils.c
@@ -427,3 +427,40 @@ Result format_size(uint64_t size, char** out_string) {
|
||||
// Format the string
|
||||
return format(out_string, "%.2f%s", value, unit);
|
||||
}
|
||||
|
||||
Result parse_size(const char* string, uint64_t* out_size) {
|
||||
// Parse the size
|
||||
char* endptr;
|
||||
uint64_t size = strtoull(string, &endptr, 10);
|
||||
|
||||
if (endptr == string) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to parse the size '%s'.", string);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
// Determine the unit
|
||||
if (*endptr == '\0') {
|
||||
// No unit
|
||||
*out_size = size;
|
||||
} else if (strcmp(endptr, "K") == 0 || strcmp(endptr, "KB") == 0 || strcmp(endptr, "KiB") == 0) {
|
||||
// Kilobytes
|
||||
*out_size = size * 1024;
|
||||
} else if (strcmp(endptr, "M") == 0 || strcmp(endptr, "MB") == 0 || strcmp(endptr, "MiB") == 0) {
|
||||
// Megabytes
|
||||
*out_size = size * 1024 * 1024;
|
||||
} else if (strcmp(endptr, "G") == 0 || strcmp(endptr, "GB") == 0 || strcmp(endptr, "GiB") == 0) {
|
||||
// Gigabytes
|
||||
*out_size = size * 1024 * 1024 * 1024;
|
||||
} else if (strcmp(endptr, "T") == 0 || strcmp(endptr, "TB") == 0 || strcmp(endptr, "TiB") == 0) {
|
||||
// Terabytes
|
||||
*out_size = size * 1024 * 1024 * 1024 * 1024;
|
||||
} else if (strcmp(endptr, "P") == 0 || strcmp(endptr, "PB") == 0 || strcmp(endptr, "PiB") == 0) {
|
||||
// Petabytes
|
||||
*out_size = size * 1024 * 1024 * 1024 * 1024 * 1024;
|
||||
} else {
|
||||
log_message(LOG_LEVEL_ERROR, "Unknown unit '%s'.", endptr);
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
Reference in New Issue
Block a user