[RFC PATCH 3/8] libcamera: base: file: Fix string usage in class API

Laurent Pinchart laurent.pinchart at ideasonboard.com
Mon Dec 16 00:02:01 CET 2024


Follow the string usage guidelines documented in the libcamera coding
style in the File class API. The rationale is explained in the
guidelines.

Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
---
 include/libcamera/base/file.h      |  6 +++---
 src/libcamera/base/file.cpp        | 14 +++++++-------
 src/libcamera/sysfs.cpp            |  2 +-
 test/camera/camera_reconfigure.cpp |  2 +-
 test/hotplug-cameras.cpp           |  3 ++-
 5 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/include/libcamera/base/file.h b/include/libcamera/base/file.h
index 6d3f106d5900f7a7..dc2841d92c8f3573 100644
--- a/include/libcamera/base/file.h
+++ b/include/libcamera/base/file.h
@@ -40,12 +40,12 @@ public:
 
 	using OpenMode = Flags<OpenModeFlag>;
 
-	File(const std::string &name);
+	File(std::string name);
 	File();
 	~File();
 
 	const std::string &fileName() const { return name_; }
-	void setFileName(const std::string &name);
+	void setFileName(std::string name);
 	bool exists() const;
 
 	bool open(OpenMode mode);
@@ -66,7 +66,7 @@ public:
 			  MapFlags flags = MapFlag::NoOption);
 	bool unmap(uint8_t *addr);
 
-	static bool exists(const std::string &name);
+	static bool exists(const char *name);
 
 private:
 	LIBCAMERA_DISABLE_COPY(File)
diff --git a/src/libcamera/base/file.cpp b/src/libcamera/base/file.cpp
index 2b83a51775b04661..5cff82064101bc2b 100644
--- a/src/libcamera/base/file.cpp
+++ b/src/libcamera/base/file.cpp
@@ -83,8 +83,8 @@ LOG_DEFINE_CATEGORY(File)
  * Upon construction the File object is closed and shall be opened with open()
  * before performing I/O operations.
  */
-File::File(const std::string &name)
-	: name_(name), mode_(OpenModeFlag::NotOpen), error_(0)
+File::File(std::string name)
+	: name_(std::move(name)), mode_(OpenModeFlag::NotOpen), error_(0)
 {
 }
 
@@ -126,7 +126,7 @@ File::~File()
  *
  * Any memory mapping associated with the File is unmapped.
  */
-void File::setFileName(const std::string &name)
+void File::setFileName(std::string name)
 {
 	if (isOpen()) {
 		LOG(File, Error)
@@ -136,7 +136,7 @@ void File::setFileName(const std::string &name)
 
 	unmapAll();
 
-	name_ = name;
+	name_ = std::move(name);
 }
 
 /**
@@ -151,7 +151,7 @@ void File::setFileName(const std::string &name)
  */
 bool File::exists() const
 {
-	return exists(name_);
+	return exists(name_.c_str());
 }
 
 /**
@@ -464,10 +464,10 @@ void File::unmapAll()
  * \param[in] name The file name
  * \return True if the file exists, false otherwise
  */
-bool File::exists(const std::string &name)
+bool File::exists(const char *name)
 {
 	struct stat st;
-	int ret = stat(name.c_str(), &st);
+	int ret = stat(name, &st);
 	if (ret < 0)
 		return false;
 
diff --git a/src/libcamera/sysfs.cpp b/src/libcamera/sysfs.cpp
index 3d9885b080c6f911..cbde72d86fd62b41 100644
--- a/src/libcamera/sysfs.cpp
+++ b/src/libcamera/sysfs.cpp
@@ -92,7 +92,7 @@ std::string firmwareNodePath(const std::string &device)
 
 	/* Lookup for ACPI-based systems */
 	node = device + "/firmware_node/path";
-	if (File::exists(node)) {
+	if (File::exists(node.c_str())) {
 		std::ifstream file(node);
 		if (!file.is_open())
 			return {};
diff --git a/test/camera/camera_reconfigure.cpp b/test/camera/camera_reconfigure.cpp
index 06c87730a1305952..51bd1bf3493d9a86 100644
--- a/test/camera/camera_reconfigure.cpp
+++ b/test/camera/camera_reconfigure.cpp
@@ -179,7 +179,7 @@ private:
 				continue;
 
 			string pname("/proc/" + string(ptr->d_name) + "/comm");
-			if (File::exists(pname)) {
+			if (File::exists(pname.c_str())) {
 				ifstream pfile(pname.c_str());
 				string comm;
 				getline(pfile, comm);
diff --git a/test/hotplug-cameras.cpp b/test/hotplug-cameras.cpp
index 530e9a31120970de..37652c9941e75bb5 100644
--- a/test/hotplug-cameras.cpp
+++ b/test/hotplug-cameras.cpp
@@ -73,7 +73,8 @@ protected:
 		dir = opendir(uvcDriverDir_.c_str());
 		/* Find a UVC device directory, which we can bind/unbind. */
 		while ((dirent = readdir(dir)) != nullptr) {
-			if (!File::exists(uvcDriverDir_ + dirent->d_name + "/video4linux"))
+			std::string fileName = uvcDriverDir_ + dirent->d_name + "/video4linux";
+			if (!File::exists(fileName.c_str()))
 				continue;
 
 			uvcDeviceDir = dirent->d_name;
-- 
Regards,

Laurent Pinchart



More information about the libcamera-devel mailing list