Miscellaneous utility functions. More...
#include "private.h"
#include <stdarg.h>
Enumerations | |
enum | { UNKNOWN, WORKS, BROKEN } |
Thousand separator support in uint64_to_str() and uint64_to_nicestr(). More... | |
Functions | |
void * | xrealloc (void *ptr, size_t size) |
Safe realloc() that never returns NULL. | |
char * | xstrdup (const char *src) |
Safe strdup() that never returns NULL. | |
uint64_t | str_to_uint64 (const char *name, const char *value, uint64_t min, uint64_t max) |
Fancy version of strtoull(). | |
uint64_t | round_up_to_mib (uint64_t n) |
Round an integer up to the next full MiB and convert to MiB. | |
static void | check_thousand_sep (uint32_t slot) |
const char * | uint64_to_str (uint64_t value, uint32_t slot) |
Convert uint64_t to a string. | |
const char * | uint64_to_nicestr (uint64_t value, enum nicestr_unit unit_min, enum nicestr_unit unit_max, bool always_also_bytes, uint32_t slot) |
Convert uint64_t to a nice human readable string. | |
void | my_snprintf (char **pos, size_t *left, const char *fmt,...) |
bool | is_empty_filename (const char *filename) |
Check if filename is empty and print an error message. | |
bool | is_tty_stdin (void) |
Test if stdin is a terminal. | |
bool | is_tty_stdout (void) |
Test if stdout is a terminal. | |
Variables | |
static char | bufs [4][128] |
Buffers for uint64_to_str() and uint64_to_nicestr(). | |
static enum { ... } | thousand |
Thousand separator support in uint64_to_str() and uint64_to_nicestr(). |
Miscellaneous utility functions.
anonymous enum |
Thousand separator support in uint64_to_str() and uint64_to_nicestr().
void* xrealloc | ( | void * | ptr, | |
size_t | size | |||
) |
Safe realloc() that never returns NULL.
References message_fatal().
char* xstrdup | ( | const char * | src | ) |
Safe strdup() that never returns NULL.
References xmalloc.
Referenced by parse_options(), and suffix_set().
uint64_t str_to_uint64 | ( | const char * | name, | |
const char * | value, | |||
uint64_t | min, | |||
uint64_t | max | |||
) |
Fancy version of strtoull().
name | Name of the option to show in case of an error | |
value | String containing the number to be parsed; may contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi" | |
min | Minimum valid value | |
max | Maximum valid value |
References message(), message_fatal(), and V_ERROR.
Referenced by parse_memlimit(), and parse_options().
uint64_t round_up_to_mib | ( | uint64_t | n | ) |
Round an integer up to the next full MiB and convert to MiB.
This is used when printing memory usage and limit.
Referenced by memlimit_show(), and message_mem_needed().
static void check_thousand_sep | ( | uint32_t | slot | ) | [static] |
Check if thousand separator is supported. Run-time checking is easiest, because it seems to be sometimes lacking even on POSIXish system.
References bufs, and thousand.
Referenced by uint64_to_nicestr(), and uint64_to_str().
const char* uint64_to_str | ( | uint64_t | value, | |
uint32_t | slot | |||
) |
Convert uint64_t to a string.
Convert the given value to a string with locale-specific thousand separators, if supported by the snprintf() implementation. The string is stored into an internal static buffer indicated by the slot argument. A pointer to the selected buffer is returned.
This function exists, because non-POSIX systems don't support thousand separator in format strings. Solving the problem in a simple way doesn't work, because it breaks gettext (specifically, the xgettext tool).
References bufs, check_thousand_sep(), and thousand.
Referenced by memlimit_show(), and message_mem_needed().
const char* uint64_to_nicestr | ( | uint64_t | value, | |
enum nicestr_unit | unit_min, | |||
enum nicestr_unit | unit_max, | |||
bool | always_also_bytes, | |||
uint32_t | slot | |||
) |
Convert uint64_t to a nice human readable string.
This is like uint64_to_str() but uses B, KiB, MiB, GiB, or TiB suffix and optionally includes the exact size in parenthesis.
value | Value to be printed | |
unit_min | Smallest unit to use. This and unit_max are used e.g. when showing the progress indicator to force the unit to MiB. | |
unit_max | Biggest unit to use. assert(unit_min <= unit_max). | |
always_also_bytes | Show also the exact byte value in parenthesis if the nicely formatted string uses bigger unit than bytes. | |
slot | Which static buffer to use to hold the string. This is shared with uint64_to_str(). |
References bufs, check_thousand_sep(), my_snprintf(), and thousand.
Referenced by progress_sizes().
bool is_empty_filename | ( | const char * | filename | ) |
Check if filename is empty and print an error message.
References message_error().
Referenced by io_open_src().
bool is_tty_stdin | ( | void | ) |
Test if stdin is a terminal.
If stdin is a terminal, an error message is printed and exit status set to EXIT_ERROR.
References message_error().
bool is_tty_stdout | ( | void | ) |
Test if stdout is a terminal.
If stdout is a terminal, an error message is printed and exit status set to EXIT_ERROR.
References message_error().
char bufs[4][128] [static] |
Buffers for uint64_to_str() and uint64_to_nicestr().
Referenced by check_thousand_sep(), uint64_to_nicestr(), and uint64_to_str().
enum { ... } thousand [static] |
Thousand separator support in uint64_to_str() and uint64_to_nicestr().
Referenced by check_thousand_sep(), uint64_to_nicestr(), and uint64_to_str().