1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Miscellaneous utility functions
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 /// \brief Safe malloc() that never returns NULL
15 /// \note xmalloc(), xrealloc(), and xstrdup() must not be used when
16 /// there are files open for writing, that should be cleaned up
18 #define xmalloc(size) xrealloc(NULL, size)
21 /// \brief Safe realloc() that never returns NULL
22 extern void *xrealloc(void *ptr, size_t size);
25 /// \brief Safe strdup() that never returns NULL
26 extern char *xstrdup(const char *src);
29 /// \brief Fancy version of strtoull()
31 /// \param name Name of the option to show in case of an error
32 /// \param value String containing the number to be parsed; may
33 /// contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi"
34 /// \param min Minimum valid value
35 /// \param max Maximum valid value
37 /// \return Parsed value that is in the range [min, max]. Does not return
38 /// if an error occurs.
40 extern uint64_t str_to_uint64(const char *name, const char *value,
41 uint64_t min, uint64_t max);
44 /// \brief Convert uint64_t to a string
46 /// Convert the given value to a string with locale-specific thousand
47 /// separators, if supported by the snprintf() implementation. The string
48 /// is stored into an internal static buffer indicated by the slot argument.
49 /// A pointer to the selected buffer is returned.
51 /// This function exists, because non-POSIX systems don't support thousand
52 /// separator in format strings. Solving the problem in a simple way doesn't
53 /// work, because it breaks gettext (specifically, the xgettext tool).
54 extern const char *uint64_to_str(uint64_t value, uint32_t slot);
57 /// \brief Convert double to a string with one decimal place
59 /// This is like uint64_to_str() except that this converts a double and
60 /// uses exactly one decimal place.
61 extern const char *double_to_str(double value);
64 /// \brief Check if filename is empty and print an error message
65 extern bool is_empty_filename(const char *filename);
68 /// \brief Test if stdin is a terminal
70 /// If stdin is a terminal, an error message is printed and exit status set
72 extern bool is_tty_stdin(void);
75 /// \brief Test if stdout is a terminal
77 /// If stdout is a terminal, an error message is printed and exit status set
79 extern bool is_tty_stdout(void);