From patchwork Tue Jan 29 05:03:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omair Javaid X-Patchwork-Id: 156929 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp4230693jaa; Mon, 28 Jan 2019 21:04:04 -0800 (PST) X-Google-Smtp-Source: ALg8bN5k09vbCfNhVae87HHoBZsMlotM6P0BNQQMIafiN82ij2QQrN8ke3RQcfZ8eOszx9t3ePs9 X-Received: by 2002:a17:902:442:: with SMTP id 60mr23337560ple.73.1548738244307; Mon, 28 Jan 2019 21:04:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548738244; cv=none; d=google.com; s=arc-20160816; b=O9UvC1pQDiOote/Wyry2y46+x3yRXKdxBmVOa+AfYeWwZ2+2sgTjHxPY86gER1JPNk xREuQydl+jBWsWVSEEu/0/RJZ476oyVU/WSvdx4O7RF9309onkd7SAuzlSLN+ZT/Ut9J GPtxfxL3VW4spLM8zQd5SoLC1wjukoCOtkbvT/aN+IuELFh5RqoLisSbDeHXykx6AZEi 6+gLpuvdh7dTa8wQLxfRaqf9DIU+bIr/HYDPfv0GeXqu0KmOX2rUPuhKMG6cai0d5dNV eXK6eUV2qj96aeJQzyk/bFvvu7nBnDBgmHRfp/XnL5bfZt6yYl87bcU9vxs5DTVCcKkn /zPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:delivered-to:sender:list-help:list-post:list-archive :list-subscribe:list-unsubscribe:list-id:precedence:mailing-list :dkim-signature:domainkey-signature; bh=lQ6P9lWlbm7JC7ZMvD/OxqK92aZFskoJBlVZMPQS8n4=; b=qbEoM8xttm5c2fmzipz32KCn0enGQRc3om+I1Ypx1xDb0rTsHBgWaXekumElUM7rTO zVRphL5tIw45eLu9zcrwonSnNcymNeX94Jkxr/5Nv9sL+iAqM0zZHh3KgYpjbzxll/o0 0/3uxmLvXBeqirmi9gO8SFNs2vw49JAvyCViNUZ4ehuQgmniGOLyEli6ESx2XbDkwZfB L8CepuCPPpLFCCzpiAqlYMd3dmUO7Q7Ahf63XmMh78h/MiTwsiQ4YMwElYHRaQiT9vmK /Z5xamwQZ4SMCqaoVVq4s0U2ZnW8TdiUk2MYMPhvJ4G0zUiZ7h5uCp1Qb/zaPQIbBck7 oyyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=EgClo9vM; dkim=pass header.i=@linaro.org header.s=google header.b=KHxR755u; spf=pass (google.com: domain of gdb-patches-return-153708-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gdb-patches-return-153708-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id b18si34069171plz.105.2019.01.28.21.04.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 28 Jan 2019 21:04:04 -0800 (PST) Received-SPF: pass (google.com: domain of gdb-patches-return-153708-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=EgClo9vM; dkim=pass header.i=@linaro.org header.s=google header.b=KHxR755u; spf=pass (google.com: domain of gdb-patches-return-153708-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gdb-patches-return-153708-patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=yVOapR36t4K7pb7z1kSFx/dkR+8aPQO BatfUKSW5VohNOGSipYNm/6KON9IgdqAcSKKThL/q0CU7NeXN2ujhvLXGI1Fqoqv tBc6v1M1PtC+mBe0V3C7zvO+Bhzx+fnxtCleHxVChqHY07LIVl7nIB5Ef5JTAmg5 5vx2OceQnx3s= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=tkIeHa3WVJ9X0wTm0kAHrP4umj8=; b=EgClo 9vMJgYRj3jmGF5DgZlo4fFIwKASNaimGUgo/fpUfOIsAN48Hp9jHJ8lv3HRCXNul mxqlP0y8gs6R1wUNT0xGflTs7N6oZbeZEsRw3t6XPYc3C/NrKZpT30L4C451oqlM qF+YzFLhJQ5iGOAew2p0YhOqJs2xxdzmwxEH5A= Received: (qmail 36343 invoked by alias); 29 Jan 2019 05:03: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 36294 invoked by uid 89); 29 Jan 2019 05:03:50 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=approximate, whitespace X-HELO: mail-wm1-f46.google.com Received: from mail-wm1-f46.google.com (HELO mail-wm1-f46.google.com) (209.85.128.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 29 Jan 2019 05:03:48 +0000 Received: by mail-wm1-f46.google.com with SMTP id y185so12030159wmd.1 for ; Mon, 28 Jan 2019 21:03:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=lQ6P9lWlbm7JC7ZMvD/OxqK92aZFskoJBlVZMPQS8n4=; b=KHxR755uSyX0Km6oEjJxRznfa282lTvRQ1NGDw8aHHlz/o/A1zLobHgNVQ2kmaqe4o X+Pysp/tLJMfuOlIgSOA6d/XOp5BD6dmW9lwhxgSKLic184IqwQeqQv7TAzF1EXyt9CA MR1GmKgNe1ZGdk8Ak9uAGNOr5xlofVGBFEl0k= Return-Path: Received: from localhost.localdomain ([43.251.253.48]) by smtp.gmail.com with ESMTPSA id s1sm170325615wro.9.2019.01.28.21.03.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 Jan 2019 21:03:45 -0800 (PST) From: Omair Javaid To: gdb-patches@sourceware.org Cc: palves@redhat.com, prudo@linux.ibm.com, arnez@linux.vnet.ibm.com, peter.griffin@linaro.org, Ulrich.Weigand@de.ibm.com, kieran@linuxembedded.co.uk Subject: [RFC PATCH 2/6] Add libiberty/concat styled concat_path function Date: Tue, 29 Jan 2019 10:03:15 +0500 Message-Id: <1548738199-9403-3-git-send-email-omair.javaid@linaro.org> In-Reply-To: <1548738199-9403-1-git-send-email-omair.javaid@linaro.org> References: <1548738199-9403-1-git-send-email-omair.javaid@linaro.org> X-IsSubscribed: yes From: Philipp Rudo 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(+) -- 2.7.4 diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h index b2cb516..bb7c5b1 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 2394443..8c8e152 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3077,6 +3077,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 bc319de..f8178f8 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -301,6 +301,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);