{"id":2867,"url":"https://patchwork.libcamera.org/api/1.1/patches/2867/?format=json","web_url":"https://patchwork.libcamera.org/patch/2867/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200221163130.4791-2-kieran.bingham@ideasonboard.com>","date":"2020-02-21T16:31:25","name":"[libcamera-devel,v4,1/6] libcamera: utils: Add a C++ dirname implementation","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"c0458799182e7e3de65d137cf061228badde988f","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/1.1/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2867/mbox/","series":[{"id":689,"url":"https://patchwork.libcamera.org/api/1.1/series/689/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=689","date":"2020-02-21T16:31:24","name":"Support loading IPAs from the build tree","version":4,"mbox":"https://patchwork.libcamera.org/series/689/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2867/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2867/checks/","tags":{},"headers":{"Return-Path":"<kieran.bingham@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0146D6262D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 21 Feb 2020 17:31:35 +0100 (CET)","from localhost.localdomain\n\t(cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8494EA53;\n\tFri, 21 Feb 2020 17:31:34 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1582302694;\n\tbh=o9PQFCR1f94r3LacThxrHefONDhTKBExmHz6YKNo/r0=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=C5nAkQasskB8+PpShxgxpwBVxCfSq3bMQc6wBLv7dAjTssABlq378k9+vAuxyKkPC\n\tSod5f+9FvyG7XQHWu2z3xkecv7hWh/9KSvtThbmQnS0RIzU2Fv6G03jTWUaiZQC45a\n\tW/ODDwcwlP1KluRHgYKBmNav58lFtZv/xyMNj5KY=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"libcamera devel <libcamera-devel@lists.libcamera.org>","Date":"Fri, 21 Feb 2020 16:31:25 +0000","Message-Id":"<20200221163130.4791-2-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<20200221163130.4791-1-kieran.bingham@ideasonboard.com>","References":"<20200221163130.4791-1-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 1/6] libcamera: utils: Add a C++\n\tdirname implementation","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Fri, 21 Feb 2020 16:31:35 -0000"},"content":"Provide a std::string based implementation which conforms to the\nbehaviour of the dirname() fucntion defined by POSIX.\n\nTests are added to cover expected corner cases of the implementation.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n src/libcamera/include/utils.h |  1 +\n src/libcamera/utils.cpp       | 48 +++++++++++++++++++++++++++++++\n test/utils.cpp                | 54 +++++++++++++++++++++++++++++++++++\n 3 files changed, 103 insertions(+)","diff":"diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\nindex 080ea6614de0..940597760ee2 100644\n--- a/src/libcamera/include/utils.h\n+++ b/src/libcamera/include/utils.h\n@@ -33,6 +33,7 @@ namespace utils {\n const char *basename(const char *path);\n \n char *secure_getenv(const char *name);\n+std::string dirname(const std::string &path);\n \n template<class InputIt1, class InputIt2>\n unsigned int set_overlap(InputIt1 first1, InputIt1 last1,\ndiff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\nindex 453e3b3b5995..f566e88cec5b 100644\n--- a/src/libcamera/utils.cpp\n+++ b/src/libcamera/utils.cpp\n@@ -70,6 +70,54 @@ char *secure_getenv(const char *name)\n #endif\n }\n \n+/**\n+ * \\brief Identify the dirname portion of a path\n+ * \\param[in] path The full path to parse\n+ *\n+ * This function conforms with the behaviour of the %dirname() function as\n+ * defined by POSIX.\n+ *\n+ * \\return A string of the directory component of the path\n+ */\n+std::string dirname(const std::string &path)\n+{\n+\tif (path.empty())\n+\t\treturn \".\";\n+\n+\t/*\n+\t * Skip all trailing slashes. If the path is only made of slashes,\n+\t * return \"/\".\n+\t */\n+\tsize_t pos = path.size() - 1;\n+\twhile (path[pos] == '/') {\n+\t\tif (!pos)\n+\t\t\treturn \"/\";\n+\t\tpos--;\n+\t}\n+\n+\t/*\n+\t * Find the previous slash. If the path contains no non-trailing slash,\n+\t * return \".\".\n+\t */\n+\twhile (path[pos] != '/') {\n+\t\tif (!pos)\n+\t\t\treturn \".\";\n+\t\tpos--;\n+\t}\n+\n+\t/*\n+\t * Return the directory name up to (but not including) any trailing\n+\t * slash. If this would result in an empty string, return \"/\".\n+\t */\n+\twhile (path[pos] == '/') {\n+\t\tif (!pos)\n+\t\t\treturn \"/\";\n+\t\tpos--;\n+\t}\n+\n+\treturn path.substr(0, pos + 1);\n+}\n+\n /**\n  * \\fn libcamera::utils::set_overlap(InputIt1 first1, InputIt1 last1,\n  *\t\t\t\t     InputIt2 first2, InputIt2 last2)\ndiff --git a/test/utils.cpp b/test/utils.cpp\nindex db1fbdde847d..e4184e39ce32 100644\n--- a/test/utils.cpp\n+++ b/test/utils.cpp\n@@ -19,6 +19,56 @@ using namespace libcamera;\n class UtilsTest : public Test\n {\n protected:\n+\tint testDirname()\n+\t{\n+\t\tstd::vector<std::string> paths = {\n+\t\t\t\"\",\n+\t\t\t\"///\",\n+\t\t\t\"/bin\",\n+\t\t\t\"/usr/bin\",\n+\t\t\t\"//etc////\",\n+\t\t\t\"//tmp//d//\",\n+\t\t\t\"current_file\",\n+\t\t\t\"./current_file\",\n+\t\t\t\"./current_dir/\",\n+\t\t\t\"current_dir/\",\n+\t\t};\n+\n+\t\tstd::vector<std::string> expected = {\n+\t\t\t\".\",\n+\t\t\t\"/\",\n+\t\t\t\"/\",\n+\t\t\t\"/usr\",\n+\t\t\t\"/\",\n+\t\t\t\"//tmp\",\n+\t\t\t\".\",\n+\t\t\t\".\",\n+\t\t\t\".\",\n+\t\t\t\".\",\n+\t\t};\n+\n+\t\tstd::vector<std::string> results;\n+\n+\t\tfor (const auto &path : paths)\n+\t\t\tresults.push_back(utils::dirname(path));\n+\n+\t\tif (results != expected) {\n+\t\t\tcerr << \"utils::dirname() tests failed\" << endl;\n+\n+\t\t\tcerr << \"expected: \" << endl;\n+\t\t\tfor (const auto &path : expected)\n+\t\t\t\tcerr << \" \" << path << endl;\n+\n+\t\t\tcerr << \"results: \" << endl;\n+\t\t\tfor (const auto &path : results)\n+\t\t\t\tcerr << \" \" << path << endl;\n+\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\treturn 0;\n+\t}\n+\n \tint run()\n \t{\n \t\t/* utils::hex() test. */\n@@ -71,6 +121,10 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\t/* utils::dirname() tests. */\n+\t\tif (testDirname())\n+\t\t\treturn TestFail;\n+\n \t\treturn TestPass;\n \t}\n };\n","prefixes":["libcamera-devel","v4","1/6"]}