gdb: remove unused includes from dwarf2/index-write.c
[binutils-gdb.git] / gdbsupport / common-utils.cc
blobaf25bf0ee9c6e894e6c886c4fa885bedb1d12bdc
1 /* Shared general utility routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-utils.h"
21 #include "host-defs.h"
22 #include "gdbsupport/gdb-safe-ctype.h"
23 #include "gdbsupport/gdb-xfree.h"
25 void *
26 xzalloc (size_t size)
28 return xcalloc (1, size);
31 /* Like asprintf/vasprintf but get an internal_error if the call
32 fails. */
34 gdb::unique_xmalloc_ptr<char>
35 xstrprintf (const char *format, ...)
37 va_list args;
39 va_start (args, format);
40 gdb::unique_xmalloc_ptr<char> ret = xstrvprintf (format, args);
41 va_end (args);
42 return ret;
45 gdb::unique_xmalloc_ptr<char>
46 xstrvprintf (const char *format, va_list ap)
48 char *ret = NULL;
49 int status = vasprintf (&ret, format, ap);
51 /* NULL is returned when there was a memory allocation problem, or
52 any other error (for instance, a bad format string). A negative
53 status (the printed length) with a non-NULL buffer should never
54 happen, but just to be sure. */
55 if (ret == NULL || status < 0)
56 internal_error (_("vasprintf call failed"));
57 return gdb::unique_xmalloc_ptr<char> (ret);
60 int
61 xsnprintf (char *str, size_t size, const char *format, ...)
63 va_list args;
64 int ret;
66 va_start (args, format);
67 ret = vsnprintf (str, size, format, args);
68 gdb_assert (ret < size);
69 va_end (args);
71 return ret;
74 /* See documentation in common-utils.h. */
76 std::string
77 string_printf (const char* fmt, ...)
79 va_list vp;
80 int size;
82 va_start (vp, fmt);
83 size = vsnprintf (NULL, 0, fmt, vp);
84 va_end (vp);
86 std::string str (size, '\0');
88 /* C++11 and later guarantee std::string uses contiguous memory and
89 always includes the terminating '\0'. */
90 va_start (vp, fmt);
91 vsprintf (&str[0], fmt, vp); /* ARI: vsprintf */
92 va_end (vp);
94 return str;
97 /* See documentation in common-utils.h. */
99 std::string
100 string_vprintf (const char* fmt, va_list args)
102 va_list vp;
103 size_t size;
105 va_copy (vp, args);
106 size = vsnprintf (NULL, 0, fmt, vp);
107 va_end (vp);
109 std::string str (size, '\0');
111 /* C++11 and later guarantee std::string uses contiguous memory and
112 always includes the terminating '\0'. */
113 vsprintf (&str[0], fmt, args); /* ARI: vsprintf */
115 return str;
119 /* See documentation in common-utils.h. */
121 std::string &
122 string_appendf (std::string &str, const char *fmt, ...)
124 va_list vp;
126 va_start (vp, fmt);
127 string_vappendf (str, fmt, vp);
128 va_end (vp);
130 return str;
134 /* See documentation in common-utils.h. */
136 std::string &
137 string_vappendf (std::string &str, const char *fmt, va_list args)
139 va_list vp;
140 int grow_size;
142 va_copy (vp, args);
143 grow_size = vsnprintf (NULL, 0, fmt, vp);
144 va_end (vp);
146 size_t curr_size = str.size ();
147 str.resize (curr_size + grow_size);
149 /* C++11 and later guarantee std::string uses contiguous memory and
150 always includes the terminating '\0'. */
151 vsprintf (&str[curr_size], fmt, args); /* ARI: vsprintf */
153 return str;
156 char *
157 savestring (const char *ptr, size_t len)
159 char *p = (char *) xmalloc (len + 1);
161 memcpy (p, ptr, len);
162 p[len] = 0;
163 return p;
166 /* See documentation in common-utils.h. */
168 std::string
169 extract_string_maybe_quoted (const char **arg)
171 bool squote = false;
172 bool dquote = false;
173 bool bsquote = false;
174 std::string result;
175 const char *p = *arg;
177 /* Find the start of the argument. */
178 p = skip_spaces (p);
180 /* Parse p similarly to gdb_argv buildargv function. */
181 while (*p != '\0')
183 if (ISSPACE (*p) && !squote && !dquote && !bsquote)
184 break;
185 else
187 if (bsquote)
189 bsquote = false;
190 result += *p;
192 else if (*p == '\\')
193 bsquote = true;
194 else if (squote)
196 if (*p == '\'')
197 squote = false;
198 else
199 result += *p;
201 else if (dquote)
203 if (*p == '"')
204 dquote = false;
205 else
206 result += *p;
208 else
210 if (*p == '\'')
211 squote = true;
212 else if (*p == '"')
213 dquote = true;
214 else
215 result += *p;
217 p++;
221 *arg = p;
222 return result;
225 /* See gdbsupport/common-utils.h. */
227 std::string
228 make_quoted_string (const char *str)
230 gdb_assert (str != nullptr);
232 std::string result;
234 for (; *str != '\0'; ++str)
236 const char ch = *str;
238 if (strchr ("\"' \t\n", ch) != nullptr)
239 result.push_back ('\\');
240 result.push_back (ch);
243 return result;
246 /* The bit offset of the highest byte in a ULONGEST, for overflow
247 checking. */
249 #define HIGH_BYTE_POSN ((sizeof (ULONGEST) - 1) * HOST_CHAR_BIT)
251 /* True (non-zero) iff DIGIT is a valid digit in radix BASE,
252 where 2 <= BASE <= 36. */
254 static int
255 is_digit_in_base (unsigned char digit, int base)
257 if (!ISALNUM (digit))
258 return 0;
259 if (base <= 10)
260 return (ISDIGIT (digit) && digit < base + '0');
261 else
262 return (ISDIGIT (digit) || TOLOWER (digit) < base - 10 + 'a');
265 static int
266 digit_to_int (unsigned char c)
268 if (ISDIGIT (c))
269 return c - '0';
270 else
271 return TOLOWER (c) - 'a' + 10;
274 /* As for strtoul, but for ULONGEST results. */
276 ULONGEST
277 strtoulst (const char *num, const char **trailer, int base)
279 unsigned int high_part;
280 ULONGEST result;
281 int minus = 0;
282 int i = 0;
284 /* Skip leading whitespace. */
285 while (ISSPACE (num[i]))
286 i++;
288 /* Handle prefixes. */
289 if (num[i] == '+')
290 i++;
291 else if (num[i] == '-')
293 minus = 1;
294 i++;
297 if (base == 0 || base == 16)
299 if (num[i] == '0' && (num[i + 1] == 'x' || num[i + 1] == 'X'))
301 i += 2;
302 if (base == 0)
303 base = 16;
307 if (base == 0 && num[i] == '0')
308 base = 8;
310 if (base == 0)
311 base = 10;
313 if (base < 2 || base > 36)
315 errno = EINVAL;
316 return 0;
319 result = high_part = 0;
320 for (; is_digit_in_base (num[i], base); i += 1)
322 result = result * base + digit_to_int (num[i]);
323 high_part = high_part * base + (unsigned int) (result >> HIGH_BYTE_POSN);
324 result &= ((ULONGEST) 1 << HIGH_BYTE_POSN) - 1;
325 if (high_part > 0xff)
327 errno = ERANGE;
328 result = ~ (ULONGEST) 0;
329 high_part = 0;
330 minus = 0;
331 break;
335 if (trailer != NULL)
336 *trailer = &num[i];
338 result = result + ((ULONGEST) high_part << HIGH_BYTE_POSN);
339 if (minus)
340 return -result;
341 else
342 return result;
345 /* See documentation in common-utils.h. */
347 char *
348 skip_spaces (char *chp)
350 if (chp == NULL)
351 return NULL;
352 while (*chp && ISSPACE (*chp))
353 chp++;
354 return chp;
357 /* A const-correct version of the above. */
359 const char *
360 skip_spaces (const char *chp)
362 if (chp == NULL)
363 return NULL;
364 while (*chp && ISSPACE (*chp))
365 chp++;
366 return chp;
369 /* See documentation in common-utils.h. */
371 const char *
372 skip_to_space (const char *chp)
374 if (chp == NULL)
375 return NULL;
376 while (*chp && !ISSPACE (*chp))
377 chp++;
378 return chp;
381 /* See documentation in common-utils.h. */
383 char *
384 skip_to_space (char *chp)
386 return (char *) skip_to_space ((const char *) chp);
389 /* See gdbsupport/common-utils.h. */
391 void
392 free_vector_argv (std::vector<char *> &v)
394 for (char *el : v)
395 xfree (el);
397 v.clear ();
400 /* See gdbsupport/common-utils.h. */
402 ULONGEST
403 align_up (ULONGEST v, int n)
405 /* Check that N is really a power of two. */
406 gdb_assert (n && (n & (n-1)) == 0);
407 return (v + n - 1) & -n;
410 /* See gdbsupport/common-utils.h. */
412 ULONGEST
413 align_down (ULONGEST v, int n)
415 /* Check that N is really a power of two. */
416 gdb_assert (n && (n & (n-1)) == 0);
417 return (v & -n);
420 /* See gdbsupport/common-utils.h. */
423 fromhex (int a)
425 if (a >= '0' && a <= '9')
426 return a - '0';
427 else if (a >= 'a' && a <= 'f')
428 return a - 'a' + 10;
429 else if (a >= 'A' && a <= 'F')
430 return a - 'A' + 10;
431 else
432 error (_("Invalid hex digit %d"), a);
435 /* See gdbsupport/common-utils.h. */
438 hex2bin (const char *hex, gdb_byte *bin, int count)
440 int i;
442 for (i = 0; i < count; i++)
444 if (hex[0] == 0 || hex[1] == 0)
446 /* Hex string is short, or of uneven length.
447 Return the count that has been converted so far. */
448 return i;
450 *bin++ = fromhex (hex[0]) * 16 + fromhex (hex[1]);
451 hex += 2;
453 return i;
456 /* See gdbsupport/common-utils.h. */
458 gdb::byte_vector
459 hex2bin (const char *hex)
461 size_t bin_len = strlen (hex) / 2;
462 gdb::byte_vector bin (bin_len);
464 hex2bin (hex, bin.data (), bin_len);
466 return bin;
469 /* See gdbsupport/common-utils.h. */
471 std::string
472 bytes_to_string (gdb::array_view<const gdb_byte> bytes)
474 std::string ret;
476 for (size_t i = 0; i < bytes.size (); i++)
478 if (i == 0)
479 ret += string_printf ("%02x", bytes[i]);
480 else
481 ret += string_printf (" %02x", bytes[i]);
484 return ret;