From patchwork Mon Mar 12 15:31:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Rudo X-Patchwork-Id: 26285 Received: (qmail 4114 invoked by alias); 12 Mar 2018 15:31:29 -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 4017 invoked by uid 89); 12 Mar 2018 15:31:28 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.3 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= 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; Mon, 12 Mar 2018 15:31:27 +0000 Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w2CFV3Vn072447 for ; Mon, 12 Mar 2018 11:31:25 -0400 Received: from e06smtp15.uk.ibm.com (e06smtp15.uk.ibm.com [195.75.94.111]) by mx0a-001b2d01.pphosted.com with ESMTP id 2gnut5sj5w-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Mon, 12 Mar 2018 11:31:24 -0400 Received: from localhost by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 12 Mar 2018 15:31:21 -0000 Received: from b06cxnps4075.portsmouth.uk.ibm.com (9.149.109.197) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 12 Mar 2018 15:31:19 -0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w2CFVJvP53477596; Mon, 12 Mar 2018 15:31:19 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 685075204B; Mon, 12 Mar 2018 14:22:51 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTPS id 2B2EC52041; Mon, 12 Mar 2018 14:22:51 +0000 (GMT) From: Philipp Rudo To: gdb-patches@sourceware.org Cc: Omair Javaid , Yao Qi , arnez@linux.vnet.ibm.com Subject: [RFC PATCH v5 2/9] Add libiberty/concat styled concat_path function Date: Mon, 12 Mar 2018 16:31:08 +0100 In-Reply-To: <20180312153115.47321-1-prudo@linux.vnet.ibm.com> References: <20180312153115.47321-1-prudo@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18031215-0020-0000-0000-000004026C8B X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18031215-0021-0000-0000-00004296BDCF Message-Id: <20180312153115.47321-3-prudo@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-03-12_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1803120176 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 | 16 ++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index 5408c35469..8e6eb05c2d 100644 --- a/gdb/common/common-utils.h +++ b/gdb/common/common-utils.h @@ -109,6 +109,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 d4f1398d14..4ce3909fca 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3072,6 +3072,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 (const std::initializer_list args, + const 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 (const std::initializer_list args, + const std::string &dir_separator) +{ + std::string ret; + ret.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 (ret.c_str (), dir_separator.c_str ())) + ret.erase (ret.length () - dir_separator.length (), + dir_separator.length ()); + + else if (!ret.empty () + && !startswith (arg.c_str (), dir_separator.c_str ()) + && !endswith (ret.c_str (), dir_separator.c_str ()) + && ret != TARGET_SYSROOT_PREFIX) + ret += dir_separator; + + ret += arg; + } + + ret.shrink_to_fit (); + return ret; +} + #ifdef HAVE_WAITPID #ifdef SIGALRM diff --git a/gdb/utils.h b/gdb/utils.h index 7e6a39ee82..aec9c6194d 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -307,6 +307,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 (const std::initializer_list args, + const 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);