GNU Binutils with patches for OS216
Revision | 75d79af4135482bb3cb4b9c348439c64a8e29ca2 (tree) |
---|---|
Zeit | 2017-02-08 00:25:53 |
Autor | Philipp Rudo <prudo@linu...> |
Commiter | Andreas Arnez |
Add libiberty/concat styled concat_path function
* common/common-utils.h (endswith): New function.
* utils.c (_concat_path, approx_path_length): New function.
* utils.h (_concat_path): New export.
(concat_path): New define.
@@ -83,6 +83,17 @@ startswith (const char *string, const char *pattern) | ||
83 | 83 | return strncmp (string, pattern, strlen (pattern)) == 0; |
84 | 84 | } |
85 | 85 | |
86 | +/* Return non-zero if the end of STRING matches PATTERN, zero | |
87 | + otherwise. */ | |
88 | + | |
89 | +static inline int | |
90 | +endswith (const char *string, const char *pattern) | |
91 | +{ | |
92 | + return (strlen (string) > strlen (pattern) | |
93 | + && strncmp (string + strlen (string) - strlen (pattern), pattern, | |
94 | + strlen (pattern)) == 0); | |
95 | +} | |
96 | + | |
86 | 97 | ULONGEST strtoulst (const char *num, const char **trailer, int base); |
87 | 98 | |
88 | 99 | /* Skip leading whitespace characters in INP, returning an updated |
@@ -3179,6 +3179,52 @@ substitute_path_component (std::string &str, const std::string &from, | ||
3179 | 3179 | } |
3180 | 3180 | } |
3181 | 3181 | |
3182 | +/* Approximate length of final path. Helper for concat_path. */ | |
3183 | + | |
3184 | +static inline unsigned long | |
3185 | +approx_path_length (std::initializer_list<std::string> args, | |
3186 | + std::string dir_separator) | |
3187 | +{ | |
3188 | + size_t length = 0; | |
3189 | + | |
3190 | + for (const std::string &arg: args) | |
3191 | + length += arg.length () + dir_separator.length (); | |
3192 | + | |
3193 | + return length; | |
3194 | +} | |
3195 | + | |
3196 | +/* See utils.h. */ | |
3197 | + | |
3198 | +std::string | |
3199 | +_concat_path (std::initializer_list<std::string> args, | |
3200 | + std::string dir_separator) | |
3201 | +{ | |
3202 | + std::string dst; | |
3203 | + dst.reserve (approx_path_length (args, dir_separator)); | |
3204 | + | |
3205 | + for (const std::string &arg : args) | |
3206 | + { | |
3207 | + if (arg.empty ()) | |
3208 | + continue; | |
3209 | + | |
3210 | + if (startswith (arg.c_str (), dir_separator.c_str ()) | |
3211 | + && endswith (dst.c_str (), dir_separator.c_str ())) | |
3212 | + dst.erase (dst.length () - dir_separator.length (), | |
3213 | + dir_separator.length ()); | |
3214 | + | |
3215 | + else if (!dst.empty () | |
3216 | + && !startswith (arg.c_str (), dir_separator.c_str ()) | |
3217 | + && !endswith (dst.c_str (), dir_separator.c_str ()) | |
3218 | + && dst != TARGET_SYSROOT_PREFIX) | |
3219 | + dst += dir_separator; | |
3220 | + | |
3221 | + dst += arg; | |
3222 | + } | |
3223 | + | |
3224 | + dst.shrink_to_fit (); | |
3225 | + return dst; | |
3226 | +} | |
3227 | + | |
3182 | 3228 | #ifdef HAVE_WAITPID |
3183 | 3229 | |
3184 | 3230 | #ifdef SIGALRM |
@@ -24,6 +24,7 @@ | ||
24 | 24 | #include "exceptions.h" |
25 | 25 | #include "common/scoped_restore.h" |
26 | 26 | #include <chrono> |
27 | +#include <string> | |
27 | 28 | |
28 | 29 | extern void initialize_utils (void); |
29 | 30 |
@@ -140,6 +141,22 @@ extern void substitute_path_component (std::string &str, | ||
140 | 141 | const std::string &from, |
141 | 142 | const std::string &to); |
142 | 143 | |
144 | +/* Concatenate an arbitrary number of path elements. Adds and removes | |
145 | + directory separators as needed. | |
146 | + | |
147 | + concat_path (/first, second) => /first/second | |
148 | + concat_path (first, second) => first/second | |
149 | + concat_path (first/, second) => first/second | |
150 | + concat_path (first, /second) => first/second | |
151 | + concat_path (first/, /second) => first/second | |
152 | + concat_path (target:, second) => target:second | |
153 | + */ | |
154 | + | |
155 | +extern std::string _concat_path (std::initializer_list<std::string> args, | |
156 | + std::string dir_separator); | |
157 | + | |
158 | +#define concat_path(...) _concat_path ({__VA_ARGS__}, SLASH_STRING) | |
159 | + | |
143 | 160 | char *ldirname (const char *filename); |
144 | 161 | |
145 | 162 | extern int count_path_elements (const char *path); |