remote: C++ify thread_item and threads_listing_context
[deliverable/binutils-gdb.git] / gdb / common / common-utils.h
CommitLineData
d26e3629
KY
1/* Shared general utility routines for GDB, the GNU debugger.
2
61baf725 3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
d26e3629
KY
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#ifndef COMMON_UTILS_H
21#define COMMON_UTILS_H
22
d4081a38 23#include <string>
7c5ded6a 24#include <vector>
d4081a38 25
3a80edfc
JB
26/* If possible, define FUNCTION_NAME, a macro containing the name of
27 the function being defined. Since this macro may not always be
28 defined, all uses must be protected by appropriate macro definition
29 checks (Eg: "#ifdef FUNCTION_NAME").
30
31 Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
df049a58
DE
32 which contains the name of the function currently being defined.
33 This is broken in G++ before version 2.6.
34 C9x has a similar variable called __func__, but prefer the GCC one since
35 it demangles C++ function names. */
36#if (GCC_VERSION >= 2004)
37#define FUNCTION_NAME __PRETTY_FUNCTION__
38#else
39#if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
46bbb3ed 40#define FUNCTION_NAME __func__ /* ARI: func */
df049a58
DE
41#endif
42#endif
43
d26e3629
KY
44/* xmalloc(), xrealloc() and xcalloc() have already been declared in
45 "libiberty.h". */
46
47/* Like xmalloc, but zero the memory. */
48void *xzalloc (size_t);
49
50void xfree (void *);
51
52/* Like asprintf and vasprintf, but return the string, throw an error
53 if no memory. */
54char *xstrprintf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2);
55char *xstrvprintf (const char *format, va_list ap)
56 ATTRIBUTE_PRINTF (1, 0);
57
d26e3629
KY
58/* Like snprintf, but throw an error if the output buffer is too small. */
59int xsnprintf (char *str, size_t size, const char *format, ...)
60 ATTRIBUTE_PRINTF (3, 4);
61
d4081a38
PA
62/* Returns a std::string built from a printf-style format string. */
63std::string string_printf (const char* fmt, ...)
64 ATTRIBUTE_PRINTF (1, 2);
65
bd413795
TT
66/* Like string_printf, but takes a va_list. */
67std::string string_vprintf (const char* fmt, va_list args)
68 ATTRIBUTE_PRINTF (1, 0);
69
31b833b3
PA
70/* Like string_printf, but appends to DEST instead of returning a new
71 std::string. */
72void string_appendf (std::string &dest, const char* fmt, ...)
73 ATTRIBUTE_PRINTF (2, 3);
74
75/* Like string_appendf, but takes a va_list. */
76void string_vappendf (std::string &dest, const char* fmt, va_list args)
77 ATTRIBUTE_PRINTF (2, 0);
78
baea0dae
PA
79/* Make a copy of the string at PTR with LEN characters
80 (and add a null character at the end in the copy).
81 Uses malloc to get the space. Returns the address of the copy. */
82
83char *savestring (const char *ptr, size_t len);
84
fb23d554
SDJ
85/* The strerror() function can return NULL for errno values that are
86 out of range. Provide a "safe" version that always returns a
87 printable string. */
88
89extern char *safe_strerror (int);
90
61012eef
GB
91/* Return non-zero if the start of STRING matches PATTERN, zero
92 otherwise. */
93
94static inline int
95startswith (const char *string, const char *pattern)
96{
97 return strncmp (string, pattern, strlen (pattern)) == 0;
98}
99
03aef70f
JK
100ULONGEST strtoulst (const char *num, const char **trailer, int base);
101
102/* Skip leading whitespace characters in INP, returning an updated
103 pointer. If INP is NULL, return NULL. */
104
105extern char *skip_spaces (char *inp);
106
107/* A const-correct version of the above. */
108
f1735a53 109extern const char *skip_spaces (const char *inp);
03aef70f
JK
110
111/* Skip leading non-whitespace characters in INP, returning an updated
112 pointer. If INP is NULL, return NULL. */
113
f1735a53 114extern char *skip_to_space (char *inp);
03aef70f
JK
115
116/* A const-correct version of the above. */
117
f1735a53 118extern const char *skip_to_space (const char *inp);
03aef70f 119
7c5ded6a
SDJ
120/* Assumes that V is an argv for a program, and iterates through
121 freeing all the elements. */
122extern void free_vector_argv (std::vector<char *> &v);
123
2090129c
SDJ
124/* Given a vector of arguments ARGV, return a string equivalent to
125 joining all the arguments with a whitespace separating them. */
126extern std::string stringify_argv (const std::vector<char *> &argv);
127
b020ff80
SM
128/* Return true if VALUE is in [LOW, HIGH]. */
129
130template <typename T>
131static bool
132in_inclusive_range (T value, T low, T high)
133{
134 return value >= low && value <= high;
135}
136
d26e3629 137#endif
This page took 0.42377 seconds and 4 git commands to generate.