[libcamera-devel] [PATCH v3 1/6] libcamera: utils: Add an internal dirname helper

Kieran Bingham kieran.bingham at ideasonboard.com
Thu Feb 20 17:56:59 CET 2020


Provide a wrapped dirname call which returns a std::string.

Signed-off-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
---
 src/libcamera/include/utils.h |  1 +
 src/libcamera/utils.cpp       | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h
index 080ea6614de0..940597760ee2 100644
--- a/src/libcamera/include/utils.h
+++ b/src/libcamera/include/utils.h
@@ -33,6 +33,7 @@ namespace utils {
 const char *basename(const char *path);
 
 char *secure_getenv(const char *name);
+std::string dirname(const std::string &path);
 
 template<class InputIt1, class InputIt2>
 unsigned int set_overlap(InputIt1 first1, InputIt1 last1,
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index 453e3b3b5995..3fd3aeaf822a 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -70,6 +70,23 @@ char *secure_getenv(const char *name)
 #endif
 }
 
+/**
+ * \brief identify the dirname portion of a path
+ * \param[in] path The full path to parse
+ *
+ * \returns A string of the directory component of the path
+ */
+std::string dirname(const std::string &path)
+{
+	size_t pos = path.rfind('/', path.length());
+
+	if (pos != std::string::npos) {
+		return (path.substr(0, pos));
+	}
+
+	return path;
+}
+
 /**
  * \fn libcamera::utils::set_overlap(InputIt1 first1, InputIt1 last1,
  *				     InputIt2 first2, InputIt2 last2)
-- 
2.20.1



More information about the libcamera-devel mailing list