1145132Sanholt/////////////////////////////////////////////////////////////////////////////// 2145132Sanholt// 3145132Sanholt/// \file util.h 4145132Sanholt/// \brief Miscellaneous utility functions 5145132Sanholt// 6145132Sanholt// Author: Lasse Collin 7145132Sanholt// 8145132Sanholt// This file has been put into the public domain. 9145132Sanholt// You can do whatever you want with this file. 10145132Sanholt// 11145132Sanholt/////////////////////////////////////////////////////////////////////////////// 12145132Sanholt 13145132Sanholt/// \brief Safe malloc() that never returns NULL 14145132Sanholt/// 15145132Sanholt/// \note xmalloc(), xrealloc(), and xstrdup() must not be used when 16145132Sanholt/// there are files open for writing, that should be cleaned up 17145132Sanholt/// before exiting. 18145132Sanholt#define xmalloc(size) xrealloc(NULL, size) 19145132Sanholt 20145132Sanholt 21145132Sanholt/// \brief Safe realloc() that never returns NULL 22145132Sanholtextern void *xrealloc(void *ptr, size_t size) 23145132Sanholt lzma_attribute((__malloc__)) lzma_attr_alloc_size(2); 24152909Sanholt 25152909Sanholt 26152909Sanholt/// \brief Safe strdup() that never returns NULL 27182080Srnolandextern char *xstrdup(const char *src) lzma_attribute((__malloc__)); 28182080Srnoland 29182080Srnoland 30182080Srnoland/// \brief Fancy version of strtoull() 31182080Srnoland/// 32145132Sanholt/// \param name Name of the option to show in case of an error 33145132Sanholt/// \param value String containing the number to be parsed; may 34145132Sanholt/// contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi" 35145132Sanholt/// \param min Minimum valid value 36145132Sanholt/// \param max Maximum valid value 37145132Sanholt/// 38145132Sanholt/// \return Parsed value that is in the range [min, max]. Does not return 39145132Sanholt/// if an error occurs. 40145132Sanholt/// 41194759Srnolandextern uint64_t str_to_uint64(const char *name, const char *value, 42145132Sanholt uint64_t min, uint64_t max); 43145132Sanholt 44145132Sanholt 45145132Sanholt/// \brief Round an integer up to the next full MiB and convert to MiB 46145132Sanholt/// 47145132Sanholt/// This is used when printing memory usage and limit. 48145132Sanholtextern uint64_t round_up_to_mib(uint64_t n); 49145132Sanholt 50145132Sanholt 51194759Srnoland/// \brief Convert uint64_t to a string 52145132Sanholt/// 53145132Sanholt/// Convert the given value to a string with locale-specific thousand 54145132Sanholt/// separators, if supported by the snprintf() implementation. The string 55145132Sanholt/// is stored into an internal static buffer indicated by the slot argument. 56145132Sanholt/// A pointer to the selected buffer is returned. 57145132Sanholt/// 58145132Sanholt/// This function exists, because non-POSIX systems don't support thousand 59145132Sanholt/// separator in format strings. Solving the problem in a simple way doesn't 60182080Srnoland/// work, because it breaks gettext (specifically, the xgettext tool). 61145132Sanholtextern const char *uint64_to_str(uint64_t value, uint32_t slot); 62145132Sanholt 63145132Sanholt 64145132Sanholtenum nicestr_unit { 65145132Sanholt NICESTR_B, 66145132Sanholt NICESTR_KIB, 67183833Srnoland NICESTR_MIB, 68145132Sanholt NICESTR_GIB, 69145132Sanholt NICESTR_TIB, 70145132Sanholt}; 71145132Sanholt 72145132Sanholt 73145132Sanholt/// \brief Convert uint64_t to a nice human readable string 74145132Sanholt/// 75145132Sanholt/// This is like uint64_to_str() but uses B, KiB, MiB, GiB, or TiB suffix 76145132Sanholt/// and optionally includes the exact size in parenthesis. 77145132Sanholt/// 78145132Sanholt/// \param value Value to be printed 79145132Sanholt/// \param unit_min Smallest unit to use. This and unit_max are used 80145132Sanholt/// e.g. when showing the progress indicator to force 81145132Sanholt/// the unit to MiB. 82145132Sanholt/// \param unit_max Biggest unit to use. assert(unit_min <= unit_max). 83145132Sanholt/// \param always_also_bytes 84145132Sanholt/// Show also the exact byte value in parenthesis 85145132Sanholt/// if the nicely formatted string uses bigger unit 86145132Sanholt/// than bytes. 87145132Sanholt/// \param slot Which static buffer to use to hold the string. 88145132Sanholt/// This is shared with uint64_to_str(). 89145132Sanholt/// 90145132Sanholt/// \return Pointer to statically allocated buffer containing the string. 91145132Sanholt/// 92145132Sanholt/// \note This uses double_to_str() internally so the static buffer 93145132Sanholt/// in double_to_str() will be overwritten. 94145132Sanholt/// 95145132Sanholtextern const char *uint64_to_nicestr(uint64_t value, 96145132Sanholt enum nicestr_unit unit_min, enum nicestr_unit unit_max, 97145132Sanholt bool always_also_bytes, uint32_t slot); 98220979Skib 99145132Sanholt 100145132Sanholt/// \brief Wrapper for snprintf() to help constructing a string in pieces 101145132Sanholt/// 102145132Sanholt/// A maximum of *left bytes is written starting from *pos. *pos and *left 103145132Sanholt/// are updated accordingly. 104145132Sanholtextern void my_snprintf(char **pos, size_t *left, const char *fmt, ...) 105145132Sanholt lzma_attribute((__format__(__printf__, 3, 4))); 106145132Sanholt 107145132Sanholt 108145132Sanholt/// \brief Check if filename is empty and print an error message 109145132Sanholtextern bool is_empty_filename(const char *filename); 110145132Sanholt 111145132Sanholt 112145132Sanholt/// \brief Test if stdin is a terminal 113145132Sanholt/// 114182080Srnoland/// If stdin is a terminal, an error message is printed and exit status set 115145132Sanholt/// to EXIT_ERROR. 116145132Sanholtextern bool is_tty_stdin(void); 117145132Sanholt 118145132Sanholt 119183833Srnoland/// \brief Test if stdout is a terminal 120145132Sanholt/// 121145132Sanholt/// If stdout is a terminal, an error message is printed and exit status set 122145132Sanholt/// to EXIT_ERROR. 123145132Sanholtextern bool is_tty_stdout(void); 124145132Sanholt