From patchwork Mon Jun 12 17:08:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Rudo X-Patchwork-Id: 20963 Received: (qmail 64133 invoked by alias); 12 Jun 2017 17:08:44 -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 64115 invoked by uid 89); 12 Jun 2017 17:08:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, KHOP_DYNAMIC, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy= 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; Mon, 12 Jun 2017 17:08:42 +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 v5CH8j4r175010 for ; Mon, 12 Jun 2017 13:08:45 -0400 Received: from e06smtp12.uk.ibm.com (e06smtp12.uk.ibm.com [195.75.94.108]) by mx0b-001b2d01.pphosted.com with ESMTP id 2b1vx0ehux-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 12 Jun 2017 13:08:45 -0400 Received: from localhost by e06smtp12.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 12 Jun 2017 18:08:43 +0100 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp12.uk.ibm.com (192.168.101.142) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 12 Jun 2017 18:08:40 +0100 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v5CH8eJI38797546; Mon, 12 Jun 2017 17:08:40 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C48554C044; Mon, 12 Jun 2017 18:06:54 +0100 (BST) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 96CBF4C040; Mon, 12 Jun 2017 18:06:54 +0100 (BST) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Mon, 12 Jun 2017 18:06:54 +0100 (BST) From: Philipp Rudo To: gdb-patches@sourceware.org Cc: omair.javaid@linaro.org, yao.qi@linaro.org, peter.griffin@linaro.org, arnez@linux.vnet.ibm.com Subject: [RFC v4 2/9] Add libiberty/concat styled concat_path function Date: Mon, 12 Jun 2017 19:08:29 +0200 In-Reply-To: <20170612170836.25174-1-prudo@linux.vnet.ibm.com> References: <20170612170836.25174-1-prudo@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17061217-0008-0000-0000-00000465015F X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17061217-0009-0000-0000-00001DEDB1F6 Message-Id: <20170612170836.25174-3-prudo@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-06-12_11:, , 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-1703280000 definitions=main-1706120298 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 787bac9a03..465e1342af 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -84,6 +84,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 2a7d39801e..53123f7a06 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3126,6 +3126,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 9726025c12..506e178d85 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) + std::string ldirname (const char *filename); extern int count_path_elements (const char *path);