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
22219001Smmextern void *xrealloc(void *ptr, size_t size)
23223935Smm		lzma_attribute((__malloc__)) lzma_attr_alloc_size(2);
24207753Smm
25207753Smm
26207753Smm/// \brief      Safe strdup() that never returns NULL
27223935Smmextern char *xstrdup(const char *src) lzma_attribute((__malloc__));
28207753Smm
29207753Smm
30207753Smm/// \brief      Fancy version of strtoull()
31207753Smm///
32207753Smm/// \param      name    Name of the option to show in case of an error
33207753Smm/// \param      value   String containing the number to be parsed; may
34207753Smm///                     contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi"
35207753Smm/// \param      min     Minimum valid value
36207753Smm/// \param      max     Maximum valid value
37207753Smm///
38207753Smm/// \return     Parsed value that is in the range [min, max]. Does not return
39207753Smm///             if an error occurs.
40207753Smm///
41207753Smmextern uint64_t str_to_uint64(const char *name, const char *value,
42207753Smm		uint64_t min, uint64_t max);
43207753Smm
44207753Smm
45207753Smm/// \brief      Round an integer up to the next full MiB and convert to MiB
46207753Smm///
47207753Smm/// This is used when printing memory usage and limit.
48207753Smmextern uint64_t round_up_to_mib(uint64_t n);
49207753Smm
50207753Smm
51207753Smm/// \brief      Convert uint64_t to a string
52207753Smm///
53207753Smm/// Convert the given value to a string with locale-specific thousand
54207753Smm/// separators, if supported by the snprintf() implementation. The string
55207753Smm/// is stored into an internal static buffer indicated by the slot argument.
56207753Smm/// A pointer to the selected buffer is returned.
57207753Smm///
58207753Smm/// This function exists, because non-POSIX systems don't support thousand
59207753Smm/// separator in format strings. Solving the problem in a simple way doesn't
60207753Smm/// work, because it breaks gettext (specifically, the xgettext tool).
61207753Smmextern const char *uint64_to_str(uint64_t value, uint32_t slot);
62207753Smm
63207753Smm
64207753Smmenum nicestr_unit {
65207753Smm	NICESTR_B,
66207753Smm	NICESTR_KIB,
67207753Smm	NICESTR_MIB,
68207753Smm	NICESTR_GIB,
69207753Smm	NICESTR_TIB,
70207753Smm};
71207753Smm
72207753Smm
73207753Smm/// \brief      Convert uint64_t to a nice human readable string
74207753Smm///
75207753Smm/// This is like uint64_to_str() but uses B, KiB, MiB, GiB, or TiB suffix
76207753Smm/// and optionally includes the exact size in parenthesis.
77207753Smm///
78207753Smm/// \param      value     Value to be printed
79207753Smm/// \param      unit_min  Smallest unit to use. This and unit_max are used
80207753Smm///                       e.g. when showing the progress indicator to force
81207753Smm///                       the unit to MiB.
82207753Smm/// \param      unit_max  Biggest unit to use. assert(unit_min <= unit_max).
83207753Smm/// \param      always_also_bytes
84207753Smm///                       Show also the exact byte value in parenthesis
85207753Smm///                       if the nicely formatted string uses bigger unit
86207753Smm///                       than bytes.
87207753Smm/// \param      slot      Which static buffer to use to hold the string.
88207753Smm///                       This is shared with uint64_to_str().
89207753Smm///
90207753Smm/// \return     Pointer to statically allocated buffer containing the string.
91207753Smm///
92207753Smm/// \note       This uses double_to_str() internally so the static buffer
93207753Smm///             in double_to_str() will be overwritten.
94207753Smm///
95207753Smmextern const char *uint64_to_nicestr(uint64_t value,
96207753Smm		enum nicestr_unit unit_min, enum nicestr_unit unit_max,
97207753Smm		bool always_also_bytes, uint32_t slot);
98207753Smm
99207753Smm
100207753Smm/// \brief      Wrapper for snprintf() to help constructing a string in pieces
101207753Smm///
102207753Smm/// A maximum of *left bytes is written starting from *pos. *pos and *left
103207753Smm/// are updated accordingly.
104207753Smmextern void my_snprintf(char **pos, size_t *left, const char *fmt, ...)
105223935Smm		lzma_attribute((__format__(__printf__, 3, 4)));
106207753Smm
107207753Smm
108207753Smm/// \brief      Check if filename is empty and print an error message
109207753Smmextern bool is_empty_filename(const char *filename);
110207753Smm
111207753Smm
112207753Smm/// \brief      Test if stdin is a terminal
113207753Smm///
114207753Smm/// If stdin is a terminal, an error message is printed and exit status set
115207753Smm/// to EXIT_ERROR.
116207753Smmextern bool is_tty_stdin(void);
117207753Smm
118207753Smm
119207753Smm/// \brief      Test if stdout is a terminal
120207753Smm///
121207753Smm/// If stdout is a terminal, an error message is printed and exit status set
122207753Smm/// to EXIT_ERROR.
123207753Smmextern bool is_tty_stdout(void);
124