irccd  3.0.3
fs_util.hpp
1 /*
2  * fs_util.hpp -- filesystem utilities
3  *
4  * Copyright (c) 2013-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_FS_UTIL_HPP
20 #define IRCCD_FS_UTIL_HPP
21 
27 #include <regex>
28 #include <string>
29 
30 #include <boost/filesystem.hpp>
31 
35 namespace irccd::fs_util {
36 
37 // {{{ base_name
38 
47 auto base_name(const std::string& path) -> std::string;
48 
49 // }}}
50 
51 // {{{ dir_name
52 
61 auto dir_name(const std::string& path) -> std::string;
62 
63 // }}}
64 
65 // {{{ find_if
66 
83 template <typename Predicate>
84 auto find_if(const std::string& base, bool recursive, Predicate&& predicate) -> std::string
85 {
86  const auto find = [&] (auto it) -> std::string {
87  for (const auto& entry : it)
88  if (predicate(entry))
89  return entry.path().string();
90 
91  return "";
92  };
93 
94  return recursive
95  ? find(boost::filesystem::recursive_directory_iterator(base))
96  : find(boost::filesystem::directory_iterator(base));
97 }
98 
99 // }}}
100 
101 // {{{ find
102 
112 auto find(const std::string& base, const std::string& name, bool recursive = false) -> std::string;
113 
123 auto find(const std::string& base, const std::regex& regex, bool recursive = false) -> std::string;
124 
125 // }}}
126 
127 } // !irccd::fs_util
128 
129 #endif // !IRCCD_FS_UTIL_HPP
Filesystem utilities.
Definition: fs_util.hpp:35
auto dir_name(const std::string &path) -> std::string
auto find(const std::string &base, const std::string &name, bool recursive=false) -> std::string
auto find_if(const std::string &base, bool recursive, Predicate &&predicate) -> std::string
Definition: fs_util.hpp:84
auto base_name(const std::string &path) -> std::string