Commit | Line | Data |
---|---|---|
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 PA |
23 | #include <string> |
24 | ||
3a80edfc JB |
25 | /* If possible, define FUNCTION_NAME, a macro containing the name of |
26 | the function being defined. Since this macro may not always be | |
27 | defined, all uses must be protected by appropriate macro definition | |
28 | checks (Eg: "#ifdef FUNCTION_NAME"). | |
29 | ||
30 | Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__' | |
df049a58 DE |
31 | which contains the name of the function currently being defined. |
32 | This is broken in G++ before version 2.6. | |
33 | C9x has a similar variable called __func__, but prefer the GCC one since | |
34 | it demangles C++ function names. */ | |
35 | #if (GCC_VERSION >= 2004) | |
36 | #define FUNCTION_NAME __PRETTY_FUNCTION__ | |
37 | #else | |
38 | #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L | |
46bbb3ed | 39 | #define FUNCTION_NAME __func__ /* ARI: func */ |
df049a58 DE |
40 | #endif |
41 | #endif | |
42 | ||
d26e3629 KY |
43 | /* xmalloc(), xrealloc() and xcalloc() have already been declared in |
44 | "libiberty.h". */ | |
45 | ||
46 | /* Like xmalloc, but zero the memory. */ | |
47 | void *xzalloc (size_t); | |
48 | ||
49 | void xfree (void *); | |
50 | ||
51 | /* Like asprintf and vasprintf, but return the string, throw an error | |
52 | if no memory. */ | |
53 | char *xstrprintf (const char *format, ...) ATTRIBUTE_PRINTF (1, 2); | |
54 | char *xstrvprintf (const char *format, va_list ap) | |
55 | ATTRIBUTE_PRINTF (1, 0); | |
56 | ||
d26e3629 KY |
57 | /* Like snprintf, but throw an error if the output buffer is too small. */ |
58 | int xsnprintf (char *str, size_t size, const char *format, ...) | |
59 | ATTRIBUTE_PRINTF (3, 4); | |
60 | ||
d4081a38 PA |
61 | /* Returns a std::string built from a printf-style format string. */ |
62 | std::string string_printf (const char* fmt, ...) | |
63 | ATTRIBUTE_PRINTF (1, 2); | |
64 | ||
baea0dae PA |
65 | /* Make a copy of the string at PTR with LEN characters |
66 | (and add a null character at the end in the copy). | |
67 | Uses malloc to get the space. Returns the address of the copy. */ | |
68 | ||
69 | char *savestring (const char *ptr, size_t len); | |
70 | ||
fb23d554 SDJ |
71 | /* The strerror() function can return NULL for errno values that are |
72 | out of range. Provide a "safe" version that always returns a | |
73 | printable string. */ | |
74 | ||
75 | extern char *safe_strerror (int); | |
76 | ||
61012eef GB |
77 | /* Return non-zero if the start of STRING matches PATTERN, zero |
78 | otherwise. */ | |
79 | ||
80 | static inline int | |
81 | startswith (const char *string, const char *pattern) | |
82 | { | |
83 | return strncmp (string, pattern, strlen (pattern)) == 0; | |
84 | } | |
85 | ||
03aef70f JK |
86 | ULONGEST strtoulst (const char *num, const char **trailer, int base); |
87 | ||
88 | /* Skip leading whitespace characters in INP, returning an updated | |
89 | pointer. If INP is NULL, return NULL. */ | |
90 | ||
91 | extern char *skip_spaces (char *inp); | |
92 | ||
93 | /* A const-correct version of the above. */ | |
94 | ||
95 | extern const char *skip_spaces_const (const char *inp); | |
96 | ||
97 | /* Skip leading non-whitespace characters in INP, returning an updated | |
98 | pointer. If INP is NULL, return NULL. */ | |
99 | ||
100 | #define skip_to_space(INP) ((char *) skip_to_space_const (INP)) | |
101 | ||
102 | /* A const-correct version of the above. */ | |
103 | ||
104 | extern const char *skip_to_space_const (const char *inp); | |
105 | ||
d26e3629 | 106 | #endif |