From patchwork Thu Mar 16 16:57:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Rudo X-Patchwork-Id: 19606 Received: (qmail 49401 invoked by alias); 16 Mar 2017 16:57:51 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 49288 invoked by uid 89); 16 Mar 2017 16:57:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy=inp X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0a-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.156.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 16 Mar 2017 16:57:49 +0000 Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v2GGrZWX037531 for ; Thu, 16 Mar 2017 12:57:48 -0400 Received: from e06smtp10.uk.ibm.com (e06smtp10.uk.ibm.com [195.75.94.106]) by mx0a-001b2d01.pphosted.com with ESMTP id 297nd0w7qw-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 16 Mar 2017 12:57:48 -0400 Received: from localhost by e06smtp10.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 16 Mar 2017 16:57:46 -0000 Received: from b06cxnps4075.portsmouth.uk.ibm.com (9.149.109.197) by e06smtp10.uk.ibm.com (192.168.101.140) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 16 Mar 2017 16:57:43 -0000 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v2GGvgDj19398768; Thu, 16 Mar 2017 16:57:42 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6D466AE059; Thu, 16 Mar 2017 16:57:23 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 43F1AAE04D; Thu, 16 Mar 2017 16:57:23 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Thu, 16 Mar 2017 16:57:23 +0000 (GMT) From: Philipp Rudo To: gdb-patches@sourceware.org Cc: Yao Qi , Peter Griffin , Omair Javaid , Andreas Arnez Subject: [RFC v3 2/8] Add libiberty/concat styled concat_path function Date: Thu, 16 Mar 2017 17:57:33 +0100 In-Reply-To: <20170316165739.88524-1-prudo@linux.vnet.ibm.com> References: <20170316165739.88524-1-prudo@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17031616-0040-0000-0000-000003477676 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17031616-0041-0000-0000-0000248F1416 Message-Id: <20170316165739.88524-3-prudo@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-03-16_12:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1702020001 definitions=main-1703160132 X-IsSubscribed: yes 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 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 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 +#include 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 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);