From patchwork Tue Feb 7 16:58:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Rudo X-Patchwork-Id: 19149 Received: (qmail 69979 invoked by alias); 7 Feb 2017 16:59:02 -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 69855 invoked by uid 89); 7 Feb 2017 16:59:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.5 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 spammy=836, concat, utils.c, UD:utils.c X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0b-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.158.5) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 07 Feb 2017 16:58:59 +0000 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v17Gwn8O076236 for ; Tue, 7 Feb 2017 11:58:58 -0500 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0b-001b2d01.pphosted.com with ESMTP id 28fch52dch-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Tue, 07 Feb 2017 11:58:57 -0500 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 7 Feb 2017 16:58:55 -0000 Received: from d06dlp03.portsmouth.uk.ibm.com (9.149.20.15) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 7 Feb 2017 16:58:53 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id 89D4F1B08072; Tue, 7 Feb 2017 17:01:45 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v17Gwq3E8388900; Tue, 7 Feb 2017 16:58:52 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D4F6411C04C; Tue, 7 Feb 2017 15:56:39 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AEE2311C050; Tue, 7 Feb 2017 15:56:39 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Tue, 7 Feb 2017 15:56:39 +0000 (GMT) From: Philipp Rudo To: gdb-patches@sourceware.org Cc: peter.griffin@linaro.org, arnez@linux.vnet.ibm.com, yao.qi@arm.com Subject: [RFC v2 2/8] Add libiberty/concat styled concat_path function Date: Tue, 7 Feb 2017 17:58:43 +0100 In-Reply-To: <20170207165849.23216-1-prudo@linux.vnet.ibm.com> References: <20170207165849.23216-1-prudo@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17020716-0012-0000-0000-000004C901F4 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17020716-0013-0000-0000-0000172B428F Message-Id: <20170207165849.23216-3-prudo@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-07_09:, , 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-1702070161 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 2b947aa..1103850 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3179,6 +3179,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);