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