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 { "M", UINT64_C(1000000) },
68 { "G", UINT64_C(1000000000) },
69 { "Ki", UINT64_C(1024) },
70 { "Mi", UINT64_C(1048576) },
71 { "Gi", UINT64_C(1073741824) },
75 uint64_t multiplier = 0;
76 for (size_t i = 0; suffixes[i].name != NULL; ++i) {
77 if (strcmp(value, suffixes[i].name) == 0) {
78 multiplier = suffixes[i].multiplier;
83 if (multiplier == 0) {
84 errmsg(V_ERROR, _("%s: Invalid multiplier suffix. "
85 "Valid suffixes:"), value);
86 errmsg(V_ERROR, "`k' (10^3), `M' (10^6), `G' (10^9) "
87 "`Ki' (2^10), `Mi' (2^20), "
92 // Don't overflow here either.
93 if (result > UINT64_MAX / multiplier)
99 if (result < min || result > max)
105 errmsg(V_ERROR, _("Value of the option `%s' must be in the range "
106 "[%llu, %llu]"), name,
107 (unsigned long long)(min),
108 (unsigned long long)(max));
113 /// \brief Gets filename part from pathname+filename
115 /// \return Pointer in the filename where the actual filename starts.
116 /// If the last character is a slash, NULL is returned.
119 str_filename(const char *name)
121 const char *base = strrchr(name, '/');
125 } else if (*++base == '\0') {
127 errmsg(V_ERROR, _("%s: Invalid filename"), name);
135 /// \brief Simple quoting to get rid of ASCII control characters
137 /// This is not so cool and locale-dependent, but should be good enough
138 /// At least we don't print any control characters on the terminal.
141 str_quote(const char *str)
144 bool has_ctrl = false;
146 while (str[dest_len] != '\0')
147 if (*(unsigned char *)(str + dest_len++) < 0x20)
150 char *dest = malloc(dest_len + 1);
153 for (size_t i = 0; i < dest_len; ++i)
154 if (*(unsigned char *)(str + i) < 0x20)
159 dest[dest_len] = '\0';
162 // Usually there are no control characters,
163 // so we can optimize.
164 memcpy(dest, str, dest_len + 1);
174 is_empty_filename(const char *filename)
176 if (filename[0] == '\0') {
177 errmsg(V_WARNING, _("Empty filename, skipping"));