changeset 688:a18dd69afdd0

Common: style and cleanup in fs_util.hpp
author David Demelier <markand@malikania.fr>
date Fri, 13 Apr 2018 22:42:06 +0200
parents 84537e43b352
children c0c2926a35df
files libcommon/irccd/fs_util.hpp
diffstat 1 files changed, 13 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/libcommon/irccd/fs_util.hpp	Fri Apr 13 22:32:30 2018 +0200
+++ b/libcommon/irccd/fs_util.hpp	Fri Apr 13 22:42:06 2018 +0200
@@ -31,12 +31,15 @@
 
 namespace irccd {
 
+/**
+ * \brief Filesystem utilities.
+ */
 namespace fs_util {
 
 /**
  * Get the base name from a path.
  *
- * Example, baseName("/etc/foo.conf") // foo.conf
+ * Example, base_name("/etc/foo.conf") returns foo.conf
  *
  * \param path the path
  * \return the base name
@@ -49,12 +52,12 @@
 /**
  * Get the parent directory from a path.
  *
- * Example, dirName("/etc/foo.conf") // /etc
+ * Example, dir_name("/etc/foo.conf") returns /etc
  *
  * \param path the path
  * \return the parent directory
  */
-inline std::string dir_name(std::string path)
+inline std::string dir_name(const std::string& path)
 {
     return boost::filesystem::path(path).parent_path().string();
 }
@@ -73,12 +76,12 @@
  * \param predicate the predicate
  * \param recursive true to do recursive search
  * \return the full path name to the file or empty string if never found
- * \throw std::runtime_error on read errors
+ * \throw boost::system::system_error on errors
  */
 template <typename Predicate>
 std::string find_if(const std::string& base, bool recursive, Predicate&& predicate)
 {
-    auto find = [&] (auto it) -> std::string {
+    const auto find = [&] (auto it) -> std::string {
         for (const auto& entry : it)
             if (predicate(entry))
                 return entry.path().string();
@@ -86,10 +89,9 @@
         return "";
     };
 
-    if (recursive)
-        return find(boost::filesystem::recursive_directory_iterator(base));
-
-    return find(boost::filesystem::directory_iterator(base));
+    return recursive
+        ? find(boost::filesystem::recursive_directory_iterator(base))
+        : find(boost::filesystem::directory_iterator(base));
 }
 
 /**
@@ -99,7 +101,7 @@
  * \param name the file name
  * \param recursive true to do recursive search
  * \return the full path name to the file or empty string if never found
- * \throw std::runtime_error on read errors
+ * \throw boost::system::system_error on errors
  */
 inline std::string find(const std::string& base, const std::string& name, bool recursive = false)
 {
@@ -115,7 +117,7 @@
  * \param regex the regular expression
  * \param recursive true to do recursive search
  * \return the full path name to the file or empty string if never found
- * \throw std::runtime_error on read errors
+ * \throw boost::system::system_error on errors
  */
 inline std::string find(const std::string& base, const std::regex& regex, bool recursive = false)
 {