From patchwork Thu Jan 12 11:32:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Rudo X-Patchwork-Id: 18872 Received: (qmail 109328 invoked by alias); 12 Jan 2017 11:32:33 -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 109289 invoked by uid 89); 12 Jan 2017 11:32:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 spammy=*string, 1436, *num 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, 12 Jan 2017 11:32:28 +0000 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id v0CBSrk9018376 for ; Thu, 12 Jan 2017 06:32:26 -0500 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0a-001b2d01.pphosted.com with ESMTP id 27x7xftf1m-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 12 Jan 2017 06:32:26 -0500 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 12 Jan 2017 11:32:23 -0000 Received: from d06dlp01.portsmouth.uk.ibm.com (9.149.20.13) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 12 Jan 2017 11:32:21 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id 5242417D805A; Thu, 12 Jan 2017 11:35:12 +0000 (GMT) Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v0CBWKJc14483960; Thu, 12 Jan 2017 11:32:20 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 053365204B; Thu, 12 Jan 2017 10:30:50 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTPS id CAA7C5204D; Thu, 12 Jan 2017 10:30:49 +0000 (GMT) From: Philipp Rudo To: gdb-patches@sourceware.org Cc: peter.griffin@linaro.org, yao.qi@arm.com, arnez@linux.vnet.ibm.com Subject: [RFC 2/7] Add libiberty/concat styled concat_path function Date: Thu, 12 Jan 2017 12:32:12 +0100 In-Reply-To: <20170112113217.48852-1-prudo@linux.vnet.ibm.com> References: <20170112113217.48852-1-prudo@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17011211-0012-0000-0000-000004B61C3F X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17011211-0013-0000-0000-000016DF2C89 Message-Id: <20170112113217.48852-3-prudo@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-01-12_08:, , 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-1612050000 definitions=main-1701120162 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 (startswith): Convert to C++. (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 | 16 +++++++++++++--- gdb/utils.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ gdb/utils.h | 17 +++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index 618d266..be3c106 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -74,13 +74,23 @@ char *savestring (const char *ptr, size_t len); extern char *safe_strerror (int); -/* Return non-zero if the start of STRING matches PATTERN, zero +/* Return non-zero if the start of STR matches PATTERN, zero otherwise. */ static inline int -startswith (const char *string, const char *pattern) +startswith (std::string str, std::string pattern) { - return strncmp (string, pattern, strlen (pattern)) == 0; + return (str.find (pattern) == 0); +} + +/* Return non-zero if the end of STR matches PATTERN, zero + otherwise. */ + +static inline int +endswith (std::string str, std::string pattern) +{ + return (str.rfind (pattern) != std::string::npos + && str.rfind (pattern) == (str.length () - pattern.length ())); } ULONGEST strtoulst (const char *num, const char **trailer, int base); diff --git a/gdb/utils.c b/gdb/utils.c index ee37e49..e924b2c 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3193,6 +3193,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 (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 (std::string arg: args) + { + if (arg.empty ()) + continue; + + if (startswith (arg, dir_separator) + && endswith (dst, dir_separator)) + dst.erase (dst.length () - dir_separator.length (), + dir_separator.length ()); + + else if (!dst.empty () + && !startswith (arg, dir_separator) + && !endswith (dst, dir_separator) + && 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 674f672..5d68a18 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); @@ -143,6 +144,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);