rcpputils  master
C++ API providing common utilities and data structures.
filesystem_helper.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019, Open Source Robotics Foundation, Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 //
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 //
13 // * Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 
29 // This file is originally from:
30 // https://github.com/ros/pluginlib/blob/1a4de29fa55173e9b897ca8ff57ebc88c047e0b3/pluginlib/include/pluginlib/impl/filesystem_helper.hpp
31 
39 #ifndef RCPPUTILS__FILESYSTEM_HELPER_HPP_
40 #define RCPPUTILS__FILESYSTEM_HELPER_HPP_
41 
42 #include <string>
43 #include <vector>
44 
46 
47 namespace rcpputils
48 {
49 namespace fs
50 {
51 
52 #ifdef _WIN32
53 # define RCPPUTILS_IMPL_OS_DIRSEP '\\'
54 #else
55 # define RCPPUTILS_IMPL_OS_DIRSEP '/'
56 #endif
57 
63 static constexpr const char kPreferredSeparator = RCPPUTILS_IMPL_OS_DIRSEP;
64 
65 #undef RCPPUTILS_IMPL_OS_DIRSEP
66 
67 
74 class path
75 {
76 public:
81  path() = default;
82 
89  path(const std::string & p); // NOLINT(runtime/explicit): this is a conversion constructor
90 
94  RCPPUTILS_PUBLIC path(const path & p) = default;
95 
102 
108  RCPPUTILS_PUBLIC bool exists() const;
109 
115  RCPPUTILS_PUBLIC bool is_directory() const noexcept;
116 
122  RCPPUTILS_PUBLIC bool is_regular_file() const noexcept;
123 
130  RCPPUTILS_PUBLIC uint64_t file_size() const;
131 
137  RCPPUTILS_PUBLIC bool empty() const;
138 
144  RCPPUTILS_PUBLIC bool is_absolute() const;
145 
152 
159 
166 
175 
182 
189  RCPPUTILS_PUBLIC path operator/(const std::string & other) const;
190 
197  RCPPUTILS_PUBLIC path & operator/=(const std::string & other);
198 
205  RCPPUTILS_PUBLIC path operator/(const path & other) const;
206 
213  RCPPUTILS_PUBLIC path & operator/=(const path & other);
214 
215 private:
216  std::string path_;
217  std::vector<std::string> path_as_vector_;
218 };
219 
226 RCPPUTILS_PUBLIC bool is_regular_file(const path & p) noexcept;
227 
234 RCPPUTILS_PUBLIC bool is_directory(const path & p) noexcept;
235 
244 RCPPUTILS_PUBLIC uint64_t file_size(const path & p);
245 
252 RCPPUTILS_PUBLIC bool exists(const path & path_to_check);
253 
254 
265 
280  const std::string & base_name,
281  const path & parent_path = temp_directory_path());
282 
291 
299 
306 RCPPUTILS_PUBLIC bool remove(const path & p);
307 
316 RCPPUTILS_PUBLIC bool remove_all(const path & p);
317 
327 RCPPUTILS_PUBLIC path remove_extension(const path & file_path, int n_times = 1);
328 
334 RCPPUTILS_PUBLIC bool operator==(const path & a, const path & b);
335 RCPPUTILS_PUBLIC bool operator!=(const path & a, const path & b);
336 
345 
346 } // namespace fs
347 } // namespace rcpputils
348 
349 #endif // RCPPUTILS__FILESYSTEM_HELPER_HPP_
rcpputils::fs::create_temp_directory
path create_temp_directory(const std::string &base_name, const path &parent_path=temp_directory_path())
Construct a uniquely named temporary directory, in "parent", with format base_nameXXXXXX.
rcpputils::fs::path::exists
bool exists() const
Check if this path exists.
rcpputils::fs::is_regular_file
bool is_regular_file(const path &p) noexcept
Check if the path is a regular file.
rcpputils::fs::remove
bool remove(const path &p)
Remove the file or directory at the path p.
std::string
rcpputils::fs::temp_directory_path
path temp_directory_path()
Get a path to a location in the temporary directory, if it's available.
rcpputils::fs::file_size
uint64_t file_size(const path &p)
Get the file size of the path.
rcpputils
Definition: asserts.hpp:37
std::vector
RCPPUTILS_IMPL_OS_DIRSEP
#define RCPPUTILS_IMPL_OS_DIRSEP
Definition: filesystem_helper.hpp:55
rcpputils::fs::current_path
path current_path()
Return current working directory.
rcpputils::fs::path::filename
path filename() const
Get the last element in this path.
rcpputils::fs::path::parent_path
path parent_path() const
Get the parent directory of this path.
rcpputils::fs::path::cend
std::vector< std::string >::const_iterator cend() const
rcpputils::fs::path::empty
bool empty() const
Check if the path is empty.
rcpputils::fs::operator==
bool operator==(const path &a, const path &b)
Compare two paths for equality.
rcpputils::fs::path::is_absolute
bool is_absolute() const
Check if the path is an absolute path.
rcpputils::fs::remove_extension
path remove_extension(const path &file_path, int n_times=1)
Remove extension(s) from a path.
rcpputils::fs::path::file_size
uint64_t file_size() const
Return the size of the file in bytes.
std::ostream
rcpputils::fs::is_directory
bool is_directory(const path &p) noexcept
Check if the path is a directory.
rcpputils::fs::path::string
std::string string() const
Get the path delimited using this system's path separator.
rcpputils::fs::path
Drop-in replacement for std::filesystem::path.
Definition: filesystem_helper.hpp:74
rcpputils::fs::path::cbegin
std::vector< std::string >::const_iterator cbegin() const
Const iterator to first element of this path.
rcpputils::fs::path::extension
path extension() const
Get a relative path to the component including and following the last '.'.
rcpputils::fs::exists
bool exists(const path &path_to_check)
Check if a path exists.
visibility_control.hpp
Macros for controlling visibilty of exported iterfaces.
rcpputils::fs::create_directories
bool create_directories(const path &p)
Create a directory with the given path p.
rcpputils::fs::path::is_directory
bool is_directory() const noexcept
Check if the path exists and it is a directory.
rcpputils::fs::path::operator/
path operator/(const std::string &other) const
Concatenate a path and a string into a single path.
rcpputils::fs::operator<<
std::ostream & operator<<(std::ostream &os, const path &p)
Convert the path to a string for ostream usage, such as in logging or string formatting.
rcpputils::fs::operator!=
bool operator!=(const path &a, const path &b)
RCPPUTILS_PUBLIC
#define RCPPUTILS_PUBLIC
Declares symbols and functions will be visible for export.
Definition: visibility_control.hpp:81
rcpputils::fs::path::path
path()=default
Constructs an empty path.
rcpputils::fs::remove_all
bool remove_all(const path &p)
Remove the directory at the path p and its content.
rcpputils::fs::path::operator/=
path & operator/=(const std::string &other)
Append a string component to this path.
rcpputils::fs::path::is_regular_file
bool is_regular_file() const noexcept
Check if the path is a regular file.