From patchwork Sun Dec 15 23:02:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 22323 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id B6EA7C32F6 for ; Sun, 15 Dec 2024 23:02:33 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AF06E67F37; Mon, 16 Dec 2024 00:02:30 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="VkqNKq4o"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C4B8567F27 for ; Mon, 16 Dec 2024 00:02:26 +0100 (CET) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 96E1A2C6; Mon, 16 Dec 2024 00:01:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1734303710; bh=I6tt0xIyGyxaF9ry/0EGE4JJlwugKl+FhWvr+mPUC2k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VkqNKq4o6k/hGNH1sqt2z5a7h/VGFIL0x25lJZgK0yzcOOJ6pJWL75bxhmUnXl3Ku zSSrv2F6IIJaDyArQdma5gUK+aiWXb8843om1QqfsdeFTmGiRi5yaTzNYYHT0Vvyzw bHJMUdDGz3CD5v3Hxzb1ClX8J3cg/+VJ3b6GXXSs= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH 3/8] libcamera: base: file: Fix string usage in class API Date: Mon, 16 Dec 2024 01:02:01 +0200 Message-ID: <20241215230206.11002-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241215230206.11002-1-laurent.pinchart@ideasonboard.com> References: <20241215230206.11002-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 --- 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; - 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;