]> icculus.org git repositories - icculus/xz.git/blob - src/xz/util.h
Updated comments to match renamed files.
[icculus/xz.git] / src / xz / util.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       util.h
4 /// \brief      Miscellaneous utility functions
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 /// \brief      Safe malloc() that never returns NULL
14 ///
15 /// \note       xmalloc(), xrealloc(), and xstrdup() must not be used when
16 ///             there are files open for writing, that should be cleaned up
17 ///             before exiting.
18 #define xmalloc(size) xrealloc(NULL, size)
19
20
21 /// \brief      Safe realloc() that never returns NULL
22 extern void *xrealloc(void *ptr, size_t size);
23
24
25 /// \brief      Safe strdup() that never returns NULL
26 extern char *xstrdup(const char *src);
27
28
29 /// \brief      Fancy version of strtoull()
30 ///
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
36 ///
37 /// \return     Parsed value that is in the range [min, max]. Does not return
38 ///             if an error occurs.
39 ///
40 extern uint64_t str_to_uint64(const char *name, const char *value,
41                 uint64_t min, uint64_t max);
42
43
44 /// \brief      Convert uint64_t to a string
45 ///
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.
50 ///
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);
55
56
57 /// \brief      Convert double to a string with one decimal place
58 ///
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);
62
63
64 /// \brief      Check if filename is empty and print an error message
65 extern bool is_empty_filename(const char *filename);
66
67
68 /// \brief      Test if stdin is a terminal
69 ///
70 /// If stdin is a terminal, an error message is printed and exit status set
71 /// to EXIT_ERROR.
72 extern bool is_tty_stdin(void);
73
74
75 /// \brief      Test if stdout is a terminal
76 ///
77 /// If stdout is a terminal, an error message is printed and exit status set
78 /// to EXIT_ERROR.
79 extern bool is_tty_stdout(void);