GNU Binutils with patches for OS216
Revisión | c48c4ca2318c992e196c48eb96060a7405c4a814 (tree) |
---|---|
Tiempo | 2017-02-08 00:25:53 |
Autor | Philipp Rudo <prudo@linu...> |
Commiter | Andreas Arnez |
Convert substitute_path_component to C++
Simplify the code of utils.c:substiute_path_component by converting it to C++.
gdb/ChangeLog:
* utils.c (substitute_path_component): Convert to C++.
* utils.h (substitute_path_componetn): Adjust declatation.
* auto-load.c (auto_load_expand_dir_vars): Adjust.
@@ -40,6 +40,7 @@ | ||
40 | 40 | #include "filestuff.h" |
41 | 41 | #include "extension.h" |
42 | 42 | #include "gdb/section-scripts.h" |
43 | +#include <string> | |
43 | 44 | |
44 | 45 | /* The section to look in for auto-loaded scripts (in file formats that |
45 | 46 | support sections). |
@@ -175,21 +176,20 @@ static VEC (char_ptr) *auto_load_safe_path_vec; | ||
175 | 176 | this vector must be freed by free_char_ptr_vec by the caller. */ |
176 | 177 | |
177 | 178 | static VEC (char_ptr) * |
178 | -auto_load_expand_dir_vars (const char *string) | |
179 | +auto_load_expand_dir_vars (std::string orig) | |
179 | 180 | { |
180 | 181 | VEC (char_ptr) *dir_vec; |
181 | - char *s; | |
182 | + std::string str = orig; | |
182 | 183 | |
183 | - s = xstrdup (string); | |
184 | - substitute_path_component (&s, "$datadir", gdb_datadir); | |
185 | - substitute_path_component (&s, "$debugdir", debug_file_directory); | |
184 | + substitute_path_component (str, "$datadir", gdb_datadir); | |
185 | + substitute_path_component (str, "$debugdir", debug_file_directory); | |
186 | 186 | |
187 | - if (debug_auto_load && strcmp (s, string) != 0) | |
187 | + if (debug_auto_load && str.compare (orig) != 0) | |
188 | 188 | fprintf_unfiltered (gdb_stdlog, |
189 | - _("auto-load: Expanded $-variables to \"%s\".\n"), s); | |
189 | + _("auto-load: Expanded $-variables to \"%s\".\n"), | |
190 | + str.c_str ()); | |
190 | 191 | |
191 | - dir_vec = dirnames_to_char_ptr_vec (s); | |
192 | - xfree(s); | |
192 | + dir_vec = dirnames_to_char_ptr_vec (str.c_str ()); | |
193 | 193 | |
194 | 194 | return dir_vec; |
195 | 195 | } |
@@ -66,6 +66,8 @@ | ||
66 | 66 | #include "interps.h" |
67 | 67 | #include "gdb_regex.h" |
68 | 68 | |
69 | +#include <string> | |
70 | + | |
69 | 71 | #if !HAVE_DECL_MALLOC |
70 | 72 | extern PTR malloc (); /* ARI: PTR */ |
71 | 73 | #endif |
@@ -3156,49 +3158,25 @@ make_cleanup_free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec) | ||
3156 | 3158 | return make_cleanup (do_free_char_ptr_vec, char_ptr_vec); |
3157 | 3159 | } |
3158 | 3160 | |
3159 | -/* Substitute all occurences of string FROM by string TO in *STRINGP. *STRINGP | |
3160 | - must come from xrealloc-compatible allocator and it may be updated. FROM | |
3161 | - needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be | |
3162 | - located at the start or end of *STRINGP. */ | |
3161 | +/* See utils.h. */ | |
3163 | 3162 | |
3164 | 3163 | void |
3165 | -substitute_path_component (char **stringp, const char *from, const char *to) | |
3164 | +substitute_path_component (std::string &str, const std::string &from, | |
3165 | + const std::string &to) | |
3166 | 3166 | { |
3167 | - char *string = *stringp, *s; | |
3168 | - const size_t from_len = strlen (from); | |
3169 | - const size_t to_len = strlen (to); | |
3170 | - | |
3171 | - for (s = string;;) | |
3167 | + for (size_t pos = str.find (from); pos != std::string::npos; | |
3168 | + pos = str.find (from, pos + 1)) | |
3172 | 3169 | { |
3173 | - s = strstr (s, from); | |
3174 | - if (s == NULL) | |
3175 | - break; | |
3176 | - | |
3177 | - if ((s == string || IS_DIR_SEPARATOR (s[-1]) | |
3178 | - || s[-1] == DIRNAME_SEPARATOR) | |
3179 | - && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len]) | |
3180 | - || s[from_len] == DIRNAME_SEPARATOR)) | |
3170 | + char start, end; | |
3171 | + start = str[pos - 1]; | |
3172 | + end = str[pos + from.length ()]; | |
3173 | + if ((pos == 0 || IS_DIR_SEPARATOR (start) || start == DIRNAME_SEPARATOR) | |
3174 | + && (end == '\0' || IS_DIR_SEPARATOR (end) | |
3175 | + || end == DIRNAME_SEPARATOR)) | |
3181 | 3176 | { |
3182 | - char *string_new; | |
3183 | - | |
3184 | - string_new | |
3185 | - = (char *) xrealloc (string, (strlen (string) + to_len + 1)); | |
3186 | - | |
3187 | - /* Relocate the current S pointer. */ | |
3188 | - s = s - string + string_new; | |
3189 | - string = string_new; | |
3190 | - | |
3191 | - /* Replace from by to. */ | |
3192 | - memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1); | |
3193 | - memcpy (s, to, to_len); | |
3194 | - | |
3195 | - s += to_len; | |
3177 | + str.replace (pos, from.length (), to); | |
3196 | 3178 | } |
3197 | - else | |
3198 | - s++; | |
3199 | 3179 | } |
3200 | - | |
3201 | - *stringp = string; | |
3202 | 3180 | } |
3203 | 3181 | |
3204 | 3182 | #ifdef HAVE_WAITPID |
@@ -132,8 +132,13 @@ extern char *gdb_abspath (const char *); | ||
132 | 132 | extern int gdb_filename_fnmatch (const char *pattern, const char *string, |
133 | 133 | int flags); |
134 | 134 | |
135 | -extern void substitute_path_component (char **stringp, const char *from, | |
136 | - const char *to); | |
135 | +/* Substitute all occurences of string FROM by string TO in STR. FROM | |
136 | + needs to be delimited by IS_DIR_SEPARATOR or DIRNAME_SEPARATOR (or be | |
137 | + located at the start or end of STR). */ | |
138 | + | |
139 | +extern void substitute_path_component (std::string &str, | |
140 | + const std::string &from, | |
141 | + const std::string &to); | |
137 | 142 | |
138 | 143 | char *ldirname (const char *filename); |
139 | 144 |