Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/string.c
| Show First 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | |||||
| /** | /** | ||||
| * Format a size in bytes using binary units. | * Format a size in bytes using binary units. | ||||
| * 1000 -> 1 KB | * 1000 -> 1 KB | ||||
| * Number of decimal places grows with the used unit (e.g. 1.5 MB, 1.55 GB, 1.545 TB). | * Number of decimal places grows with the used unit (e.g. 1.5 MB, 1.55 GB, 1.545 TB). | ||||
| * | * | ||||
| * \param dst: The resulting string. | * \param dst: The resulting string. | ||||
| * Dimension of 14 to support largest possible value for \a bytes (#LLONG_MAX). | * Dimension of 14 to support largest possible value for \a bytes (#LLONG_MAX). | ||||
| * \param bytes: Number to format. | * \param bytes: Number to format. | ||||
| * \param base_10: Calculate using base 10 (GB, MB, ...) or 2 (GiB, MiB, ...). | |||||
| */ | */ | ||||
| void BLI_str_format_byte_unit(char dst[15], long long int bytes, const bool base_10) | void BLI_str_format_byte_unit(char dst[15], long long int bytes) | ||||
| { | { | ||||
| double bytes_converted = bytes; | double bytes_converted = bytes; | ||||
| int order = 0; | int order = 0; | ||||
| int decimals; | int decimals; | ||||
| const int base = base_10 ? 1000 : 1024; | |||||
| const char *units_base_10[] = {"B", "KB", "MB", "GB", "TB", "PB"}; | const char *units_base_10[] = {"B", "KB", "MB", "GB", "TB", "PB"}; | ||||
| const char *units_base_2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"}; | const char *units_base_2[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"}; | ||||
| const int tot_units = ARRAY_SIZE(units_base_2); | const int tot_units = ARRAY_SIZE(units_base_2); | ||||
| #ifdef WIN32 | |||||
| /* Binary values with ambigiuous (decimal) units. */ | |||||
| const int base = 1024; | |||||
| const bool units_b10 = true; | |||||
| #else | |||||
| /* Decimal values with decimal units. */ | |||||
| const int base = 1000; | |||||
| const bool units_b10 = true; | |||||
| #endif | |||||
| BLI_STATIC_ASSERT(ARRAY_SIZE(units_base_2) == ARRAY_SIZE(units_base_10), "array size mismatch"); | BLI_STATIC_ASSERT(ARRAY_SIZE(units_base_2) == ARRAY_SIZE(units_base_10), "array size mismatch"); | ||||
| while ((ABS(bytes_converted) >= base) && ((order + 1) < tot_units)) { | while ((ABS(bytes_converted) >= base) && ((order + 1) < tot_units)) { | ||||
| bytes_converted /= base; | bytes_converted /= base; | ||||
| order++; | order++; | ||||
| } | } | ||||
| decimals = MAX2(order - 1, 0); | decimals = MAX2(order - 1, 0); | ||||
| /* Format value first, stripping away floating zeroes. */ | /* Format value first, stripping away floating zeroes. */ | ||||
| const size_t dst_len = 15; | const size_t dst_len = 15; | ||||
| size_t len = BLI_snprintf_rlen(dst, dst_len, "%.*f", decimals, bytes_converted); | size_t len = BLI_snprintf_rlen(dst, dst_len, "%.*f", decimals, bytes_converted); | ||||
| len -= (size_t)BLI_str_rstrip_float_zero(dst, '\0'); | len -= (size_t)BLI_str_rstrip_float_zero(dst, '\0'); | ||||
| dst[len++] = ' '; | dst[len++] = ' '; | ||||
| BLI_strncpy(dst + len, base_10 ? units_base_10[order] : units_base_2[order], dst_len - len); | BLI_strncpy(dst + len, units_b10 ? units_base_10[order] : units_base_2[order], dst_len - len); | ||||
| } | } | ||||
| /** | /** | ||||
| * Find the ranges needed to split \a str into its individual words. | * Find the ranges needed to split \a str into its individual words. | ||||
| * | * | ||||
| * \param str: The string to search for words. | * \param str: The string to search for words. | ||||
| * \param len: Size of the string to search. | * \param len: Size of the string to search. | ||||
| * \param delim: Character to use as a delimiter. | * \param delim: Character to use as a delimiter. | ||||
| Show All 38 Lines | |||||