From: Philipp Rudo <prudo@linux.vnet.ibm.com>
To: gdb-patches@sourceware.org
Cc: Yao Qi <yao.qi@linaro.org>,
Peter Griffin <peter.griffin@linaro.org>,
Omair Javaid <omair.javaid@linaro.org>,
Andreas Arnez <arnez@linux.vnet.ibm.com>
Subject: [RFC v3 2/8] Add libiberty/concat styled concat_path function
Date: Thu, 16 Mar 2017 16:58:00 -0000 [thread overview]
Message-ID: <20170316165739.88524-3-prudo@linux.vnet.ibm.com> (raw)
In-Reply-To: <20170316165739.88524-1-prudo@linux.vnet.ibm.com>
This commit adds concat_path function to concatenate an arbitrary number of
path elements. The function automatically adds an directory separator between
two elements as needed.
gdb/ChangeLog:
* 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.
---
gdb/common/common-utils.h | 11 +++++++++++
gdb/utils.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
gdb/utils.h | 17 +++++++++++++++++
3 files changed, 74 insertions(+)
diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h
index 618d266..47fdeeb 100644
--- a/gdb/common/common-utils.h
+++ b/gdb/common/common-utils.h
@@ -83,6 +83,17 @@ startswith (const char *string, const char *pattern)
return strncmp (string, pattern, strlen (pattern)) == 0;
}
+/* Return non-zero if the end of STRING matches PATTERN, zero
+ otherwise. */
+
+static inline int
+endswith (const char *string, const char *pattern)
+{
+ return (strlen (string) > strlen (pattern)
+ && strncmp (string + strlen (string) - strlen (pattern), pattern,
+ strlen (pattern)) == 0);
+}
+
ULONGEST strtoulst (const char *num, const char **trailer, int base);
/* Skip leading whitespace characters in INP, returning an updated
diff --git a/gdb/utils.c b/gdb/utils.c
index bef619a..9722d8d 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3181,6 +3181,52 @@ substitute_path_component (std::string &str, const std::string &from,
}
}
+/* Approximate length of final path. Helper for concat_path. */
+
+static inline unsigned long
+approx_path_length (std::initializer_list<std::string> args,
+ std::string dir_separator)
+{
+ size_t length = 0;
+
+ for (const std::string &arg: args)
+ length += arg.length () + dir_separator.length ();
+
+ return length;
+}
+
+/* See utils.h. */
+
+std::string
+_concat_path (std::initializer_list<std::string> args,
+ std::string dir_separator)
+{
+ std::string dst;
+ dst.reserve (approx_path_length (args, dir_separator));
+
+ for (const std::string &arg : args)
+ {
+ if (arg.empty ())
+ continue;
+
+ if (startswith (arg.c_str (), dir_separator.c_str ())
+ && endswith (dst.c_str (), dir_separator.c_str ()))
+ dst.erase (dst.length () - dir_separator.length (),
+ dir_separator.length ());
+
+ else if (!dst.empty ()
+ && !startswith (arg.c_str (), dir_separator.c_str ())
+ && !endswith (dst.c_str (), dir_separator.c_str ())
+ && dst != TARGET_SYSROOT_PREFIX)
+ dst += dir_separator;
+
+ dst += arg;
+ }
+
+ dst.shrink_to_fit ();
+ return dst;
+}
+
#ifdef HAVE_WAITPID
#ifdef SIGALRM
diff --git a/gdb/utils.h b/gdb/utils.h
index d32114e..d9dbaec 100644
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -24,6 +24,7 @@
#include "exceptions.h"
#include "common/scoped_restore.h"
#include <chrono>
+#include <string>
extern void initialize_utils (void);
@@ -140,6 +141,22 @@ extern void substitute_path_component (std::string &str,
const std::string &from,
const std::string &to);
+/* Concatenate an arbitrary number of path elements. Adds and removes
+ directory separators as needed.
+
+ concat_path (/first, second) => /first/second
+ concat_path (first, second) => first/second
+ concat_path (first/, second) => first/second
+ concat_path (first, /second) => first/second
+ concat_path (first/, /second) => first/second
+ concat_path (target:, second) => target:second
+ */
+
+extern std::string _concat_path (std::initializer_list<std::string> args,
+ std::string dir_separator);
+
+#define concat_path(...) _concat_path ({__VA_ARGS__}, SLASH_STRING)
+
char *ldirname (const char *filename);
extern int count_path_elements (const char *path);
--
2.8.4
next prev parent reply other threads:[~2017-03-16 16:57 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-16 16:57 [RFC v3 0/8] Support for Linux kernel debugging Philipp Rudo
2017-03-16 16:57 ` [RFC v3 1/8] Convert substitute_path_component to C++ Philipp Rudo
2017-04-20 20:02 ` Sergio Durigan Junior
2017-05-03 16:20 ` Philipp Rudo
2017-03-16 16:58 ` [RFC v3 5/8] Add commands for linux-kernel target Philipp Rudo
2017-03-16 16:58 ` [RFC v3 3/8] Add basic Linux kernel support Philipp Rudo
2017-04-16 22:59 ` Omair Javaid
2017-05-03 14:38 ` Philipp Rudo
2017-04-20 11:09 ` Omair Javaid
2017-04-24 15:24 ` Andreas Arnez
2017-05-03 14:13 ` Omair Javaid
2017-05-03 15:20 ` Philipp Rudo
2017-05-03 14:38 ` Philipp Rudo
2017-05-02 11:14 ` Yao Qi
2017-05-03 15:36 ` Philipp Rudo
2017-05-07 23:54 ` Omair Javaid
[not found] ` <20170508132204.7a733dc2@ThinkPad>
[not found] ` <CADrjBPqijRQFH4jthAedFzOzMLchpyvM53aXc9grOCjS2YUNCw@mail.gmail.com>
2017-05-10 9:03 ` Philipp Rudo
2017-05-10 9:36 ` Philipp Rudo
2017-05-19 8:45 ` Yao Qi
2017-05-19 15:24 ` Andreas Arnez
2017-05-19 16:28 ` John Baldwin
2017-05-19 17:05 ` Andreas Arnez
2017-05-19 17:40 ` John Baldwin
2017-05-22 10:18 ` Andreas Arnez
2017-03-16 16:58 ` [RFC v3 7/8] Add privileged registers for s390x Philipp Rudo
2017-03-16 16:58 ` [RFC v3 6/8] Seperate common s390-tdep.* from s390-linux-tdep.* Philipp Rudo
2017-03-16 16:58 ` [RFC v3 4/8] Add kernel module support for linux-kernel target Philipp Rudo
2017-05-02 13:15 ` Yao Qi
2017-05-03 16:16 ` Philipp Rudo
2017-05-05 21:33 ` Yao Qi
2017-05-08 9:18 ` Philipp Rudo
2017-05-08 13:05 ` Yao Qi via gdb-patches
2017-03-16 16:58 ` Philipp Rudo [this message]
2017-03-16 16:58 ` [RFC v3 8/8] Add S390 " Philipp Rudo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170316165739.88524-3-prudo@linux.vnet.ibm.com \
--to=prudo@linux.vnet.ibm.com \
--cc=arnez@linux.vnet.ibm.com \
--cc=gdb-patches@sourceware.org \
--cc=omair.javaid@linaro.org \
--cc=peter.griffin@linaro.org \
--cc=yao.qi@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox