util.h revision 207753
1207753Smm/////////////////////////////////////////////////////////////////////////////// 2207753Smm// 3207753Smm/// \file util.h 4207753Smm/// \brief Miscellaneous utility functions 5207753Smm// 6207753Smm// Author: Lasse Collin 7207753Smm// 8207753Smm// This file has been put into the public domain. 9207753Smm// You can do whatever you want with this file. 10207753Smm// 11207753Smm/////////////////////////////////////////////////////////////////////////////// 12207753Smm 13207753Smm/// \brief Safe malloc() that never returns NULL 14207753Smm/// 15207753Smm/// \note xmalloc(), xrealloc(), and xstrdup() must not be used when 16207753Smm/// there are files open for writing, that should be cleaned up 17207753Smm/// before exiting. 18207753Smm#define xmalloc(size) xrealloc(NULL, size) 19207753Smm 20207753Smm 21207753Smm/// \brief Safe realloc() that never returns NULL 22207753Smmextern void *xrealloc(void *ptr, size_t size); 23207753Smm 24207753Smm 25207753Smm/// \brief Safe strdup() that never returns NULL 26207753Smmextern char *xstrdup(const char *src); 27207753Smm 28207753Smm 29207753Smm/// \brief Fancy version of strtoull() 30207753Smm/// 31207753Smm/// \param name Name of the option to show in case of an error 32207753Smm/// \param value String containing the number to be parsed; may 33207753Smm/// contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi" 34207753Smm/// \param min Minimum valid value 35207753Smm/// \param max Maximum valid value 36207753Smm/// 37207753Smm/// \return Parsed value that is in the range [min, max]. Does not return 38207753Smm/// if an error occurs. 39207753Smm/// 40207753Smmextern uint64_t str_to_uint64(const char *name, const char *value, 41207753Smm uint64_t min, uint64_t max); 42207753Smm 43207753Smm 44207753Smm/// \brief Round an integer up to the next full MiB and convert to MiB 45207753Smm/// 46207753Smm/// This is used when printing memory usage and limit. 47207753Smmextern uint64_t round_up_to_mib(uint64_t n); 48207753Smm 49207753Smm 50207753Smm/// \brief Convert uint64_t to a string 51207753Smm/// 52207753Smm/// Convert the given value to a string with locale-specific thousand 53207753Smm/// separators, if supported by the snprintf() implementation. The string 54207753Smm/// is stored into an internal static buffer indicated by the slot argument. 55207753Smm/// A pointer to the selected buffer is returned. 56207753Smm/// 57207753Smm/// This function exists, because non-POSIX systems don't support thousand 58207753Smm/// separator in format strings. Solving the problem in a simple way doesn't 59207753Smm/// work, because it breaks gettext (specifically, the xgettext tool). 60207753Smmextern const char *uint64_to_str(uint64_t value, uint32_t slot); 61207753Smm 62207753Smm 63207753Smmenum nicestr_unit { 64207753Smm NICESTR_B, 65207753Smm NICESTR_KIB, 66207753Smm NICESTR_MIB, 67207753Smm NICESTR_GIB, 68207753Smm NICESTR_TIB, 69207753Smm}; 70207753Smm 71207753Smm 72207753Smm/// \brief Convert uint64_t to a nice human readable string 73207753Smm/// 74207753Smm/// This is like uint64_to_str() but uses B, KiB, MiB, GiB, or TiB suffix 75207753Smm/// and optionally includes the exact size in parenthesis. 76207753Smm/// 77207753Smm/// \param value Value to be printed 78207753Smm/// \param unit_min Smallest unit to use. This and unit_max are used 79207753Smm/// e.g. when showing the progress indicator to force 80207753Smm/// the unit to MiB. 81207753Smm/// \param unit_max Biggest unit to use. assert(unit_min <= unit_max). 82207753Smm/// \param always_also_bytes 83207753Smm/// Show also the exact byte value in parenthesis 84207753Smm/// if the nicely formatted string uses bigger unit 85207753Smm/// than bytes. 86207753Smm/// \param slot Which static buffer to use to hold the string. 87207753Smm/// This is shared with uint64_to_str(). 88207753Smm/// 89207753Smm/// \return Pointer to statically allocated buffer containing the string. 90207753Smm/// 91207753Smm/// \note This uses double_to_str() internally so the static buffer 92207753Smm/// in double_to_str() will be overwritten. 93207753Smm/// 94207753Smmextern const char *uint64_to_nicestr(uint64_t value, 95207753Smm enum nicestr_unit unit_min, enum nicestr_unit unit_max, 96207753Smm bool always_also_bytes, uint32_t slot); 97207753Smm 98207753Smm 99207753Smm/// \brief Convert double to a string with one decimal place 100207753Smm/// 101207753Smm/// This is like uint64_to_str() except that this converts a double and 102207753Smm/// uses exactly one decimal place. 103207753Smmextern const char *double_to_str(double value); 104207753Smm 105207753Smm 106207753Smm/// \brief Wrapper for snprintf() to help constructing a string in pieces 107207753Smm/// 108207753Smm/// A maximum of *left bytes is written starting from *pos. *pos and *left 109207753Smm/// are updated accordingly. 110207753Smmextern void my_snprintf(char **pos, size_t *left, const char *fmt, ...) 111207753Smm lzma_attribute((format(printf, 3, 4))); 112207753Smm 113207753Smm 114207753Smm/// \brief Check if filename is empty and print an error message 115207753Smmextern bool is_empty_filename(const char *filename); 116207753Smm 117207753Smm 118207753Smm/// \brief Test if stdin is a terminal 119207753Smm/// 120207753Smm/// If stdin is a terminal, an error message is printed and exit status set 121207753Smm/// to EXIT_ERROR. 122207753Smmextern bool is_tty_stdin(void); 123207753Smm 124207753Smm 125207753Smm/// \brief Test if stdout is a terminal 126207753Smm/// 127207753Smm/// If stdout is a terminal, an error message is printed and exit status set 128207753Smm/// to EXIT_ERROR. 129207753Smmextern bool is_tty_stdout(void); 130