1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Miscellaneous utility functions
6 // Copyright (C) 2007 Lasse Collin
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
23 /// \brief Fancy version of strtoull()
25 /// \param name Name of the option to show in case of an error
26 /// \param value String containing the number to be parsed; may
27 /// contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi"
28 /// \param min Minimum valid value
29 /// \param max Maximum valid value
31 /// \return Parsed value that is in the range [min, max]. Does not return
32 /// if an error occurs.
35 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
40 while (*value == ' ' || *value == '\t')
43 if (*value < '0' || *value > '9') {
44 errmsg(V_ERROR, _("%s: Value is not a non-negative "
52 if (result > (UINT64_MAX - 9) / 10)
56 result += *value - '0';
58 } while (*value >= '0' && *value <= '9');
66 { "k", UINT64_C(1000) },
67 { "kB", UINT64_C(1000) },
68 { "M", UINT64_C(1000000) },
69 { "MB", UINT64_C(1000000) },
70 { "G", UINT64_C(1000000000) },
71 { "GB", UINT64_C(1000000000) },
72 { "Ki", UINT64_C(1024) },
73 { "KiB", UINT64_C(1024) },
74 { "Mi", UINT64_C(1048576) },
75 { "MiB", UINT64_C(1048576) },
76 { "Gi", UINT64_C(1073741824) },
77 { "GiB", UINT64_C(1073741824) }
80 uint64_t multiplier = 0;
81 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
82 if (strcmp(value, suffixes[i].name) == 0) {
83 multiplier = suffixes[i].multiplier;
88 if (multiplier == 0) {
89 errmsg(V_ERROR, _("%s: Invalid multiplier suffix. "
90 "Valid suffixes:"), value);
91 errmsg(V_ERROR, "`k' (10^3), `M' (10^6), `G' (10^9) "
92 "`Ki' (2^10), `Mi' (2^20), "
97 // Don't overflow here either.
98 if (result > UINT64_MAX / multiplier)
101 result *= multiplier;
104 if (result < min || result > max)
110 errmsg(V_ERROR, _("Value of the option `%s' must be in the range "
111 "[%llu, %llu]"), name,
112 (unsigned long long)(min),
113 (unsigned long long)(max));
118 /// \brief Gets filename part from pathname+filename
120 /// \return Pointer in the filename where the actual filename starts.
121 /// If the last character is a slash, NULL is returned.
124 str_filename(const char *name)
126 const char *base = strrchr(name, '/');
130 } else if (*++base == '\0') {
132 errmsg(V_ERROR, _("%s: Invalid filename"), name);
140 /// \brief Simple quoting to get rid of ASCII control characters
142 /// This is not so cool and locale-dependent, but should be good enough
143 /// At least we don't print any control characters on the terminal.
146 str_quote(const char *str)
149 bool has_ctrl = false;
151 while (str[dest_len] != '\0')
152 if (*(unsigned char *)(str + dest_len++) < 0x20)
155 char *dest = malloc(dest_len + 1);
158 for (size_t i = 0; i < dest_len; ++i)
159 if (*(unsigned char *)(str + i) < 0x20)
164 dest[dest_len] = '\0';
167 // Usually there are no control characters,
168 // so we can optimize.
169 memcpy(dest, str, dest_len + 1);
179 is_empty_filename(const char *filename)
181 if (filename[0] == '\0') {
182 errmsg(V_WARNING, _("Empty filename, skipping"));